版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

103 rader
3.5 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * Handles events.
  5. */
  6. class EventTarget {
  7. constructor() {
  8. this._listeners = {};
  9. this._listenerOptions = {};
  10. }
  11. /**
  12. * Adds an event listener.
  13. *
  14. * @param type Event type.
  15. * @param listener Listener.
  16. * @param options An object that specifies characteristics about the event listener.(currently only once)
  17. * @param options.once
  18. */
  19. addEventListener(type, listener, options) {
  20. this._listeners[type] = this._listeners[type] || [];
  21. this._listenerOptions[type] = this._listenerOptions[type] || [];
  22. this._listeners[type].push(listener);
  23. this._listenerOptions[type].push(options || null);
  24. }
  25. /**
  26. * Adds an event listener.
  27. *
  28. * @param type Event type.
  29. * @param listener Listener.
  30. */
  31. removeEventListener(type, listener) {
  32. if (this._listeners[type]) {
  33. const index = this._listeners[type].indexOf(listener);
  34. if (index !== -1) {
  35. this._listeners[type].splice(index, 1);
  36. this._listenerOptions[type].splice(index, 1);
  37. }
  38. }
  39. }
  40. /**
  41. * Dispatches an event.
  42. *
  43. * @param event Event.
  44. * @returns The return value is false if event is cancelable and at least one of the event handlers which handled this event called Event.preventDefault().
  45. */
  46. dispatchEvent(event) {
  47. if (!event._target) {
  48. event._target = this;
  49. }
  50. event._currentTarget = this;
  51. const onEventName = 'on' + event.type.toLowerCase();
  52. if (typeof this[onEventName] === 'function') {
  53. this[onEventName].call(this, event);
  54. }
  55. if (this._listeners[event.type]) {
  56. for (let i = 0, max = this._listeners[event.type].length; i < max; i++) {
  57. const listener = this._listeners[event.type][i];
  58. const options = this._listenerOptions[event.type][i];
  59. if (listener.handleEvent) {
  60. listener.handleEvent(event);
  61. }
  62. else {
  63. listener.call(this, event);
  64. }
  65. if (options?.once) {
  66. this.removeEventListener(event.type, listener);
  67. }
  68. if (event._immediatePropagationStopped) {
  69. return !(event.cancelable && event.defaultPrevented);
  70. }
  71. }
  72. }
  73. return !(event.cancelable && event.defaultPrevented);
  74. }
  75. /**
  76. * Adds an event listener.
  77. *
  78. * TODO:
  79. * Was used by with IE8- and Opera. React believed Happy DOM was a legacy browser and used them, but that is no longer the case, so we should remove this method after that this is verified.
  80. *
  81. * @deprecated
  82. * @param type Event type.
  83. * @param listener Listener.
  84. */
  85. attachEvent(type, listener) {
  86. this.addEventListener(type.replace('on', ''), listener);
  87. }
  88. /**
  89. * Removes an event listener.
  90. *
  91. * TODO:
  92. * Was used by IE8- and Opera. React believed Happy DOM was a legacy browser and used them, but that is no longer the case, so we should remove this method after that this is verified.
  93. *
  94. * @deprecated
  95. * @param type Event type.
  96. * @param listener Listener.
  97. */
  98. detachEvent(type, listener) {
  99. this.removeEventListener(type.replace('on', ''), listener);
  100. }
  101. }
  102. exports.default = EventTarget;
  103. //# sourceMappingURL=EventTarget.js.map