@whitesev/domutils 1.9.9 → 1.9.10

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
@@ -1,4 +1,4 @@
1
- const version = "1.9.9";
1
+ const version = "1.9.10";
2
2
 
3
3
  class WindowApi {
4
4
  /** 默认的配置 */
@@ -1378,7 +1378,7 @@ class ElementEvent extends ElementAnimate {
1378
1378
  * @param startIndex
1379
1379
  * @param option
1380
1380
  */
1381
- function getOption(args, startIndex, option) {
1381
+ const getOption = function (args, startIndex, option) {
1382
1382
  const currentParam = args[startIndex];
1383
1383
  if (typeof currentParam === "boolean") {
1384
1384
  option.capture = currentParam;
@@ -1400,7 +1400,7 @@ class ElementEvent extends ElementAnimate {
1400
1400
  option.isComposedPath = currentParam.isComposedPath;
1401
1401
  }
1402
1402
  return option;
1403
- }
1403
+ };
1404
1404
  const that = this;
1405
1405
  // eslint-disable-next-line prefer-rest-params
1406
1406
  const args = arguments;
@@ -1458,39 +1458,52 @@ class ElementEvent extends ElementAnimate {
1458
1458
  /**
1459
1459
  * 如果是once,那么删除该监听和元素上的事件和监听
1460
1460
  */
1461
- const checkOptionOnceToRemoveEventListener = () => {
1461
+ const checkOptionOnceToRemoveEventListener = ($el) => {
1462
1462
  if (listenerOption.once) {
1463
- that.off(element, eventType, selector, callback, option);
1463
+ this.off($el, eventTypeList, selector, callback, option);
1464
1464
  }
1465
1465
  };
1466
- $elList.forEach((elementItem) => {
1466
+ $elList.forEach(($elItem) => {
1467
1467
  /**
1468
1468
  * 事件回调
1469
1469
  * @param event
1470
1470
  */
1471
1471
  const handlerCallBack = function (event) {
1472
+ let call_this = void 0;
1473
+ let call_event = void 0;
1474
+ let call_$selector = void 0;
1475
+ let execCallback = false;
1472
1476
  if (selectorList.length) {
1473
- /* 存在子元素选择器 */
1477
+ // 存在子元素选择器
1474
1478
  // 这时候的this和target都是子元素选择器的元素
1475
- let eventTarget = listenerOption.isComposedPath
1476
- ? event.composedPath()[0]
1477
- : event.target;
1478
- let totalParent = elementItem;
1479
- if (CommonUtils.isWin(totalParent)) {
1480
- if (totalParent === that.windowApi.document) {
1481
- totalParent = that.windowApi.document.documentElement;
1479
+ let $target;
1480
+ if (listenerOption.isComposedPath) {
1481
+ // 可能为空
1482
+ const composedPath = event.composedPath();
1483
+ if (!composedPath.length && event.target) {
1484
+ composedPath.push(event.target);
1482
1485
  }
1486
+ $target = composedPath[0];
1487
+ }
1488
+ else {
1489
+ $target = event.target;
1490
+ }
1491
+ let $parent = $elItem;
1492
+ if (CommonUtils.isWin($parent)) {
1493
+ // window和document共用一个对象
1494
+ // 这样就能处理子元素选择器无法匹配的问题
1495
+ $parent = that.windowApi.document.documentElement;
1483
1496
  }
1484
- const findValue = selectorList.find((selectorItem) => {
1497
+ const findValue = selectorList.find((selectors) => {
1485
1498
  // 判断目标元素是否匹配选择器
1486
- if (that.matches(eventTarget, selectorItem)) {
1487
- /* 当前目标可以被selector所匹配到 */
1499
+ if (that.matches($target, selectors)) {
1500
+ // 当前目标可以被selector所匹配到
1488
1501
  return true;
1489
1502
  }
1490
- /* 在上层与主元素之间寻找可以被selector所匹配到的 */
1491
- const $closestMatches = that.closest(eventTarget, selectorItem);
1492
- if ($closestMatches && totalParent?.contains?.($closestMatches)) {
1493
- eventTarget = $closestMatches;
1503
+ // 在上层与主元素之间寻找可以被selector所匹配到的
1504
+ const $closestMatches = that.closest($target, selectors);
1505
+ if ($closestMatches && $parent?.contains?.($closestMatches)) {
1506
+ $target = $closestMatches;
1494
1507
  return true;
1495
1508
  }
1496
1509
  return false;
@@ -1500,29 +1513,38 @@ class ElementEvent extends ElementAnimate {
1500
1513
  try {
1501
1514
  OriginPrototype.Object.defineProperty(event, "target", {
1502
1515
  get() {
1503
- return eventTarget;
1516
+ return $target;
1504
1517
  },
1505
1518
  });
1519
+ // oxlint-disable-next-line no-empty
1506
1520
  }
1507
- catch {
1508
- // TODO
1509
- }
1510
- listenerCallBack.call(eventTarget, event, eventTarget);
1511
- checkOptionOnceToRemoveEventListener();
1521
+ catch { }
1522
+ execCallback = true;
1523
+ call_this = $target;
1524
+ call_event = event;
1525
+ call_$selector = $target;
1512
1526
  }
1513
1527
  }
1514
1528
  else {
1515
- // 这时候的this指向监听的元素
1516
- listenerCallBack.call(elementItem, event);
1517
- checkOptionOnceToRemoveEventListener();
1529
+ execCallback = true;
1530
+ call_this = $elItem;
1531
+ call_event = event;
1532
+ }
1533
+ if (execCallback) {
1534
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
1535
+ checkOptionOnceToRemoveEventListener($elItem);
1536
+ if (typeof result === "boolean" && !result) {
1537
+ return false;
1538
+ }
1518
1539
  }
1519
1540
  };
1520
- /* 遍历事件名设置元素事件 */
1541
+ // 遍历事件名设置元素事件
1521
1542
  eventTypeList.forEach((eventName) => {
1522
- elementItem.addEventListener(eventName, handlerCallBack, listenerOption);
1523
- /* 获取对象上的事件 */
1524
- const elementEvents = Reflect.get(elementItem, GlobalData.domEventSymbol) || {};
1525
- /* 初始化对象上的xx事件 */
1543
+ // add listener
1544
+ $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
1545
+ // 获取对象上的事件
1546
+ const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1547
+ // 初始化对象上的xx事件
1526
1548
  elementEvents[eventName] = elementEvents[eventName] || [];
1527
1549
  elementEvents[eventName].push({
1528
1550
  selector: selectorList,
@@ -1530,8 +1552,8 @@ class ElementEvent extends ElementAnimate {
1530
1552
  handlerCallBack: handlerCallBack,
1531
1553
  callback: listenerCallBack,
1532
1554
  });
1533
- /* 覆盖事件 */
1534
- Reflect.set(elementItem, GlobalData.domEventSymbol, elementEvents);
1555
+ // 覆盖事件
1556
+ Reflect.set($elItem, GlobalData.domEventSymbol, elementEvents);
1535
1557
  });
1536
1558
  });
1537
1559
  return {
@@ -1545,7 +1567,7 @@ class ElementEvent extends ElementAnimate {
1545
1567
  /**
1546
1568
  * 主动触发事件
1547
1569
  * @param extraDetails 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
1548
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了$selector的没有值
1570
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了`$selector`的没有值
1549
1571
  */
1550
1572
  emit: (extraDetails, useDispatchToTriggerEvent) => {
1551
1573
  that.emit($elList, eventTypeList, extraDetails, useDispatchToTriggerEvent);
@@ -1559,7 +1581,7 @@ class ElementEvent extends ElementAnimate {
1559
1581
  * @param startIndex
1560
1582
  * @param option
1561
1583
  */
1562
- function getOption(args1, startIndex, option) {
1584
+ const getOption = function (args1, startIndex, option) {
1563
1585
  const currentParam = args1[startIndex];
1564
1586
  if (typeof currentParam === "boolean") {
1565
1587
  option.capture = currentParam;
@@ -1568,7 +1590,7 @@ class ElementEvent extends ElementAnimate {
1568
1590
  option.capture = currentParam.capture;
1569
1591
  }
1570
1592
  return option;
1571
- }
1593
+ };
1572
1594
  const that = this;
1573
1595
  // eslint-disable-next-line prefer-rest-params
1574
1596
  const args = arguments;
@@ -1626,7 +1648,7 @@ class ElementEvent extends ElementAnimate {
1626
1648
  filter = option;
1627
1649
  }
1628
1650
  $elList.forEach(($elItem) => {
1629
- /* 获取对象上的事件 */
1651
+ // 获取对象上的事件
1630
1652
  const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1631
1653
  eventTypeList.forEach((eventName) => {
1632
1654
  const handlers = elementEvents[eventName] || [];
@@ -1659,7 +1681,7 @@ class ElementEvent extends ElementAnimate {
1659
1681
  }
1660
1682
  }
1661
1683
  if (handlers.length === 0) {
1662
- /* 如果没有任意的handler,那么删除该属性 */
1684
+ // 如果没有任意的handler,那么删除该属性
1663
1685
  CommonUtils.delete(elementEvents, eventType);
1664
1686
  }
1665
1687
  });
@@ -2420,11 +2442,11 @@ class ElementEvent extends ElementAnimate {
2420
2442
  * 阻止事件的默认行为发生,并阻止事件传播
2421
2443
  */
2422
2444
  const stopEvent = (event, onlyStopPropagation) => {
2445
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2446
+ event?.stopPropagation();
2447
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2448
+ event?.stopImmediatePropagation();
2423
2449
  if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
2424
- // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2425
- event?.stopPropagation();
2426
- // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2427
- event?.stopImmediatePropagation();
2428
2450
  return;
2429
2451
  }
2430
2452
  // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字