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