@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.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,56 @@ 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
|
+
OriginPrototype.Object.defineProperty(event, "target", {
|
|
287
|
+
get() {
|
|
288
|
+
return $closestMatches;
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
listenerCallBack.call($closestMatches, event);
|
|
292
|
+
checkOptionOnceToRemoveEventListener();
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
288
295
|
}
|
|
289
296
|
}
|
|
290
297
|
}
|
|
291
298
|
else {
|
|
292
|
-
|
|
299
|
+
listenerCallBack.call(elementItem, event);
|
|
293
300
|
checkOptionOnceToRemoveEventListener();
|
|
294
301
|
}
|
|
295
302
|
}
|
|
296
303
|
/* 遍历事件名设置元素事件 */
|
|
297
304
|
eventTypeList.forEach((eventName) => {
|
|
298
|
-
elementItem.addEventListener(eventName, domUtilsEventCallBack,
|
|
305
|
+
elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
|
|
299
306
|
/* 获取对象上的事件 */
|
|
300
307
|
let elementEvents = elementItem[DOMUtilsData.SymbolEvents] || {};
|
|
301
308
|
/* 初始化对象上的xx事件 */
|
|
302
309
|
elementEvents[eventName] = elementEvents[eventName] || [];
|
|
303
310
|
elementEvents[eventName].push({
|
|
304
311
|
selector: selectorList,
|
|
305
|
-
option:
|
|
312
|
+
option: listenerOption,
|
|
306
313
|
callback: domUtilsEventCallBack,
|
|
307
|
-
originCallBack:
|
|
314
|
+
originCallBack: listenerCallBack,
|
|
308
315
|
});
|
|
309
316
|
/* 覆盖事件 */
|
|
310
317
|
// @ts-ignore
|
|
@@ -320,12 +327,13 @@ class DOMUtilsEvent {
|
|
|
320
327
|
* @param option
|
|
321
328
|
*/
|
|
322
329
|
function getOption(args1, startIndex, option) {
|
|
323
|
-
|
|
324
|
-
|
|
330
|
+
let currentParam = args1[startIndex];
|
|
331
|
+
if (typeof currentParam === "boolean") {
|
|
332
|
+
option.capture = currentParam;
|
|
325
333
|
}
|
|
326
|
-
else if (typeof
|
|
327
|
-
"capture" in
|
|
328
|
-
option.capture =
|
|
334
|
+
else if (typeof currentParam === "object" &&
|
|
335
|
+
"capture" in currentParam) {
|
|
336
|
+
option.capture = currentParam.capture;
|
|
329
337
|
}
|
|
330
338
|
return option;
|
|
331
339
|
}
|
|
@@ -363,20 +371,31 @@ class DOMUtilsEvent {
|
|
|
363
371
|
/**
|
|
364
372
|
* 事件的回调函数
|
|
365
373
|
*/
|
|
366
|
-
let
|
|
374
|
+
let listenerCallBack = callback;
|
|
367
375
|
/**
|
|
368
376
|
* 事件的配置
|
|
369
377
|
*/
|
|
370
|
-
let
|
|
378
|
+
let listenerOption = {
|
|
371
379
|
capture: false,
|
|
372
380
|
};
|
|
373
381
|
if (typeof selector === "function") {
|
|
374
382
|
/* 这是为没有selector的情况 */
|
|
375
|
-
|
|
376
|
-
|
|
383
|
+
listenerCallBack = selector;
|
|
384
|
+
listenerOption = getOption(args, 3, listenerOption);
|
|
377
385
|
}
|
|
378
386
|
else {
|
|
379
|
-
|
|
387
|
+
listenerOption = getOption(args, 4, listenerOption);
|
|
388
|
+
}
|
|
389
|
+
// 是否移除所有事件
|
|
390
|
+
let isRemoveAll = false;
|
|
391
|
+
if (args.length === 2) {
|
|
392
|
+
// 目标函数、事件名
|
|
393
|
+
isRemoveAll = true;
|
|
394
|
+
}
|
|
395
|
+
else if ((args.length === 3 && typeof args[2] === "string") ||
|
|
396
|
+
Array.isArray(args[2])) {
|
|
397
|
+
// 目标函数、事件名、子元素选择器
|
|
398
|
+
isRemoveAll = true;
|
|
380
399
|
}
|
|
381
400
|
elementList.forEach((elementItem) => {
|
|
382
401
|
/* 获取对象上的事件 */
|
|
@@ -389,26 +408,25 @@ class DOMUtilsEvent {
|
|
|
389
408
|
}
|
|
390
409
|
for (let index = 0; index < handlers.length; index++) {
|
|
391
410
|
let handler = handlers[index];
|
|
392
|
-
let flag =
|
|
393
|
-
if (
|
|
394
|
-
|
|
395
|
-
|
|
411
|
+
let flag = true;
|
|
412
|
+
if (flag &&
|
|
413
|
+
listenerCallBack &&
|
|
414
|
+
handler.originCallBack !== listenerCallBack) {
|
|
415
|
+
// callback不同
|
|
416
|
+
flag = false;
|
|
396
417
|
}
|
|
397
|
-
|
|
398
|
-
if (
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
flag = true;
|
|
418
|
+
if (flag && selectorList.length && Array.isArray(handler.selector)) {
|
|
419
|
+
if (JSON.stringify(handler.selector) !== JSON.stringify(selectorList)) {
|
|
420
|
+
// 子元素选择器不同
|
|
421
|
+
flag = false;
|
|
402
422
|
}
|
|
403
423
|
}
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
/* callback不为空,且callback相同 */
|
|
408
|
-
flag = true;
|
|
424
|
+
if (flag && listenerOption.capture !== handler.option.capture) {
|
|
425
|
+
// 事件的配置项不同
|
|
426
|
+
flag = false;
|
|
409
427
|
}
|
|
410
|
-
if (flag) {
|
|
411
|
-
elementItem.removeEventListener(eventName, handler.callback,
|
|
428
|
+
if (flag || isRemoveAll) {
|
|
429
|
+
elementItem.removeEventListener(eventName, handler.callback, handler.option);
|
|
412
430
|
handlers.splice(index--, 1);
|
|
413
431
|
}
|
|
414
432
|
}
|