1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/tabs/observer.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +'use strict'; 1.8 + 1.9 +module.metadata = { 1.10 + "stability": "unstable" 1.11 +}; 1.12 + 1.13 +const { EventEmitterTrait: EventEmitter } = require("../deprecated/events"); 1.14 +const { DOMEventAssembler } = require("../deprecated/events/assembler"); 1.15 +const { Trait } = require("../deprecated/light-traits"); 1.16 +const { getActiveTab, getTabs, getTabContainer } = require("./utils"); 1.17 +const { browserWindowIterator } = require("../deprecated/window-utils"); 1.18 +const { isBrowser } = require('../window/utils'); 1.19 +const { observer: windowObserver } = require("../windows/observer"); 1.20 + 1.21 +const EVENTS = { 1.22 + "TabOpen": "open", 1.23 + "TabClose": "close", 1.24 + "TabSelect": "select", 1.25 + "TabMove": "move", 1.26 + "TabPinned": "pinned", 1.27 + "TabUnpinned": "unpinned" 1.28 +}; 1.29 + 1.30 + 1.31 +// Event emitter objects used to register listeners and emit events on them 1.32 +// when they occur. 1.33 +const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({ 1.34 + /** 1.35 + * Method is implemented by `EventEmitter` and is used just for emitting 1.36 + * events on registered listeners. 1.37 + */ 1.38 + _emit: Trait.required, 1.39 + /** 1.40 + * Events that are supported and emitted by the module. 1.41 + */ 1.42 + supportedEventsTypes: Object.keys(EVENTS), 1.43 + /** 1.44 + * Function handles all the supported events on all the windows that are 1.45 + * observed. Method is used to proxy events to the listeners registered on 1.46 + * this event emitter. 1.47 + * @param {Event} event 1.48 + * Keyboard event being emitted. 1.49 + */ 1.50 + handleEvent: function handleEvent(event) { 1.51 + this._emit(EVENTS[event.type], event.target, event); 1.52 + } 1.53 +}); 1.54 + 1.55 +// Currently Gecko does not dispatch any event on the previously selected 1.56 +// tab before / after "TabSelect" is dispatched. In order to work around this 1.57 +// limitation we keep track of selected tab and emit "deactivate" event with 1.58 +// that before emitting "activate" on selected tab. 1.59 +var selectedTab = null; 1.60 +function onTabSelect(tab) { 1.61 + if (selectedTab !== tab) { 1.62 + if (selectedTab) observer._emit('deactivate', selectedTab); 1.63 + if (tab) observer._emit('activate', selectedTab = tab); 1.64 + } 1.65 +}; 1.66 +observer.on('select', onTabSelect); 1.67 + 1.68 +// We also observe opening / closing windows in order to add / remove it's 1.69 +// containers to the observed list. 1.70 +function onWindowOpen(chromeWindow) { 1.71 + if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. 1.72 + observer.observe(getTabContainer(chromeWindow)); 1.73 +} 1.74 +windowObserver.on("open", onWindowOpen); 1.75 + 1.76 +function onWindowClose(chromeWindow) { 1.77 + if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. 1.78 + // Bug 751546: Emit `deactivate` event on window close immediatly 1.79 + // Otherwise we are going to face "dead object" exception on `select` event 1.80 + if (getActiveTab(chromeWindow) == selectedTab) { 1.81 + observer._emit("deactivate", selectedTab); 1.82 + selectedTab = null; 1.83 + } 1.84 + observer.ignore(getTabContainer(chromeWindow)); 1.85 +} 1.86 +windowObserver.on("close", onWindowClose); 1.87 + 1.88 + 1.89 +// Currently gecko does not dispatches "TabSelect" events when different 1.90 +// window gets activated. To work around this limitation we emulate "select" 1.91 +// event for this case. 1.92 +windowObserver.on("activate", function onWindowActivate(chromeWindow) { 1.93 + if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. 1.94 + observer._emit("select", getActiveTab(chromeWindow)); 1.95 +}); 1.96 + 1.97 +// We should synchronize state, since probably we already have at least one 1.98 +// window open. 1.99 +for each (let window in browserWindowIterator()) onWindowOpen(window); 1.100 + 1.101 +exports.observer = observer;