addon-sdk/source/lib/sdk/windows/observer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/windows/observer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,51 @@
     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 { WindowTracker, windowIterator } = require("../deprecated/window-utils");
    1.15 +const { DOMEventAssembler } = require("../deprecated/events/assembler");
    1.16 +const { Trait } = require("../deprecated/light-traits");
    1.17 +
    1.18 +// Event emitter objects used to register listeners and emit events on them
    1.19 +// when they occur.
    1.20 +const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({
    1.21 +  /**
    1.22 +   * Method is implemented by `EventEmitter` and is used just for emitting
    1.23 +   * events on registered listeners.
    1.24 +   */
    1.25 +  _emit: Trait.required,
    1.26 +  /**
    1.27 +   * Events that are supported and emitted by the module.
    1.28 +   */
    1.29 +  supportedEventsTypes: [ "activate", "deactivate" ],
    1.30 +  /**
    1.31 +   * Function handles all the supported events on all the windows that are
    1.32 +   * observed. Method is used to proxy events to the listeners registered on
    1.33 +   * this event emitter.
    1.34 +   * @param {Event} event
    1.35 +   *    Keyboard event being emitted.
    1.36 +   */
    1.37 +  handleEvent: function handleEvent(event) {
    1.38 +    this._emit(event.type, event.target, event);
    1.39 +  }
    1.40 +});
    1.41 +
    1.42 +// Using `WindowTracker` to track window events.
    1.43 +WindowTracker({
    1.44 +  onTrack: function onTrack(chromeWindow) {
    1.45 +    observer._emit("open", chromeWindow);
    1.46 +    observer.observe(chromeWindow);
    1.47 +  },
    1.48 +  onUntrack: function onUntrack(chromeWindow) {
    1.49 +    observer._emit("close", chromeWindow);
    1.50 +    observer.ignore(chromeWindow);
    1.51 +  }
    1.52 +});
    1.53 +
    1.54 +exports.observer = observer;

mercurial