@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.esm.js CHANGED
@@ -176,8 +176,9 @@ class DOMUtilsEvent {
176
176
  * @param option
177
177
  */
178
178
  function getOption(args, startIndex, option) {
179
- if (typeof args[startIndex] === "boolean") {
180
- option.capture = args[startIndex];
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 args[startIndex] === "object" &&
189
- ("capture" in args[startIndex] ||
190
- "once" in args[startIndex] ||
191
- "passive" in args[startIndex])) {
192
- option.capture = args[startIndex].capture;
193
- option.once = args[startIndex].once;
194
- option.passive = args[startIndex].passive;
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 _callback_ = callback;
234
+ let listenerCallBack = callback;
232
235
  // 事件配置
233
- let _option_ = {
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
- _callback_ = selector;
241
- _option_ = getOption(args, 3, _option_);
244
+ listenerCallBack = selector;
245
+ listenerOption = getOption(args, 3, listenerOption);
242
246
  }
243
247
  else {
244
248
  /* 这是存在selector的情况 */
245
- _option_ = getOption(args, 4, _option_);
249
+ listenerOption = getOption(args, 4, listenerOption);
246
250
  }
247
251
  /**
248
252
  * 如果是once,那么删除该监听和元素上的事件和监听
249
253
  */
250
254
  function checkOptionOnceToRemoveEventListener() {
251
- if (_option_.once) {
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 target = event.target;
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 (target.matches(selectorItem)) {
275
+ if (eventTarget.matches(selectorItem)) {
270
276
  /* 当前目标可以被selector所匹配到 */
271
- _callback_.call(target, event);
277
+ listenerCallBack.call(eventTarget, event);
272
278
  checkOptionOnceToRemoveEventListener();
273
279
  break;
274
280
  }
275
- else if (target.closest(selectorItem) &&
276
- totalParent.contains(target.closest(selectorItem))) {
281
+ else {
277
282
  /* 在上层与主元素之间寻找可以被selector所匹配到的 */
278
- let closestElement = target.closest(selectorItem);
279
- /* event的target值不能直接修改 */
280
- OriginPrototype.Object.defineProperty(event, "target", {
281
- get() {
282
- return closestElement;
283
- },
284
- });
285
- _callback_.call(closestElement, event);
286
- checkOptionOnceToRemoveEventListener();
287
- break;
283
+ let $closestMatches = eventTarget.closest(selectorItem);
284
+ if ($closestMatches && totalParent.contains($closestMatches)) {
285
+ /* eventtarget值不能直接修改 */
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
- _callback_.call(elementItem, event);
303
+ listenerCallBack.call(elementItem, event);
293
304
  checkOptionOnceToRemoveEventListener();
294
305
  }
295
306
  }
296
307
  /* 遍历事件名设置元素事件 */
297
308
  eventTypeList.forEach((eventName) => {
298
- elementItem.addEventListener(eventName, domUtilsEventCallBack, _option_);
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: _option_,
316
+ option: listenerOption,
306
317
  callback: domUtilsEventCallBack,
307
- originCallBack: _callback_,
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
- if (typeof args1[startIndex] === "boolean") {
324
- option.capture = args1[startIndex];
334
+ let currentParam = args1[startIndex];
335
+ if (typeof currentParam === "boolean") {
336
+ option.capture = currentParam;
325
337
  }
326
- else if (typeof args1[startIndex] === "object" &&
327
- "capture" in args1[startIndex]) {
328
- option.capture = args1[startIndex].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 _callback_ = callback;
378
+ let listenerCallBack = callback;
367
379
  /**
368
380
  * 事件的配置
369
381
  */
370
- let _option_ = {
382
+ let listenerOption = {
371
383
  capture: false,
372
384
  };
373
385
  if (typeof selector === "function") {
374
386
  /* 这是为没有selector的情况 */
375
- _callback_ = selector;
376
- _option_ = getOption(args, 3, _option_);
387
+ listenerCallBack = selector;
388
+ listenerOption = getOption(args, 3, listenerOption);
377
389
  }
378
390
  else {
379
- _option_ = getOption(args, 4, _option_);
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 = false;
393
- if (!selectorList.length) {
394
- // selectorList是空的,默认移除
395
- flag = true;
415
+ let flag = true;
416
+ if (flag &&
417
+ listenerCallBack &&
418
+ handler.originCallBack !== listenerCallBack) {
419
+ // callback不同
420
+ flag = false;
396
421
  }
397
- else {
398
- if (Array.isArray(handler.selector) &&
399
- JSON.stringify(handler.selector) === JSON.stringify(selectorList)) {
400
- // 元素上的selectorList不为空且和传入的相同
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 (!_callback_ ||
405
- handler.callback === _callback_ ||
406
- handler.originCallBack === _callback_) {
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, _option_);
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.3";
1035
+ version = "2024.12.4";
1014
1036
  attr(element, attrName, attrValue) {
1015
1037
  let DOMUtilsContext = this;
1016
1038
  if (typeof element === "string") {