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