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