@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.umd.js CHANGED
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.DOMUtils = factory());
5
5
  })(this, (function () { 'use strict';
6
6
 
7
- const version = "1.9.5";
7
+ const version = "1.9.6";
8
8
 
9
9
  class WindowApi {
10
10
  /** 默认的配置 */
@@ -594,10 +594,7 @@
594
594
  "slideDown",
595
595
  "slideToggle",
596
596
  "slideUp",
597
- "sort",
598
- "splice",
599
597
  "text",
600
- "toArray",
601
598
  "toggle",
602
599
  "toggleClass",
603
600
  "trigger",
@@ -1863,35 +1860,43 @@
1863
1860
  else {
1864
1861
  $elList.push(element);
1865
1862
  }
1863
+ /**
1864
+ * 主动添加属性
1865
+ */
1866
+ const addExtraProp = (event, obj) => {
1867
+ if (event instanceof Event && typeof obj === "object" && obj != null && !Array.isArray(obj)) {
1868
+ const detailKeys = Object.keys(obj);
1869
+ detailKeys.forEach((keyName) => {
1870
+ const value = Reflect.get(obj, keyName);
1871
+ // 在event上添加属性
1872
+ Reflect.set(event, keyName, value);
1873
+ });
1874
+ }
1875
+ };
1866
1876
  let eventTypeList = [];
1877
+ /**
1878
+ * 主动传递的事件
1879
+ */
1880
+ let __event__ = null;
1867
1881
  if (Array.isArray(eventType)) {
1868
1882
  eventTypeList = eventType.filter((it) => typeof it === "string" && it.trim() !== "");
1869
1883
  }
1870
1884
  else if (typeof eventType === "string") {
1871
1885
  eventTypeList = eventType.split(" ");
1872
1886
  }
1887
+ else if (eventType instanceof Event) {
1888
+ __event__ = eventType;
1889
+ addExtraProp(__event__, extraDetails);
1890
+ }
1873
1891
  $elList.forEach(($elItem) => {
1874
1892
  /* 获取对象上的事件 */
1875
1893
  const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
1876
- eventTypeList.forEach((eventTypeItem) => {
1877
- let event = null;
1878
- if (extraDetails && extraDetails instanceof Event) {
1879
- event = extraDetails;
1880
- }
1881
- else {
1882
- // 构造事件
1883
- event = new Event(eventTypeItem);
1884
- if (typeof extraDetails === "object" && extraDetails != null) {
1885
- const detailKeys = Object.keys(extraDetails);
1886
- detailKeys.forEach((keyName) => {
1887
- const value = Reflect.get(extraDetails, keyName);
1888
- // 在event上添加属性
1889
- Reflect.set(event, keyName, value);
1890
- });
1891
- }
1892
- }
1894
+ /**
1895
+ * 触发事件
1896
+ */
1897
+ const dispatchEvent = (event, eventTypeItem) => {
1893
1898
  if (useDispatchToTriggerEvent == false && eventTypeItem in elementEvents) {
1894
- // 直接调用监听的事件
1899
+ // 直接调用.on监听的事件
1895
1900
  elementEvents[eventTypeItem].forEach((eventsItem) => {
1896
1901
  eventsItem.handlerCallBack(event);
1897
1902
  });
@@ -1899,7 +1904,21 @@
1899
1904
  else {
1900
1905
  $elItem.dispatchEvent(event);
1901
1906
  }
1902
- });
1907
+ };
1908
+ if (__event__) {
1909
+ // 使用主动传递的事件直接触发
1910
+ const event = __event__;
1911
+ const eventTypeItem = event.type;
1912
+ dispatchEvent(event, eventTypeItem);
1913
+ }
1914
+ else {
1915
+ eventTypeList.forEach((eventTypeItem) => {
1916
+ // 构造事件
1917
+ const event = new Event(eventTypeItem);
1918
+ addExtraProp(event, extraDetails);
1919
+ dispatchEvent(event, eventTypeItem);
1920
+ });
1921
+ }
1903
1922
  });
1904
1923
  }
1905
1924
  /**
@@ -1935,7 +1954,8 @@
1935
1954
  that.emit(element, "click", details, useDispatchToEmit);
1936
1955
  }
1937
1956
  else {
1938
- that.on(element, "click", null, handler);
1957
+ const listener = that.on(element, "click", null, handler);
1958
+ return listener;
1939
1959
  }
1940
1960
  }
1941
1961
  /**
@@ -1971,7 +1991,8 @@
1971
1991
  that.emit(element, "blur", details, useDispatchToEmit);
1972
1992
  }
1973
1993
  else {
1974
- that.on(element, "blur", null, handler);
1994
+ const listener = that.on(element, "blur", null, handler);
1995
+ return listener;
1975
1996
  }
1976
1997
  }
1977
1998
  /**
@@ -2007,7 +2028,8 @@
2007
2028
  that.emit(element, "focus", details, useDispatchToEmit);
2008
2029
  }
2009
2030
  else {
2010
- that.on(element, "focus", null, handler);
2031
+ const listener = that.on(element, "focus", null, handler);
2032
+ return listener;
2011
2033
  }
2012
2034
  }
2013
2035
  /**
@@ -2034,13 +2056,30 @@
2034
2056
  }
2035
2057
  if (CommonUtils.isNodeList(element)) {
2036
2058
  // 设置
2059
+ const listenerList = [];
2037
2060
  element.forEach(($ele) => {
2038
- that.onHover($ele, handler, option);
2061
+ const listener = that.onHover($ele, handler, option);
2062
+ listenerList.push(listener);
2039
2063
  });
2040
- return;
2064
+ return {
2065
+ off() {
2066
+ listenerList.forEach((listener) => {
2067
+ if (!listener) {
2068
+ return;
2069
+ }
2070
+ listener.off();
2071
+ });
2072
+ },
2073
+ };
2041
2074
  }
2042
- that.on(element, "mouseenter", null, handler, option);
2043
- that.on(element, "mouseleave", null, handler, option);
2075
+ const mouseenter_listener = that.on(element, "mouseenter", null, handler, option);
2076
+ const mouseleave_listener = that.on(element, "mouseleave", null, handler, option);
2077
+ return {
2078
+ off() {
2079
+ mouseenter_listener.off();
2080
+ mouseleave_listener.off();
2081
+ },
2082
+ };
2044
2083
  }
2045
2084
  /**
2046
2085
  * 监听动画结束
@@ -2123,12 +2162,23 @@
2123
2162
  }
2124
2163
  if (CommonUtils.isNodeList(element)) {
2125
2164
  // 设置
2165
+ const listenerList = [];
2126
2166
  element.forEach(($ele) => {
2127
- that.onKeyup($ele, handler, option);
2167
+ const listener = that.onKeyup($ele, handler, option);
2168
+ listenerList.push(listener);
2128
2169
  });
2129
- return;
2170
+ return {
2171
+ off() {
2172
+ listenerList.forEach((listener) => {
2173
+ if (!listener) {
2174
+ return;
2175
+ }
2176
+ listener.off();
2177
+ });
2178
+ },
2179
+ };
2130
2180
  }
2131
- that.on(element, "keyup", null, handler, option);
2181
+ return that.on(element, "keyup", null, handler, option);
2132
2182
  }
2133
2183
  /**
2134
2184
  * 当按键按下时触发事件
@@ -2155,12 +2205,23 @@
2155
2205
  }
2156
2206
  if (CommonUtils.isNodeList(element)) {
2157
2207
  // 设置
2208
+ const listenerList = [];
2158
2209
  element.forEach(($ele) => {
2159
- that.onKeydown($ele, handler, option);
2210
+ const listener = that.onKeydown($ele, handler, option);
2211
+ listenerList.push(listener);
2160
2212
  });
2161
- return;
2213
+ return {
2214
+ off() {
2215
+ listenerList.forEach((listener) => {
2216
+ if (!listener) {
2217
+ return;
2218
+ }
2219
+ listener.off();
2220
+ });
2221
+ },
2222
+ };
2162
2223
  }
2163
- that.on(element, "keydown", null, handler, option);
2224
+ return that.on(element, "keydown", null, handler, option);
2164
2225
  }
2165
2226
  /**
2166
2227
  * 当按键按下时触发事件
@@ -2187,12 +2248,23 @@
2187
2248
  }
2188
2249
  if (CommonUtils.isNodeList(element)) {
2189
2250
  // 设置
2251
+ const listenerList = [];
2190
2252
  element.forEach(($ele) => {
2191
- that.onKeypress($ele, handler, option);
2253
+ const listener = that.onKeypress($ele, handler, option);
2254
+ listenerList.push(listener);
2192
2255
  });
2193
- return;
2256
+ return {
2257
+ off() {
2258
+ listenerList.forEach((listener) => {
2259
+ if (!listener) {
2260
+ return;
2261
+ }
2262
+ listener.off();
2263
+ });
2264
+ },
2265
+ };
2194
2266
  }
2195
- that.on(element, "keypress", null, handler, option);
2267
+ return that.on(element, "keypress", null, handler, option);
2196
2268
  }
2197
2269
  /**
2198
2270
  * 监听某个元素键盘按键事件或window全局按键事件
@@ -2285,12 +2357,8 @@
2285
2357
  handler(keyName, keyValue, otherCodeList, event);
2286
2358
  }
2287
2359
  };
2288
- that.on(element, eventName, keyboardEventCallBack, options);
2289
- return {
2290
- removeListen: () => {
2291
- that.off(element, eventName, keyboardEventCallBack, options);
2292
- },
2293
- };
2360
+ const listener = that.on(element, eventName, keyboardEventCallBack, options);
2361
+ return listener;
2294
2362
  }
2295
2363
  /**
2296
2364
  * 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
@@ -2321,11 +2389,14 @@
2321
2389
  const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
2322
2390
  const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
2323
2391
  return {
2324
- off: () => {
2392
+ off() {
2325
2393
  inputListener.off();
2326
2394
  compositionStartListener.off();
2327
2395
  compositionEndListener.off();
2328
2396
  },
2397
+ emit(details, useDispatchToEmit) {
2398
+ inputListener.emit(details, useDispatchToEmit);
2399
+ },
2329
2400
  };
2330
2401
  }
2331
2402
  onDoubleClick(...args) {
@@ -2408,28 +2479,56 @@
2408
2479
  /**
2409
2480
  * 阻止事件的默认行为发生,并阻止事件传播
2410
2481
  */
2411
- const stopEvent = (event) => {
2412
- /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
2482
+ const stopEvent = (event, onlyStopPropagation) => {
2483
+ if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
2484
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2485
+ event?.stopPropagation();
2486
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2487
+ event?.stopImmediatePropagation();
2488
+ return;
2489
+ }
2490
+ // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
2413
2491
  event?.preventDefault();
2414
- /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
2415
- event?.stopPropagation();
2416
- /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
2417
- event?.stopImmediatePropagation();
2418
2492
  return false;
2419
2493
  };
2420
- if (args.length === 1) {
2421
- /* 直接阻止事件 */
2422
- return stopEvent(args[0]);
2494
+ if (args[0] instanceof Event) {
2495
+ // 直接阻止事件
2496
+ const onlyStopPropagation = args[1];
2497
+ return stopEvent(args[0], onlyStopPropagation);
2423
2498
  }
2424
2499
  else {
2425
2500
  const $el = args[0];
2426
2501
  let eventNameList = args[1];
2427
- const capture = args[2];
2428
- /* 添加对应的事件来阻止触发 */
2502
+ let selector = void 0;
2503
+ let capture = false;
2504
+ let onlyStopPropagation = false;
2505
+ // 添加对应的事件来阻止触发
2429
2506
  if (typeof eventNameList === "string") {
2430
2507
  eventNameList = [eventNameList];
2431
2508
  }
2432
- this.on($el, eventNameList, stopEvent, { capture: Boolean(capture) });
2509
+ let option = void 0;
2510
+ if (typeof args[2] === "string" || Array.isArray(args[2])) {
2511
+ // selector
2512
+ selector = args[2];
2513
+ if (typeof args[3] === "object" && args[3] != null) {
2514
+ option = args[3];
2515
+ }
2516
+ }
2517
+ else if (typeof args[2] === "object" && args[2] != null && !Array.isArray(args[2])) {
2518
+ // option
2519
+ option = args[2];
2520
+ }
2521
+ else {
2522
+ throw new TypeError("Invalid argument");
2523
+ }
2524
+ if (option) {
2525
+ capture = Boolean(option.capture);
2526
+ onlyStopPropagation = Boolean(option.onlyStopPropagation);
2527
+ }
2528
+ const listener = this.on($el, eventNameList, selector, (evt) => {
2529
+ return stopEvent(evt, onlyStopPropagation);
2530
+ }, { capture: capture });
2531
+ return listener;
2433
2532
  }
2434
2533
  }
2435
2534
  }