native-document 1.0.161 → 1.0.163

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,62 +26,111 @@ Object.defineProperty(NDElement.prototype, 'nd', {
26
26
  // ----------------------------------------------------------------
27
27
  EVENTS.forEach(eventSourceName => {
28
28
  const eventName = eventSourceName.toLowerCase();
29
- NDElement.prototype['on'+eventSourceName] = function(callback = null) {
30
- this.$element.addEventListener(eventName, callback);
29
+ NDElement.prototype['on'+eventSourceName] = function(callback = null, options = {}) {
30
+ this.$element.addEventListener(eventName, callback, {
31
+ signal: this.$getSignal(),
32
+ ...options
33
+ });
31
34
  return this;
32
35
  };
33
36
  })
34
37
 
35
38
  EVENTS_WITH_STOP.forEach(eventSourceName => {
36
39
  const eventName = eventSourceName.toLowerCase();
37
- NDElement.prototype['onStop'+eventSourceName] = function(callback = null) {
38
- _stop(this.$element, eventName, callback);
40
+ NDElement.prototype['onStop'+eventSourceName] = function(callback = null, options = {}) {
41
+ _stop(this.$element, eventName, callback, {
42
+ signal: this.$getSignal(),
43
+ ...options
44
+ });
39
45
  return this;
40
46
  };
41
- NDElement.prototype['onPreventStop'+eventSourceName] = function(callback = null) {
42
- _preventStop(this.$element, eventName, callback);
47
+ NDElement.prototype['onPreventStop'+eventSourceName] = function(callback = null, options = {}) {
48
+ _preventStop(this.$element, eventName, callback, {
49
+ signal: this.$getSignal(),
50
+ ...options
51
+ });
43
52
  return this;
44
53
  };
45
54
  });
46
55
 
47
56
  EVENTS_WITH_PREVENT.forEach(eventSourceName => {
48
57
  const eventName = eventSourceName.toLowerCase();
49
- NDElement.prototype['onPrevent'+eventSourceName] = function(callback = null) {
50
- _prevent(this.$element, eventName, callback);
58
+ NDElement.prototype['onPrevent'+eventSourceName] = function(callback = null, options = {}) {
59
+ _prevent(this.$element, eventName, callback, {
60
+ signal: this.$getSignal(),
61
+ ...options
62
+ });
51
63
  return this;
52
64
  };
53
65
  });
54
66
 
67
+ NDElement.prototype.$getSignal = function() {
68
+ if(!this.$controller) {
69
+ this.$controller = new AbortController();
70
+ this.beforeUnmount('abort-controller', () => {
71
+ this.$controller.abort();
72
+ this.$controller = null;
73
+ });
74
+ }
75
+ return this.$controller.signal;
76
+ };
77
+
55
78
  NDElement.prototype.on = function(name, callback, options) {
56
- this.$element.addEventListener(name.toLowerCase(), callback, options);
79
+ this.$element.addEventListener(name.toLowerCase(), callback, {
80
+ signal: this.$getSignal(),
81
+ ...options
82
+ });
83
+ return this;
84
+ };
85
+
86
+ NDElement.prototype.off = function(name, callback) {
87
+ this.$element.removeEventListener(name.toLowerCase(), callback);
88
+ return this;
89
+ };
90
+
91
+ NDElement.prototype.once = function(name, callback) {
92
+ this.$element.addEventListener(name.toLowerCase(), callback, {
93
+ signal: this.$getSignal(),
94
+ once: true
95
+ });
96
+ return this;
97
+ };
98
+
99
+ NDElement.prototype.emit = function(name, detail = null) {
100
+ const event = new CustomEvent(name, {
101
+ detail,
102
+ bubbles: true,
103
+ cancelable: true,
104
+ });
105
+ this.$element.dispatchEvent(event);
57
106
  return this;
58
107
  };
59
108
 
60
- const _prevent = function(element, eventName, callback) {
109
+ const _prevent = function(element, eventName, callback, options) {
61
110
  const handler = (event) => {
62
111
  event.preventDefault();
63
112
  callback && callback.call(element, event);
64
113
  };
65
- element.addEventListener(eventName, handler);
114
+ element.addEventListener(eventName, handler, options);
66
115
  return this;
67
116
  }
68
117
 
69
- const _stop = function(element, eventName, callback) {
118
+ const _stop = function(element, eventName, callback, options) {
70
119
  const handler = (event) => {
71
120
  event.stopPropagation();
72
121
  callback && callback.call(element, event);
73
122
  };
74
- element.addEventListener(eventName, handler);
123
+ element.addEventListener(eventName, handler, options);
75
124
  return this;
76
125
  };
77
126
 
78
- const _preventStop = function(element, eventName, callback) {
127
+ const _preventStop = function(element, eventName, callback, options) {
79
128
  const handler = (event) => {
80
129
  event.stopPropagation();
81
130
  event.preventDefault();
82
131
  callback && callback.call(element, event);
83
132
  };
84
- element.addEventListener(eventName, handler);
133
+ element.addEventListener(eventName, handler, options);
85
134
  return this;
86
135
  };
87
136