Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event-target.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. function flattenOptions(options) {
  3. if (options !== Object(options)) {
  4. return {
  5. capture: Boolean(options),
  6. once: false,
  7. passive: false
  8. };
  9. }
  10. return {
  11. capture: Boolean(options.capture),
  12. once: Boolean(options.once),
  13. passive: Boolean(options.passive)
  14. };
  15. }
  16. function not(fn) {
  17. return function() {
  18. return !fn.apply(this, arguments);
  19. };
  20. }
  21. function hasListenerFilter(listener, capture) {
  22. return function(listenerSpec) {
  23. return (
  24. listenerSpec.capture === capture &&
  25. listenerSpec.listener === listener
  26. );
  27. };
  28. }
  29. var EventTarget = {
  30. // https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener
  31. addEventListener: function addEventListener(
  32. event,
  33. listener,
  34. providedOptions
  35. ) {
  36. // 3. Let capture, passive, and once be the result of flattening more options.
  37. // Flatten property before executing step 2,
  38. // feture detection is usually based on registering handler with options object,
  39. // that has getter defined
  40. // addEventListener("load", () => {}, {
  41. // get once() { supportsOnce = true; }
  42. // });
  43. var options = flattenOptions(providedOptions);
  44. // 2. If callback is null, then return.
  45. if (listener === null || listener === undefined) {
  46. return;
  47. }
  48. this.eventListeners = this.eventListeners || {};
  49. this.eventListeners[event] = this.eventListeners[event] || [];
  50. // 4. If context object’s associated list of event listener
  51. // does not contain an event listener whose type is type,
  52. // callback is callback, and capture is capture, then append
  53. // a new event listener to it, whose type is type, callback is
  54. // callback, capture is capture, passive is passive, and once is once.
  55. if (
  56. !this.eventListeners[event].some(
  57. hasListenerFilter(listener, options.capture)
  58. )
  59. ) {
  60. this.eventListeners[event].push({
  61. listener: listener,
  62. capture: options.capture,
  63. once: options.once
  64. });
  65. }
  66. },
  67. // https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener
  68. removeEventListener: function removeEventListener(
  69. event,
  70. listener,
  71. providedOptions
  72. ) {
  73. if (!this.eventListeners || !this.eventListeners[event]) {
  74. return;
  75. }
  76. // 2. Let capture be the result of flattening options.
  77. var options = flattenOptions(providedOptions);
  78. // 3. If there is an event listener in the associated list of
  79. // event listeners whose type is type, callback is callback,
  80. // and capture is capture, then set that event listener’s
  81. // removed to true and remove it from the associated list of event listeners.
  82. this.eventListeners[event] = this.eventListeners[event].filter(
  83. not(hasListenerFilter(listener, options.capture))
  84. );
  85. },
  86. dispatchEvent: function dispatchEvent(event) {
  87. if (!this.eventListeners || !this.eventListeners[event.type]) {
  88. return Boolean(event.defaultPrevented);
  89. }
  90. var self = this;
  91. var type = event.type;
  92. var listeners = self.eventListeners[type];
  93. // Remove listeners, that should be dispatched once
  94. // before running dispatch loop to avoid nested dispatch issues
  95. self.eventListeners[type] = listeners.filter(function(listenerSpec) {
  96. return !listenerSpec.once;
  97. });
  98. listeners.forEach(function(listenerSpec) {
  99. var listener = listenerSpec.listener;
  100. if (typeof listener === "function") {
  101. listener.call(self, event);
  102. } else {
  103. listener.handleEvent(event);
  104. }
  105. });
  106. return Boolean(event.defaultPrevented);
  107. }
  108. };
  109. module.exports = EventTarget;