@whitesev/domutils 1.7.5 → 1.8.1

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.
package/dist/index.esm.js CHANGED
@@ -113,47 +113,7 @@ const LAST_NUMBER_WEAK_MAP = new WeakMap();
113
113
  const cache = createCache(LAST_NUMBER_WEAK_MAP);
114
114
  const generateUniqueNumber = createGenerateUniqueNumber(cache, LAST_NUMBER_WEAK_MAP);
115
115
 
116
- const isMessagePort = (sender) => {
117
- return typeof sender.start === 'function';
118
- };
119
-
120
- const PORT_MAP = new WeakMap();
121
-
122
- const extendBrokerImplementation = (partialBrokerImplementation) => ({
123
- ...partialBrokerImplementation,
124
- connect: ({ call }) => {
125
- return async () => {
126
- const { port1, port2 } = new MessageChannel();
127
- const portId = await call('connect', { port: port1 }, [port1]);
128
- PORT_MAP.set(port2, portId);
129
- return port2;
130
- };
131
- },
132
- disconnect: ({ call }) => {
133
- return async (port) => {
134
- const portId = PORT_MAP.get(port);
135
- if (portId === undefined) {
136
- throw new Error('The given port is not connected.');
137
- }
138
- await call('disconnect', { portId });
139
- };
140
- },
141
- isSupported: ({ call }) => {
142
- return () => call('isSupported');
143
- }
144
- });
145
-
146
- const ONGOING_REQUESTS = new WeakMap();
147
- const createOrGetOngoingRequests = (sender) => {
148
- if (ONGOING_REQUESTS.has(sender)) {
149
- // @todo TypeScript needs to be convinced that has() works as expected.
150
- return ONGOING_REQUESTS.get(sender);
151
- }
152
- const ongoingRequests = new Map();
153
- ONGOING_REQUESTS.set(sender, ongoingRequests);
154
- return ongoingRequests;
155
- };
156
- const createBroker = (brokerImplementation) => {
116
+ const createBrokerFactory = (createOrGetOngoingRequests, extendBrokerImplementation, generateUniqueNumber, isMessagePort) => (brokerImplementation) => {
157
117
  const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
158
118
  return (sender) => {
159
119
  const ongoingRequests = createOrGetOngoingRequests(sender);
@@ -196,81 +156,115 @@ const createBroker = (brokerImplementation) => {
196
156
  };
197
157
  };
198
158
 
199
- // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
200
- const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
201
- const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
202
- const wrap = createBroker({
203
- clearInterval: ({ call }) => {
204
- return (timerId) => {
205
- if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
206
- scheduledIntervalsState.set(timerId, null);
207
- call('clear', { timerId, timerType: 'interval' }).then(() => {
208
- scheduledIntervalsState.delete(timerId);
209
- });
210
- }
159
+ const createCreateOrGetOngoingRequests = (ongoingRequestsMap) => (sender) => {
160
+ if (ongoingRequestsMap.has(sender)) {
161
+ // @todo TypeScript needs to be convinced that has() works as expected.
162
+ return ongoingRequestsMap.get(sender);
163
+ }
164
+ const ongoingRequests = new Map();
165
+ ongoingRequestsMap.set(sender, ongoingRequests);
166
+ return ongoingRequests;
167
+ };
168
+
169
+ const createExtendBrokerImplementation = (portMap) => (partialBrokerImplementation) => ({
170
+ ...partialBrokerImplementation,
171
+ connect: ({ call }) => {
172
+ return async () => {
173
+ const { port1, port2 } = new MessageChannel();
174
+ const portId = await call('connect', { port: port1 }, [port1]);
175
+ portMap.set(port2, portId);
176
+ return port2;
211
177
  };
212
178
  },
213
- clearTimeout: ({ call }) => {
214
- return (timerId) => {
215
- if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
216
- scheduledTimeoutsState.set(timerId, null);
217
- call('clear', { timerId, timerType: 'timeout' }).then(() => {
218
- scheduledTimeoutsState.delete(timerId);
219
- });
179
+ disconnect: ({ call }) => {
180
+ return async (port) => {
181
+ const portId = portMap.get(port);
182
+ if (portId === undefined) {
183
+ throw new Error('The given port is not connected.');
220
184
  }
185
+ await call('disconnect', { portId });
221
186
  };
222
187
  },
223
- setInterval: ({ call }) => {
224
- return (func, delay = 0, ...args) => {
225
- const symbol = Symbol();
226
- const timerId = generateUniqueNumber(scheduledIntervalsState);
227
- scheduledIntervalsState.set(timerId, symbol);
228
- const schedule = () => call('set', {
229
- delay,
230
- now: performance.timeOrigin + performance.now(),
231
- timerId,
232
- timerType: 'interval'
233
- }).then(() => {
234
- const state = scheduledIntervalsState.get(timerId);
235
- if (state === undefined) {
236
- throw new Error('The timer is in an undefined state.');
237
- }
238
- if (state === symbol) {
239
- func(...args);
240
- // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
241
- if (scheduledIntervalsState.get(timerId) === symbol) {
242
- schedule();
243
- }
244
- }
245
- });
246
- schedule();
247
- return timerId;
248
- };
249
- },
250
- setTimeout: ({ call }) => {
251
- return (func, delay = 0, ...args) => {
252
- const symbol = Symbol();
253
- const timerId = generateUniqueNumber(scheduledTimeoutsState);
254
- scheduledTimeoutsState.set(timerId, symbol);
255
- call('set', {
256
- delay,
257
- now: performance.timeOrigin + performance.now(),
258
- timerId,
259
- timerType: 'timeout'
260
- }).then(() => {
261
- const state = scheduledTimeoutsState.get(timerId);
262
- if (state === undefined) {
263
- throw new Error('The timer is in an undefined state.');
264
- }
265
- if (state === symbol) {
266
- // A timeout can be savely deleted because it is only called once.
267
- scheduledTimeoutsState.delete(timerId);
268
- func(...args);
269
- }
270
- });
271
- return timerId;
272
- };
188
+ isSupported: ({ call }) => {
189
+ return () => call('isSupported');
190
+ }
191
+ });
192
+
193
+ const isMessagePort = (sender) => {
194
+ return typeof sender.start === 'function';
195
+ };
196
+
197
+ const createBroker = createBrokerFactory(createCreateOrGetOngoingRequests(new WeakMap()), createExtendBrokerImplementation(new WeakMap()), generateUniqueNumber, isMessagePort);
198
+
199
+ const createClearIntervalFactory = (scheduledIntervalsState) => (clear) => (timerId) => {
200
+ if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
201
+ scheduledIntervalsState.set(timerId, null);
202
+ clear(timerId).then(() => {
203
+ scheduledIntervalsState.delete(timerId);
204
+ });
273
205
  }
206
+ };
207
+
208
+ const createClearTimeoutFactory = (scheduledTimeoutsState) => (clear) => (timerId) => {
209
+ if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
210
+ scheduledTimeoutsState.set(timerId, null);
211
+ clear(timerId).then(() => {
212
+ scheduledTimeoutsState.delete(timerId);
213
+ });
214
+ }
215
+ };
216
+
217
+ const createSetIntervalFactory = (generateUniqueNumber, scheduledIntervalsState) => (set) => (func, delay = 0, ...args) => {
218
+ const symbol = Symbol();
219
+ const timerId = generateUniqueNumber(scheduledIntervalsState);
220
+ scheduledIntervalsState.set(timerId, symbol);
221
+ const schedule = () => set(delay, timerId).then(() => {
222
+ const state = scheduledIntervalsState.get(timerId);
223
+ if (state === undefined) {
224
+ throw new Error('The timer is in an undefined state.');
225
+ }
226
+ if (state === symbol) {
227
+ func(...args);
228
+ // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
229
+ if (scheduledIntervalsState.get(timerId) === symbol) {
230
+ schedule();
231
+ }
232
+ }
233
+ });
234
+ schedule();
235
+ return timerId;
236
+ };
237
+
238
+ const createSetTimeoutFactory = (generateUniqueNumber, scheduledTimeoutsState) => (set) => (func, delay = 0, ...args) => {
239
+ const symbol = Symbol();
240
+ const timerId = generateUniqueNumber(scheduledTimeoutsState);
241
+ scheduledTimeoutsState.set(timerId, symbol);
242
+ set(delay, timerId).then(() => {
243
+ const state = scheduledTimeoutsState.get(timerId);
244
+ if (state === undefined) {
245
+ throw new Error('The timer is in an undefined state.');
246
+ }
247
+ if (state === symbol) {
248
+ // A timeout can be savely deleted because it is only called once.
249
+ scheduledTimeoutsState.delete(timerId);
250
+ func(...args);
251
+ }
252
+ });
253
+ return timerId;
254
+ };
255
+
256
+ // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
257
+ const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
258
+ const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
259
+ const createClearInterval = createClearIntervalFactory(scheduledIntervalsState);
260
+ const createClearTimeout = createClearTimeoutFactory(scheduledTimeoutsState);
261
+ const createSetInterval = createSetIntervalFactory(generateUniqueNumber, scheduledIntervalsState);
262
+ const createSetTimeout = createSetTimeoutFactory(generateUniqueNumber, scheduledTimeoutsState);
263
+ const wrap = createBroker({
264
+ clearInterval: ({ call }) => createClearInterval((timerId) => call('clear', { timerId, timerType: 'interval' })),
265
+ clearTimeout: ({ call }) => createClearTimeout((timerId) => call('clear', { timerId, timerType: 'timeout' })),
266
+ setInterval: ({ call }) => createSetInterval((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'interval' })),
267
+ setTimeout: ({ call }) => createSetTimeout((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'timeout' }))
274
268
  });
275
269
  const load = (url) => {
276
270
  const worker = new Worker(url);
@@ -293,7 +287,7 @@ const createLoadOrReturnBroker = (loadBroker, worker) => {
293
287
  };
294
288
 
295
289
  // This is the minified and stringified code of the worker-timers-worker package.
296
- const worker = `(()=>{var e={455:function(e,t){!function(e){"use strict";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,s=2*o,a=function(e,t){return function(r){var a=t.get(r),i=void 0===a?r.size:a<s?a+1:0;if(!r.has(i))return e(r,i);if(r.size<o){for(;r.has(i);)i=Math.floor(Math.random()*s);return e(r,i)}if(r.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;r.has(i);)i=Math.floor(Math.random()*n);return e(r,i)}},i=new WeakMap,u=r(i),c=a(u,i),l=t(c);e.addUniqueNumber=l,e.generateUniqueNumber=c}(t)}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}(()=>{"use strict";const e=-32603,t=-32602,n=-32601,o=(e,t)=>Object.assign(new Error(e),{status:t}),s=t=>o('The handler of the method called "'.concat(t,'" returned an unexpected result.'),e),a=(t,r)=>async({data:{id:a,method:i,params:u}})=>{const c=r[i];try{if(void 0===c)throw(e=>o('The requested method called "'.concat(e,'" is not supported.'),n))(i);const r=void 0===u?c():c(u);if(void 0===r)throw(t=>o('The handler of the method called "'.concat(t,'" returned no required result.'),e))(i);const l=r instanceof Promise?await r:r;if(null===a){if(void 0!==l.result)throw s(i)}else{if(void 0===l.result)throw s(i);const{result:e,transferables:r=[]}=l;t.postMessage({id:a,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:a})}};var i=r(455);const u=new Map,c=(e,r,n)=>({...r,connect:({port:t})=>{t.start();const n=e(t,r),o=(0,i.generateUniqueNumber)(u);return u.set(o,()=>{n(),t.close(),u.delete(o)}),{result:o}},disconnect:({portId:e})=>{const r=u.get(e);if(void 0===r)throw(e=>o('The specified parameter called "portId" with the given value "'.concat(e,'" does not identify a port connected to this worker.'),t))(e);return r(),{result:null}},isSupported:async()=>{if(await new Promise(e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=({data:t})=>e(null!==t),n.postMessage(t,[t])})){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),l=(e,t,r=()=>!0)=>{const n=c(l,t,r),o=a(e,n);return e.addEventListener("message",o),()=>e.removeEventListener("message",o)},d=(e,t)=>r=>{const n=t.get(r);if(void 0===n)return Promise.resolve(!1);const[o,s]=n;return e(o),t.delete(r),s(!1),Promise.resolve(!0)},f=(e,t,r,n)=>(o,s,a)=>{const i=o+s-t.timeOrigin,u=i-t.now();return new Promise(t=>{e.set(a,[r(n,u,i,e,t,a),t])})},m=new Map,h=d(globalThis.clearTimeout,m),p=new Map,v=d(globalThis.clearTimeout,p),w=((e,t)=>{const r=(n,o,s,a)=>{const i=n-e.now();i>0?o.set(a,[t(r,i,n,o,s,a),s]):(o.delete(a),s(!0))};return r})(performance,globalThis.setTimeout),g=f(m,performance,globalThis.setTimeout,w),T=f(p,performance,globalThis.setTimeout,w);l(self,{clear:async({timerId:e,timerType:t})=>({result:await("interval"===t?h(e):v(e))}),set:async({delay:e,now:t,timerId:r,timerType:n})=>({result:await("interval"===n?g:T)(e,t,r)})})})()})();`; // tslint:disable-line:max-line-length
290
+ const worker = `(()=>{var e={455(e,t){!function(e){"use strict";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,s=2*o,a=function(e,t){return function(r){var a=t.get(r),i=void 0===a?r.size:a<s?a+1:0;if(!r.has(i))return e(r,i);if(r.size<o){for(;r.has(i);)i=Math.floor(Math.random()*s);return e(r,i)}if(r.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;r.has(i);)i=Math.floor(Math.random()*n);return e(r,i)}},i=new WeakMap,u=r(i),c=a(u,i),l=t(c);e.addUniqueNumber=l,e.generateUniqueNumber=c}(t)}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}(()=>{"use strict";const e=-32603,t=-32602,n=-32601,o=(e,t)=>Object.assign(new Error(e),{status:t}),s=t=>o('The handler of the method called "'.concat(t,'" returned an unexpected result.'),e),a=(t,r)=>async({data:{id:a,method:i,params:u}})=>{const c=r[i];try{if(void 0===c)throw(e=>o('The requested method called "'.concat(e,'" is not supported.'),n))(i);const r=void 0===u?c():c(u);if(void 0===r)throw(t=>o('The handler of the method called "'.concat(t,'" returned no required result.'),e))(i);const l=r instanceof Promise?await r:r;if(null===a){if(void 0!==l.result)throw s(i)}else{if(void 0===l.result)throw s(i);const{result:e,transferables:r=[]}=l;t.postMessage({id:a,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:a})}};var i=r(455);const u=new Map,c=(e,r,n)=>({...r,connect:({port:t})=>{t.start();const n=e(t,r),o=(0,i.generateUniqueNumber)(u);return u.set(o,()=>{n(),t.close(),u.delete(o)}),{result:o}},disconnect:({portId:e})=>{const r=u.get(e);if(void 0===r)throw(e=>o('The specified parameter called "portId" with the given value "'.concat(e,'" does not identify a port connected to this worker.'),t))(e);return r(),{result:null}},isSupported:async()=>{if(await new Promise(e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=({data:t})=>e(null!==t),n.postMessage(t,[t])})){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),l=(e,t,r=()=>!0)=>{const n=c(l,t,r),o=a(e,n);return e.addEventListener("message",o),()=>e.removeEventListener("message",o)},d=(e,t)=>r=>{const n=t.get(r);if(void 0===n)return Promise.resolve(!1);const[o,s]=n;return e(o),t.delete(r),s(!1),Promise.resolve(!0)},m=(e,t,r,n)=>(o,s,a)=>{const i=o+s-t.timeOrigin,u=i-t.now();return new Promise(t=>{e.set(a,[r(n,u,i,e,t,a),t])})},f=new Map,h=d(globalThis.clearTimeout,f),p=new Map,v=d(globalThis.clearTimeout,p),w=((e,t)=>{const r=(n,o,s,a)=>{const i=n-e.now();i>0?o.set(a,[t(r,i,n,o,s,a),s]):(o.delete(a),s(!0))};return r})(performance,globalThis.setTimeout),g=m(f,performance,globalThis.setTimeout,w),T=m(p,performance,globalThis.setTimeout,w);l(self,{clear:async({timerId:e,timerType:t})=>({result:await("interval"===t?h(e):v(e))}),set:async({delay:e,now:t,timerId:r,timerType:n})=>({result:await("interval"===n?g:T)(e,t,r)})})})()})();`; // tslint:disable-line:max-line-length
297
291
 
298
292
  const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
299
293
  const clearInterval$1 = (timerId) => loadOrReturnBroker().clearInterval(timerId);
@@ -521,13 +515,7 @@ const CommonUtils = {
521
515
  },
522
516
  };
523
517
 
524
- const version = "1.7.5";
525
-
526
- /* 数据 */
527
- const GlobalData = {
528
- /** .on添加在元素存储的事件 */
529
- domEventSymbol: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
530
- };
518
+ const version = "1.8.1";
531
519
 
532
520
  class ElementSelector {
533
521
  windowApi;
@@ -1620,6 +1608,12 @@ class ElementAnimate extends ElementWait {
1620
1608
  }
1621
1609
  new ElementAnimate();
1622
1610
 
1611
+ /* 数据 */
1612
+ const GlobalData = {
1613
+ /** .on添加在元素存储的事件 */
1614
+ domEventSymbol: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
1615
+ };
1616
+
1623
1617
  const OriginPrototype = {
1624
1618
  Object: {
1625
1619
  defineProperty: Object.defineProperty,
@@ -1679,7 +1673,7 @@ class ElementEvent extends ElementAnimate {
1679
1673
  if (element == null) {
1680
1674
  return {
1681
1675
  off() { },
1682
- trigger() { },
1676
+ emit() { },
1683
1677
  };
1684
1678
  }
1685
1679
  let $elList = [];
@@ -1814,10 +1808,10 @@ class ElementEvent extends ElementAnimate {
1814
1808
  /**
1815
1809
  * 主动触发事件
1816
1810
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
1817
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
1811
+ * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
1818
1812
  */
1819
- trigger: (details, useDispatchToTriggerEvent) => {
1820
- that.trigger($elList, eventTypeList, details, useDispatchToTriggerEvent);
1813
+ emit: (details, useDispatchToEmit) => {
1814
+ that.emit($elList, eventTypeList, details, useDispatchToEmit);
1821
1815
  },
1822
1816
  };
1823
1817
  }
@@ -1996,7 +1990,7 @@ class ElementEvent extends ElementAnimate {
1996
1990
  });
1997
1991
  });
1998
1992
  }
1999
- ready(...args) {
1993
+ onReady(...args) {
2000
1994
  const callback = args[0];
2001
1995
  // 异步回调
2002
1996
  let resolve = void 0;
@@ -2090,16 +2084,16 @@ class ElementEvent extends ElementAnimate {
2090
2084
  * @param element 需要触发的元素|元素数组|window
2091
2085
  * @param eventType 需要触发的事件
2092
2086
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
2093
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
2087
+ * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
2094
2088
  * @example
2095
2089
  * // 触发元素a.xx的click事件
2096
- * DOMUtils.trigger(document.querySelector("a.xx"),"click")
2097
- * DOMUtils.trigger("a.xx","click")
2090
+ * DOMUtils.emit(document.querySelector("a.xx"),"click")
2091
+ * DOMUtils.emit("a.xx","click")
2098
2092
  * // 触发元素a.xx的click、tap、hover事件
2099
- * DOMUtils.trigger(document.querySelector("a.xx"),"click tap hover")
2100
- * DOMUtils.trigger("a.xx",["click","tap","hover"])
2093
+ * DOMUtils.emit(document.querySelector("a.xx"),"click tap hover")
2094
+ * DOMUtils.emit("a.xx",["click","tap","hover"])
2101
2095
  */
2102
- trigger(element, eventType, details, useDispatchToTriggerEvent = true) {
2096
+ emit(element, eventType, details, useDispatchToEmit = true) {
2103
2097
  const that = this;
2104
2098
  if (typeof element === "string") {
2105
2099
  element = that.selectorAll(element);
@@ -2141,7 +2135,7 @@ class ElementEvent extends ElementAnimate {
2141
2135
  });
2142
2136
  }
2143
2137
  }
2144
- if (useDispatchToTriggerEvent == false && eventTypeItem in elementEvents) {
2138
+ if (useDispatchToEmit == false && eventTypeItem in elementEvents) {
2145
2139
  // 直接调用监听的事件
2146
2140
  elementEvents[eventTypeItem].forEach((eventsItem) => {
2147
2141
  eventsItem.callback(event);
@@ -2154,11 +2148,11 @@ class ElementEvent extends ElementAnimate {
2154
2148
  });
2155
2149
  }
2156
2150
  /**
2157
- * 绑定或触发元素的click事件
2151
+ * 监听或触发元素的click事件
2158
2152
  * @param element 目标元素
2159
2153
  * @param handler (可选)事件处理函数
2160
2154
  * @param details (可选)赋予触发的Event的额外属性
2161
- * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true
2155
+ * @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
2162
2156
  * @example
2163
2157
  * // 触发元素a.xx的click事件
2164
2158
  * DOMUtils.click(document.querySelector("a.xx"))
@@ -2167,7 +2161,7 @@ class ElementEvent extends ElementAnimate {
2167
2161
  * console.log("触发click事件成功")
2168
2162
  * })
2169
2163
  * */
2170
- click(element, handler, details, useDispatchToTriggerEvent) {
2164
+ click(element, handler, details, useDispatchToEmit) {
2171
2165
  const that = this;
2172
2166
  if (typeof element === "string") {
2173
2167
  element = that.selectorAll(element);
@@ -2178,23 +2172,23 @@ class ElementEvent extends ElementAnimate {
2178
2172
  if (CommonUtils.isNodeList(element)) {
2179
2173
  // 设置
2180
2174
  element.forEach(($ele) => {
2181
- that.click($ele, handler, details, useDispatchToTriggerEvent);
2175
+ that.click($ele, handler, details, useDispatchToEmit);
2182
2176
  });
2183
2177
  return;
2184
2178
  }
2185
2179
  if (handler == null) {
2186
- that.trigger(element, "click", details, useDispatchToTriggerEvent);
2180
+ that.emit(element, "click", details, useDispatchToEmit);
2187
2181
  }
2188
2182
  else {
2189
2183
  that.on(element, "click", null, handler);
2190
2184
  }
2191
2185
  }
2192
2186
  /**
2193
- * 绑定或触发元素的blur事件
2187
+ * 监听或触发元素的blur事件
2194
2188
  * @param element 目标元素
2195
2189
  * @param handler (可选)事件处理函数
2196
2190
  * @param details (可选)赋予触发的Event的额外属性
2197
- * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true
2191
+ * @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
2198
2192
  * @example
2199
2193
  * // 触发元素a.xx的blur事件
2200
2194
  * DOMUtils.blur(document.querySelector("a.xx"))
@@ -2203,7 +2197,7 @@ class ElementEvent extends ElementAnimate {
2203
2197
  * console.log("触发blur事件成功")
2204
2198
  * })
2205
2199
  * */
2206
- blur(element, handler, details, useDispatchToTriggerEvent) {
2200
+ blur(element, handler, details, useDispatchToEmit) {
2207
2201
  const that = this;
2208
2202
  if (typeof element === "string") {
2209
2203
  element = that.selectorAll(element);
@@ -2214,23 +2208,23 @@ class ElementEvent extends ElementAnimate {
2214
2208
  if (CommonUtils.isNodeList(element)) {
2215
2209
  // 设置
2216
2210
  element.forEach(($ele) => {
2217
- that.focus($ele, handler, details, useDispatchToTriggerEvent);
2211
+ that.focus($ele, handler, details, useDispatchToEmit);
2218
2212
  });
2219
2213
  return;
2220
2214
  }
2221
2215
  if (handler === null) {
2222
- that.trigger(element, "blur", details, useDispatchToTriggerEvent);
2216
+ that.emit(element, "blur", details, useDispatchToEmit);
2223
2217
  }
2224
2218
  else {
2225
2219
  that.on(element, "blur", null, handler);
2226
2220
  }
2227
2221
  }
2228
2222
  /**
2229
- * 绑定或触发元素的focus事件
2223
+ * 监听或触发元素的focus事件
2230
2224
  * @param element 目标元素
2231
2225
  * @param handler (可选)事件处理函数
2232
2226
  * @param details (可选)赋予触发的Event的额外属性
2233
- * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true
2227
+ * @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
2234
2228
  * @example
2235
2229
  * // 触发元素a.xx的focus事件
2236
2230
  * DOMUtils.focus(document.querySelector("a.xx"))
@@ -2239,7 +2233,7 @@ class ElementEvent extends ElementAnimate {
2239
2233
  * console.log("触发focus事件成功")
2240
2234
  * })
2241
2235
  * */
2242
- focus(element, handler, details, useDispatchToTriggerEvent) {
2236
+ focus(element, handler, details, useDispatchToEmit) {
2243
2237
  const that = this;
2244
2238
  if (typeof element === "string") {
2245
2239
  element = that.selectorAll(element);
@@ -2250,12 +2244,12 @@ class ElementEvent extends ElementAnimate {
2250
2244
  if (CommonUtils.isNodeList(element)) {
2251
2245
  // 设置
2252
2246
  element.forEach(($ele) => {
2253
- that.focus($ele, handler, details, useDispatchToTriggerEvent);
2247
+ that.focus($ele, handler, details, useDispatchToEmit);
2254
2248
  });
2255
2249
  return;
2256
2250
  }
2257
2251
  if (handler == null) {
2258
- that.trigger(element, "focus", details, useDispatchToTriggerEvent);
2252
+ that.emit(element, "focus", details, useDispatchToEmit);
2259
2253
  }
2260
2254
  else {
2261
2255
  that.on(element, "focus", null, handler);
@@ -2268,14 +2262,14 @@ class ElementEvent extends ElementAnimate {
2268
2262
  * @param option 配置
2269
2263
  * @example
2270
2264
  * // 监听a.xx元素的移入或移出
2271
- * DOMUtils.hover(document.querySelector("a.xx"),()=>{
2265
+ * DOMUtils.onHover(document.querySelector("a.xx"),()=>{
2272
2266
  * console.log("移入/移除");
2273
2267
  * })
2274
- * DOMUtils.hover("a.xx",()=>{
2268
+ * DOMUtils.onHover("a.xx",()=>{
2275
2269
  * console.log("移入/移除");
2276
2270
  * })
2277
2271
  */
2278
- hover(element, handler, option) {
2272
+ onHover(element, handler, option) {
2279
2273
  const that = this;
2280
2274
  if (typeof element === "string") {
2281
2275
  element = that.selectorAll(element);
@@ -2286,7 +2280,7 @@ class ElementEvent extends ElementAnimate {
2286
2280
  if (CommonUtils.isNodeList(element)) {
2287
2281
  // 设置
2288
2282
  element.forEach(($ele) => {
2289
- that.hover($ele, handler, option);
2283
+ that.onHover($ele, handler, option);
2290
2284
  });
2291
2285
  return;
2292
2286
  }
@@ -2294,12 +2288,12 @@ class ElementEvent extends ElementAnimate {
2294
2288
  that.on(element, "mouseleave", null, handler, option);
2295
2289
  }
2296
2290
  /**
2297
- * 当动画结束时触发事件
2291
+ * 监听动画结束
2298
2292
  * @param element 监听的元素
2299
2293
  * @param handler 触发的回调函数
2300
2294
  * @param option 配置项,这里默认配置once为true
2301
2295
  */
2302
- animationend(element, handler, option) {
2296
+ onAnimationend(element, handler, option) {
2303
2297
  const that = this;
2304
2298
  if (typeof element === "string") {
2305
2299
  element = that.selector(element);
@@ -2322,12 +2316,12 @@ class ElementEvent extends ElementAnimate {
2322
2316
  }
2323
2317
  }
2324
2318
  /**
2325
- * 当过渡结束时触发事件
2319
+ * 监听过渡结束
2326
2320
  * @param element 监听的元素
2327
2321
  * @param handler 触发的回调函数
2328
2322
  * @param option 配置项,这里默认配置once为true
2329
2323
  */
2330
- transitionend(element, handler, option) {
2324
+ onTransitionend(element, handler, option) {
2331
2325
  const that = this;
2332
2326
  if (typeof element === "string") {
2333
2327
  element = that.selector(element);
@@ -2364,7 +2358,7 @@ class ElementEvent extends ElementAnimate {
2364
2358
  * console.log("按键松开");
2365
2359
  * })
2366
2360
  */
2367
- keyup(element, handler, option) {
2361
+ onKeyup(element, handler, option) {
2368
2362
  const that = this;
2369
2363
  if (element == null) {
2370
2364
  return;
@@ -2375,7 +2369,7 @@ class ElementEvent extends ElementAnimate {
2375
2369
  if (CommonUtils.isNodeList(element)) {
2376
2370
  // 设置
2377
2371
  element.forEach(($ele) => {
2378
- that.keyup($ele, handler, option);
2372
+ that.onKeyup($ele, handler, option);
2379
2373
  });
2380
2374
  return;
2381
2375
  }
@@ -2396,7 +2390,7 @@ class ElementEvent extends ElementAnimate {
2396
2390
  * console.log("按键按下");
2397
2391
  * })
2398
2392
  */
2399
- keydown(element, handler, option) {
2393
+ onKeydown(element, handler, option) {
2400
2394
  const that = this;
2401
2395
  if (element == null) {
2402
2396
  return;
@@ -2407,7 +2401,7 @@ class ElementEvent extends ElementAnimate {
2407
2401
  if (CommonUtils.isNodeList(element)) {
2408
2402
  // 设置
2409
2403
  element.forEach(($ele) => {
2410
- that.keydown($ele, handler, option);
2404
+ that.onKeydown($ele, handler, option);
2411
2405
  });
2412
2406
  return;
2413
2407
  }
@@ -2428,7 +2422,7 @@ class ElementEvent extends ElementAnimate {
2428
2422
  * console.log("按键按下");
2429
2423
  * })
2430
2424
  */
2431
- keypress(element, handler, option) {
2425
+ onKeypress(element, handler, option) {
2432
2426
  const that = this;
2433
2427
  if (element == null) {
2434
2428
  return;
@@ -2439,76 +2433,76 @@ class ElementEvent extends ElementAnimate {
2439
2433
  if (CommonUtils.isNodeList(element)) {
2440
2434
  // 设置
2441
2435
  element.forEach(($ele) => {
2442
- that.keypress($ele, handler, option);
2436
+ that.onKeypress($ele, handler, option);
2443
2437
  });
2444
2438
  return;
2445
2439
  }
2446
2440
  that.on(element, "keypress", null, handler, option);
2447
2441
  }
2448
2442
  /**
2449
- * 监听某个元素键盘按键事件或window全局按键事件
2450
- * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
2451
- * @param element 需要监听的对象,可以是全局Window或者某个元素
2452
- * @param eventName 事件名,默认keypress
2453
- * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
2443
+ * 监听某个元素键盘按键事件或window全局按键事件
2444
+ * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
2445
+ * @param element 需要监听的对象,可以是全局Window或者某个元素
2446
+ * @param eventName 事件名,默认keypress,keydown - > keypress - > keyup
2447
+ * @param handler 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
2454
2448
  * @param options 监听事件的配置
2455
- * @example
2456
- Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
2457
- if(keyName === "Enter"){
2458
- console.log("回车按键的值是:"+keyValue)
2459
- }
2460
- if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
2461
- console.log("Ctrl和回车键");
2462
- }
2463
- })
2464
- * @example
2465
- 字母和数字键的键码值(keyCode)
2466
- 按键 键码 按键 键码 按键 键码 按键 键码
2467
- A 65 J 74 S 83 1 49
2468
- B 66 K 75 T 84 2 50
2469
- C 67 L 76 U 85 3 51
2470
- D 68 M 77 V 86 4 52
2471
- E 69 N 78 W 87 5 53
2472
- F 70 O 79 X 88 6 54
2473
- G 71 P 80 Y 89 7 55
2474
- H 72 Q 81 Z 90 8 56
2475
- I 73 R 82 0 48 9 57
2476
-
2477
- 数字键盘上的键的键码值(keyCode)
2478
- 功能键键码值(keyCode)
2479
- 按键 键码 按键 键码 按键 键码 按键 键码
2480
- 0 96 8 104 F1 112 F7 118
2481
- 1 97 9 105 F2 113 F8 119
2482
- 2 98 * 106 F3 114 F9 120
2483
- 3 99 + 107 F4 115 F10 121
2484
- 4 100 Enter 108 F5 116 F11 122
2485
- 5 101 - 109 F6 117 F12 123
2486
- 6 102 . 110
2487
- 7 103 / 111
2488
-
2489
- 控制键键码值(keyCode)
2490
- 按键 键码 按键 键码 按键 键码 按键 键码
2491
- BackSpace 8 Esc 27 → 39 -_ 189
2492
- Tab 9 Spacebar 32 ↓ 40 .> 190
2493
- Clear 12 Page Up 33 Insert 45 /? 191
2494
- Enter 13 Page Down 34 Delete 46 `~ 192
2495
- Shift 16 End 35 Num Lock 144 [{ 219
2496
- Control 17 Home 36 ;: 186 \| 220
2497
- Alt 18 ← 37 =+ 187 ]} 221
2498
- Cape Lock 20 ↑ 38 ,< 188 '" 222
2499
-
2500
- 多媒体键码值(keyCode)
2501
- 按键 键码
2502
- 音量加 175
2503
- 音量减 174
2504
- 停止 179
2505
- 静音 173
2506
- 浏览器 172
2507
- 邮件 180
2508
- 搜索 170
2509
- 收藏 171
2510
- **/
2511
- listenKeyboard(element, eventName = "keypress", callback, options) {
2449
+ * @example
2450
+ Utils.onKeyboard(window,(keyName,keyValue,otherKey,event)=>{
2451
+ if(keyName === "Enter"){
2452
+ console.log("回车按键的值是:"+keyValue)
2453
+ }
2454
+ if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
2455
+ console.log("Ctrl和回车键");
2456
+ }
2457
+ })
2458
+ * @example
2459
+ 字母和数字键的键码值(keyCode)
2460
+ 按键 键码 按键 键码 按键 键码 按键 键码
2461
+ A 65 J 74 S 83 1 49
2462
+ B 66 K 75 T 84 2 50
2463
+ C 67 L 76 U 85 3 51
2464
+ D 68 M 77 V 86 4 52
2465
+ E 69 N 78 W 87 5 53
2466
+ F 70 O 79 X 88 6 54
2467
+ G 71 P 80 Y 89 7 55
2468
+ H 72 Q 81 Z 90 8 56
2469
+ I 73 R 82 0 48 9 57
2470
+
2471
+ 数字键盘上的键的键码值(keyCode)
2472
+ 功能键键码值(keyCode)
2473
+ 按键 键码 按键 键码 按键 键码 按键 键码
2474
+ 0 96 8 104 F1 112 F7 118
2475
+ 1 97 9 105 F2 113 F8 119
2476
+ 2 98 * 106 F3 114 F9 120
2477
+ 3 99 + 107 F4 115 F10 121
2478
+ 4 100 Enter 108 F5 116 F11 122
2479
+ 5 101 - 109 F6 117 F12 123
2480
+ 6 102 . 110
2481
+ 7 103 / 111
2482
+
2483
+ 控制键键码值(keyCode)
2484
+ 按键 键码 按键 键码 按键 键码 按键 键码
2485
+ BackSpace 8 Esc 27 → 39 -_ 189
2486
+ Tab 9 Spacebar 32 ↓ 40 .> 190
2487
+ Clear 12 Page Up 33 Insert 45 /? 191
2488
+ Enter 13 Page Down 34 Delete 46 `~ 192
2489
+ Shift 16 End 35 Num Lock 144 [{ 219
2490
+ Control 17 Home 36 ;: 186 \| 220
2491
+ Alt 18 ← 37 =+ 187 ]} 221
2492
+ Cape Lock 20 ↑ 38 ,< 188 '" 222
2493
+
2494
+ 多媒体键码值(keyCode)
2495
+ 按键 键码
2496
+ 音量加 175
2497
+ 音量减 174
2498
+ 停止 179
2499
+ 静音 173
2500
+ 浏览器 172
2501
+ 邮件 180
2502
+ 搜索 170
2503
+ 收藏 171
2504
+ **/
2505
+ onKeyboard(element, eventName = "keypress", handler, options) {
2512
2506
  const that = this;
2513
2507
  if (typeof element === "string") {
2514
2508
  element = that.selectorAll(element);
@@ -2532,8 +2526,8 @@ class ElementEvent extends ElementAnimate {
2532
2526
  if (event.shiftKey) {
2533
2527
  otherCodeList.push("shift");
2534
2528
  }
2535
- if (typeof callback === "function") {
2536
- callback(keyName, keyValue, otherCodeList, event);
2529
+ if (typeof handler === "function") {
2530
+ handler(keyName, keyValue, otherCodeList, event);
2537
2531
  }
2538
2532
  };
2539
2533
  that.on(element, eventName, keyboardEventCallBack, options);
@@ -2543,6 +2537,122 @@ class ElementEvent extends ElementAnimate {
2543
2537
  },
2544
2538
  };
2545
2539
  }
2540
+ /**
2541
+ * 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
2542
+ * @param $el 输入框元素
2543
+ * @param handler 回调函数
2544
+ * @param option 配置
2545
+ */
2546
+ onInput($el, handler, option) {
2547
+ /**
2548
+ * 是否正在输入中
2549
+ */
2550
+ let isComposite = false;
2551
+ const __callback = async (event) => {
2552
+ if (isComposite)
2553
+ return;
2554
+ await handler(event);
2555
+ };
2556
+ const __composition_start_callback = () => {
2557
+ isComposite = true;
2558
+ };
2559
+ const __composition_end_callback = () => {
2560
+ isComposite = false;
2561
+ this.emit($el, "input", {
2562
+ isComposite,
2563
+ });
2564
+ };
2565
+ const inputListener = this.on($el, "input", __callback, option);
2566
+ const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
2567
+ const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
2568
+ return {
2569
+ off: () => {
2570
+ inputListener.off();
2571
+ compositionStartListener.off();
2572
+ compositionEndListener.off();
2573
+ },
2574
+ };
2575
+ }
2576
+ onDoubleClick(...args) {
2577
+ const $el = args[0];
2578
+ let selector = void 0;
2579
+ let handler;
2580
+ let options;
2581
+ if (args.length === 2) {
2582
+ if (typeof args[1] === "function") {
2583
+ handler = args[1];
2584
+ }
2585
+ else {
2586
+ throw new TypeError("handler is not a function");
2587
+ }
2588
+ }
2589
+ else if (args.length === 3) {
2590
+ if (typeof args[1] === "function") {
2591
+ handler = args[1];
2592
+ options = args[2];
2593
+ }
2594
+ else {
2595
+ selector = args[1];
2596
+ handler = args[2];
2597
+ }
2598
+ }
2599
+ else if (args.length === 4) {
2600
+ selector = args[1];
2601
+ handler = args[2];
2602
+ options = args[3];
2603
+ }
2604
+ else {
2605
+ throw new Error("args length error");
2606
+ }
2607
+ let $click = null;
2608
+ let isDoubleClick = false;
2609
+ let timer = void 0;
2610
+ /** 是否是移动端点击 */
2611
+ let isMobileTouch = false;
2612
+ const dblclick_handler = async (evt, option) => {
2613
+ if (evt.type === "dblclick" && isMobileTouch) {
2614
+ // 禁止在移动端触发dblclick事件
2615
+ return;
2616
+ }
2617
+ await handler(evt, option);
2618
+ };
2619
+ const dblClickListener = this.on($el, "dblclick", (evt) => {
2620
+ dblclick_handler(evt, {
2621
+ isDoubleClick: true,
2622
+ });
2623
+ }, options);
2624
+ const touchEndListener = this.on($el, "touchend", selector, (evt, selectorTarget) => {
2625
+ isMobileTouch = true;
2626
+ CommonUtils.clearTimeout(timer);
2627
+ timer = void 0;
2628
+ if (isDoubleClick && $click === selectorTarget) {
2629
+ isDoubleClick = false;
2630
+ $click = null;
2631
+ /* 判定为双击 */
2632
+ dblclick_handler(evt, {
2633
+ isDoubleClick: true,
2634
+ });
2635
+ }
2636
+ else {
2637
+ timer = CommonUtils.setTimeout(() => {
2638
+ isDoubleClick = false;
2639
+ // 判断为单击
2640
+ dblclick_handler(evt, {
2641
+ isDoubleClick: false,
2642
+ });
2643
+ }, 200);
2644
+ isDoubleClick = true;
2645
+ $click = selectorTarget;
2646
+ }
2647
+ }, options);
2648
+ return {
2649
+ off() {
2650
+ $click = null;
2651
+ dblClickListener.off();
2652
+ touchEndListener.off();
2653
+ },
2654
+ };
2655
+ }
2546
2656
  preventEvent(...args) {
2547
2657
  /**
2548
2658
  * 阻止事件的默认行为发生,并阻止事件传播