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