addon-sdk/source/lib/sdk/keyboard/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/keyboard/observer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,57 @@
     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 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "unstable"
    1.12 +};
    1.13 +
    1.14 +const { Trait } = require("../deprecated/light-traits");
    1.15 +const { EventEmitterTrait: EventEmitter } = require("../deprecated/events");
    1.16 +const { DOMEventAssembler } = require("../deprecated/events/assembler");
    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 +// Event emitter objects used to register listeners and emit events on them
    1.22 +// when they occur.
    1.23 +const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({
    1.24 +  /**
    1.25 +   * Method is implemented by `EventEmitter` and is used just for emitting
    1.26 +   * events on registered listeners.
    1.27 +   */
    1.28 +  _emit: Trait.required,
    1.29 +  /**
    1.30 +   * Events that are supported and emitted by the module.
    1.31 +   */
    1.32 +  supportedEventsTypes: [ "keydown", "keyup", "keypress" ],
    1.33 +  /**
    1.34 +   * Function handles all the supported events on all the windows that are
    1.35 +   * observed. Method is used to proxy events to the listeners registered on
    1.36 +   * this event emitter.
    1.37 +   * @param {Event} event
    1.38 +   *    Keyboard event being emitted.
    1.39 +   */
    1.40 +  handleEvent: function handleEvent(event) {
    1.41 +    this._emit(event.type, event, event.target.ownerDocument.defaultView);
    1.42 +  }
    1.43 +});
    1.44 +
    1.45 +// Adding each opened window to a list of observed windows.
    1.46 +windowObserver.on("open", function onOpen(window) {
    1.47 +  if (isBrowser(window))
    1.48 +    observer.observe(window);
    1.49 +});
    1.50 +// Removing each closed window form the list of observed windows.
    1.51 +windowObserver.on("close", function onClose(window) {
    1.52 +  if (isBrowser(window))
    1.53 +    observer.ignore(window);
    1.54 +});
    1.55 +
    1.56 +// Making observer aware of already opened windows.
    1.57 +for each (let window in browserWindowIterator())
    1.58 +  observer.observe(window);
    1.59 +
    1.60 +exports.observer = observer;

mercurial