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