michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { EventEmitterTrait: EventEmitter } = require("../deprecated/events"); michael@0: const { DOMEventAssembler } = require("../deprecated/events/assembler"); michael@0: const { Trait } = require("../deprecated/light-traits"); michael@0: const { getActiveTab, getTabs, getTabContainer } = require("./utils"); michael@0: const { browserWindowIterator } = require("../deprecated/window-utils"); michael@0: const { isBrowser } = require('../window/utils'); michael@0: const { observer: windowObserver } = require("../windows/observer"); michael@0: michael@0: const EVENTS = { michael@0: "TabOpen": "open", michael@0: "TabClose": "close", michael@0: "TabSelect": "select", michael@0: "TabMove": "move", michael@0: "TabPinned": "pinned", michael@0: "TabUnpinned": "unpinned" michael@0: }; michael@0: michael@0: michael@0: // Event emitter objects used to register listeners and emit events on them michael@0: // when they occur. michael@0: const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({ michael@0: /** michael@0: * Method is implemented by `EventEmitter` and is used just for emitting michael@0: * events on registered listeners. michael@0: */ michael@0: _emit: Trait.required, michael@0: /** michael@0: * Events that are supported and emitted by the module. michael@0: */ michael@0: supportedEventsTypes: Object.keys(EVENTS), michael@0: /** michael@0: * Function handles all the supported events on all the windows that are michael@0: * observed. Method is used to proxy events to the listeners registered on michael@0: * this event emitter. michael@0: * @param {Event} event michael@0: * Keyboard event being emitted. michael@0: */ michael@0: handleEvent: function handleEvent(event) { michael@0: this._emit(EVENTS[event.type], event.target, event); michael@0: } michael@0: }); michael@0: michael@0: // Currently Gecko does not dispatch any event on the previously selected michael@0: // tab before / after "TabSelect" is dispatched. In order to work around this michael@0: // limitation we keep track of selected tab and emit "deactivate" event with michael@0: // that before emitting "activate" on selected tab. michael@0: var selectedTab = null; michael@0: function onTabSelect(tab) { michael@0: if (selectedTab !== tab) { michael@0: if (selectedTab) observer._emit('deactivate', selectedTab); michael@0: if (tab) observer._emit('activate', selectedTab = tab); michael@0: } michael@0: }; michael@0: observer.on('select', onTabSelect); michael@0: michael@0: // We also observe opening / closing windows in order to add / remove it's michael@0: // containers to the observed list. michael@0: function onWindowOpen(chromeWindow) { michael@0: if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. michael@0: observer.observe(getTabContainer(chromeWindow)); michael@0: } michael@0: windowObserver.on("open", onWindowOpen); michael@0: michael@0: function onWindowClose(chromeWindow) { michael@0: if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. michael@0: // Bug 751546: Emit `deactivate` event on window close immediatly michael@0: // Otherwise we are going to face "dead object" exception on `select` event michael@0: if (getActiveTab(chromeWindow) == selectedTab) { michael@0: observer._emit("deactivate", selectedTab); michael@0: selectedTab = null; michael@0: } michael@0: observer.ignore(getTabContainer(chromeWindow)); michael@0: } michael@0: windowObserver.on("close", onWindowClose); michael@0: michael@0: michael@0: // Currently gecko does not dispatches "TabSelect" events when different michael@0: // window gets activated. To work around this limitation we emulate "select" michael@0: // event for this case. michael@0: windowObserver.on("activate", function onWindowActivate(chromeWindow) { michael@0: if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. michael@0: observer._emit("select", getActiveTab(chromeWindow)); michael@0: }); michael@0: michael@0: // We should synchronize state, since probably we already have at least one michael@0: // window open. michael@0: for each (let window in browserWindowIterator()) onWindowOpen(window); michael@0: michael@0: exports.observer = observer;