events.js 5.8 KB

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