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