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