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: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { Trait } = require("../deprecated/light-traits"); michael@0: const { EventEmitterTrait: EventEmitter } = require("../deprecated/events"); michael@0: const { DOMEventAssembler } = require("../deprecated/events/assembler"); 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: // 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: [ "keydown", "keyup", "keypress" ], 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(event.type, event, event.target.ownerDocument.defaultView); michael@0: } michael@0: }); michael@0: michael@0: // Adding each opened window to a list of observed windows. michael@0: windowObserver.on("open", function onOpen(window) { michael@0: if (isBrowser(window)) michael@0: observer.observe(window); michael@0: }); michael@0: // Removing each closed window form the list of observed windows. michael@0: windowObserver.on("close", function onClose(window) { michael@0: if (isBrowser(window)) michael@0: observer.ignore(window); michael@0: }); michael@0: michael@0: // Making observer aware of already opened windows. michael@0: for each (let window in browserWindowIterator()) michael@0: observer.observe(window); michael@0: michael@0: exports.observer = observer;