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