@whitesev/domutils 1.9.5 → 1.9.7
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 +157 -58
- 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 +157 -58
- 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 +157 -58
- 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 +157 -58
- 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 +157 -58
- 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 +157 -58
- 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 +83 -28
- package/package.json +1 -1
- package/src/ElementEvent.ts +257 -74
- package/src/Utils.ts +0 -3
package/dist/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const version = "1.9.
|
|
1
|
+
const version = "1.9.6";
|
|
2
2
|
|
|
3
3
|
class WindowApi {
|
|
4
4
|
/** 默认的配置 */
|
|
@@ -588,10 +588,7 @@ class Utils {
|
|
|
588
588
|
"slideDown",
|
|
589
589
|
"slideToggle",
|
|
590
590
|
"slideUp",
|
|
591
|
-
"sort",
|
|
592
|
-
"splice",
|
|
593
591
|
"text",
|
|
594
|
-
"toArray",
|
|
595
592
|
"toggle",
|
|
596
593
|
"toggleClass",
|
|
597
594
|
"trigger",
|
|
@@ -1857,35 +1854,43 @@ class ElementEvent extends ElementAnimate {
|
|
|
1857
1854
|
else {
|
|
1858
1855
|
$elList.push(element);
|
|
1859
1856
|
}
|
|
1857
|
+
/**
|
|
1858
|
+
* 主动添加属性
|
|
1859
|
+
*/
|
|
1860
|
+
const addExtraProp = (event, obj) => {
|
|
1861
|
+
if (event instanceof Event && typeof obj === "object" && obj != null && !Array.isArray(obj)) {
|
|
1862
|
+
const detailKeys = Object.keys(obj);
|
|
1863
|
+
detailKeys.forEach((keyName) => {
|
|
1864
|
+
const value = Reflect.get(obj, keyName);
|
|
1865
|
+
// 在event上添加属性
|
|
1866
|
+
Reflect.set(event, keyName, value);
|
|
1867
|
+
});
|
|
1868
|
+
}
|
|
1869
|
+
};
|
|
1860
1870
|
let eventTypeList = [];
|
|
1871
|
+
/**
|
|
1872
|
+
* 主动传递的事件
|
|
1873
|
+
*/
|
|
1874
|
+
let __event__ = null;
|
|
1861
1875
|
if (Array.isArray(eventType)) {
|
|
1862
1876
|
eventTypeList = eventType.filter((it) => typeof it === "string" && it.trim() !== "");
|
|
1863
1877
|
}
|
|
1864
1878
|
else if (typeof eventType === "string") {
|
|
1865
1879
|
eventTypeList = eventType.split(" ");
|
|
1866
1880
|
}
|
|
1881
|
+
else if (eventType instanceof Event) {
|
|
1882
|
+
__event__ = eventType;
|
|
1883
|
+
addExtraProp(__event__, extraDetails);
|
|
1884
|
+
}
|
|
1867
1885
|
$elList.forEach(($elItem) => {
|
|
1868
1886
|
/* 获取对象上的事件 */
|
|
1869
1887
|
const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
}
|
|
1875
|
-
else {
|
|
1876
|
-
// 构造事件
|
|
1877
|
-
event = new Event(eventTypeItem);
|
|
1878
|
-
if (typeof extraDetails === "object" && extraDetails != null) {
|
|
1879
|
-
const detailKeys = Object.keys(extraDetails);
|
|
1880
|
-
detailKeys.forEach((keyName) => {
|
|
1881
|
-
const value = Reflect.get(extraDetails, keyName);
|
|
1882
|
-
// 在event上添加属性
|
|
1883
|
-
Reflect.set(event, keyName, value);
|
|
1884
|
-
});
|
|
1885
|
-
}
|
|
1886
|
-
}
|
|
1888
|
+
/**
|
|
1889
|
+
* 触发事件
|
|
1890
|
+
*/
|
|
1891
|
+
const dispatchEvent = (event, eventTypeItem) => {
|
|
1887
1892
|
if (useDispatchToTriggerEvent == false && eventTypeItem in elementEvents) {
|
|
1888
|
-
//
|
|
1893
|
+
// 直接调用.on监听的事件
|
|
1889
1894
|
elementEvents[eventTypeItem].forEach((eventsItem) => {
|
|
1890
1895
|
eventsItem.handlerCallBack(event);
|
|
1891
1896
|
});
|
|
@@ -1893,7 +1898,21 @@ class ElementEvent extends ElementAnimate {
|
|
|
1893
1898
|
else {
|
|
1894
1899
|
$elItem.dispatchEvent(event);
|
|
1895
1900
|
}
|
|
1896
|
-
}
|
|
1901
|
+
};
|
|
1902
|
+
if (__event__) {
|
|
1903
|
+
// 使用主动传递的事件直接触发
|
|
1904
|
+
const event = __event__;
|
|
1905
|
+
const eventTypeItem = event.type;
|
|
1906
|
+
dispatchEvent(event, eventTypeItem);
|
|
1907
|
+
}
|
|
1908
|
+
else {
|
|
1909
|
+
eventTypeList.forEach((eventTypeItem) => {
|
|
1910
|
+
// 构造事件
|
|
1911
|
+
const event = new Event(eventTypeItem);
|
|
1912
|
+
addExtraProp(event, extraDetails);
|
|
1913
|
+
dispatchEvent(event, eventTypeItem);
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1897
1916
|
});
|
|
1898
1917
|
}
|
|
1899
1918
|
/**
|
|
@@ -1929,7 +1948,8 @@ class ElementEvent extends ElementAnimate {
|
|
|
1929
1948
|
that.emit(element, "click", details, useDispatchToEmit);
|
|
1930
1949
|
}
|
|
1931
1950
|
else {
|
|
1932
|
-
that.on(element, "click", null, handler);
|
|
1951
|
+
const listener = that.on(element, "click", null, handler);
|
|
1952
|
+
return listener;
|
|
1933
1953
|
}
|
|
1934
1954
|
}
|
|
1935
1955
|
/**
|
|
@@ -1965,7 +1985,8 @@ class ElementEvent extends ElementAnimate {
|
|
|
1965
1985
|
that.emit(element, "blur", details, useDispatchToEmit);
|
|
1966
1986
|
}
|
|
1967
1987
|
else {
|
|
1968
|
-
that.on(element, "blur", null, handler);
|
|
1988
|
+
const listener = that.on(element, "blur", null, handler);
|
|
1989
|
+
return listener;
|
|
1969
1990
|
}
|
|
1970
1991
|
}
|
|
1971
1992
|
/**
|
|
@@ -2001,7 +2022,8 @@ class ElementEvent extends ElementAnimate {
|
|
|
2001
2022
|
that.emit(element, "focus", details, useDispatchToEmit);
|
|
2002
2023
|
}
|
|
2003
2024
|
else {
|
|
2004
|
-
that.on(element, "focus", null, handler);
|
|
2025
|
+
const listener = that.on(element, "focus", null, handler);
|
|
2026
|
+
return listener;
|
|
2005
2027
|
}
|
|
2006
2028
|
}
|
|
2007
2029
|
/**
|
|
@@ -2028,13 +2050,30 @@ class ElementEvent extends ElementAnimate {
|
|
|
2028
2050
|
}
|
|
2029
2051
|
if (CommonUtils.isNodeList(element)) {
|
|
2030
2052
|
// 设置
|
|
2053
|
+
const listenerList = [];
|
|
2031
2054
|
element.forEach(($ele) => {
|
|
2032
|
-
that.onHover($ele, handler, option);
|
|
2055
|
+
const listener = that.onHover($ele, handler, option);
|
|
2056
|
+
listenerList.push(listener);
|
|
2033
2057
|
});
|
|
2034
|
-
return
|
|
2058
|
+
return {
|
|
2059
|
+
off() {
|
|
2060
|
+
listenerList.forEach((listener) => {
|
|
2061
|
+
if (!listener) {
|
|
2062
|
+
return;
|
|
2063
|
+
}
|
|
2064
|
+
listener.off();
|
|
2065
|
+
});
|
|
2066
|
+
},
|
|
2067
|
+
};
|
|
2035
2068
|
}
|
|
2036
|
-
that.on(element, "mouseenter", null, handler, option);
|
|
2037
|
-
that.on(element, "mouseleave", null, handler, option);
|
|
2069
|
+
const mouseenter_listener = that.on(element, "mouseenter", null, handler, option);
|
|
2070
|
+
const mouseleave_listener = that.on(element, "mouseleave", null, handler, option);
|
|
2071
|
+
return {
|
|
2072
|
+
off() {
|
|
2073
|
+
mouseenter_listener.off();
|
|
2074
|
+
mouseleave_listener.off();
|
|
2075
|
+
},
|
|
2076
|
+
};
|
|
2038
2077
|
}
|
|
2039
2078
|
/**
|
|
2040
2079
|
* 监听动画结束
|
|
@@ -2117,12 +2156,23 @@ class ElementEvent extends ElementAnimate {
|
|
|
2117
2156
|
}
|
|
2118
2157
|
if (CommonUtils.isNodeList(element)) {
|
|
2119
2158
|
// 设置
|
|
2159
|
+
const listenerList = [];
|
|
2120
2160
|
element.forEach(($ele) => {
|
|
2121
|
-
that.onKeyup($ele, handler, option);
|
|
2161
|
+
const listener = that.onKeyup($ele, handler, option);
|
|
2162
|
+
listenerList.push(listener);
|
|
2122
2163
|
});
|
|
2123
|
-
return
|
|
2164
|
+
return {
|
|
2165
|
+
off() {
|
|
2166
|
+
listenerList.forEach((listener) => {
|
|
2167
|
+
if (!listener) {
|
|
2168
|
+
return;
|
|
2169
|
+
}
|
|
2170
|
+
listener.off();
|
|
2171
|
+
});
|
|
2172
|
+
},
|
|
2173
|
+
};
|
|
2124
2174
|
}
|
|
2125
|
-
that.on(element, "keyup", null, handler, option);
|
|
2175
|
+
return that.on(element, "keyup", null, handler, option);
|
|
2126
2176
|
}
|
|
2127
2177
|
/**
|
|
2128
2178
|
* 当按键按下时触发事件
|
|
@@ -2149,12 +2199,23 @@ class ElementEvent extends ElementAnimate {
|
|
|
2149
2199
|
}
|
|
2150
2200
|
if (CommonUtils.isNodeList(element)) {
|
|
2151
2201
|
// 设置
|
|
2202
|
+
const listenerList = [];
|
|
2152
2203
|
element.forEach(($ele) => {
|
|
2153
|
-
that.onKeydown($ele, handler, option);
|
|
2204
|
+
const listener = that.onKeydown($ele, handler, option);
|
|
2205
|
+
listenerList.push(listener);
|
|
2154
2206
|
});
|
|
2155
|
-
return
|
|
2207
|
+
return {
|
|
2208
|
+
off() {
|
|
2209
|
+
listenerList.forEach((listener) => {
|
|
2210
|
+
if (!listener) {
|
|
2211
|
+
return;
|
|
2212
|
+
}
|
|
2213
|
+
listener.off();
|
|
2214
|
+
});
|
|
2215
|
+
},
|
|
2216
|
+
};
|
|
2156
2217
|
}
|
|
2157
|
-
that.on(element, "keydown", null, handler, option);
|
|
2218
|
+
return that.on(element, "keydown", null, handler, option);
|
|
2158
2219
|
}
|
|
2159
2220
|
/**
|
|
2160
2221
|
* 当按键按下时触发事件
|
|
@@ -2181,12 +2242,23 @@ class ElementEvent extends ElementAnimate {
|
|
|
2181
2242
|
}
|
|
2182
2243
|
if (CommonUtils.isNodeList(element)) {
|
|
2183
2244
|
// 设置
|
|
2245
|
+
const listenerList = [];
|
|
2184
2246
|
element.forEach(($ele) => {
|
|
2185
|
-
that.onKeypress($ele, handler, option);
|
|
2247
|
+
const listener = that.onKeypress($ele, handler, option);
|
|
2248
|
+
listenerList.push(listener);
|
|
2186
2249
|
});
|
|
2187
|
-
return
|
|
2250
|
+
return {
|
|
2251
|
+
off() {
|
|
2252
|
+
listenerList.forEach((listener) => {
|
|
2253
|
+
if (!listener) {
|
|
2254
|
+
return;
|
|
2255
|
+
}
|
|
2256
|
+
listener.off();
|
|
2257
|
+
});
|
|
2258
|
+
},
|
|
2259
|
+
};
|
|
2188
2260
|
}
|
|
2189
|
-
that.on(element, "keypress", null, handler, option);
|
|
2261
|
+
return that.on(element, "keypress", null, handler, option);
|
|
2190
2262
|
}
|
|
2191
2263
|
/**
|
|
2192
2264
|
* 监听某个元素键盘按键事件或window全局按键事件
|
|
@@ -2279,12 +2351,8 @@ class ElementEvent extends ElementAnimate {
|
|
|
2279
2351
|
handler(keyName, keyValue, otherCodeList, event);
|
|
2280
2352
|
}
|
|
2281
2353
|
};
|
|
2282
|
-
that.on(element, eventName, keyboardEventCallBack, options);
|
|
2283
|
-
return
|
|
2284
|
-
removeListen: () => {
|
|
2285
|
-
that.off(element, eventName, keyboardEventCallBack, options);
|
|
2286
|
-
},
|
|
2287
|
-
};
|
|
2354
|
+
const listener = that.on(element, eventName, keyboardEventCallBack, options);
|
|
2355
|
+
return listener;
|
|
2288
2356
|
}
|
|
2289
2357
|
/**
|
|
2290
2358
|
* 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
|
|
@@ -2315,11 +2383,14 @@ class ElementEvent extends ElementAnimate {
|
|
|
2315
2383
|
const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
|
|
2316
2384
|
const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
|
|
2317
2385
|
return {
|
|
2318
|
-
off
|
|
2386
|
+
off() {
|
|
2319
2387
|
inputListener.off();
|
|
2320
2388
|
compositionStartListener.off();
|
|
2321
2389
|
compositionEndListener.off();
|
|
2322
2390
|
},
|
|
2391
|
+
emit(details, useDispatchToEmit) {
|
|
2392
|
+
inputListener.emit(details, useDispatchToEmit);
|
|
2393
|
+
},
|
|
2323
2394
|
};
|
|
2324
2395
|
}
|
|
2325
2396
|
onDoubleClick(...args) {
|
|
@@ -2402,28 +2473,56 @@ class ElementEvent extends ElementAnimate {
|
|
|
2402
2473
|
/**
|
|
2403
2474
|
* 阻止事件的默认行为发生,并阻止事件传播
|
|
2404
2475
|
*/
|
|
2405
|
-
const stopEvent = (event) => {
|
|
2406
|
-
|
|
2476
|
+
const stopEvent = (event, onlyStopPropagation) => {
|
|
2477
|
+
if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
|
|
2478
|
+
// 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
|
|
2479
|
+
event?.stopPropagation();
|
|
2480
|
+
// 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
|
|
2481
|
+
event?.stopImmediatePropagation();
|
|
2482
|
+
return;
|
|
2483
|
+
}
|
|
2484
|
+
// 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
|
|
2407
2485
|
event?.preventDefault();
|
|
2408
|
-
/* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
|
|
2409
|
-
event?.stopPropagation();
|
|
2410
|
-
/* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
|
|
2411
|
-
event?.stopImmediatePropagation();
|
|
2412
2486
|
return false;
|
|
2413
2487
|
};
|
|
2414
|
-
if (args
|
|
2415
|
-
|
|
2416
|
-
|
|
2488
|
+
if (args[0] instanceof Event) {
|
|
2489
|
+
// 直接阻止事件
|
|
2490
|
+
const onlyStopPropagation = args[1];
|
|
2491
|
+
return stopEvent(args[0], onlyStopPropagation);
|
|
2417
2492
|
}
|
|
2418
2493
|
else {
|
|
2419
2494
|
const $el = args[0];
|
|
2420
2495
|
let eventNameList = args[1];
|
|
2421
|
-
|
|
2422
|
-
|
|
2496
|
+
let selector = void 0;
|
|
2497
|
+
let capture = false;
|
|
2498
|
+
let onlyStopPropagation = false;
|
|
2499
|
+
// 添加对应的事件来阻止触发
|
|
2423
2500
|
if (typeof eventNameList === "string") {
|
|
2424
2501
|
eventNameList = [eventNameList];
|
|
2425
2502
|
}
|
|
2426
|
-
|
|
2503
|
+
let option = void 0;
|
|
2504
|
+
if (typeof args[2] === "string" || Array.isArray(args[2])) {
|
|
2505
|
+
// selector
|
|
2506
|
+
selector = args[2];
|
|
2507
|
+
if (typeof args[3] === "object" && args[3] != null) {
|
|
2508
|
+
option = args[3];
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
else if (typeof args[2] === "object" && args[2] != null && !Array.isArray(args[2])) {
|
|
2512
|
+
// option
|
|
2513
|
+
option = args[2];
|
|
2514
|
+
}
|
|
2515
|
+
else {
|
|
2516
|
+
throw new TypeError("Invalid argument");
|
|
2517
|
+
}
|
|
2518
|
+
if (option) {
|
|
2519
|
+
capture = Boolean(option.capture);
|
|
2520
|
+
onlyStopPropagation = Boolean(option.onlyStopPropagation);
|
|
2521
|
+
}
|
|
2522
|
+
const listener = this.on($el, eventNameList, selector, (evt) => {
|
|
2523
|
+
return stopEvent(evt, onlyStopPropagation);
|
|
2524
|
+
}, { capture: capture });
|
|
2525
|
+
return listener;
|
|
2427
2526
|
}
|
|
2428
2527
|
}
|
|
2429
2528
|
}
|