|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 'use strict'; |
|
5 |
|
6 module.metadata = { |
|
7 "stability": "unstable" |
|
8 }; |
|
9 |
|
10 const { EventEmitterTrait: EventEmitter } = require("../deprecated/events"); |
|
11 const { DOMEventAssembler } = require("../deprecated/events/assembler"); |
|
12 const { Trait } = require("../deprecated/light-traits"); |
|
13 const { getActiveTab, getTabs, getTabContainer } = require("./utils"); |
|
14 const { browserWindowIterator } = require("../deprecated/window-utils"); |
|
15 const { isBrowser } = require('../window/utils'); |
|
16 const { observer: windowObserver } = require("../windows/observer"); |
|
17 |
|
18 const EVENTS = { |
|
19 "TabOpen": "open", |
|
20 "TabClose": "close", |
|
21 "TabSelect": "select", |
|
22 "TabMove": "move", |
|
23 "TabPinned": "pinned", |
|
24 "TabUnpinned": "unpinned" |
|
25 }; |
|
26 |
|
27 |
|
28 // Event emitter objects used to register listeners and emit events on them |
|
29 // when they occur. |
|
30 const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({ |
|
31 /** |
|
32 * Method is implemented by `EventEmitter` and is used just for emitting |
|
33 * events on registered listeners. |
|
34 */ |
|
35 _emit: Trait.required, |
|
36 /** |
|
37 * Events that are supported and emitted by the module. |
|
38 */ |
|
39 supportedEventsTypes: Object.keys(EVENTS), |
|
40 /** |
|
41 * Function handles all the supported events on all the windows that are |
|
42 * observed. Method is used to proxy events to the listeners registered on |
|
43 * this event emitter. |
|
44 * @param {Event} event |
|
45 * Keyboard event being emitted. |
|
46 */ |
|
47 handleEvent: function handleEvent(event) { |
|
48 this._emit(EVENTS[event.type], event.target, event); |
|
49 } |
|
50 }); |
|
51 |
|
52 // Currently Gecko does not dispatch any event on the previously selected |
|
53 // tab before / after "TabSelect" is dispatched. In order to work around this |
|
54 // limitation we keep track of selected tab and emit "deactivate" event with |
|
55 // that before emitting "activate" on selected tab. |
|
56 var selectedTab = null; |
|
57 function onTabSelect(tab) { |
|
58 if (selectedTab !== tab) { |
|
59 if (selectedTab) observer._emit('deactivate', selectedTab); |
|
60 if (tab) observer._emit('activate', selectedTab = tab); |
|
61 } |
|
62 }; |
|
63 observer.on('select', onTabSelect); |
|
64 |
|
65 // We also observe opening / closing windows in order to add / remove it's |
|
66 // containers to the observed list. |
|
67 function onWindowOpen(chromeWindow) { |
|
68 if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. |
|
69 observer.observe(getTabContainer(chromeWindow)); |
|
70 } |
|
71 windowObserver.on("open", onWindowOpen); |
|
72 |
|
73 function onWindowClose(chromeWindow) { |
|
74 if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. |
|
75 // Bug 751546: Emit `deactivate` event on window close immediatly |
|
76 // Otherwise we are going to face "dead object" exception on `select` event |
|
77 if (getActiveTab(chromeWindow) == selectedTab) { |
|
78 observer._emit("deactivate", selectedTab); |
|
79 selectedTab = null; |
|
80 } |
|
81 observer.ignore(getTabContainer(chromeWindow)); |
|
82 } |
|
83 windowObserver.on("close", onWindowClose); |
|
84 |
|
85 |
|
86 // Currently gecko does not dispatches "TabSelect" events when different |
|
87 // window gets activated. To work around this limitation we emulate "select" |
|
88 // event for this case. |
|
89 windowObserver.on("activate", function onWindowActivate(chromeWindow) { |
|
90 if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. |
|
91 observer._emit("select", getActiveTab(chromeWindow)); |
|
92 }); |
|
93 |
|
94 // We should synchronize state, since probably we already have at least one |
|
95 // window open. |
|
96 for each (let window in browserWindowIterator()) onWindowOpen(window); |
|
97 |
|
98 exports.observer = observer; |