addon-sdk/source/lib/sdk/dom/events.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "unstable"
michael@0 9 };
michael@0 10
michael@0 11 // Utility function that returns copy of the given `text` with last character
michael@0 12 // removed if it is `"s"`.
michael@0 13 function singularify(text) {
michael@0 14 return text[text.length - 1] === "s" ? text.substr(0, text.length - 1) : text;
michael@0 15 }
michael@0 16
michael@0 17 // Utility function that takes event type, argument is passed to
michael@0 18 // `document.createEvent` and returns name of the initializer method of the
michael@0 19 // given event. Please note that there are some event types whose initializer
michael@0 20 // methods can't be guessed by this function. For more details see following
michael@0 21 // link: https://developer.mozilla.org/En/DOM/Document.createEvent
michael@0 22 function getInitializerName(category) {
michael@0 23 return "init" + singularify(category);
michael@0 24 }
michael@0 25
michael@0 26 /**
michael@0 27 * Registers an event `listener` on a given `element`, that will be called
michael@0 28 * when events of specified `type` is dispatched on the `element`.
michael@0 29 * @param {Element} element
michael@0 30 * Dom element to register listener on.
michael@0 31 * @param {String} type
michael@0 32 * A string representing the
michael@0 33 * [event type](https://developer.mozilla.org/en/DOM/event.type) to
michael@0 34 * listen for.
michael@0 35 * @param {Function} listener
michael@0 36 * Function that is called whenever an event of the specified `type`
michael@0 37 * occurs.
michael@0 38 * @param {Boolean} capture
michael@0 39 * If true, indicates that the user wishes to initiate capture. After
michael@0 40 * initiating capture, all events of the specified type will be dispatched
michael@0 41 * to the registered listener before being dispatched to any `EventTarget`s
michael@0 42 * beneath it in the DOM tree. Events which are bubbling upward through
michael@0 43 * the tree will not trigger a listener designated to use capture.
michael@0 44 * See [DOM Level 3 Events](http://www.w3.org/TR/DOM-Level-3-Events/#event-flow)
michael@0 45 * for a detailed explanation.
michael@0 46 */
michael@0 47 function on(element, type, listener, capture) {
michael@0 48 // `capture` defaults to `false`.
michael@0 49 capture = capture || false;
michael@0 50 element.addEventListener(type, listener, capture);
michael@0 51 }
michael@0 52 exports.on = on;
michael@0 53
michael@0 54 /**
michael@0 55 * Registers an event `listener` on a given `element`, that will be called
michael@0 56 * only once, next time event of specified `type` is dispatched on the
michael@0 57 * `element`.
michael@0 58 * @param {Element} element
michael@0 59 * Dom element to register listener on.
michael@0 60 * @param {String} type
michael@0 61 * A string representing the
michael@0 62 * [event type](https://developer.mozilla.org/en/DOM/event.type) to
michael@0 63 * listen for.
michael@0 64 * @param {Function} listener
michael@0 65 * Function that is called whenever an event of the specified `type`
michael@0 66 * occurs.
michael@0 67 * @param {Boolean} capture
michael@0 68 * If true, indicates that the user wishes to initiate capture. After
michael@0 69 * initiating capture, all events of the specified type will be dispatched
michael@0 70 * to the registered listener before being dispatched to any `EventTarget`s
michael@0 71 * beneath it in the DOM tree. Events which are bubbling upward through
michael@0 72 * the tree will not trigger a listener designated to use capture.
michael@0 73 * See [DOM Level 3 Events](http://www.w3.org/TR/DOM-Level-3-Events/#event-flow)
michael@0 74 * for a detailed explanation.
michael@0 75 */
michael@0 76 function once(element, type, listener, capture) {
michael@0 77 on(element, type, function selfRemovableListener(event) {
michael@0 78 removeListener(element, type, selfRemovableListener, capture);
michael@0 79 listener.apply(this, arguments);
michael@0 80 }, capture);
michael@0 81 }
michael@0 82 exports.once = once;
michael@0 83
michael@0 84 /**
michael@0 85 * Unregisters an event `listener` on a given `element` for the events of the
michael@0 86 * specified `type`.
michael@0 87 *
michael@0 88 * @param {Element} element
michael@0 89 * Dom element to unregister listener from.
michael@0 90 * @param {String} type
michael@0 91 * A string representing the
michael@0 92 * [event type](https://developer.mozilla.org/en/DOM/event.type) to
michael@0 93 * listen for.
michael@0 94 * @param {Function} listener
michael@0 95 * Function that is called whenever an event of the specified `type`
michael@0 96 * occurs.
michael@0 97 * @param {Boolean} capture
michael@0 98 * If true, indicates that the user wishes to initiate capture. After
michael@0 99 * initiating capture, all events of the specified type will be dispatched
michael@0 100 * to the registered listener before being dispatched to any `EventTarget`s
michael@0 101 * beneath it in the DOM tree. Events which are bubbling upward through
michael@0 102 * the tree will not trigger a listener designated to use capture.
michael@0 103 * See [DOM Level 3 Events](http://www.w3.org/TR/DOM-Level-3-Events/#event-flow)
michael@0 104 * for a detailed explanation.
michael@0 105 */
michael@0 106 function removeListener(element, type, listener, capture) {
michael@0 107 element.removeEventListener(type, listener, capture);
michael@0 108 }
michael@0 109 exports.removeListener = removeListener;
michael@0 110
michael@0 111 /**
michael@0 112 * Emits event of the specified `type` and `category` on the given `element`.
michael@0 113 * Specified `settings` are used to initialize event before dispatching it.
michael@0 114 * @param {Element} element
michael@0 115 * Dom element to dispatch event on.
michael@0 116 * @param {String} type
michael@0 117 * A string representing the
michael@0 118 * [event type](https://developer.mozilla.org/en/DOM/event.type).
michael@0 119 * @param {Object} options
michael@0 120 * Options object containing following properties:
michael@0 121 * - `category`: String passed to the `document.createEvent`. Option is
michael@0 122 * optional and defaults to "UIEvents".
michael@0 123 * - `initializer`: If passed it will be used as name of the method used
michael@0 124 * to initialize event. If omitted name will be generated from the
michael@0 125 * `category` field by prefixing it with `"init"` and removing last
michael@0 126 * character if it matches `"s"`.
michael@0 127 * - `settings`: Array of settings that are forwarded to the event
michael@0 128 * initializer after firs `type` argument.
michael@0 129 * @see https://developer.mozilla.org/En/DOM/Document.createEvent
michael@0 130 */
michael@0 131 function emit(element, type, { category, initializer, settings }) {
michael@0 132 category = category || "UIEvents";
michael@0 133 initializer = initializer || getInitializerName(category);
michael@0 134 let document = element.ownerDocument;
michael@0 135 let event = document.createEvent(category);
michael@0 136 event[initializer].apply(event, [type].concat(settings));
michael@0 137 element.dispatchEvent(event);
michael@0 138 };
michael@0 139 exports.emit = emit;

mercurial