@whitesev/domutils 1.7.4 → 1.8.0
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/README.md +4 -4
- package/dist/index.amd.js +249 -136
- 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 +249 -136
- 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 +249 -136
- 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 +249 -136
- 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 +249 -136
- 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 +249 -136
- 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/eslint.config.d.mts +2 -0
- package/dist/types/src/ElementEvent.d.ts +123 -105
- package/dist/types/src/types/DOMUtilsEvent.d.ts +2 -2
- package/package.json +10 -10
- package/src/ElementEvent.ts +270 -135
- package/src/Utils.ts +28 -26
- package/src/types/DOMUtilsEvent.d.ts +2 -2
package/dist/index.iife.js
CHANGED
|
@@ -524,13 +524,7 @@ var DOMUtils = (function () {
|
|
|
524
524
|
},
|
|
525
525
|
};
|
|
526
526
|
|
|
527
|
-
const version = "1.
|
|
528
|
-
|
|
529
|
-
/* 数据 */
|
|
530
|
-
const GlobalData = {
|
|
531
|
-
/** .on添加在元素存储的事件 */
|
|
532
|
-
domEventSymbol: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
|
|
533
|
-
};
|
|
527
|
+
const version = "1.8.0";
|
|
534
528
|
|
|
535
529
|
class ElementSelector {
|
|
536
530
|
windowApi;
|
|
@@ -902,35 +896,41 @@ var DOMUtils = (function () {
|
|
|
902
896
|
if (target == null) {
|
|
903
897
|
target = {};
|
|
904
898
|
}
|
|
899
|
+
// 当前遍历的目标对象
|
|
900
|
+
let iteratorTarget;
|
|
905
901
|
if (isAdd) {
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
const targetValue = Reflect.get(target, targetKeyName);
|
|
909
|
-
const sourceValue = Reflect.get(source, sourceKeyName);
|
|
910
|
-
if (typeof sourceValue === "object" && sourceValue != null && sourceKeyName in target && !isDOM(sourceValue)) {
|
|
911
|
-
/* 源端的值是object类型,且不是元素节点 */
|
|
912
|
-
Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
|
|
913
|
-
continue;
|
|
914
|
-
}
|
|
915
|
-
Reflect.set(target, sourceKeyName, sourceValue);
|
|
916
|
-
}
|
|
902
|
+
// 追加并覆盖是以source为准
|
|
903
|
+
iteratorTarget = source;
|
|
917
904
|
}
|
|
918
905
|
else {
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
906
|
+
// 覆盖以target为准
|
|
907
|
+
iteratorTarget = target;
|
|
908
|
+
}
|
|
909
|
+
for (const keyName in iteratorTarget) {
|
|
910
|
+
if (!isAdd && !(keyName in source)) {
|
|
911
|
+
// 仅替换 但是源端没有此键
|
|
912
|
+
continue;
|
|
913
|
+
}
|
|
914
|
+
const targetValue = Reflect.get(target, keyName);
|
|
915
|
+
const sourceValue = Reflect.get(source, keyName);
|
|
916
|
+
if (typeof sourceValue === "object" && sourceValue != null && keyName in target && !isDOM(sourceValue)) {
|
|
917
|
+
// 源端的值是object类型,且不是元素节点
|
|
918
|
+
// 如果是数组,那此数组中有值,清空旧的数组再赋值
|
|
919
|
+
let childObjectValue;
|
|
920
|
+
if (Array.isArray(sourceValue)) {
|
|
921
|
+
if (Array.isArray(targetValue)) {
|
|
922
|
+
targetValue.length = 0;
|
|
930
923
|
}
|
|
931
|
-
|
|
932
|
-
|
|
924
|
+
childObjectValue = sourceValue;
|
|
925
|
+
}
|
|
926
|
+
else {
|
|
927
|
+
childObjectValue = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
933
928
|
}
|
|
929
|
+
Reflect.set(target, keyName, childObjectValue);
|
|
930
|
+
}
|
|
931
|
+
else {
|
|
932
|
+
/* 直接赋值 */
|
|
933
|
+
Reflect.set(target, keyName, sourceValue);
|
|
934
934
|
}
|
|
935
935
|
}
|
|
936
936
|
return target;
|
|
@@ -1617,6 +1617,12 @@ var DOMUtils = (function () {
|
|
|
1617
1617
|
}
|
|
1618
1618
|
new ElementAnimate();
|
|
1619
1619
|
|
|
1620
|
+
/* 数据 */
|
|
1621
|
+
const GlobalData = {
|
|
1622
|
+
/** .on添加在元素存储的事件 */
|
|
1623
|
+
domEventSymbol: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
|
|
1624
|
+
};
|
|
1625
|
+
|
|
1620
1626
|
const OriginPrototype = {
|
|
1621
1627
|
Object: {
|
|
1622
1628
|
defineProperty: Object.defineProperty,
|
|
@@ -1676,7 +1682,7 @@ var DOMUtils = (function () {
|
|
|
1676
1682
|
if (element == null) {
|
|
1677
1683
|
return {
|
|
1678
1684
|
off() { },
|
|
1679
|
-
|
|
1685
|
+
emit() { },
|
|
1680
1686
|
};
|
|
1681
1687
|
}
|
|
1682
1688
|
let $elList = [];
|
|
@@ -1811,10 +1817,10 @@ var DOMUtils = (function () {
|
|
|
1811
1817
|
/**
|
|
1812
1818
|
* 主动触发事件
|
|
1813
1819
|
* @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
|
|
1814
|
-
* @param
|
|
1820
|
+
* @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
|
|
1815
1821
|
*/
|
|
1816
|
-
|
|
1817
|
-
that.
|
|
1822
|
+
emit: (details, useDispatchToEmit) => {
|
|
1823
|
+
that.emit($elList, eventTypeList, details, useDispatchToEmit);
|
|
1818
1824
|
},
|
|
1819
1825
|
};
|
|
1820
1826
|
}
|
|
@@ -1993,7 +1999,7 @@ var DOMUtils = (function () {
|
|
|
1993
1999
|
});
|
|
1994
2000
|
});
|
|
1995
2001
|
}
|
|
1996
|
-
|
|
2002
|
+
onReady(...args) {
|
|
1997
2003
|
const callback = args[0];
|
|
1998
2004
|
// 异步回调
|
|
1999
2005
|
let resolve = void 0;
|
|
@@ -2087,16 +2093,16 @@ var DOMUtils = (function () {
|
|
|
2087
2093
|
* @param element 需要触发的元素|元素数组|window
|
|
2088
2094
|
* @param eventType 需要触发的事件
|
|
2089
2095
|
* @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
|
|
2090
|
-
* @param
|
|
2096
|
+
* @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
|
|
2091
2097
|
* @example
|
|
2092
2098
|
* // 触发元素a.xx的click事件
|
|
2093
|
-
* DOMUtils.
|
|
2094
|
-
* DOMUtils.
|
|
2099
|
+
* DOMUtils.emit(document.querySelector("a.xx"),"click")
|
|
2100
|
+
* DOMUtils.emit("a.xx","click")
|
|
2095
2101
|
* // 触发元素a.xx的click、tap、hover事件
|
|
2096
|
-
* DOMUtils.
|
|
2097
|
-
* DOMUtils.
|
|
2102
|
+
* DOMUtils.emit(document.querySelector("a.xx"),"click tap hover")
|
|
2103
|
+
* DOMUtils.emit("a.xx",["click","tap","hover"])
|
|
2098
2104
|
*/
|
|
2099
|
-
|
|
2105
|
+
emit(element, eventType, details, useDispatchToEmit = true) {
|
|
2100
2106
|
const that = this;
|
|
2101
2107
|
if (typeof element === "string") {
|
|
2102
2108
|
element = that.selectorAll(element);
|
|
@@ -2138,7 +2144,7 @@ var DOMUtils = (function () {
|
|
|
2138
2144
|
});
|
|
2139
2145
|
}
|
|
2140
2146
|
}
|
|
2141
|
-
if (
|
|
2147
|
+
if (useDispatchToEmit == false && eventTypeItem in elementEvents) {
|
|
2142
2148
|
// 直接调用监听的事件
|
|
2143
2149
|
elementEvents[eventTypeItem].forEach((eventsItem) => {
|
|
2144
2150
|
eventsItem.callback(event);
|
|
@@ -2151,11 +2157,11 @@ var DOMUtils = (function () {
|
|
|
2151
2157
|
});
|
|
2152
2158
|
}
|
|
2153
2159
|
/**
|
|
2154
|
-
*
|
|
2160
|
+
* 监听或触发元素的click事件
|
|
2155
2161
|
* @param element 目标元素
|
|
2156
2162
|
* @param handler (可选)事件处理函数
|
|
2157
2163
|
* @param details (可选)赋予触发的Event的额外属性
|
|
2158
|
-
* @param
|
|
2164
|
+
* @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
|
|
2159
2165
|
* @example
|
|
2160
2166
|
* // 触发元素a.xx的click事件
|
|
2161
2167
|
* DOMUtils.click(document.querySelector("a.xx"))
|
|
@@ -2164,7 +2170,7 @@ var DOMUtils = (function () {
|
|
|
2164
2170
|
* console.log("触发click事件成功")
|
|
2165
2171
|
* })
|
|
2166
2172
|
* */
|
|
2167
|
-
click(element, handler, details,
|
|
2173
|
+
click(element, handler, details, useDispatchToEmit) {
|
|
2168
2174
|
const that = this;
|
|
2169
2175
|
if (typeof element === "string") {
|
|
2170
2176
|
element = that.selectorAll(element);
|
|
@@ -2175,23 +2181,23 @@ var DOMUtils = (function () {
|
|
|
2175
2181
|
if (CommonUtils.isNodeList(element)) {
|
|
2176
2182
|
// 设置
|
|
2177
2183
|
element.forEach(($ele) => {
|
|
2178
|
-
that.click($ele, handler, details,
|
|
2184
|
+
that.click($ele, handler, details, useDispatchToEmit);
|
|
2179
2185
|
});
|
|
2180
2186
|
return;
|
|
2181
2187
|
}
|
|
2182
2188
|
if (handler == null) {
|
|
2183
|
-
that.
|
|
2189
|
+
that.emit(element, "click", details, useDispatchToEmit);
|
|
2184
2190
|
}
|
|
2185
2191
|
else {
|
|
2186
2192
|
that.on(element, "click", null, handler);
|
|
2187
2193
|
}
|
|
2188
2194
|
}
|
|
2189
2195
|
/**
|
|
2190
|
-
*
|
|
2196
|
+
* 监听或触发元素的blur事件
|
|
2191
2197
|
* @param element 目标元素
|
|
2192
2198
|
* @param handler (可选)事件处理函数
|
|
2193
2199
|
* @param details (可选)赋予触发的Event的额外属性
|
|
2194
|
-
* @param
|
|
2200
|
+
* @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
|
|
2195
2201
|
* @example
|
|
2196
2202
|
* // 触发元素a.xx的blur事件
|
|
2197
2203
|
* DOMUtils.blur(document.querySelector("a.xx"))
|
|
@@ -2200,7 +2206,7 @@ var DOMUtils = (function () {
|
|
|
2200
2206
|
* console.log("触发blur事件成功")
|
|
2201
2207
|
* })
|
|
2202
2208
|
* */
|
|
2203
|
-
blur(element, handler, details,
|
|
2209
|
+
blur(element, handler, details, useDispatchToEmit) {
|
|
2204
2210
|
const that = this;
|
|
2205
2211
|
if (typeof element === "string") {
|
|
2206
2212
|
element = that.selectorAll(element);
|
|
@@ -2211,23 +2217,23 @@ var DOMUtils = (function () {
|
|
|
2211
2217
|
if (CommonUtils.isNodeList(element)) {
|
|
2212
2218
|
// 设置
|
|
2213
2219
|
element.forEach(($ele) => {
|
|
2214
|
-
that.focus($ele, handler, details,
|
|
2220
|
+
that.focus($ele, handler, details, useDispatchToEmit);
|
|
2215
2221
|
});
|
|
2216
2222
|
return;
|
|
2217
2223
|
}
|
|
2218
2224
|
if (handler === null) {
|
|
2219
|
-
that.
|
|
2225
|
+
that.emit(element, "blur", details, useDispatchToEmit);
|
|
2220
2226
|
}
|
|
2221
2227
|
else {
|
|
2222
2228
|
that.on(element, "blur", null, handler);
|
|
2223
2229
|
}
|
|
2224
2230
|
}
|
|
2225
2231
|
/**
|
|
2226
|
-
*
|
|
2232
|
+
* 监听或触发元素的focus事件
|
|
2227
2233
|
* @param element 目标元素
|
|
2228
2234
|
* @param handler (可选)事件处理函数
|
|
2229
2235
|
* @param details (可选)赋予触发的Event的额外属性
|
|
2230
|
-
* @param
|
|
2236
|
+
* @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
|
|
2231
2237
|
* @example
|
|
2232
2238
|
* // 触发元素a.xx的focus事件
|
|
2233
2239
|
* DOMUtils.focus(document.querySelector("a.xx"))
|
|
@@ -2236,7 +2242,7 @@ var DOMUtils = (function () {
|
|
|
2236
2242
|
* console.log("触发focus事件成功")
|
|
2237
2243
|
* })
|
|
2238
2244
|
* */
|
|
2239
|
-
focus(element, handler, details,
|
|
2245
|
+
focus(element, handler, details, useDispatchToEmit) {
|
|
2240
2246
|
const that = this;
|
|
2241
2247
|
if (typeof element === "string") {
|
|
2242
2248
|
element = that.selectorAll(element);
|
|
@@ -2247,12 +2253,12 @@ var DOMUtils = (function () {
|
|
|
2247
2253
|
if (CommonUtils.isNodeList(element)) {
|
|
2248
2254
|
// 设置
|
|
2249
2255
|
element.forEach(($ele) => {
|
|
2250
|
-
that.focus($ele, handler, details,
|
|
2256
|
+
that.focus($ele, handler, details, useDispatchToEmit);
|
|
2251
2257
|
});
|
|
2252
2258
|
return;
|
|
2253
2259
|
}
|
|
2254
2260
|
if (handler == null) {
|
|
2255
|
-
that.
|
|
2261
|
+
that.emit(element, "focus", details, useDispatchToEmit);
|
|
2256
2262
|
}
|
|
2257
2263
|
else {
|
|
2258
2264
|
that.on(element, "focus", null, handler);
|
|
@@ -2265,14 +2271,14 @@ var DOMUtils = (function () {
|
|
|
2265
2271
|
* @param option 配置
|
|
2266
2272
|
* @example
|
|
2267
2273
|
* // 监听a.xx元素的移入或移出
|
|
2268
|
-
* DOMUtils.
|
|
2274
|
+
* DOMUtils.onHover(document.querySelector("a.xx"),()=>{
|
|
2269
2275
|
* console.log("移入/移除");
|
|
2270
2276
|
* })
|
|
2271
|
-
* DOMUtils.
|
|
2277
|
+
* DOMUtils.onHover("a.xx",()=>{
|
|
2272
2278
|
* console.log("移入/移除");
|
|
2273
2279
|
* })
|
|
2274
2280
|
*/
|
|
2275
|
-
|
|
2281
|
+
onHover(element, handler, option) {
|
|
2276
2282
|
const that = this;
|
|
2277
2283
|
if (typeof element === "string") {
|
|
2278
2284
|
element = that.selectorAll(element);
|
|
@@ -2283,7 +2289,7 @@ var DOMUtils = (function () {
|
|
|
2283
2289
|
if (CommonUtils.isNodeList(element)) {
|
|
2284
2290
|
// 设置
|
|
2285
2291
|
element.forEach(($ele) => {
|
|
2286
|
-
that.
|
|
2292
|
+
that.onHover($ele, handler, option);
|
|
2287
2293
|
});
|
|
2288
2294
|
return;
|
|
2289
2295
|
}
|
|
@@ -2291,12 +2297,12 @@ var DOMUtils = (function () {
|
|
|
2291
2297
|
that.on(element, "mouseleave", null, handler, option);
|
|
2292
2298
|
}
|
|
2293
2299
|
/**
|
|
2294
|
-
*
|
|
2300
|
+
* 监听动画结束
|
|
2295
2301
|
* @param element 监听的元素
|
|
2296
2302
|
* @param handler 触发的回调函数
|
|
2297
2303
|
* @param option 配置项,这里默认配置once为true
|
|
2298
2304
|
*/
|
|
2299
|
-
|
|
2305
|
+
onAnimationend(element, handler, option) {
|
|
2300
2306
|
const that = this;
|
|
2301
2307
|
if (typeof element === "string") {
|
|
2302
2308
|
element = that.selector(element);
|
|
@@ -2319,12 +2325,12 @@ var DOMUtils = (function () {
|
|
|
2319
2325
|
}
|
|
2320
2326
|
}
|
|
2321
2327
|
/**
|
|
2322
|
-
*
|
|
2328
|
+
* 监听过渡结束
|
|
2323
2329
|
* @param element 监听的元素
|
|
2324
2330
|
* @param handler 触发的回调函数
|
|
2325
2331
|
* @param option 配置项,这里默认配置once为true
|
|
2326
2332
|
*/
|
|
2327
|
-
|
|
2333
|
+
onTransitionend(element, handler, option) {
|
|
2328
2334
|
const that = this;
|
|
2329
2335
|
if (typeof element === "string") {
|
|
2330
2336
|
element = that.selector(element);
|
|
@@ -2361,7 +2367,7 @@ var DOMUtils = (function () {
|
|
|
2361
2367
|
* console.log("按键松开");
|
|
2362
2368
|
* })
|
|
2363
2369
|
*/
|
|
2364
|
-
|
|
2370
|
+
onKeyup(element, handler, option) {
|
|
2365
2371
|
const that = this;
|
|
2366
2372
|
if (element == null) {
|
|
2367
2373
|
return;
|
|
@@ -2372,7 +2378,7 @@ var DOMUtils = (function () {
|
|
|
2372
2378
|
if (CommonUtils.isNodeList(element)) {
|
|
2373
2379
|
// 设置
|
|
2374
2380
|
element.forEach(($ele) => {
|
|
2375
|
-
that.
|
|
2381
|
+
that.onKeyup($ele, handler, option);
|
|
2376
2382
|
});
|
|
2377
2383
|
return;
|
|
2378
2384
|
}
|
|
@@ -2393,7 +2399,7 @@ var DOMUtils = (function () {
|
|
|
2393
2399
|
* console.log("按键按下");
|
|
2394
2400
|
* })
|
|
2395
2401
|
*/
|
|
2396
|
-
|
|
2402
|
+
onKeydown(element, handler, option) {
|
|
2397
2403
|
const that = this;
|
|
2398
2404
|
if (element == null) {
|
|
2399
2405
|
return;
|
|
@@ -2404,7 +2410,7 @@ var DOMUtils = (function () {
|
|
|
2404
2410
|
if (CommonUtils.isNodeList(element)) {
|
|
2405
2411
|
// 设置
|
|
2406
2412
|
element.forEach(($ele) => {
|
|
2407
|
-
that.
|
|
2413
|
+
that.onKeydown($ele, handler, option);
|
|
2408
2414
|
});
|
|
2409
2415
|
return;
|
|
2410
2416
|
}
|
|
@@ -2425,7 +2431,7 @@ var DOMUtils = (function () {
|
|
|
2425
2431
|
* console.log("按键按下");
|
|
2426
2432
|
* })
|
|
2427
2433
|
*/
|
|
2428
|
-
|
|
2434
|
+
onKeypress(element, handler, option) {
|
|
2429
2435
|
const that = this;
|
|
2430
2436
|
if (element == null) {
|
|
2431
2437
|
return;
|
|
@@ -2436,76 +2442,76 @@ var DOMUtils = (function () {
|
|
|
2436
2442
|
if (CommonUtils.isNodeList(element)) {
|
|
2437
2443
|
// 设置
|
|
2438
2444
|
element.forEach(($ele) => {
|
|
2439
|
-
that.
|
|
2445
|
+
that.onKeypress($ele, handler, option);
|
|
2440
2446
|
});
|
|
2441
2447
|
return;
|
|
2442
2448
|
}
|
|
2443
2449
|
that.on(element, "keypress", null, handler, option);
|
|
2444
2450
|
}
|
|
2445
2451
|
/**
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2452
|
+
* 监听某个元素键盘按键事件或window全局按键事件
|
|
2453
|
+
* 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
|
|
2454
|
+
* @param element 需要监听的对象,可以是全局Window或者某个元素
|
|
2455
|
+
* @param eventName 事件名,默认keypress,keydown - > keypress - > keyup
|
|
2456
|
+
* @param handler 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
|
|
2451
2457
|
* @param options 监听事件的配置
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2458
|
+
* @example
|
|
2459
|
+
Utils.onKeyboard(window,(keyName,keyValue,otherKey,event)=>{
|
|
2460
|
+
if(keyName === "Enter"){
|
|
2461
|
+
console.log("回车按键的值是:"+keyValue)
|
|
2462
|
+
}
|
|
2463
|
+
if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
|
|
2464
|
+
console.log("Ctrl和回车键");
|
|
2465
|
+
}
|
|
2466
|
+
})
|
|
2467
|
+
* @example
|
|
2468
|
+
字母和数字键的键码值(keyCode)
|
|
2469
|
+
按键 键码 按键 键码 按键 键码 按键 键码
|
|
2470
|
+
A 65 J 74 S 83 1 49
|
|
2471
|
+
B 66 K 75 T 84 2 50
|
|
2472
|
+
C 67 L 76 U 85 3 51
|
|
2473
|
+
D 68 M 77 V 86 4 52
|
|
2474
|
+
E 69 N 78 W 87 5 53
|
|
2475
|
+
F 70 O 79 X 88 6 54
|
|
2476
|
+
G 71 P 80 Y 89 7 55
|
|
2477
|
+
H 72 Q 81 Z 90 8 56
|
|
2478
|
+
I 73 R 82 0 48 9 57
|
|
2479
|
+
|
|
2480
|
+
数字键盘上的键的键码值(keyCode)
|
|
2481
|
+
功能键键码值(keyCode)
|
|
2482
|
+
按键 键码 按键 键码 按键 键码 按键 键码
|
|
2483
|
+
0 96 8 104 F1 112 F7 118
|
|
2484
|
+
1 97 9 105 F2 113 F8 119
|
|
2485
|
+
2 98 * 106 F3 114 F9 120
|
|
2486
|
+
3 99 + 107 F4 115 F10 121
|
|
2487
|
+
4 100 Enter 108 F5 116 F11 122
|
|
2488
|
+
5 101 - 109 F6 117 F12 123
|
|
2489
|
+
6 102 . 110
|
|
2490
|
+
7 103 / 111
|
|
2491
|
+
|
|
2492
|
+
控制键键码值(keyCode)
|
|
2493
|
+
按键 键码 按键 键码 按键 键码 按键 键码
|
|
2494
|
+
BackSpace 8 Esc 27 → 39 -_ 189
|
|
2495
|
+
Tab 9 Spacebar 32 ↓ 40 .> 190
|
|
2496
|
+
Clear 12 Page Up 33 Insert 45 /? 191
|
|
2497
|
+
Enter 13 Page Down 34 Delete 46 `~ 192
|
|
2498
|
+
Shift 16 End 35 Num Lock 144 [{ 219
|
|
2499
|
+
Control 17 Home 36 ;: 186 \| 220
|
|
2500
|
+
Alt 18 ← 37 =+ 187 ]} 221
|
|
2501
|
+
Cape Lock 20 ↑ 38 ,< 188 '" 222
|
|
2502
|
+
|
|
2503
|
+
多媒体键码值(keyCode)
|
|
2504
|
+
按键 键码
|
|
2505
|
+
音量加 175
|
|
2506
|
+
音量减 174
|
|
2507
|
+
停止 179
|
|
2508
|
+
静音 173
|
|
2509
|
+
浏览器 172
|
|
2510
|
+
邮件 180
|
|
2511
|
+
搜索 170
|
|
2512
|
+
收藏 171
|
|
2513
|
+
**/
|
|
2514
|
+
onKeyboard(element, eventName = "keypress", handler, options) {
|
|
2509
2515
|
const that = this;
|
|
2510
2516
|
if (typeof element === "string") {
|
|
2511
2517
|
element = that.selectorAll(element);
|
|
@@ -2529,8 +2535,8 @@ var DOMUtils = (function () {
|
|
|
2529
2535
|
if (event.shiftKey) {
|
|
2530
2536
|
otherCodeList.push("shift");
|
|
2531
2537
|
}
|
|
2532
|
-
if (typeof
|
|
2533
|
-
|
|
2538
|
+
if (typeof handler === "function") {
|
|
2539
|
+
handler(keyName, keyValue, otherCodeList, event);
|
|
2534
2540
|
}
|
|
2535
2541
|
};
|
|
2536
2542
|
that.on(element, eventName, keyboardEventCallBack, options);
|
|
@@ -2540,6 +2546,113 @@ var DOMUtils = (function () {
|
|
|
2540
2546
|
},
|
|
2541
2547
|
};
|
|
2542
2548
|
}
|
|
2549
|
+
/**
|
|
2550
|
+
* 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
|
|
2551
|
+
* @param $el 输入框元素
|
|
2552
|
+
* @param handler 回调函数
|
|
2553
|
+
* @param option 配置
|
|
2554
|
+
*/
|
|
2555
|
+
onInput($el, handler, option) {
|
|
2556
|
+
/**
|
|
2557
|
+
* 是否正在输入中
|
|
2558
|
+
*/
|
|
2559
|
+
let isComposite = false;
|
|
2560
|
+
const __callback = async (event) => {
|
|
2561
|
+
if (isComposite)
|
|
2562
|
+
return;
|
|
2563
|
+
await handler(event);
|
|
2564
|
+
};
|
|
2565
|
+
const __composition_start_callback = () => {
|
|
2566
|
+
isComposite = true;
|
|
2567
|
+
};
|
|
2568
|
+
const __composition_end_callback = () => {
|
|
2569
|
+
isComposite = false;
|
|
2570
|
+
this.emit($el, "input", {
|
|
2571
|
+
isComposite,
|
|
2572
|
+
});
|
|
2573
|
+
};
|
|
2574
|
+
const inputListener = this.on($el, "input", __callback, option);
|
|
2575
|
+
const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
|
|
2576
|
+
const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
|
|
2577
|
+
return {
|
|
2578
|
+
off: () => {
|
|
2579
|
+
inputListener.off();
|
|
2580
|
+
compositionStartListener.off();
|
|
2581
|
+
compositionEndListener.off();
|
|
2582
|
+
},
|
|
2583
|
+
};
|
|
2584
|
+
}
|
|
2585
|
+
onDoubleClick(...args) {
|
|
2586
|
+
const $el = args[0];
|
|
2587
|
+
let selector = void 0;
|
|
2588
|
+
let handler;
|
|
2589
|
+
let options;
|
|
2590
|
+
if (args.length === 2) {
|
|
2591
|
+
if (typeof args[1] === "function") {
|
|
2592
|
+
handler = args[1];
|
|
2593
|
+
}
|
|
2594
|
+
else {
|
|
2595
|
+
throw new TypeError("handler is not a function");
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
else if (args.length === 3) {
|
|
2599
|
+
if (typeof args[1] === "function") {
|
|
2600
|
+
handler = args[1];
|
|
2601
|
+
options = args[2];
|
|
2602
|
+
}
|
|
2603
|
+
else {
|
|
2604
|
+
selector = args[1];
|
|
2605
|
+
handler = args[2];
|
|
2606
|
+
}
|
|
2607
|
+
}
|
|
2608
|
+
else if (args.length === 4) {
|
|
2609
|
+
selector = args[1];
|
|
2610
|
+
handler = args[2];
|
|
2611
|
+
options = args[3];
|
|
2612
|
+
}
|
|
2613
|
+
else {
|
|
2614
|
+
throw new Error("args length error");
|
|
2615
|
+
}
|
|
2616
|
+
let $click = null;
|
|
2617
|
+
let isDoubleClick = false;
|
|
2618
|
+
let timer = void 0;
|
|
2619
|
+
/** 是否是移动端点击 */
|
|
2620
|
+
let isMobileTouch = false;
|
|
2621
|
+
const dblclick_handler = async (evt) => {
|
|
2622
|
+
if (evt.type === "dblclick" && isMobileTouch) {
|
|
2623
|
+
// 禁止在移动端触发dblclick事件
|
|
2624
|
+
return;
|
|
2625
|
+
}
|
|
2626
|
+
await handler(evt);
|
|
2627
|
+
};
|
|
2628
|
+
const dblClickListener = this.on($el, "dblclick", dblclick_handler, options);
|
|
2629
|
+
const touchEndListener = this.on($el, "touchend", selector, (evt, selectorTarget) => {
|
|
2630
|
+
isMobileTouch = true;
|
|
2631
|
+
CommonUtils.clearTimeout(timer);
|
|
2632
|
+
timer = void 0;
|
|
2633
|
+
if (isDoubleClick && $click === selectorTarget) {
|
|
2634
|
+
isDoubleClick = false;
|
|
2635
|
+
$click = null;
|
|
2636
|
+
/* 判定为双击 */
|
|
2637
|
+
dblclick_handler(evt);
|
|
2638
|
+
}
|
|
2639
|
+
else {
|
|
2640
|
+
timer = CommonUtils.setTimeout(() => {
|
|
2641
|
+
isDoubleClick = false;
|
|
2642
|
+
// 判断为单击
|
|
2643
|
+
}, 200);
|
|
2644
|
+
isDoubleClick = true;
|
|
2645
|
+
$click = selectorTarget;
|
|
2646
|
+
}
|
|
2647
|
+
}, options);
|
|
2648
|
+
return {
|
|
2649
|
+
off() {
|
|
2650
|
+
$click = null;
|
|
2651
|
+
dblClickListener.off();
|
|
2652
|
+
touchEndListener.off();
|
|
2653
|
+
},
|
|
2654
|
+
};
|
|
2655
|
+
}
|
|
2543
2656
|
preventEvent(...args) {
|
|
2544
2657
|
/**
|
|
2545
2658
|
* 阻止事件的默认行为发生,并阻止事件传播
|