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