@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.umd.js CHANGED
@@ -182,8 +182,9 @@
182
182
  * @param option
183
183
  */
184
184
  function getOption(args, startIndex, option) {
185
- if (typeof args[startIndex] === "boolean") {
186
- option.capture = args[startIndex];
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 args[startIndex] === "object" &&
195
- ("capture" in args[startIndex] ||
196
- "once" in args[startIndex] ||
197
- "passive" in args[startIndex])) {
198
- option.capture = args[startIndex].capture;
199
- option.once = args[startIndex].once;
200
- option.passive = args[startIndex].passive;
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 _callback_ = callback;
240
+ let listenerCallBack = callback;
238
241
  // 事件配置
239
- let _option_ = {
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
- _callback_ = selector;
247
- _option_ = getOption(args, 3, _option_);
250
+ listenerCallBack = selector;
251
+ listenerOption = getOption(args, 3, listenerOption);
248
252
  }
249
253
  else {
250
254
  /* 这是存在selector的情况 */
251
- _option_ = getOption(args, 4, _option_);
255
+ listenerOption = getOption(args, 4, listenerOption);
252
256
  }
253
257
  /**
254
258
  * 如果是once,那么删除该监听和元素上的事件和监听
255
259
  */
256
260
  function checkOptionOnceToRemoveEventListener() {
257
- if (_option_.once) {
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 target = event.target;
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 (target.matches(selectorItem)) {
281
+ if (eventTarget.matches(selectorItem)) {
276
282
  /* 当前目标可以被selector所匹配到 */
277
- _callback_.call(target, event);
283
+ listenerCallBack.call(eventTarget, event);
278
284
  checkOptionOnceToRemoveEventListener();
279
285
  break;
280
286
  }
281
- else if (target.closest(selectorItem) &&
282
- totalParent.contains(target.closest(selectorItem))) {
287
+ else {
283
288
  /* 在上层与主元素之间寻找可以被selector所匹配到的 */
284
- let closestElement = target.closest(selectorItem);
285
- /* event的target值不能直接修改 */
286
- OriginPrototype.Object.defineProperty(event, "target", {
287
- get() {
288
- return closestElement;
289
- },
290
- });
291
- _callback_.call(closestElement, event);
292
- checkOptionOnceToRemoveEventListener();
293
- break;
289
+ let $closestMatches = eventTarget.closest(selectorItem);
290
+ if ($closestMatches && totalParent.contains($closestMatches)) {
291
+ /* eventtarget值不能直接修改 */
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
- _callback_.call(elementItem, event);
309
+ listenerCallBack.call(elementItem, event);
299
310
  checkOptionOnceToRemoveEventListener();
300
311
  }
301
312
  }
302
313
  /* 遍历事件名设置元素事件 */
303
314
  eventTypeList.forEach((eventName) => {
304
- elementItem.addEventListener(eventName, domUtilsEventCallBack, _option_);
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: _option_,
322
+ option: listenerOption,
312
323
  callback: domUtilsEventCallBack,
313
- originCallBack: _callback_,
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
- if (typeof args1[startIndex] === "boolean") {
330
- option.capture = args1[startIndex];
340
+ let currentParam = args1[startIndex];
341
+ if (typeof currentParam === "boolean") {
342
+ option.capture = currentParam;
331
343
  }
332
- else if (typeof args1[startIndex] === "object" &&
333
- "capture" in args1[startIndex]) {
334
- option.capture = args1[startIndex].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 _callback_ = callback;
384
+ let listenerCallBack = callback;
373
385
  /**
374
386
  * 事件的配置
375
387
  */
376
- let _option_ = {
388
+ let listenerOption = {
377
389
  capture: false,
378
390
  };
379
391
  if (typeof selector === "function") {
380
392
  /* 这是为没有selector的情况 */
381
- _callback_ = selector;
382
- _option_ = getOption(args, 3, _option_);
393
+ listenerCallBack = selector;
394
+ listenerOption = getOption(args, 3, listenerOption);
383
395
  }
384
396
  else {
385
- _option_ = getOption(args, 4, _option_);
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 = false;
399
- if (!selectorList.length) {
400
- // selectorList是空的,默认移除
401
- flag = true;
421
+ let flag = true;
422
+ if (flag &&
423
+ listenerCallBack &&
424
+ handler.originCallBack !== listenerCallBack) {
425
+ // callback不同
426
+ flag = false;
402
427
  }
403
- else {
404
- if (Array.isArray(handler.selector) &&
405
- JSON.stringify(handler.selector) === JSON.stringify(selectorList)) {
406
- // 元素上的selectorList不为空且和传入的相同
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 (!_callback_ ||
411
- handler.callback === _callback_ ||
412
- handler.originCallBack === _callback_) {
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, _option_);
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.3";
1041
+ version = "2024.12.4";
1020
1042
  attr(element, attrName, attrValue) {
1021
1043
  let DOMUtilsContext = this;
1022
1044
  if (typeof element === "string") {