@whitesev/domutils 1.9.7 → 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.
@@ -3,7 +3,7 @@ System.register('DOMUtils', [], (function (exports) {
3
3
  return {
4
4
  execute: (function () {
5
5
 
6
- const version = "1.9.6";
6
+ const version = "1.9.10";
7
7
 
8
8
  class WindowApi {
9
9
  /** 默认的配置 */
@@ -540,7 +540,6 @@ System.register('DOMUtils', [], (function (exports) {
540
540
  "is",
541
541
  "jquery",
542
542
  "keydown",
543
- "keypress",
544
543
  "keyup",
545
544
  "last",
546
545
  "load",
@@ -1384,7 +1383,7 @@ System.register('DOMUtils', [], (function (exports) {
1384
1383
  * @param startIndex
1385
1384
  * @param option
1386
1385
  */
1387
- function getOption(args, startIndex, option) {
1386
+ const getOption = function (args, startIndex, option) {
1388
1387
  const currentParam = args[startIndex];
1389
1388
  if (typeof currentParam === "boolean") {
1390
1389
  option.capture = currentParam;
@@ -1406,7 +1405,7 @@ System.register('DOMUtils', [], (function (exports) {
1406
1405
  option.isComposedPath = currentParam.isComposedPath;
1407
1406
  }
1408
1407
  return option;
1409
- }
1408
+ };
1410
1409
  const that = this;
1411
1410
  // eslint-disable-next-line prefer-rest-params
1412
1411
  const args = arguments;
@@ -1464,39 +1463,52 @@ System.register('DOMUtils', [], (function (exports) {
1464
1463
  /**
1465
1464
  * 如果是once,那么删除该监听和元素上的事件和监听
1466
1465
  */
1467
- const checkOptionOnceToRemoveEventListener = () => {
1466
+ const checkOptionOnceToRemoveEventListener = ($el) => {
1468
1467
  if (listenerOption.once) {
1469
- that.off(element, eventType, selector, callback, option);
1468
+ this.off($el, eventTypeList, selector, callback, option);
1470
1469
  }
1471
1470
  };
1472
- $elList.forEach((elementItem) => {
1471
+ $elList.forEach(($elItem) => {
1473
1472
  /**
1474
1473
  * 事件回调
1475
1474
  * @param event
1476
1475
  */
1477
1476
  const handlerCallBack = function (event) {
1477
+ let call_this = void 0;
1478
+ let call_event = void 0;
1479
+ let call_$selector = void 0;
1480
+ let execCallback = false;
1478
1481
  if (selectorList.length) {
1479
- /* 存在子元素选择器 */
1482
+ // 存在子元素选择器
1480
1483
  // 这时候的this和target都是子元素选择器的元素
1481
- let eventTarget = listenerOption.isComposedPath
1482
- ? event.composedPath()[0]
1483
- : event.target;
1484
- let totalParent = elementItem;
1485
- if (CommonUtils.isWin(totalParent)) {
1486
- if (totalParent === that.windowApi.document) {
1487
- totalParent = that.windowApi.document.documentElement;
1484
+ let $target;
1485
+ if (listenerOption.isComposedPath) {
1486
+ // 可能为空
1487
+ const composedPath = event.composedPath();
1488
+ if (!composedPath.length && event.target) {
1489
+ composedPath.push(event.target);
1488
1490
  }
1491
+ $target = composedPath[0];
1489
1492
  }
1490
- const findValue = selectorList.find((selectorItem) => {
1493
+ else {
1494
+ $target = event.target;
1495
+ }
1496
+ let $parent = $elItem;
1497
+ if (CommonUtils.isWin($parent)) {
1498
+ // window和document共用一个对象
1499
+ // 这样就能处理子元素选择器无法匹配的问题
1500
+ $parent = that.windowApi.document.documentElement;
1501
+ }
1502
+ const findValue = selectorList.find((selectors) => {
1491
1503
  // 判断目标元素是否匹配选择器
1492
- if (that.matches(eventTarget, selectorItem)) {
1493
- /* 当前目标可以被selector所匹配到 */
1504
+ if (that.matches($target, selectors)) {
1505
+ // 当前目标可以被selector所匹配到
1494
1506
  return true;
1495
1507
  }
1496
- /* 在上层与主元素之间寻找可以被selector所匹配到的 */
1497
- const $closestMatches = that.closest(eventTarget, selectorItem);
1498
- if ($closestMatches && totalParent?.contains?.($closestMatches)) {
1499
- eventTarget = $closestMatches;
1508
+ // 在上层与主元素之间寻找可以被selector所匹配到的
1509
+ const $closestMatches = that.closest($target, selectors);
1510
+ if ($closestMatches && $parent?.contains?.($closestMatches)) {
1511
+ $target = $closestMatches;
1500
1512
  return true;
1501
1513
  }
1502
1514
  return false;
@@ -1506,29 +1518,38 @@ System.register('DOMUtils', [], (function (exports) {
1506
1518
  try {
1507
1519
  OriginPrototype.Object.defineProperty(event, "target", {
1508
1520
  get() {
1509
- return eventTarget;
1521
+ return $target;
1510
1522
  },
1511
1523
  });
1524
+ // oxlint-disable-next-line no-empty
1512
1525
  }
1513
- catch {
1514
- // TODO
1515
- }
1516
- listenerCallBack.call(eventTarget, event, eventTarget);
1517
- checkOptionOnceToRemoveEventListener();
1526
+ catch { }
1527
+ execCallback = true;
1528
+ call_this = $target;
1529
+ call_event = event;
1530
+ call_$selector = $target;
1518
1531
  }
1519
1532
  }
1520
1533
  else {
1521
- // 这时候的this指向监听的元素
1522
- listenerCallBack.call(elementItem, event);
1523
- checkOptionOnceToRemoveEventListener();
1534
+ execCallback = true;
1535
+ call_this = $elItem;
1536
+ call_event = event;
1537
+ }
1538
+ if (execCallback) {
1539
+ const result = listenerCallBack.call(call_this, call_event, call_$selector);
1540
+ checkOptionOnceToRemoveEventListener($elItem);
1541
+ if (typeof result === "boolean" && !result) {
1542
+ return false;
1543
+ }
1524
1544
  }
1525
1545
  };
1526
- /* 遍历事件名设置元素事件 */
1546
+ // 遍历事件名设置元素事件
1527
1547
  eventTypeList.forEach((eventName) => {
1528
- elementItem.addEventListener(eventName, handlerCallBack, listenerOption);
1529
- /* 获取对象上的事件 */
1530
- const elementEvents = Reflect.get(elementItem, GlobalData.domEventSymbol) || {};
1531
- /* 初始化对象上的xx事件 */
1548
+ // add listener
1549
+ $elItem.addEventListener(eventName, handlerCallBack, listenerOption);
1550
+ // 获取对象上的事件
1551
+ const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1552
+ // 初始化对象上的xx事件
1532
1553
  elementEvents[eventName] = elementEvents[eventName] || [];
1533
1554
  elementEvents[eventName].push({
1534
1555
  selector: selectorList,
@@ -1536,8 +1557,8 @@ System.register('DOMUtils', [], (function (exports) {
1536
1557
  handlerCallBack: handlerCallBack,
1537
1558
  callback: listenerCallBack,
1538
1559
  });
1539
- /* 覆盖事件 */
1540
- Reflect.set(elementItem, GlobalData.domEventSymbol, elementEvents);
1560
+ // 覆盖事件
1561
+ Reflect.set($elItem, GlobalData.domEventSymbol, elementEvents);
1541
1562
  });
1542
1563
  });
1543
1564
  return {
@@ -1551,7 +1572,7 @@ System.register('DOMUtils', [], (function (exports) {
1551
1572
  /**
1552
1573
  * 主动触发事件
1553
1574
  * @param extraDetails 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
1554
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了$selector的没有值
1575
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了`$selector`的没有值
1555
1576
  */
1556
1577
  emit: (extraDetails, useDispatchToTriggerEvent) => {
1557
1578
  that.emit($elList, eventTypeList, extraDetails, useDispatchToTriggerEvent);
@@ -1565,7 +1586,7 @@ System.register('DOMUtils', [], (function (exports) {
1565
1586
  * @param startIndex
1566
1587
  * @param option
1567
1588
  */
1568
- function getOption(args1, startIndex, option) {
1589
+ const getOption = function (args1, startIndex, option) {
1569
1590
  const currentParam = args1[startIndex];
1570
1591
  if (typeof currentParam === "boolean") {
1571
1592
  option.capture = currentParam;
@@ -1574,7 +1595,7 @@ System.register('DOMUtils', [], (function (exports) {
1574
1595
  option.capture = currentParam.capture;
1575
1596
  }
1576
1597
  return option;
1577
- }
1598
+ };
1578
1599
  const that = this;
1579
1600
  // eslint-disable-next-line prefer-rest-params
1580
1601
  const args = arguments;
@@ -1627,22 +1648,12 @@ System.register('DOMUtils', [], (function (exports) {
1627
1648
  // 这是存在selector的情况
1628
1649
  listenerOption = getOption(args, 4, listenerOption);
1629
1650
  }
1630
- // 是否移除所有事件
1631
- let isRemoveAll = false;
1632
- if (args.length === 2) {
1633
- // 目标函数、事件名
1634
- isRemoveAll = true;
1635
- }
1636
- else if ((args.length === 3 && typeof args[2] === "string") || Array.isArray(args[2])) {
1637
- // 目标函数、事件名、子元素选择器
1638
- isRemoveAll = true;
1639
- }
1640
1651
  if (args.length === 5 && typeof args[4] === "function" && typeof filter !== "function") {
1641
1652
  // 目标函数、事件名、回调函数、事件配置、过滤函数
1642
1653
  filter = option;
1643
1654
  }
1644
1655
  $elList.forEach(($elItem) => {
1645
- /* 获取对象上的事件 */
1656
+ // 获取对象上的事件
1646
1657
  const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1647
1658
  eventTypeList.forEach((eventName) => {
1648
1659
  const handlers = elementEvents[eventName] || [];
@@ -1666,7 +1677,7 @@ System.register('DOMUtils', [], (function (exports) {
1666
1677
  // 事件的配置项不同
1667
1678
  flag = false;
1668
1679
  }
1669
- if (flag || isRemoveAll) {
1680
+ if (flag) {
1670
1681
  $elItem.removeEventListener(eventName, handler.handlerCallBack, handler.option);
1671
1682
  const findIndex = handlers.findIndex((item) => item === handler);
1672
1683
  if (findIndex !== -1) {
@@ -1675,7 +1686,7 @@ System.register('DOMUtils', [], (function (exports) {
1675
1686
  }
1676
1687
  }
1677
1688
  if (handlers.length === 0) {
1678
- /* 如果没有任意的handler,那么删除该属性 */
1689
+ // 如果没有任意的handler,那么删除该属性
1679
1690
  CommonUtils.delete(elementEvents, eventType);
1680
1691
  }
1681
1692
  });
@@ -2222,49 +2233,6 @@ System.register('DOMUtils', [], (function (exports) {
2222
2233
  }
2223
2234
  return that.on(element, "keydown", null, handler, option);
2224
2235
  }
2225
- /**
2226
- * 当按键按下时触发事件
2227
- * keydown - > keypress - > keyup
2228
- * @param element 目标
2229
- * @param handler 事件处理函数
2230
- * @param option 配置
2231
- * @example
2232
- * // 监听a.xx元素的按键按下
2233
- * DOMUtils.keypress(document.querySelector("a.xx"),()=>{
2234
- * console.log("按键按下");
2235
- * })
2236
- * DOMUtils.keypress("a.xx",()=>{
2237
- * console.log("按键按下");
2238
- * })
2239
- */
2240
- onKeypress(element, handler, option) {
2241
- const that = this;
2242
- if (element == null) {
2243
- return;
2244
- }
2245
- if (typeof element === "string") {
2246
- element = that.selectorAll(element);
2247
- }
2248
- if (CommonUtils.isNodeList(element)) {
2249
- // 设置
2250
- const listenerList = [];
2251
- element.forEach(($ele) => {
2252
- const listener = that.onKeypress($ele, handler, option);
2253
- listenerList.push(listener);
2254
- });
2255
- return {
2256
- off() {
2257
- listenerList.forEach((listener) => {
2258
- if (!listener) {
2259
- return;
2260
- }
2261
- listener.off();
2262
- });
2263
- },
2264
- };
2265
- }
2266
- return that.on(element, "keypress", null, handler, option);
2267
- }
2268
2236
  /**
2269
2237
  * 监听某个元素键盘按键事件或window全局按键事件
2270
2238
  * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
@@ -2328,7 +2296,7 @@ System.register('DOMUtils', [], (function (exports) {
2328
2296
  搜索 170
2329
2297
  收藏 171
2330
2298
  **/
2331
- onKeyboard(element, eventName = "keypress", handler, options) {
2299
+ onKeyboard(element, eventName = "keydown", handler, options) {
2332
2300
  const that = this;
2333
2301
  if (typeof element === "string") {
2334
2302
  element = that.selectorAll(element);
@@ -2479,11 +2447,11 @@ System.register('DOMUtils', [], (function (exports) {
2479
2447
  * 阻止事件的默认行为发生,并阻止事件传播
2480
2448
  */
2481
2449
  const stopEvent = (event, onlyStopPropagation) => {
2450
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2451
+ event?.stopPropagation();
2452
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2453
+ event?.stopImmediatePropagation();
2482
2454
  if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
2483
- // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2484
- event?.stopPropagation();
2485
- // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2486
- event?.stopImmediatePropagation();
2487
2455
  return;
2488
2456
  }
2489
2457
  // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
@@ -2506,7 +2474,8 @@ System.register('DOMUtils', [], (function (exports) {
2506
2474
  eventNameList = [eventNameList];
2507
2475
  }
2508
2476
  let option = void 0;
2509
- if (typeof args[2] === "string" || Array.isArray(args[2])) {
2477
+ if (args.length === 2) ;
2478
+ else if (typeof args[2] === "string" || Array.isArray(args[2])) {
2510
2479
  // selector
2511
2480
  selector = args[2];
2512
2481
  if (typeof args[3] === "object" && args[3] != null) {