@whitesev/domutils 1.4.3 → 1.4.5
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 +84 -62
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +84 -62
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +84 -62
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +84 -62
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +84 -62
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +84 -62
- 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/DOMUtils.ts +1 -1
- package/src/DOMUtilsEvent.ts +138 -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,60 @@
|
|
|
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
|
+
// 这里尝试使用defineProperty修改event的target值
|
|
293
|
+
try {
|
|
294
|
+
OriginPrototype.Object.defineProperty(event, "target", {
|
|
295
|
+
get() {
|
|
296
|
+
return $closestMatches;
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
catch (error) { }
|
|
301
|
+
listenerCallBack.call($closestMatches, event);
|
|
302
|
+
checkOptionOnceToRemoveEventListener();
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
294
305
|
}
|
|
295
306
|
}
|
|
296
307
|
}
|
|
297
308
|
else {
|
|
298
|
-
|
|
309
|
+
listenerCallBack.call(elementItem, event);
|
|
299
310
|
checkOptionOnceToRemoveEventListener();
|
|
300
311
|
}
|
|
301
312
|
}
|
|
302
313
|
/* 遍历事件名设置元素事件 */
|
|
303
314
|
eventTypeList.forEach((eventName) => {
|
|
304
|
-
elementItem.addEventListener(eventName, domUtilsEventCallBack,
|
|
315
|
+
elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
|
|
305
316
|
/* 获取对象上的事件 */
|
|
306
317
|
let elementEvents = elementItem[DOMUtilsData.SymbolEvents] || {};
|
|
307
318
|
/* 初始化对象上的xx事件 */
|
|
308
319
|
elementEvents[eventName] = elementEvents[eventName] || [];
|
|
309
320
|
elementEvents[eventName].push({
|
|
310
321
|
selector: selectorList,
|
|
311
|
-
option:
|
|
322
|
+
option: listenerOption,
|
|
312
323
|
callback: domUtilsEventCallBack,
|
|
313
|
-
originCallBack:
|
|
324
|
+
originCallBack: listenerCallBack,
|
|
314
325
|
});
|
|
315
326
|
/* 覆盖事件 */
|
|
316
327
|
// @ts-ignore
|
|
@@ -326,12 +337,13 @@
|
|
|
326
337
|
* @param option
|
|
327
338
|
*/
|
|
328
339
|
function getOption(args1, startIndex, option) {
|
|
329
|
-
|
|
330
|
-
|
|
340
|
+
let currentParam = args1[startIndex];
|
|
341
|
+
if (typeof currentParam === "boolean") {
|
|
342
|
+
option.capture = currentParam;
|
|
331
343
|
}
|
|
332
|
-
else if (typeof
|
|
333
|
-
"capture" in
|
|
334
|
-
option.capture =
|
|
344
|
+
else if (typeof currentParam === "object" &&
|
|
345
|
+
"capture" in currentParam) {
|
|
346
|
+
option.capture = currentParam.capture;
|
|
335
347
|
}
|
|
336
348
|
return option;
|
|
337
349
|
}
|
|
@@ -369,20 +381,31 @@
|
|
|
369
381
|
/**
|
|
370
382
|
* 事件的回调函数
|
|
371
383
|
*/
|
|
372
|
-
let
|
|
384
|
+
let listenerCallBack = callback;
|
|
373
385
|
/**
|
|
374
386
|
* 事件的配置
|
|
375
387
|
*/
|
|
376
|
-
let
|
|
388
|
+
let listenerOption = {
|
|
377
389
|
capture: false,
|
|
378
390
|
};
|
|
379
391
|
if (typeof selector === "function") {
|
|
380
392
|
/* 这是为没有selector的情况 */
|
|
381
|
-
|
|
382
|
-
|
|
393
|
+
listenerCallBack = selector;
|
|
394
|
+
listenerOption = getOption(args, 3, listenerOption);
|
|
383
395
|
}
|
|
384
396
|
else {
|
|
385
|
-
|
|
397
|
+
listenerOption = getOption(args, 4, listenerOption);
|
|
398
|
+
}
|
|
399
|
+
// 是否移除所有事件
|
|
400
|
+
let isRemoveAll = false;
|
|
401
|
+
if (args.length === 2) {
|
|
402
|
+
// 目标函数、事件名
|
|
403
|
+
isRemoveAll = true;
|
|
404
|
+
}
|
|
405
|
+
else if ((args.length === 3 && typeof args[2] === "string") ||
|
|
406
|
+
Array.isArray(args[2])) {
|
|
407
|
+
// 目标函数、事件名、子元素选择器
|
|
408
|
+
isRemoveAll = true;
|
|
386
409
|
}
|
|
387
410
|
elementList.forEach((elementItem) => {
|
|
388
411
|
/* 获取对象上的事件 */
|
|
@@ -395,26 +418,25 @@
|
|
|
395
418
|
}
|
|
396
419
|
for (let index = 0; index < handlers.length; index++) {
|
|
397
420
|
let handler = handlers[index];
|
|
398
|
-
let flag =
|
|
399
|
-
if (
|
|
400
|
-
|
|
401
|
-
|
|
421
|
+
let flag = true;
|
|
422
|
+
if (flag &&
|
|
423
|
+
listenerCallBack &&
|
|
424
|
+
handler.originCallBack !== listenerCallBack) {
|
|
425
|
+
// callback不同
|
|
426
|
+
flag = false;
|
|
402
427
|
}
|
|
403
|
-
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
flag = true;
|
|
428
|
+
if (flag && selectorList.length && Array.isArray(handler.selector)) {
|
|
429
|
+
if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
|
|
430
|
+
// 子元素选择器不同
|
|
431
|
+
flag = false;
|
|
408
432
|
}
|
|
409
433
|
}
|
|
410
|
-
if (
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
/* callback不为空,且callback相同 */
|
|
414
|
-
flag = true;
|
|
434
|
+
if (flag && listenerOption.capture !== handler.option.capture) {
|
|
435
|
+
// 事件的配置项不同
|
|
436
|
+
flag = false;
|
|
415
437
|
}
|
|
416
|
-
if (flag) {
|
|
417
|
-
elementItem.removeEventListener(eventName, handler.callback,
|
|
438
|
+
if (flag || isRemoveAll) {
|
|
439
|
+
elementItem.removeEventListener(eventName, handler.callback, handler.option);
|
|
418
440
|
handlers.splice(index--, 1);
|
|
419
441
|
}
|
|
420
442
|
}
|
|
@@ -1016,7 +1038,7 @@
|
|
|
1016
1038
|
super(option);
|
|
1017
1039
|
}
|
|
1018
1040
|
/** 版本号 */
|
|
1019
|
-
version = "2024.12.
|
|
1041
|
+
version = "2024.12.4";
|
|
1020
1042
|
attr(element, attrName, attrValue) {
|
|
1021
1043
|
let DOMUtilsContext = this;
|
|
1022
1044
|
if (typeof element === "string") {
|