@whitesev/domutils 1.9.4 → 1.9.6

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.
@@ -3,7 +3,7 @@ System.register('DOMUtils', [], (function (exports) {
3
3
  return {
4
4
  execute: (function () {
5
5
 
6
- const version = "1.9.4";
6
+ const version = "1.9.6";
7
7
 
8
8
  class WindowApi {
9
9
  /** 默认的配置 */
@@ -1934,7 +1934,8 @@ System.register('DOMUtils', [], (function (exports) {
1934
1934
  that.emit(element, "click", details, useDispatchToEmit);
1935
1935
  }
1936
1936
  else {
1937
- that.on(element, "click", null, handler);
1937
+ const listener = that.on(element, "click", null, handler);
1938
+ return listener;
1938
1939
  }
1939
1940
  }
1940
1941
  /**
@@ -1970,7 +1971,8 @@ System.register('DOMUtils', [], (function (exports) {
1970
1971
  that.emit(element, "blur", details, useDispatchToEmit);
1971
1972
  }
1972
1973
  else {
1973
- that.on(element, "blur", null, handler);
1974
+ const listener = that.on(element, "blur", null, handler);
1975
+ return listener;
1974
1976
  }
1975
1977
  }
1976
1978
  /**
@@ -2006,7 +2008,8 @@ System.register('DOMUtils', [], (function (exports) {
2006
2008
  that.emit(element, "focus", details, useDispatchToEmit);
2007
2009
  }
2008
2010
  else {
2009
- that.on(element, "focus", null, handler);
2011
+ const listener = that.on(element, "focus", null, handler);
2012
+ return listener;
2010
2013
  }
2011
2014
  }
2012
2015
  /**
@@ -2033,13 +2036,30 @@ System.register('DOMUtils', [], (function (exports) {
2033
2036
  }
2034
2037
  if (CommonUtils.isNodeList(element)) {
2035
2038
  // 设置
2039
+ const listenerList = [];
2036
2040
  element.forEach(($ele) => {
2037
- that.onHover($ele, handler, option);
2041
+ const listener = that.onHover($ele, handler, option);
2042
+ listenerList.push(listener);
2038
2043
  });
2039
- return;
2044
+ return {
2045
+ off() {
2046
+ listenerList.forEach((listener) => {
2047
+ if (!listener) {
2048
+ return;
2049
+ }
2050
+ listener.off();
2051
+ });
2052
+ },
2053
+ };
2040
2054
  }
2041
- that.on(element, "mouseenter", null, handler, option);
2042
- that.on(element, "mouseleave", null, handler, option);
2055
+ const mouseenter_listener = that.on(element, "mouseenter", null, handler, option);
2056
+ const mouseleave_listener = that.on(element, "mouseleave", null, handler, option);
2057
+ return {
2058
+ off() {
2059
+ mouseenter_listener.off();
2060
+ mouseleave_listener.off();
2061
+ },
2062
+ };
2043
2063
  }
2044
2064
  /**
2045
2065
  * 监听动画结束
@@ -2122,12 +2142,23 @@ System.register('DOMUtils', [], (function (exports) {
2122
2142
  }
2123
2143
  if (CommonUtils.isNodeList(element)) {
2124
2144
  // 设置
2145
+ const listenerList = [];
2125
2146
  element.forEach(($ele) => {
2126
- that.onKeyup($ele, handler, option);
2147
+ const listener = that.onKeyup($ele, handler, option);
2148
+ listenerList.push(listener);
2127
2149
  });
2128
- return;
2150
+ return {
2151
+ off() {
2152
+ listenerList.forEach((listener) => {
2153
+ if (!listener) {
2154
+ return;
2155
+ }
2156
+ listener.off();
2157
+ });
2158
+ },
2159
+ };
2129
2160
  }
2130
- that.on(element, "keyup", null, handler, option);
2161
+ return that.on(element, "keyup", null, handler, option);
2131
2162
  }
2132
2163
  /**
2133
2164
  * 当按键按下时触发事件
@@ -2154,12 +2185,23 @@ System.register('DOMUtils', [], (function (exports) {
2154
2185
  }
2155
2186
  if (CommonUtils.isNodeList(element)) {
2156
2187
  // 设置
2188
+ const listenerList = [];
2157
2189
  element.forEach(($ele) => {
2158
- that.onKeydown($ele, handler, option);
2190
+ const listener = that.onKeydown($ele, handler, option);
2191
+ listenerList.push(listener);
2159
2192
  });
2160
- return;
2193
+ return {
2194
+ off() {
2195
+ listenerList.forEach((listener) => {
2196
+ if (!listener) {
2197
+ return;
2198
+ }
2199
+ listener.off();
2200
+ });
2201
+ },
2202
+ };
2161
2203
  }
2162
- that.on(element, "keydown", null, handler, option);
2204
+ return that.on(element, "keydown", null, handler, option);
2163
2205
  }
2164
2206
  /**
2165
2207
  * 当按键按下时触发事件
@@ -2186,12 +2228,23 @@ System.register('DOMUtils', [], (function (exports) {
2186
2228
  }
2187
2229
  if (CommonUtils.isNodeList(element)) {
2188
2230
  // 设置
2231
+ const listenerList = [];
2189
2232
  element.forEach(($ele) => {
2190
- that.onKeypress($ele, handler, option);
2233
+ const listener = that.onKeypress($ele, handler, option);
2234
+ listenerList.push(listener);
2191
2235
  });
2192
- return;
2236
+ return {
2237
+ off() {
2238
+ listenerList.forEach((listener) => {
2239
+ if (!listener) {
2240
+ return;
2241
+ }
2242
+ listener.off();
2243
+ });
2244
+ },
2245
+ };
2193
2246
  }
2194
- that.on(element, "keypress", null, handler, option);
2247
+ return that.on(element, "keypress", null, handler, option);
2195
2248
  }
2196
2249
  /**
2197
2250
  * 监听某个元素键盘按键事件或window全局按键事件
@@ -2284,12 +2337,8 @@ System.register('DOMUtils', [], (function (exports) {
2284
2337
  handler(keyName, keyValue, otherCodeList, event);
2285
2338
  }
2286
2339
  };
2287
- that.on(element, eventName, keyboardEventCallBack, options);
2288
- return {
2289
- removeListen: () => {
2290
- that.off(element, eventName, keyboardEventCallBack, options);
2291
- },
2292
- };
2340
+ const listener = that.on(element, eventName, keyboardEventCallBack, options);
2341
+ return listener;
2293
2342
  }
2294
2343
  /**
2295
2344
  * 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
@@ -2320,11 +2369,14 @@ System.register('DOMUtils', [], (function (exports) {
2320
2369
  const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
2321
2370
  const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
2322
2371
  return {
2323
- off: () => {
2372
+ off() {
2324
2373
  inputListener.off();
2325
2374
  compositionStartListener.off();
2326
2375
  compositionEndListener.off();
2327
2376
  },
2377
+ emit(details, useDispatchToEmit) {
2378
+ inputListener.emit(details, useDispatchToEmit);
2379
+ },
2328
2380
  };
2329
2381
  }
2330
2382
  onDoubleClick(...args) {
@@ -2358,41 +2410,26 @@ System.register('DOMUtils', [], (function (exports) {
2358
2410
  else {
2359
2411
  throw new Error("args length error");
2360
2412
  }
2361
- let $click = null;
2413
+ let clickMap = new WeakMap();
2362
2414
  let isDoubleClick = false;
2363
2415
  let timer = void 0;
2364
- /** 是否是移动端点击 */
2365
- let isMobileTouch = false;
2366
2416
  /** 检测是否是单击的延迟时间 */
2367
2417
  const checkClickTime = 200;
2368
- const dblclick_handler = async (evt, option, $selector) => {
2369
- if (evt.type === "dblclick" && isMobileTouch) {
2370
- // 禁止在移动端触发dblclick事件
2371
- return;
2372
- }
2418
+ const dblclick_handler = (evt, option, $selector) => {
2373
2419
  if ($selector) {
2374
- await handler(evt, $selector, option);
2420
+ return handler(evt, $selector, option);
2375
2421
  }
2376
2422
  else {
2377
- await handler(evt, option);
2423
+ return handler(evt, option);
2378
2424
  }
2379
2425
  };
2380
- const dblClickListener = this.on($el, "dblclick", selector, (evt, $selector) => {
2381
- this.preventEvent(evt);
2382
- dblclick_handler(evt, {
2383
- isDoubleClick: true,
2384
- }, $selector);
2385
- }, options);
2386
2426
  const pointerUpListener = this.on($el, "pointerup", selector, (evt, $selector) => {
2387
- this.preventEvent(evt);
2388
- if (evt.pointerType === "touch") {
2389
- isMobileTouch = true;
2390
- }
2427
+ // this.preventEvent(evt);
2391
2428
  clearTimeout(timer);
2392
2429
  timer = void 0;
2393
- if (isDoubleClick && $click === $selector) {
2430
+ if (isDoubleClick && clickMap.has($selector)) {
2394
2431
  isDoubleClick = false;
2395
- $click = null;
2432
+ clickMap.delete($selector);
2396
2433
  /* 判定为双击 */
2397
2434
  dblclick_handler(evt, {
2398
2435
  isDoubleClick: true,
@@ -2407,14 +2444,14 @@ System.register('DOMUtils', [], (function (exports) {
2407
2444
  }, $selector);
2408
2445
  }, checkClickTime);
2409
2446
  isDoubleClick = true;
2410
- $click = $selector;
2447
+ clickMap.set($selector, evt);
2411
2448
  }
2412
2449
  }, options);
2413
2450
  return {
2414
2451
  off() {
2415
- $click = null;
2416
- dblClickListener.off();
2417
2452
  pointerUpListener.off();
2453
+ // @ts-expect-error
2454
+ clickMap = null;
2418
2455
  },
2419
2456
  };
2420
2457
  }
@@ -2422,28 +2459,56 @@ System.register('DOMUtils', [], (function (exports) {
2422
2459
  /**
2423
2460
  * 阻止事件的默认行为发生,并阻止事件传播
2424
2461
  */
2425
- const stopEvent = (event) => {
2426
- /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
2462
+ const stopEvent = (event, onlyStopPropagation) => {
2463
+ if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
2464
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2465
+ event?.stopPropagation();
2466
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2467
+ event?.stopImmediatePropagation();
2468
+ return;
2469
+ }
2470
+ // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
2427
2471
  event?.preventDefault();
2428
- /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
2429
- event?.stopPropagation();
2430
- /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
2431
- event?.stopImmediatePropagation();
2432
2472
  return false;
2433
2473
  };
2434
- if (args.length === 1) {
2435
- /* 直接阻止事件 */
2436
- return stopEvent(args[0]);
2474
+ if (args[0] instanceof Event) {
2475
+ // 直接阻止事件
2476
+ const onlyStopPropagation = args[1];
2477
+ return stopEvent(args[0], onlyStopPropagation);
2437
2478
  }
2438
2479
  else {
2439
2480
  const $el = args[0];
2440
2481
  let eventNameList = args[1];
2441
- const capture = args[2];
2442
- /* 添加对应的事件来阻止触发 */
2482
+ let selector = void 0;
2483
+ let capture = false;
2484
+ let onlyStopPropagation = false;
2485
+ // 添加对应的事件来阻止触发
2443
2486
  if (typeof eventNameList === "string") {
2444
2487
  eventNameList = [eventNameList];
2445
2488
  }
2446
- this.on($el, eventNameList, stopEvent, { capture: Boolean(capture) });
2489
+ let option = void 0;
2490
+ if (typeof args[2] === "string" || Array.isArray(args[2])) {
2491
+ // selector
2492
+ selector = args[2];
2493
+ if (typeof args[3] === "object" && args[3] != null) {
2494
+ option = args[3];
2495
+ }
2496
+ }
2497
+ else if (typeof args[2] === "object" && args[2] != null && !Array.isArray(args[2])) {
2498
+ // option
2499
+ option = args[2];
2500
+ }
2501
+ else {
2502
+ throw new TypeError("Invalid argument");
2503
+ }
2504
+ if (option) {
2505
+ capture = Boolean(option.capture);
2506
+ onlyStopPropagation = Boolean(option.onlyStopPropagation);
2507
+ }
2508
+ const listener = this.on($el, eventNameList, selector, (evt) => {
2509
+ return stopEvent(evt, onlyStopPropagation);
2510
+ }, { capture: capture });
2511
+ return listener;
2447
2512
  }
2448
2513
  }
2449
2514
  }