@whitesev/domutils 1.3.7 → 1.4.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.
@@ -1,12 +1,56 @@
1
1
  var DOMUtils = (function () {
2
2
  'use strict';
3
3
 
4
+ class WindowApi {
5
+ /** 默认的配置 */
6
+ defaultApi = {
7
+ document: document,
8
+ window: window,
9
+ globalThis: globalThis,
10
+ self: self,
11
+ top: top,
12
+ };
13
+ /** 使用的配置 */
14
+ api;
15
+ constructor(option) {
16
+ if (option) {
17
+ if (option.globalThis == null) {
18
+ option.globalThis = option.window;
19
+ }
20
+ if (option.self == null) {
21
+ option.self = option.window;
22
+ }
23
+ }
24
+ if (!option) {
25
+ option = Object.assign({}, this.defaultApi);
26
+ }
27
+ // @ts-ignore
28
+ this.api = Object.assign({}, option);
29
+ }
30
+ get document() {
31
+ return this.api.document;
32
+ }
33
+ get window() {
34
+ return this.api.window;
35
+ }
36
+ get globalThis() {
37
+ return this.api.globalThis;
38
+ }
39
+ get self() {
40
+ return this.api.self;
41
+ }
42
+ get top() {
43
+ return this.api.top;
44
+ }
45
+ }
46
+
4
47
  /** 通用工具类 */
5
48
  const DOMUtilsCommonUtils = {
6
- windowApi: {
7
- window: window,
49
+ windowApi: new WindowApi({
8
50
  document: document,
9
- },
51
+ window: window,
52
+ top: top,
53
+ }),
10
54
  /**
11
55
  * 判断元素是否已显示或已连接
12
56
  * @param element
@@ -112,7 +156,7 @@ var DOMUtils = (function () {
112
156
 
113
157
  /* 数据 */
114
158
  const DOMUtilsData = {
115
- /** .on绑定的事件 */
159
+ /** .on添加在元素存储的事件 */
116
160
  SymbolEvents: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
117
161
  };
118
162
 
@@ -122,40 +166,6 @@ var DOMUtils = (function () {
122
166
  },
123
167
  };
124
168
 
125
- class WindowApi {
126
- /** 默认的配置 */
127
- defaultApi = {
128
- document: document,
129
- window: window,
130
- globalThis: globalThis,
131
- self: self,
132
- top: top,
133
- };
134
- /** 使用的配置 */
135
- api;
136
- constructor(option) {
137
- if (!option) {
138
- option = Object.assign({}, this.defaultApi);
139
- }
140
- this.api = Object.assign({}, option);
141
- }
142
- get document() {
143
- return this.api.document;
144
- }
145
- get window() {
146
- return this.api.window;
147
- }
148
- get globalThis() {
149
- return this.api.globalThis;
150
- }
151
- get self() {
152
- return this.api.self;
153
- }
154
- get top() {
155
- return this.api.top;
156
- }
157
- }
158
-
159
169
  class DOMUtilsEvent {
160
170
  windowApi;
161
171
  constructor(windowApiOption) {
@@ -532,7 +542,7 @@ var DOMUtils = (function () {
532
542
  trigger(element, eventType, details, useDispatchToTriggerEvent = true) {
533
543
  let DOMUtilsContext = this;
534
544
  if (typeof element === "string") {
535
- element = DOMUtilsContext.windowApi.document.querySelector(element);
545
+ element = DOMUtilsContext.selectorAll(element);
536
546
  }
537
547
  if (element == null) {
538
548
  return;
@@ -632,7 +642,7 @@ var DOMUtils = (function () {
632
642
  blur(element, handler, details, useDispatchToTriggerEvent) {
633
643
  let DOMUtilsContext = this;
634
644
  if (typeof element === "string") {
635
- element = DOMUtilsContext.windowApi.document.querySelector(element);
645
+ element = DOMUtilsContext.selectorAll(element);
636
646
  }
637
647
  if (element == null) {
638
648
  return;
@@ -968,7 +978,7 @@ var DOMUtils = (function () {
968
978
  super(option);
969
979
  }
970
980
  /** 版本号 */
971
- version = "2024.10.23";
981
+ version = "2024.11.6";
972
982
  attr(element, attrName, attrValue) {
973
983
  let DOMUtilsContext = this;
974
984
  if (typeof element === "string") {
@@ -1465,6 +1475,41 @@ var DOMUtils = (function () {
1465
1475
  element.classList.add(itemClassName);
1466
1476
  });
1467
1477
  }
1478
+ /**
1479
+ * 判断元素是否存在className
1480
+ * @param element
1481
+ * @param className
1482
+ */
1483
+ hasClass(element, className) {
1484
+ let DOMUtilsContext = this;
1485
+ if (typeof element === "string") {
1486
+ element = DOMUtilsContext.selectorAll(element);
1487
+ }
1488
+ if (element == null) {
1489
+ return false;
1490
+ }
1491
+ if (isNodeList(element)) {
1492
+ let flag = true;
1493
+ for (let index = 0; index < element.length; index++) {
1494
+ const $ele = element[index];
1495
+ flag = flag && DOMUtilsContext.hasClass($ele, className);
1496
+ }
1497
+ return flag;
1498
+ }
1499
+ if (!element?.classList) {
1500
+ return false;
1501
+ }
1502
+ if (!Array.isArray(className)) {
1503
+ className = className.split(" ");
1504
+ }
1505
+ for (let index = 0; index < className.length; index++) {
1506
+ const item = className[index].trim();
1507
+ if (!element.classList.contains(item)) {
1508
+ return false;
1509
+ }
1510
+ }
1511
+ return true;
1512
+ }
1468
1513
  /**
1469
1514
  * 函数在元素内部末尾添加子元素或HTML字符串
1470
1515
  * @param element 目标元素
@@ -1687,7 +1732,7 @@ var DOMUtils = (function () {
1687
1732
  offset(element) {
1688
1733
  let DOMUtilsContext = this;
1689
1734
  if (typeof element === "string") {
1690
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1735
+ element = DOMUtilsContext.selector(element);
1691
1736
  }
1692
1737
  if (element == null) {
1693
1738
  return;
@@ -1703,8 +1748,7 @@ var DOMUtils = (function () {
1703
1748
  width(element, isShow = false) {
1704
1749
  let DOMUtilsContext = this;
1705
1750
  if (typeof element === "string") {
1706
- element =
1707
- DOMUtilsContext.windowApi.document.querySelector(element);
1751
+ element = DOMUtilsContext.selector(element);
1708
1752
  }
1709
1753
  if (element == null) {
1710
1754
  return;
@@ -1758,7 +1802,7 @@ var DOMUtils = (function () {
1758
1802
  .clientHeight;
1759
1803
  }
1760
1804
  if (typeof element === "string") {
1761
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1805
+ element = DOMUtilsContext.selector(element);
1762
1806
  }
1763
1807
  if (element == null) {
1764
1808
  // @ts-ignore
@@ -1808,7 +1852,7 @@ var DOMUtils = (function () {
1808
1852
  return DOMUtilsContext.windowApi.window.innerWidth;
1809
1853
  }
1810
1854
  if (typeof element === "string") {
1811
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1855
+ element = DOMUtilsContext.selector(element);
1812
1856
  }
1813
1857
  if (element == null) {
1814
1858
  // @ts-ignore
@@ -1834,7 +1878,7 @@ var DOMUtils = (function () {
1834
1878
  return DOMUtilsContext.windowApi.window.innerHeight;
1835
1879
  }
1836
1880
  if (typeof element === "string") {
1837
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1881
+ element = DOMUtilsContext.selector(element);
1838
1882
  }
1839
1883
  if (element == null) {
1840
1884
  // @ts-ignore
@@ -1957,7 +2001,7 @@ var DOMUtils = (function () {
1957
2001
  prev(element) {
1958
2002
  let DOMUtilsContext = this;
1959
2003
  if (typeof element === "string") {
1960
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2004
+ element = DOMUtilsContext.selector(element);
1961
2005
  }
1962
2006
  if (element == null) {
1963
2007
  return;
@@ -1967,7 +2011,7 @@ var DOMUtils = (function () {
1967
2011
  next(element) {
1968
2012
  let DOMUtilsContext = this;
1969
2013
  if (typeof element === "string") {
1970
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2014
+ element = DOMUtilsContext.selector(element);
1971
2015
  }
1972
2016
  if (element == null) {
1973
2017
  return;
@@ -1990,7 +2034,7 @@ var DOMUtils = (function () {
1990
2034
  siblings(element) {
1991
2035
  let DOMUtilsContext = this;
1992
2036
  if (typeof element === "string") {
1993
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2037
+ element = DOMUtilsContext.selector(element);
1994
2038
  }
1995
2039
  if (element == null) {
1996
2040
  return;
@@ -2011,16 +2055,15 @@ var DOMUtils = (function () {
2011
2055
  parent(element) {
2012
2056
  let DOMUtilsContext = this;
2013
2057
  if (typeof element === "string") {
2014
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2058
+ element = DOMUtilsContext.selector(element);
2015
2059
  }
2016
2060
  if (element == null) {
2017
2061
  return;
2018
2062
  }
2019
- if (element instanceof NodeList || element instanceof Array) {
2020
- element = element;
2063
+ if (isNodeList(element)) {
2021
2064
  let resultArray = [];
2022
- element.forEach((eleItem) => {
2023
- resultArray.push(DOMUtilsContext.parent(eleItem));
2065
+ element.forEach(($ele) => {
2066
+ resultArray.push(DOMUtilsContext.parent($ele));
2024
2067
  });
2025
2068
  return resultArray;
2026
2069
  }