@whitesev/domutils 1.4.3 → 1.4.4
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.amd.js +79 -61
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +79 -61
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +79 -61
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +79 -61
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +79 -61
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +79 -61
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtilsEvent.d.ts +18 -18
- package/dist/types/src/types/DOMUtilsEvent.d.ts +12 -1
- package/package.json +1 -1
- package/src/DOMUtilsEvent.ts +135 -103
- package/src/types/DOMUtilsEvent.d.ts +12 -1
package/dist/index.umd.js
CHANGED
|
@@ -182,8 +182,9 @@
|
|
|
182
182
|
* @param option
|
|
183
183
|
*/
|
|
184
184
|
function getOption(args, startIndex, option) {
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
let currentParam = args[startIndex];
|
|
186
|
+
if (typeof currentParam === "boolean") {
|
|
187
|
+
option.capture = currentParam;
|
|
187
188
|
if (typeof args[startIndex + 1] === "boolean") {
|
|
188
189
|
option.once = args[startIndex + 1];
|
|
189
190
|
}
|
|
@@ -191,13 +192,15 @@
|
|
|
191
192
|
option.passive = args[startIndex + 2];
|
|
192
193
|
}
|
|
193
194
|
}
|
|
194
|
-
else if (typeof
|
|
195
|
-
("capture" in
|
|
196
|
-
"once" in
|
|
197
|
-
"passive" in
|
|
198
|
-
|
|
199
|
-
option.
|
|
200
|
-
option.
|
|
195
|
+
else if (typeof currentParam === "object" &&
|
|
196
|
+
("capture" in currentParam ||
|
|
197
|
+
"once" in currentParam ||
|
|
198
|
+
"passive" in currentParam ||
|
|
199
|
+
"isComposedPath" in currentParam)) {
|
|
200
|
+
option.capture = currentParam.capture;
|
|
201
|
+
option.once = currentParam.once;
|
|
202
|
+
option.passive = currentParam.passive;
|
|
203
|
+
option.isComposedPath = currentParam.isComposedPath;
|
|
201
204
|
}
|
|
202
205
|
return option;
|
|
203
206
|
}
|
|
@@ -234,27 +237,28 @@
|
|
|
234
237
|
selectorList.push(selector);
|
|
235
238
|
}
|
|
236
239
|
// 事件回调
|
|
237
|
-
let
|
|
240
|
+
let listenerCallBack = callback;
|
|
238
241
|
// 事件配置
|
|
239
|
-
let
|
|
242
|
+
let listenerOption = {
|
|
240
243
|
capture: false,
|
|
241
244
|
once: false,
|
|
242
245
|
passive: false,
|
|
246
|
+
isComposedPath: false,
|
|
243
247
|
};
|
|
244
248
|
if (typeof selector === "function") {
|
|
245
249
|
/* 这是为没有selector的情况 */
|
|
246
|
-
|
|
247
|
-
|
|
250
|
+
listenerCallBack = selector;
|
|
251
|
+
listenerOption = getOption(args, 3, listenerOption);
|
|
248
252
|
}
|
|
249
253
|
else {
|
|
250
254
|
/* 这是存在selector的情况 */
|
|
251
|
-
|
|
255
|
+
listenerOption = getOption(args, 4, listenerOption);
|
|
252
256
|
}
|
|
253
257
|
/**
|
|
254
258
|
* 如果是once,那么删除该监听和元素上的事件和监听
|
|
255
259
|
*/
|
|
256
260
|
function checkOptionOnceToRemoveEventListener() {
|
|
257
|
-
if (
|
|
261
|
+
if (listenerOption.once) {
|
|
258
262
|
DOMUtilsContext.off(element, eventType, selector, callback, option);
|
|
259
263
|
}
|
|
260
264
|
}
|
|
@@ -264,53 +268,56 @@
|
|
|
264
268
|
* @param event
|
|
265
269
|
*/
|
|
266
270
|
function domUtilsEventCallBack(event) {
|
|
267
|
-
let
|
|
271
|
+
let eventTarget = listenerOption.isComposedPath
|
|
272
|
+
? event.composedPath()[0]
|
|
273
|
+
: event.target;
|
|
268
274
|
if (selectorList.length) {
|
|
269
|
-
/*
|
|
275
|
+
/* 存在子元素选择器 */
|
|
270
276
|
let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
|
|
271
277
|
? DOMUtilsContext.windowApi.document.documentElement
|
|
272
278
|
: elementItem;
|
|
273
279
|
for (let index = 0; index < selectorList.length; index++) {
|
|
274
280
|
const selectorItem = selectorList[index];
|
|
275
|
-
if (
|
|
281
|
+
if (eventTarget.matches(selectorItem)) {
|
|
276
282
|
/* 当前目标可以被selector所匹配到 */
|
|
277
|
-
|
|
283
|
+
listenerCallBack.call(eventTarget, event);
|
|
278
284
|
checkOptionOnceToRemoveEventListener();
|
|
279
285
|
break;
|
|
280
286
|
}
|
|
281
|
-
else
|
|
282
|
-
totalParent.contains(target.closest(selectorItem))) {
|
|
287
|
+
else {
|
|
283
288
|
/* 在上层与主元素之间寻找可以被selector所匹配到的 */
|
|
284
|
-
let
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
289
|
+
let $closestMatches = eventTarget.closest(selectorItem);
|
|
290
|
+
if ($closestMatches && totalParent.contains($closestMatches)) {
|
|
291
|
+
/* event的target值不能直接修改 */
|
|
292
|
+
OriginPrototype.Object.defineProperty(event, "target", {
|
|
293
|
+
get() {
|
|
294
|
+
return $closestMatches;
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
listenerCallBack.call($closestMatches, event);
|
|
298
|
+
checkOptionOnceToRemoveEventListener();
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
294
301
|
}
|
|
295
302
|
}
|
|
296
303
|
}
|
|
297
304
|
else {
|
|
298
|
-
|
|
305
|
+
listenerCallBack.call(elementItem, event);
|
|
299
306
|
checkOptionOnceToRemoveEventListener();
|
|
300
307
|
}
|
|
301
308
|
}
|
|
302
309
|
/* 遍历事件名设置元素事件 */
|
|
303
310
|
eventTypeList.forEach((eventName) => {
|
|
304
|
-
elementItem.addEventListener(eventName, domUtilsEventCallBack,
|
|
311
|
+
elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
|
|
305
312
|
/* 获取对象上的事件 */
|
|
306
313
|
let elementEvents = elementItem[DOMUtilsData.SymbolEvents] || {};
|
|
307
314
|
/* 初始化对象上的xx事件 */
|
|
308
315
|
elementEvents[eventName] = elementEvents[eventName] || [];
|
|
309
316
|
elementEvents[eventName].push({
|
|
310
317
|
selector: selectorList,
|
|
311
|
-
option:
|
|
318
|
+
option: listenerOption,
|
|
312
319
|
callback: domUtilsEventCallBack,
|
|
313
|
-
originCallBack:
|
|
320
|
+
originCallBack: listenerCallBack,
|
|
314
321
|
});
|
|
315
322
|
/* 覆盖事件 */
|
|
316
323
|
// @ts-ignore
|
|
@@ -326,12 +333,13 @@
|
|
|
326
333
|
* @param option
|
|
327
334
|
*/
|
|
328
335
|
function getOption(args1, startIndex, option) {
|
|
329
|
-
|
|
330
|
-
|
|
336
|
+
let currentParam = args1[startIndex];
|
|
337
|
+
if (typeof currentParam === "boolean") {
|
|
338
|
+
option.capture = currentParam;
|
|
331
339
|
}
|
|
332
|
-
else if (typeof
|
|
333
|
-
"capture" in
|
|
334
|
-
option.capture =
|
|
340
|
+
else if (typeof currentParam === "object" &&
|
|
341
|
+
"capture" in currentParam) {
|
|
342
|
+
option.capture = currentParam.capture;
|
|
335
343
|
}
|
|
336
344
|
return option;
|
|
337
345
|
}
|
|
@@ -369,20 +377,31 @@
|
|
|
369
377
|
/**
|
|
370
378
|
* 事件的回调函数
|
|
371
379
|
*/
|
|
372
|
-
let
|
|
380
|
+
let listenerCallBack = callback;
|
|
373
381
|
/**
|
|
374
382
|
* 事件的配置
|
|
375
383
|
*/
|
|
376
|
-
let
|
|
384
|
+
let listenerOption = {
|
|
377
385
|
capture: false,
|
|
378
386
|
};
|
|
379
387
|
if (typeof selector === "function") {
|
|
380
388
|
/* 这是为没有selector的情况 */
|
|
381
|
-
|
|
382
|
-
|
|
389
|
+
listenerCallBack = selector;
|
|
390
|
+
listenerOption = getOption(args, 3, listenerOption);
|
|
383
391
|
}
|
|
384
392
|
else {
|
|
385
|
-
|
|
393
|
+
listenerOption = getOption(args, 4, listenerOption);
|
|
394
|
+
}
|
|
395
|
+
// 是否移除所有事件
|
|
396
|
+
let isRemoveAll = false;
|
|
397
|
+
if (args.length === 2) {
|
|
398
|
+
// 目标函数、事件名
|
|
399
|
+
isRemoveAll = true;
|
|
400
|
+
}
|
|
401
|
+
else if ((args.length === 3 && typeof args[2] === "string") ||
|
|
402
|
+
Array.isArray(args[2])) {
|
|
403
|
+
// 目标函数、事件名、子元素选择器
|
|
404
|
+
isRemoveAll = true;
|
|
386
405
|
}
|
|
387
406
|
elementList.forEach((elementItem) => {
|
|
388
407
|
/* 获取对象上的事件 */
|
|
@@ -395,26 +414,25 @@
|
|
|
395
414
|
}
|
|
396
415
|
for (let index = 0; index < handlers.length; index++) {
|
|
397
416
|
let handler = handlers[index];
|
|
398
|
-
let flag =
|
|
399
|
-
if (
|
|
400
|
-
|
|
401
|
-
|
|
417
|
+
let flag = true;
|
|
418
|
+
if (flag &&
|
|
419
|
+
listenerCallBack &&
|
|
420
|
+
handler.originCallBack !== listenerCallBack) {
|
|
421
|
+
// callback不同
|
|
422
|
+
flag = false;
|
|
402
423
|
}
|
|
403
|
-
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
flag = true;
|
|
424
|
+
if (flag && selectorList.length && Array.isArray(handler.selector)) {
|
|
425
|
+
if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
|
|
426
|
+
// 子元素选择器不同
|
|
427
|
+
flag = false;
|
|
408
428
|
}
|
|
409
429
|
}
|
|
410
|
-
if (
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
/* callback不为空,且callback相同 */
|
|
414
|
-
flag = true;
|
|
430
|
+
if (flag && listenerOption.capture !== handler.option.capture) {
|
|
431
|
+
// 事件的配置项不同
|
|
432
|
+
flag = false;
|
|
415
433
|
}
|
|
416
|
-
if (flag) {
|
|
417
|
-
elementItem.removeEventListener(eventName, handler.callback,
|
|
434
|
+
if (flag || isRemoveAll) {
|
|
435
|
+
elementItem.removeEventListener(eventName, handler.callback, handler.option);
|
|
418
436
|
handlers.splice(index--, 1);
|
|
419
437
|
}
|
|
420
438
|
}
|