@whitesev/domutils 1.6.8 → 1.7.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/dist/index.amd.js +1928 -1047
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +2 -0
- package/dist/index.amd.min.js.map +1 -0
- package/dist/index.cjs.js +1928 -1047
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +2 -0
- package/dist/index.cjs.min.js.map +1 -0
- package/dist/index.esm.js +1928 -1047
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +2 -0
- package/dist/index.esm.min.js.map +1 -0
- package/dist/index.iife.js +1928 -1047
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +2 -0
- package/dist/index.iife.min.js.map +1 -0
- package/dist/index.system.js +1928 -1047
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +2 -0
- package/dist/index.system.min.js.map +1 -0
- package/dist/index.umd.js +1928 -1047
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +2 -0
- package/dist/index.umd.min.js.map +1 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/src/{DOMUtilsCommonUtils.d.ts → CommonUtils.d.ts} +20 -8
- package/dist/types/src/ElementAnimate.d.ts +89 -0
- package/dist/types/src/{DOMUtilsEvent.d.ts → ElementEvent.d.ts} +19 -84
- package/dist/types/src/ElementHandler.d.ts +17 -0
- package/dist/types/src/ElementSelector.d.ts +96 -0
- package/dist/types/src/ElementWait.d.ts +278 -0
- package/dist/types/src/GlobalData.d.ts +4 -0
- package/dist/types/src/{DOMUtilsOriginPrototype.d.ts → OriginPrototype.d.ts} +1 -2
- package/dist/types/src/Utils.d.ts +68 -0
- package/dist/types/src/{DOMUtils.d.ts → index.d.ts} +157 -177
- package/dist/types/src/types/env.d.ts +9 -0
- package/dist/types/src/types/global.d.ts +0 -2
- package/dist/types/src/types/gm.d.ts +0 -4
- package/index.ts +1 -1
- package/package.json +6 -2
- package/src/{DOMUtilsCommonUtils.ts → CommonUtils.ts} +25 -11
- package/src/ElementAnimate.ts +290 -0
- package/src/{DOMUtilsEvent.ts → ElementEvent.ts} +166 -361
- package/src/ElementHandler.ts +43 -0
- package/src/ElementSelector.ts +260 -0
- package/src/ElementWait.ts +699 -0
- package/src/GlobalData.ts +5 -0
- package/src/{DOMUtilsOriginPrototype.ts → OriginPrototype.ts} +1 -3
- package/src/Utils.ts +386 -0
- package/src/{DOMUtils.ts → index.ts} +678 -757
- package/src/types/env.d.ts +9 -0
- package/src/types/global.d.ts +0 -2
- package/src/types/gm.d.ts +0 -4
- package/dist/types/src/DOMUtilsData.d.ts +0 -5
- package/src/DOMUtilsData.ts +0 -7
|
@@ -1,19 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DOMUtilsEvent } from "./DOMUtilsEvent";
|
|
1
|
+
import { CommonUtils } from "./CommonUtils";
|
|
3
2
|
import type { DOMUtilsCreateElementAttributesMap } from "./types/DOMUtilsEvent";
|
|
4
|
-
import { type
|
|
3
|
+
import { type DOMUtilsTargetElementType } from "./types/global";
|
|
5
4
|
import type { WindowApiOption } from "./types/WindowApi";
|
|
6
|
-
import { version } from "
|
|
5
|
+
import { version } from "../package.json";
|
|
6
|
+
import { ElementHandler } from "./ElementHandler";
|
|
7
7
|
|
|
8
|
-
class DOMUtils extends
|
|
8
|
+
class DOMUtils extends ElementHandler {
|
|
9
9
|
constructor(option?: WindowApiOption) {
|
|
10
10
|
super(option);
|
|
11
11
|
}
|
|
12
12
|
/** 版本号 */
|
|
13
13
|
version = version;
|
|
14
|
+
/**
|
|
15
|
+
* 取消挂载在window下的DOMUtils并返回DOMUtils
|
|
16
|
+
* @example
|
|
17
|
+
* let DOMUtils = window.DOMUtils.noConflict()
|
|
18
|
+
*/
|
|
19
|
+
noConflict() {
|
|
20
|
+
const that = this;
|
|
21
|
+
if (that.windowApi.window.DOMUtils) {
|
|
22
|
+
CommonUtils.delete(window, "DOMUtils");
|
|
23
|
+
}
|
|
24
|
+
that.windowApi.window.DOMUtils = this;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
14
27
|
/**
|
|
15
28
|
* 获取元素的属性值
|
|
16
|
-
* @param
|
|
29
|
+
* @param $el 目标元素
|
|
17
30
|
* @param attrName 属性名
|
|
18
31
|
* @example
|
|
19
32
|
* // 获取a.xx元素的href属性
|
|
@@ -21,10 +34,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
21
34
|
* DOMUtils.attr("a.xx","href");
|
|
22
35
|
* > https://xxxx....
|
|
23
36
|
*/
|
|
24
|
-
attr(
|
|
37
|
+
attr($el: DOMUtilsTargetElementType | Element, attrName: string): string;
|
|
25
38
|
/**
|
|
26
39
|
* 设置元素的属性值
|
|
27
|
-
* @param
|
|
40
|
+
* @param $el 目标元素
|
|
28
41
|
* @param attrName 属性名
|
|
29
42
|
* @param attrValue 属性值
|
|
30
43
|
* @example
|
|
@@ -32,31 +45,31 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
32
45
|
* DOMUtils.attr(document.querySelector("a.xx"),"href","abcd");
|
|
33
46
|
* DOMUtils.attr("a.xx","href","abcd");
|
|
34
47
|
*/
|
|
35
|
-
attr(
|
|
36
|
-
attr(
|
|
37
|
-
const
|
|
38
|
-
if (typeof
|
|
39
|
-
|
|
48
|
+
attr($el: DOMUtilsTargetElementType | Element, attrName: string, attrValue: string | boolean | number): void;
|
|
49
|
+
attr($el: DOMUtilsTargetElementType | Element, attrName: string, attrValue?: any) {
|
|
50
|
+
const that = this;
|
|
51
|
+
if (typeof $el === "string") {
|
|
52
|
+
$el = that.selectorAll($el);
|
|
40
53
|
}
|
|
41
|
-
if (
|
|
54
|
+
if ($el == null) {
|
|
42
55
|
return;
|
|
43
56
|
}
|
|
44
|
-
if (
|
|
57
|
+
if (CommonUtils.isNodeList($el)) {
|
|
45
58
|
if (attrValue == null) {
|
|
46
59
|
// 获取属性
|
|
47
|
-
return
|
|
60
|
+
return that.attr($el[0] as HTMLElement, attrName, attrValue);
|
|
48
61
|
} else {
|
|
49
62
|
// 设置属性
|
|
50
|
-
|
|
51
|
-
|
|
63
|
+
$el.forEach(($elItem) => {
|
|
64
|
+
that.attr($elItem as HTMLElement, attrName, attrValue);
|
|
52
65
|
});
|
|
53
66
|
return;
|
|
54
67
|
}
|
|
55
68
|
}
|
|
56
69
|
if (attrValue == null) {
|
|
57
|
-
return
|
|
70
|
+
return $el.getAttribute(attrName);
|
|
58
71
|
} else {
|
|
59
|
-
|
|
72
|
+
$el.setAttribute(attrName, attrValue);
|
|
60
73
|
}
|
|
61
74
|
}
|
|
62
75
|
/**
|
|
@@ -137,11 +150,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
137
150
|
/** 自定义属性 */
|
|
138
151
|
attributes?: DOMUtilsCreateElementAttributesMap
|
|
139
152
|
): HTMLElementTagNameMap[K] {
|
|
140
|
-
const
|
|
141
|
-
const
|
|
153
|
+
const that = this;
|
|
154
|
+
const $el = that.windowApi.document.createElement(tagName);
|
|
142
155
|
if (typeof property === "string") {
|
|
143
|
-
|
|
144
|
-
return
|
|
156
|
+
that.html($el, property);
|
|
157
|
+
return $el;
|
|
145
158
|
}
|
|
146
159
|
if (property == null) {
|
|
147
160
|
property = {};
|
|
@@ -152,10 +165,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
152
165
|
Object.keys(property).forEach((key) => {
|
|
153
166
|
const value = property[key];
|
|
154
167
|
if (key === "innerHTML") {
|
|
155
|
-
|
|
168
|
+
that.html($el, value);
|
|
156
169
|
return;
|
|
157
170
|
}
|
|
158
|
-
(
|
|
171
|
+
(<any>$el)[key] = value;
|
|
159
172
|
});
|
|
160
173
|
Object.keys(attributes).forEach((key) => {
|
|
161
174
|
let value = attributes[key];
|
|
@@ -166,13 +179,13 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
166
179
|
/* function转字符串 */
|
|
167
180
|
value = value.toString();
|
|
168
181
|
}
|
|
169
|
-
|
|
182
|
+
$el.setAttribute(key, value);
|
|
170
183
|
});
|
|
171
|
-
return
|
|
184
|
+
return $el;
|
|
172
185
|
}
|
|
173
186
|
/**
|
|
174
187
|
* 获取元素的样式属性值
|
|
175
|
-
* @param
|
|
188
|
+
* @param $el 目标元素
|
|
176
189
|
* @param property 样式属性名或包含多个属性名和属性值的对象
|
|
177
190
|
* @example
|
|
178
191
|
* // 获取元素a.xx的CSS属性display
|
|
@@ -180,10 +193,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
180
193
|
* DOMUtils.css("a.xx","display");
|
|
181
194
|
* > "none"
|
|
182
195
|
* */
|
|
183
|
-
css(
|
|
196
|
+
css($el: DOMUtilsTargetElementType, property: keyof Omit<CSSStyleDeclaration, "zIndex"> | "z-index"): string;
|
|
184
197
|
/**
|
|
185
198
|
* 获取元素的样式属性值
|
|
186
|
-
* @param
|
|
199
|
+
* @param $el 目标元素
|
|
187
200
|
* @param property 样式属性名或包含多个属性名和属性值的对象
|
|
188
201
|
* @example
|
|
189
202
|
* // 获取元素a.xx的CSS属性display
|
|
@@ -191,10 +204,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
191
204
|
* DOMUtils.css("a.xx","display");
|
|
192
205
|
* > "none"
|
|
193
206
|
* */
|
|
194
|
-
css(
|
|
207
|
+
css($el: DOMUtilsTargetElementType, property: string): string;
|
|
195
208
|
/**
|
|
196
209
|
* 设置元素的样式属性
|
|
197
|
-
* @param
|
|
210
|
+
* @param $el 目标元素
|
|
198
211
|
* @param property 样式属性名或包含多个属性名和属性值的对象
|
|
199
212
|
* @param value 样式属性值
|
|
200
213
|
* @example
|
|
@@ -209,13 +222,13 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
209
222
|
* DOMUtils.css(document.querySelector("a.xx"),"top",10);
|
|
210
223
|
* */
|
|
211
224
|
css(
|
|
212
|
-
|
|
225
|
+
$el: DOMUtilsTargetElementType,
|
|
213
226
|
property: (keyof Omit<CSSStyleDeclaration, "zIndex"> | "z-index") & string,
|
|
214
227
|
value: string | number
|
|
215
228
|
): string;
|
|
216
229
|
/**
|
|
217
230
|
* 设置元素的样式属性
|
|
218
|
-
* @param
|
|
231
|
+
* @param $el 目标元素
|
|
219
232
|
* @param property 样式属性名或包含多个属性名和属性值的对象
|
|
220
233
|
* @param value 样式属性值
|
|
221
234
|
* @example
|
|
@@ -228,7 +241,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
228
241
|
* DOMUtils.css(document.querySelector("a.xx"),{ top: 10 });
|
|
229
242
|
* */
|
|
230
243
|
css(
|
|
231
|
-
|
|
244
|
+
$el: DOMUtilsTargetElementType,
|
|
232
245
|
property:
|
|
233
246
|
| {
|
|
234
247
|
[P in keyof Omit<CSSStyleDeclaration, "zIndex">]?: CSSStyleDeclaration[P];
|
|
@@ -241,7 +254,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
241
254
|
}
|
|
242
255
|
): string;
|
|
243
256
|
css(
|
|
244
|
-
|
|
257
|
+
$el: DOMUtilsTargetElementType,
|
|
245
258
|
property:
|
|
246
259
|
| keyof Omit<CSSStyleDeclaration, "zIndex">
|
|
247
260
|
| string
|
|
@@ -250,7 +263,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
250
263
|
},
|
|
251
264
|
value?: string | number
|
|
252
265
|
) {
|
|
253
|
-
const
|
|
266
|
+
const that = this;
|
|
254
267
|
/**
|
|
255
268
|
* 把纯数字没有px的加上
|
|
256
269
|
*/
|
|
@@ -264,28 +277,28 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
264
277
|
}
|
|
265
278
|
return propertyValue;
|
|
266
279
|
}
|
|
267
|
-
if (typeof
|
|
268
|
-
|
|
280
|
+
if (typeof $el === "string") {
|
|
281
|
+
$el = that.selectorAll($el);
|
|
269
282
|
}
|
|
270
|
-
if (
|
|
283
|
+
if ($el == null) {
|
|
271
284
|
return;
|
|
272
285
|
}
|
|
273
|
-
if (
|
|
286
|
+
if (CommonUtils.isNodeList($el)) {
|
|
274
287
|
if (typeof property === "string") {
|
|
275
288
|
if (value == null) {
|
|
276
289
|
// 获取属性
|
|
277
|
-
return
|
|
290
|
+
return that.css($el[0] as HTMLElement, property);
|
|
278
291
|
} else {
|
|
279
292
|
// 设置属性
|
|
280
|
-
|
|
281
|
-
|
|
293
|
+
$el.forEach(($elItem) => {
|
|
294
|
+
that.css($elItem as HTMLElement, property);
|
|
282
295
|
});
|
|
283
296
|
return;
|
|
284
297
|
}
|
|
285
298
|
} else if (typeof property === "object") {
|
|
286
299
|
// 设置属性
|
|
287
|
-
|
|
288
|
-
|
|
300
|
+
$el.forEach(($elItem) => {
|
|
301
|
+
that.css($elItem as HTMLElement, property as object);
|
|
289
302
|
});
|
|
290
303
|
return;
|
|
291
304
|
}
|
|
@@ -297,15 +310,15 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
297
310
|
.trim()
|
|
298
311
|
.replace(/!important$/gi, "")
|
|
299
312
|
.trim();
|
|
300
|
-
|
|
313
|
+
$el.style.setProperty(propertyName, propertyValue, "important");
|
|
301
314
|
} else {
|
|
302
315
|
propertyValue = handlePixe(propertyName, propertyValue);
|
|
303
|
-
|
|
316
|
+
$el.style.setProperty(propertyName, propertyValue);
|
|
304
317
|
}
|
|
305
318
|
};
|
|
306
319
|
if (typeof property === "string") {
|
|
307
320
|
if (value == null) {
|
|
308
|
-
return
|
|
321
|
+
return that.windowApi.globalThis.getComputedStyle($el).getPropertyValue(property);
|
|
309
322
|
} else {
|
|
310
323
|
setStyleProperty(property, value);
|
|
311
324
|
}
|
|
@@ -321,7 +334,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
321
334
|
}
|
|
322
335
|
/**
|
|
323
336
|
* 获取元素的文本内容,优先返回textContent
|
|
324
|
-
* @param
|
|
337
|
+
* @param $el 目标元素
|
|
325
338
|
* @returns 如果传入了text,则返回undefined;否则返回文本内容
|
|
326
339
|
* @example
|
|
327
340
|
* // 设置元素a.xx的文本内容为abcd
|
|
@@ -329,10 +342,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
329
342
|
* DOMUtils.text("a.xx","abcd")
|
|
330
343
|
* DOMUtils.text("a.xx",document.querySelector("b"))
|
|
331
344
|
* */
|
|
332
|
-
text(
|
|
345
|
+
text($el: DOMUtilsTargetElementType | Element | DocumentFragment | Node): string;
|
|
333
346
|
/**
|
|
334
347
|
* 设置元素的文本内容
|
|
335
|
-
* @param
|
|
348
|
+
* @param $el 目标元素
|
|
336
349
|
* @param text (可选)文本内容
|
|
337
350
|
* @returns 如果传入了text,则返回undefined;否则返回文本内容
|
|
338
351
|
* @example
|
|
@@ -342,43 +355,42 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
342
355
|
* DOMUtils.text("a.xx",document.querySelector("b"))
|
|
343
356
|
* */
|
|
344
357
|
text(
|
|
345
|
-
|
|
358
|
+
$el: DOMUtilsTargetElementType | Element | DocumentFragment | Node,
|
|
346
359
|
text: string | HTMLElement | Element | number
|
|
347
360
|
): void;
|
|
348
|
-
text(
|
|
349
|
-
const
|
|
350
|
-
if (typeof
|
|
351
|
-
|
|
361
|
+
text($el: DOMUtilsTargetElementType | Element | DocumentFragment | Node, text?: any) {
|
|
362
|
+
const that = this;
|
|
363
|
+
if (typeof $el === "string") {
|
|
364
|
+
$el = that.selectorAll($el);
|
|
352
365
|
}
|
|
353
|
-
if (
|
|
366
|
+
if ($el == null) {
|
|
354
367
|
return;
|
|
355
368
|
}
|
|
356
|
-
if (
|
|
369
|
+
if (CommonUtils.isNodeList($el)) {
|
|
357
370
|
if (text == null) {
|
|
358
371
|
// 获取
|
|
359
|
-
return
|
|
372
|
+
return that.text($el[0] as HTMLElement);
|
|
360
373
|
} else {
|
|
361
374
|
// 设置
|
|
362
|
-
|
|
363
|
-
|
|
375
|
+
$el.forEach(($elItem) => {
|
|
376
|
+
that.text($elItem as HTMLElement, text);
|
|
364
377
|
});
|
|
365
378
|
}
|
|
366
379
|
return;
|
|
367
380
|
}
|
|
368
381
|
if (text == null) {
|
|
369
|
-
return
|
|
382
|
+
return $el.textContent || (<HTMLElement>$el).innerText;
|
|
370
383
|
} else {
|
|
371
384
|
if (text instanceof Node) {
|
|
372
385
|
text = text.textContent || (text as HTMLElement).innerText;
|
|
373
386
|
}
|
|
374
|
-
if ("textContent" in
|
|
375
|
-
|
|
376
|
-
} else if ("innerText" in
|
|
377
|
-
(
|
|
387
|
+
if ("textContent" in $el) {
|
|
388
|
+
$el.textContent = text as string;
|
|
389
|
+
} else if ("innerText" in $el) {
|
|
390
|
+
($el as HTMLElement).innerText = text as string;
|
|
378
391
|
}
|
|
379
392
|
}
|
|
380
393
|
}
|
|
381
|
-
|
|
382
394
|
/**
|
|
383
395
|
* 设置元素的HTML内容
|
|
384
396
|
* @param element 目标元素
|
|
@@ -393,7 +405,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
393
405
|
html(element: DOMUtilsTargetElementType, html: string | HTMLElement | Element | number): void;
|
|
394
406
|
/**
|
|
395
407
|
* 获取元素的HTML内容
|
|
396
|
-
* @param
|
|
408
|
+
* @param $el 目标元素
|
|
397
409
|
* @param html (可选)HTML内容|元素
|
|
398
410
|
* @returns 如果传入了html,则返回undefined;否则返回HTML内容
|
|
399
411
|
* @example
|
|
@@ -402,37 +414,37 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
402
414
|
* DOMUtils.html("a.xx","<b>abcd</b>")
|
|
403
415
|
* DOMUtils.html("a.xx",document.querySelector("b"))
|
|
404
416
|
* */
|
|
405
|
-
html(
|
|
406
|
-
html(
|
|
407
|
-
const
|
|
408
|
-
if (typeof
|
|
409
|
-
|
|
417
|
+
html($el: DOMUtilsTargetElementType): string;
|
|
418
|
+
html($el: DOMUtilsTargetElementType, html?: any) {
|
|
419
|
+
const that = this;
|
|
420
|
+
if (typeof $el === "string") {
|
|
421
|
+
$el = that.selectorAll($el);
|
|
410
422
|
}
|
|
411
|
-
if (
|
|
423
|
+
if ($el == null) {
|
|
412
424
|
return;
|
|
413
425
|
}
|
|
414
|
-
if (
|
|
426
|
+
if (CommonUtils.isNodeList($el)) {
|
|
415
427
|
if (html == null) {
|
|
416
428
|
// 获取
|
|
417
|
-
return
|
|
429
|
+
return that.html($el[0] as HTMLElement);
|
|
418
430
|
} else {
|
|
419
431
|
// 设置
|
|
420
|
-
|
|
421
|
-
|
|
432
|
+
$el.forEach(($elItem) => {
|
|
433
|
+
that.html($elItem as HTMLElement, html);
|
|
422
434
|
});
|
|
423
435
|
}
|
|
424
436
|
return;
|
|
425
437
|
}
|
|
426
438
|
if (html == null) {
|
|
427
439
|
// 获取
|
|
428
|
-
return
|
|
440
|
+
return $el.innerHTML;
|
|
429
441
|
} else {
|
|
430
442
|
// 设置
|
|
431
443
|
if (html instanceof Element) {
|
|
432
444
|
html = html.innerHTML;
|
|
433
445
|
}
|
|
434
|
-
if ("innerHTML" in
|
|
435
|
-
|
|
446
|
+
if ("innerHTML" in $el) {
|
|
447
|
+
CommonUtils.setSafeHTML($el, html);
|
|
436
448
|
}
|
|
437
449
|
}
|
|
438
450
|
}
|
|
@@ -440,23 +452,23 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
440
452
|
* 获取移动元素的transform偏移
|
|
441
453
|
*/
|
|
442
454
|
getTransform(
|
|
443
|
-
|
|
455
|
+
$el: HTMLElement,
|
|
444
456
|
isShow: boolean = false
|
|
445
457
|
): {
|
|
446
458
|
transformLeft: number;
|
|
447
459
|
transformTop: number;
|
|
448
460
|
} {
|
|
449
|
-
const
|
|
461
|
+
const that = this;
|
|
450
462
|
let transform_left = 0;
|
|
451
463
|
let transform_top = 0;
|
|
452
|
-
if (!(isShow || (!isShow &&
|
|
464
|
+
if (!(isShow || (!isShow && CommonUtils.isShow($el)))) {
|
|
453
465
|
/* 未显示 */
|
|
454
|
-
const { recovery } =
|
|
455
|
-
const transformInfo =
|
|
466
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
467
|
+
const transformInfo = that.getTransform($el, true);
|
|
456
468
|
recovery();
|
|
457
469
|
return transformInfo;
|
|
458
470
|
}
|
|
459
|
-
const elementTransform =
|
|
471
|
+
const elementTransform = that.windowApi.globalThis.getComputedStyle($el).transform;
|
|
460
472
|
if (elementTransform != null && elementTransform !== "none" && elementTransform !== "") {
|
|
461
473
|
const elementTransformSplit = elementTransform.match(/\((.+)\)/)?.[1].split(",");
|
|
462
474
|
if (elementTransformSplit) {
|
|
@@ -475,7 +487,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
475
487
|
|
|
476
488
|
/**
|
|
477
489
|
* 设置元素的value属性值
|
|
478
|
-
* @param
|
|
490
|
+
* @param $el 目标元素
|
|
479
491
|
* @param value (可选)value属性值
|
|
480
492
|
* @returns 如果传入了value,则返回undefined;否则返回value属性值
|
|
481
493
|
* > true
|
|
@@ -485,7 +497,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
485
497
|
* DOMUtils.val("input.xx",true)
|
|
486
498
|
* */
|
|
487
499
|
val(
|
|
488
|
-
|
|
500
|
+
$el:
|
|
489
501
|
| HTMLInputElement
|
|
490
502
|
| HTMLTextAreaElement
|
|
491
503
|
| HTMLSelectElement
|
|
@@ -496,13 +508,13 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
496
508
|
): void;
|
|
497
509
|
/**
|
|
498
510
|
* 获取value属性值
|
|
499
|
-
* @param
|
|
511
|
+
* @param $el 目标元素
|
|
500
512
|
* @example
|
|
501
513
|
* // 获取元素textarea的值
|
|
502
514
|
* DOMUtils.val(document.querySelector("textarea.xx"))
|
|
503
515
|
* */
|
|
504
516
|
val(
|
|
505
|
-
|
|
517
|
+
$el:
|
|
506
518
|
| HTMLInputElement
|
|
507
519
|
| HTMLTextAreaElement
|
|
508
520
|
| HTMLSelectElement
|
|
@@ -512,14 +524,14 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
512
524
|
): string;
|
|
513
525
|
/**
|
|
514
526
|
* 获取value属性值
|
|
515
|
-
* @param
|
|
527
|
+
* @param $el 目标元素
|
|
516
528
|
* @example
|
|
517
529
|
* // 获取元素input.xx的复选框值
|
|
518
530
|
* DOMUtils.val(document.querySelector("input.xx"))
|
|
519
531
|
* DOMUtils.val("input.xx")
|
|
520
532
|
* */
|
|
521
533
|
val(
|
|
522
|
-
|
|
534
|
+
$el:
|
|
523
535
|
| HTMLInputElement
|
|
524
536
|
| HTMLTextAreaElement
|
|
525
537
|
| HTMLSelectElement
|
|
@@ -527,7 +539,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
527
539
|
| NodeListOf<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>
|
|
528
540
|
): boolean | string;
|
|
529
541
|
val(
|
|
530
|
-
|
|
542
|
+
$el:
|
|
531
543
|
| HTMLInputElement
|
|
532
544
|
| HTMLTextAreaElement
|
|
533
545
|
| HTMLSelectElement
|
|
@@ -536,45 +548,44 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
536
548
|
| NodeListOf<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>,
|
|
537
549
|
value?: string | boolean | number
|
|
538
550
|
) {
|
|
539
|
-
const
|
|
540
|
-
if (typeof
|
|
541
|
-
|
|
551
|
+
const that = this;
|
|
552
|
+
if (typeof $el === "string") {
|
|
553
|
+
$el = that.selectorAll($el);
|
|
542
554
|
}
|
|
543
|
-
if (
|
|
555
|
+
if ($el == null) {
|
|
544
556
|
return;
|
|
545
557
|
}
|
|
546
|
-
if (
|
|
558
|
+
if (CommonUtils.isNodeList($el)) {
|
|
547
559
|
if (value == null) {
|
|
548
560
|
// 获取
|
|
549
|
-
return
|
|
561
|
+
return that.val($el[0] as HTMLInputElement);
|
|
550
562
|
} else {
|
|
551
563
|
// 设置
|
|
552
|
-
|
|
553
|
-
|
|
564
|
+
$el.forEach(($elItem) => {
|
|
565
|
+
that.val($elItem as HTMLInputElement, value);
|
|
554
566
|
});
|
|
555
567
|
}
|
|
556
568
|
return;
|
|
557
569
|
}
|
|
558
570
|
if (value == null) {
|
|
559
571
|
// 获取
|
|
560
|
-
if (
|
|
561
|
-
return (
|
|
572
|
+
if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
|
|
573
|
+
return ($el as HTMLInputElement).checked;
|
|
562
574
|
} else {
|
|
563
|
-
return
|
|
575
|
+
return $el.value;
|
|
564
576
|
}
|
|
565
577
|
} else {
|
|
566
578
|
// 设置
|
|
567
|
-
if (
|
|
568
|
-
(
|
|
579
|
+
if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
|
|
580
|
+
($el as HTMLInputElement).checked = !!value;
|
|
569
581
|
} else {
|
|
570
|
-
|
|
582
|
+
$el.value = value as string;
|
|
571
583
|
}
|
|
572
584
|
}
|
|
573
585
|
}
|
|
574
|
-
|
|
575
586
|
/**
|
|
576
587
|
* 获取元素的属性值
|
|
577
|
-
* @param
|
|
588
|
+
* @param $el 目标元素
|
|
578
589
|
* @param propName 属性名
|
|
579
590
|
* @param propValue 属性值
|
|
580
591
|
* @example
|
|
@@ -583,10 +594,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
583
594
|
* DOMUtils.val("a.xx","data-value")
|
|
584
595
|
* > undefined
|
|
585
596
|
* */
|
|
586
|
-
prop<T>(
|
|
597
|
+
prop<T>($el: DOMUtilsTargetElementType | DocumentFragment, propName: string): T;
|
|
587
598
|
/**
|
|
588
599
|
* 设置元素的属性值
|
|
589
|
-
* @param
|
|
600
|
+
* @param $el 目标元素
|
|
590
601
|
* @param propName 属性名
|
|
591
602
|
* @param propValue 属性值
|
|
592
603
|
* @example
|
|
@@ -594,181 +605,146 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
594
605
|
* DOMUtils.val(document.querySelector("a.xx"),"data-value",1)
|
|
595
606
|
* DOMUtils.val("a.xx","data-value",1)
|
|
596
607
|
* */
|
|
597
|
-
prop<T>(
|
|
598
|
-
prop(
|
|
599
|
-
const
|
|
600
|
-
if (typeof
|
|
601
|
-
|
|
608
|
+
prop<T>($el: DOMUtilsTargetElementType | DocumentFragment, propName: string, propValue: T): void;
|
|
609
|
+
prop($el: DOMUtilsTargetElementType | DocumentFragment, propName: string, propValue?: any) {
|
|
610
|
+
const that = this;
|
|
611
|
+
if (typeof $el === "string") {
|
|
612
|
+
$el = that.selectorAll($el);
|
|
602
613
|
}
|
|
603
|
-
if (
|
|
614
|
+
if ($el == null) {
|
|
604
615
|
return;
|
|
605
616
|
}
|
|
606
|
-
if (
|
|
617
|
+
if (CommonUtils.isNodeList($el)) {
|
|
607
618
|
if (propValue == null) {
|
|
608
619
|
// 获取
|
|
609
|
-
return
|
|
620
|
+
return that.prop($el[0] as HTMLElement, propName);
|
|
610
621
|
} else {
|
|
611
622
|
// 设置
|
|
612
|
-
|
|
613
|
-
|
|
623
|
+
$el.forEach(($elItem) => {
|
|
624
|
+
that.prop($elItem as HTMLElement, propName, propValue);
|
|
614
625
|
});
|
|
615
626
|
}
|
|
616
627
|
return;
|
|
617
628
|
}
|
|
618
629
|
if (propValue == null) {
|
|
619
|
-
return Reflect.get(
|
|
630
|
+
return Reflect.get($el, propName);
|
|
620
631
|
} else {
|
|
621
|
-
if (
|
|
622
|
-
|
|
632
|
+
if ($el instanceof Element && propName === "innerHTML") {
|
|
633
|
+
that.html($el, propValue);
|
|
623
634
|
} else {
|
|
624
|
-
Reflect.set(
|
|
635
|
+
Reflect.set($el, propName, propValue);
|
|
625
636
|
}
|
|
626
637
|
}
|
|
627
638
|
}
|
|
628
639
|
/**
|
|
629
640
|
* 移除元素的属性
|
|
630
|
-
* @param
|
|
641
|
+
* @param $el 目标元素
|
|
631
642
|
* @param attrName 属性名
|
|
632
643
|
* @example
|
|
633
644
|
* // 移除元素a.xx的属性data-value
|
|
634
645
|
* DOMUtils.removeAttr(document.querySelector("a.xx"),"data-value")
|
|
635
646
|
* DOMUtils.removeAttr("a.xx","data-value")
|
|
636
647
|
* */
|
|
637
|
-
removeAttr(
|
|
638
|
-
const
|
|
639
|
-
if (typeof
|
|
640
|
-
|
|
648
|
+
removeAttr($el: DOMUtilsTargetElementType | Element, attrName: string) {
|
|
649
|
+
const that = this;
|
|
650
|
+
if (typeof $el === "string") {
|
|
651
|
+
$el = that.selectorAll($el);
|
|
641
652
|
}
|
|
642
|
-
if (
|
|
653
|
+
if ($el == null) {
|
|
643
654
|
return;
|
|
644
655
|
}
|
|
645
|
-
if (
|
|
656
|
+
if (CommonUtils.isNodeList($el)) {
|
|
646
657
|
// 设置
|
|
647
|
-
|
|
648
|
-
|
|
658
|
+
$el.forEach(($elItem) => {
|
|
659
|
+
that.removeAttr($elItem as HTMLElement, attrName);
|
|
649
660
|
});
|
|
650
661
|
return;
|
|
651
662
|
}
|
|
652
|
-
|
|
663
|
+
$el.removeAttribute(attrName);
|
|
653
664
|
}
|
|
654
665
|
/**
|
|
655
666
|
* 移除元素class名
|
|
656
|
-
* @param
|
|
667
|
+
* @param $el 目标元素
|
|
657
668
|
* @param className 类名
|
|
658
669
|
* @example
|
|
659
670
|
* // 移除元素a.xx的className为xx
|
|
660
671
|
* DOMUtils.removeClass(document.querySelector("a.xx"),"xx")
|
|
661
672
|
* DOMUtils.removeClass("a.xx","xx")
|
|
662
673
|
*/
|
|
663
|
-
removeClass(
|
|
664
|
-
const
|
|
665
|
-
if (typeof
|
|
666
|
-
|
|
674
|
+
removeClass($el: DOMUtilsTargetElementType | Element, className?: string | string[] | undefined | null) {
|
|
675
|
+
const that = this;
|
|
676
|
+
if (typeof $el === "string") {
|
|
677
|
+
$el = that.selectorAll($el);
|
|
667
678
|
}
|
|
668
|
-
if (
|
|
679
|
+
if ($el == null) {
|
|
669
680
|
return;
|
|
670
681
|
}
|
|
671
|
-
if (
|
|
682
|
+
if (CommonUtils.isNodeList($el)) {
|
|
672
683
|
// 设置
|
|
673
|
-
|
|
674
|
-
|
|
684
|
+
$el.forEach(($elItem) => {
|
|
685
|
+
that.removeClass($elItem as HTMLElement, className);
|
|
675
686
|
});
|
|
676
687
|
return;
|
|
677
688
|
}
|
|
678
689
|
if (className == null) {
|
|
679
690
|
// 清空全部className
|
|
680
|
-
|
|
691
|
+
$el.className = "";
|
|
681
692
|
} else {
|
|
682
693
|
if (!Array.isArray(className)) {
|
|
683
694
|
className = className.trim().split(" ");
|
|
684
695
|
}
|
|
685
696
|
className.forEach((itemClassName) => {
|
|
686
|
-
|
|
697
|
+
$el.classList.remove(itemClassName);
|
|
687
698
|
});
|
|
688
699
|
}
|
|
689
700
|
}
|
|
690
701
|
/**
|
|
691
702
|
* 移除元素的属性
|
|
692
|
-
* @param
|
|
703
|
+
* @param $el 目标元素
|
|
693
704
|
* @param propName 属性名
|
|
694
705
|
* @example
|
|
695
706
|
* // 移除元素a.xx的href属性
|
|
696
707
|
* DOMUtils.removeProp(document.querySelector("a.xx"),"href")
|
|
697
708
|
* DOMUtils.removeProp("a.xx","href")
|
|
698
709
|
* */
|
|
699
|
-
removeProp(
|
|
700
|
-
const
|
|
701
|
-
if (typeof
|
|
702
|
-
|
|
703
|
-
}
|
|
704
|
-
if (element == null) {
|
|
705
|
-
return;
|
|
706
|
-
}
|
|
707
|
-
if (DOMUtilsCommonUtils.isNodeList(element)) {
|
|
708
|
-
// 设置
|
|
709
|
-
element.forEach(($ele) => {
|
|
710
|
-
DOMUtilsContext.removeProp($ele as HTMLElement, propName);
|
|
711
|
-
});
|
|
712
|
-
return;
|
|
713
|
-
}
|
|
714
|
-
DOMUtilsCommonUtils.delete(element, propName);
|
|
715
|
-
}
|
|
716
|
-
/**
|
|
717
|
-
* 将一个元素替换为另一个元素
|
|
718
|
-
* @param element 目标元素
|
|
719
|
-
* @param newElement 新元素
|
|
720
|
-
* @example
|
|
721
|
-
* // 替换元素a.xx为b.xx
|
|
722
|
-
* DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
723
|
-
* DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
|
|
724
|
-
*/
|
|
725
|
-
replaceWith(element: DOMUtilsTargetElementType, newElement: HTMLElement | string | Node) {
|
|
726
|
-
const DOMUtilsContext = this;
|
|
727
|
-
if (typeof element === "string") {
|
|
728
|
-
element = DOMUtilsContext.selectorAll(element);
|
|
710
|
+
removeProp($el: DOMUtilsTargetElementType | DocumentFragment, propName: string) {
|
|
711
|
+
const that = this;
|
|
712
|
+
if (typeof $el === "string") {
|
|
713
|
+
$el = that.selectorAll($el);
|
|
729
714
|
}
|
|
730
|
-
if (
|
|
715
|
+
if ($el == null) {
|
|
731
716
|
return;
|
|
732
717
|
}
|
|
733
|
-
if (
|
|
718
|
+
if (CommonUtils.isNodeList($el)) {
|
|
734
719
|
// 设置
|
|
735
|
-
|
|
736
|
-
|
|
720
|
+
$el.forEach(($elItem) => {
|
|
721
|
+
that.removeProp($elItem as HTMLElement, propName);
|
|
737
722
|
});
|
|
738
723
|
return;
|
|
739
724
|
}
|
|
740
|
-
|
|
741
|
-
newElement = DOMUtilsContext.parseHTML(newElement, false, false);
|
|
742
|
-
}
|
|
743
|
-
const $parent = element.parentElement;
|
|
744
|
-
if ($parent) {
|
|
745
|
-
$parent.replaceChild(newElement as Node, element);
|
|
746
|
-
} else {
|
|
747
|
-
DOMUtilsContext.after(element, newElement as HTMLElement);
|
|
748
|
-
element.remove();
|
|
749
|
-
}
|
|
725
|
+
CommonUtils.delete($el, propName);
|
|
750
726
|
}
|
|
751
727
|
/**
|
|
752
728
|
* 给元素添加class
|
|
753
|
-
* @param
|
|
729
|
+
* @param $el 目标元素
|
|
754
730
|
* @param className class名
|
|
755
731
|
* @example
|
|
756
732
|
* // 元素a.xx的className添加_vue_
|
|
757
733
|
* DOMUtils.addClass(document.querySelector("a.xx"),"_vue_")
|
|
758
734
|
* DOMUtils.addClass("a.xx","_vue_")
|
|
759
735
|
* */
|
|
760
|
-
addClass(
|
|
761
|
-
const
|
|
762
|
-
if (typeof
|
|
763
|
-
|
|
736
|
+
addClass($el: DOMUtilsTargetElementType | Element, className: string | string[]) {
|
|
737
|
+
const that = this;
|
|
738
|
+
if (typeof $el === "string") {
|
|
739
|
+
$el = that.selectorAll($el);
|
|
764
740
|
}
|
|
765
|
-
if (
|
|
741
|
+
if ($el == null) {
|
|
766
742
|
return;
|
|
767
743
|
}
|
|
768
|
-
if (
|
|
744
|
+
if (CommonUtils.isNodeList($el)) {
|
|
769
745
|
// 设置
|
|
770
|
-
|
|
771
|
-
|
|
746
|
+
$el.forEach(($elItem) => {
|
|
747
|
+
that.addClass($elItem as HTMLElement, className);
|
|
772
748
|
});
|
|
773
749
|
return;
|
|
774
750
|
}
|
|
@@ -779,31 +755,31 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
779
755
|
if (itemClassName.trim() == "") {
|
|
780
756
|
return;
|
|
781
757
|
}
|
|
782
|
-
|
|
758
|
+
$el.classList.add(itemClassName);
|
|
783
759
|
});
|
|
784
760
|
}
|
|
785
761
|
/**
|
|
786
762
|
* 判断元素是否存在className
|
|
787
|
-
* @param
|
|
763
|
+
* @param $el
|
|
788
764
|
* @param className
|
|
789
765
|
*/
|
|
790
|
-
hasClass(
|
|
791
|
-
const
|
|
792
|
-
if (typeof
|
|
793
|
-
|
|
766
|
+
hasClass($el: DOMUtilsTargetElementType | Element, className: string | string[]): boolean {
|
|
767
|
+
const that = this;
|
|
768
|
+
if (typeof $el === "string") {
|
|
769
|
+
$el = that.selectorAll($el);
|
|
794
770
|
}
|
|
795
|
-
if (
|
|
771
|
+
if ($el == null) {
|
|
796
772
|
return false;
|
|
797
773
|
}
|
|
798
|
-
if (
|
|
774
|
+
if (CommonUtils.isNodeList($el)) {
|
|
799
775
|
let flag = true;
|
|
800
|
-
for (let index = 0; index <
|
|
801
|
-
const $
|
|
802
|
-
flag = flag &&
|
|
776
|
+
for (let index = 0; index < $el.length; index++) {
|
|
777
|
+
const $elItem = $el[index] as HTMLElement;
|
|
778
|
+
flag = flag && that.hasClass($elItem, className);
|
|
803
779
|
}
|
|
804
780
|
return flag;
|
|
805
781
|
}
|
|
806
|
-
if (
|
|
782
|
+
if (!$el?.classList) {
|
|
807
783
|
return false;
|
|
808
784
|
}
|
|
809
785
|
if (!Array.isArray(className)) {
|
|
@@ -811,7 +787,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
811
787
|
}
|
|
812
788
|
for (let index = 0; index < className.length; index++) {
|
|
813
789
|
const item = className[index].trim();
|
|
814
|
-
if (
|
|
790
|
+
if (!$el.classList.contains(item)) {
|
|
815
791
|
return false;
|
|
816
792
|
}
|
|
817
793
|
}
|
|
@@ -819,7 +795,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
819
795
|
}
|
|
820
796
|
/**
|
|
821
797
|
* 函数在元素内部末尾添加子元素或HTML字符串
|
|
822
|
-
* @param
|
|
798
|
+
* @param $el 目标元素
|
|
823
799
|
* @param content 子元素或HTML字符串
|
|
824
800
|
* @example
|
|
825
801
|
* // 元素a.xx的内部末尾添加一个元素
|
|
@@ -827,21 +803,21 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
827
803
|
* DOMUtils.append("a.xx","'<b class="xx"></b>")
|
|
828
804
|
* */
|
|
829
805
|
append(
|
|
830
|
-
|
|
806
|
+
$el: DOMUtilsTargetElementType | DocumentFragment,
|
|
831
807
|
content: HTMLElement | string | (HTMLElement | string | Element)[] | NodeList
|
|
832
808
|
) {
|
|
833
|
-
const
|
|
834
|
-
if (typeof
|
|
835
|
-
|
|
809
|
+
const that = this;
|
|
810
|
+
if (typeof $el === "string") {
|
|
811
|
+
$el = that.selectorAll($el);
|
|
836
812
|
}
|
|
837
|
-
if (
|
|
813
|
+
if ($el == null) {
|
|
838
814
|
return;
|
|
839
815
|
}
|
|
840
816
|
|
|
841
|
-
if (
|
|
817
|
+
if (CommonUtils.isNodeList($el)) {
|
|
842
818
|
// 设置
|
|
843
|
-
|
|
844
|
-
|
|
819
|
+
$el.forEach(($elItem) => {
|
|
820
|
+
that.append($elItem as HTMLElement, content);
|
|
845
821
|
});
|
|
846
822
|
return;
|
|
847
823
|
}
|
|
@@ -849,11 +825,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
849
825
|
if (typeof content === "string") {
|
|
850
826
|
if (ele instanceof DocumentFragment) {
|
|
851
827
|
if (typeof text === "string") {
|
|
852
|
-
text =
|
|
828
|
+
text = that.toElement(text, true, false);
|
|
853
829
|
}
|
|
854
830
|
ele.appendChild(text);
|
|
855
831
|
} else {
|
|
856
|
-
ele.insertAdjacentHTML("beforeend",
|
|
832
|
+
ele.insertAdjacentHTML("beforeend", CommonUtils.createSafeHTML(text as string));
|
|
857
833
|
}
|
|
858
834
|
} else {
|
|
859
835
|
ele.appendChild(text as HTMLElement);
|
|
@@ -861,91 +837,91 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
861
837
|
}
|
|
862
838
|
if (Array.isArray(content) || content instanceof NodeList) {
|
|
863
839
|
/* 数组 */
|
|
864
|
-
const fragment =
|
|
840
|
+
const fragment = that.windowApi.document.createDocumentFragment();
|
|
865
841
|
content.forEach((ele) => {
|
|
866
842
|
if (typeof ele === "string") {
|
|
867
843
|
// 转为元素
|
|
868
|
-
ele =
|
|
844
|
+
ele = that.toElement(ele, true, false);
|
|
869
845
|
}
|
|
870
846
|
fragment.appendChild(ele);
|
|
871
847
|
});
|
|
872
|
-
|
|
848
|
+
$el.appendChild(fragment);
|
|
873
849
|
} else {
|
|
874
|
-
elementAppendChild(
|
|
850
|
+
elementAppendChild($el, content);
|
|
875
851
|
}
|
|
876
852
|
}
|
|
877
853
|
/**
|
|
878
854
|
* 函数 在元素内部开头添加子元素或HTML字符串
|
|
879
|
-
* @param
|
|
855
|
+
* @param $el 目标元素
|
|
880
856
|
* @param content 子元素或HTML字符串
|
|
881
857
|
* @example
|
|
882
858
|
* // 元素a.xx内部开头添加一个元素
|
|
883
859
|
* DOMUtils.prepend(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
884
860
|
* DOMUtils.prepend("a.xx","'<b class="xx"></b>")
|
|
885
861
|
* */
|
|
886
|
-
prepend(
|
|
887
|
-
const
|
|
888
|
-
if (typeof
|
|
889
|
-
|
|
862
|
+
prepend($el: DOMUtilsTargetElementType | DocumentFragment, content: HTMLElement | string) {
|
|
863
|
+
const that = this;
|
|
864
|
+
if (typeof $el === "string") {
|
|
865
|
+
$el = that.selectorAll($el);
|
|
890
866
|
}
|
|
891
|
-
if (
|
|
867
|
+
if ($el == null) {
|
|
892
868
|
return;
|
|
893
869
|
}
|
|
894
|
-
if (
|
|
870
|
+
if (CommonUtils.isNodeList($el)) {
|
|
895
871
|
// 设置
|
|
896
|
-
|
|
897
|
-
|
|
872
|
+
$el.forEach(($elItem) => {
|
|
873
|
+
that.prepend($elItem as HTMLElement, content);
|
|
898
874
|
});
|
|
899
875
|
return;
|
|
900
876
|
}
|
|
901
877
|
if (typeof content === "string") {
|
|
902
|
-
if (
|
|
903
|
-
content =
|
|
904
|
-
|
|
878
|
+
if ($el instanceof DocumentFragment) {
|
|
879
|
+
content = that.toElement(content, true, false);
|
|
880
|
+
$el.prepend(content);
|
|
905
881
|
} else {
|
|
906
|
-
|
|
882
|
+
$el.insertAdjacentHTML("afterbegin", CommonUtils.createSafeHTML(content));
|
|
907
883
|
}
|
|
908
884
|
} else {
|
|
909
|
-
const $firstChild =
|
|
885
|
+
const $firstChild = $el.firstChild;
|
|
910
886
|
if ($firstChild == null) {
|
|
911
|
-
|
|
887
|
+
$el.prepend(content);
|
|
912
888
|
} else {
|
|
913
|
-
|
|
889
|
+
$el.insertBefore(content, $firstChild);
|
|
914
890
|
}
|
|
915
891
|
}
|
|
916
892
|
}
|
|
917
893
|
/**
|
|
918
894
|
* 在元素后面添加兄弟元素或HTML字符串
|
|
919
|
-
* @param
|
|
895
|
+
* @param $el 目标元素
|
|
920
896
|
* @param content 兄弟元素或HTML字符串
|
|
921
897
|
* @example
|
|
922
898
|
* // 元素a.xx后面添加一个元素
|
|
923
899
|
* DOMUtils.after(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
924
900
|
* DOMUtils.after("a.xx","'<b class="xx"></b>")
|
|
925
901
|
* */
|
|
926
|
-
after(
|
|
927
|
-
const
|
|
928
|
-
if (typeof
|
|
929
|
-
|
|
902
|
+
after($el: DOMUtilsTargetElementType, content: HTMLElement | string) {
|
|
903
|
+
const that = this;
|
|
904
|
+
if (typeof $el === "string") {
|
|
905
|
+
$el = that.selectorAll($el);
|
|
930
906
|
}
|
|
931
|
-
if (
|
|
907
|
+
if ($el == null) {
|
|
932
908
|
return;
|
|
933
909
|
}
|
|
934
|
-
if (
|
|
910
|
+
if (CommonUtils.isNodeList($el)) {
|
|
935
911
|
// 设置
|
|
936
|
-
|
|
937
|
-
|
|
912
|
+
$el.forEach(($elItem) => {
|
|
913
|
+
that.after($elItem as HTMLElement, content);
|
|
938
914
|
});
|
|
939
915
|
return;
|
|
940
916
|
}
|
|
941
917
|
if (typeof content === "string") {
|
|
942
|
-
|
|
918
|
+
$el.insertAdjacentHTML("afterend", CommonUtils.createSafeHTML(content));
|
|
943
919
|
} else {
|
|
944
|
-
const $parent =
|
|
945
|
-
const $nextSlibling =
|
|
920
|
+
const $parent = $el.parentElement;
|
|
921
|
+
const $nextSlibling = $el.nextSibling;
|
|
946
922
|
if (!$parent || $nextSlibling) {
|
|
947
923
|
// 任意一个不行
|
|
948
|
-
|
|
924
|
+
$el.after(content);
|
|
949
925
|
} else {
|
|
950
926
|
$parent.insertBefore(content, $nextSlibling);
|
|
951
927
|
}
|
|
@@ -953,128 +929,128 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
953
929
|
}
|
|
954
930
|
/**
|
|
955
931
|
* 在元素前面添加兄弟元素或HTML字符串
|
|
956
|
-
* @param
|
|
932
|
+
* @param $el 目标元素
|
|
957
933
|
* @param content 兄弟元素或HTML字符串
|
|
958
934
|
* @example
|
|
959
935
|
* // 元素a.xx前面添加一个元素
|
|
960
936
|
* DOMUtils.before(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
961
937
|
* DOMUtils.before("a.xx","'<b class="xx"></b>")
|
|
962
938
|
* */
|
|
963
|
-
before(
|
|
964
|
-
const
|
|
965
|
-
if (typeof
|
|
966
|
-
|
|
939
|
+
before($el: DOMUtilsTargetElementType, content: HTMLElement | string) {
|
|
940
|
+
const that = this;
|
|
941
|
+
if (typeof $el === "string") {
|
|
942
|
+
$el = that.selectorAll($el);
|
|
967
943
|
}
|
|
968
|
-
if (
|
|
944
|
+
if ($el == null) {
|
|
969
945
|
return;
|
|
970
946
|
}
|
|
971
|
-
if (
|
|
947
|
+
if (CommonUtils.isNodeList($el)) {
|
|
972
948
|
// 设置
|
|
973
|
-
|
|
974
|
-
|
|
949
|
+
$el.forEach(($elItem) => {
|
|
950
|
+
that.before($elItem as HTMLElement, content);
|
|
975
951
|
});
|
|
976
952
|
return;
|
|
977
953
|
}
|
|
978
954
|
if (typeof content === "string") {
|
|
979
|
-
|
|
955
|
+
$el.insertAdjacentHTML("beforebegin", CommonUtils.createSafeHTML(content));
|
|
980
956
|
} else {
|
|
981
|
-
const $parent =
|
|
957
|
+
const $parent = $el.parentElement;
|
|
982
958
|
if (!$parent) {
|
|
983
|
-
|
|
959
|
+
$el.before(content);
|
|
984
960
|
} else {
|
|
985
|
-
$parent.insertBefore(content,
|
|
961
|
+
$parent.insertBefore(content, $el);
|
|
986
962
|
}
|
|
987
963
|
}
|
|
988
964
|
}
|
|
989
965
|
/**
|
|
990
966
|
* 移除元素
|
|
991
|
-
* @param
|
|
967
|
+
* @param $el 目标元素
|
|
992
968
|
* @example
|
|
993
969
|
* // 元素a.xx前面添加一个元素
|
|
994
970
|
* DOMUtils.remove(document.querySelector("a.xx"))
|
|
995
971
|
* DOMUtils.remove(document.querySelectorAll("a.xx"))
|
|
996
972
|
* DOMUtils.remove("a.xx")
|
|
997
973
|
* */
|
|
998
|
-
remove(
|
|
999
|
-
const
|
|
1000
|
-
if (typeof
|
|
1001
|
-
|
|
974
|
+
remove($el: DOMUtilsTargetElementType | Element) {
|
|
975
|
+
const that = this;
|
|
976
|
+
if (typeof $el === "string") {
|
|
977
|
+
$el = that.selectorAll($el);
|
|
1002
978
|
}
|
|
1003
|
-
if (
|
|
979
|
+
if ($el == null) {
|
|
1004
980
|
return;
|
|
1005
981
|
}
|
|
1006
|
-
if (
|
|
1007
|
-
|
|
1008
|
-
|
|
982
|
+
if (CommonUtils.isNodeList($el)) {
|
|
983
|
+
$el.forEach(($elItem) => {
|
|
984
|
+
that.remove($elItem as HTMLElement);
|
|
1009
985
|
});
|
|
1010
986
|
return;
|
|
1011
987
|
}
|
|
1012
|
-
if (typeof
|
|
1013
|
-
|
|
1014
|
-
} else if (
|
|
1015
|
-
|
|
1016
|
-
} else if (
|
|
1017
|
-
|
|
988
|
+
if (typeof $el.remove === "function") {
|
|
989
|
+
$el.remove();
|
|
990
|
+
} else if ($el.parentElement) {
|
|
991
|
+
$el.parentElement.removeChild($el);
|
|
992
|
+
} else if ($el.parentNode) {
|
|
993
|
+
$el.parentNode.removeChild($el);
|
|
1018
994
|
}
|
|
1019
995
|
}
|
|
1020
996
|
/**
|
|
1021
997
|
* 移除元素的所有子元素
|
|
1022
|
-
* @param
|
|
998
|
+
* @param $el 目标元素
|
|
1023
999
|
* @example
|
|
1024
1000
|
* // 移除元素a.xx元素的所有子元素
|
|
1025
1001
|
* DOMUtils.empty(document.querySelector("a.xx"))
|
|
1026
1002
|
* DOMUtils.empty("a.xx")
|
|
1027
1003
|
* */
|
|
1028
|
-
empty(
|
|
1029
|
-
const
|
|
1030
|
-
if (typeof
|
|
1031
|
-
|
|
1004
|
+
empty($el: DOMUtilsTargetElementType | Element) {
|
|
1005
|
+
const that = this;
|
|
1006
|
+
if (typeof $el === "string") {
|
|
1007
|
+
$el = that.selectorAll($el);
|
|
1032
1008
|
}
|
|
1033
|
-
if (
|
|
1009
|
+
if ($el == null) {
|
|
1034
1010
|
return;
|
|
1035
1011
|
}
|
|
1036
|
-
if (
|
|
1012
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1037
1013
|
// 设置
|
|
1038
|
-
|
|
1039
|
-
|
|
1014
|
+
$el.forEach(($elItem) => {
|
|
1015
|
+
that.empty($elItem as HTMLElement);
|
|
1040
1016
|
});
|
|
1041
1017
|
return;
|
|
1042
1018
|
}
|
|
1043
|
-
if (
|
|
1044
|
-
|
|
1045
|
-
} else if (
|
|
1046
|
-
|
|
1019
|
+
if ($el.innerHTML) {
|
|
1020
|
+
$el.innerHTML = "";
|
|
1021
|
+
} else if ($el.textContent) {
|
|
1022
|
+
$el.textContent = "";
|
|
1047
1023
|
}
|
|
1048
1024
|
}
|
|
1049
1025
|
/**
|
|
1050
1026
|
* 获取元素相对于文档的偏移坐标(加上文档的滚动条)
|
|
1051
|
-
* @param
|
|
1027
|
+
* @param $el 目标元素
|
|
1052
1028
|
* @example
|
|
1053
1029
|
* // 获取元素a.xx的对于文档的偏移坐标
|
|
1054
1030
|
* DOMUtils.offset(document.querySelector("a.xx"))
|
|
1055
1031
|
* DOMUtils.offset("a.xx")
|
|
1056
1032
|
* > 0
|
|
1057
1033
|
*/
|
|
1058
|
-
offset(
|
|
1059
|
-
const
|
|
1060
|
-
if (typeof
|
|
1061
|
-
|
|
1034
|
+
offset($el: HTMLElement | string) {
|
|
1035
|
+
const that = this;
|
|
1036
|
+
if (typeof $el === "string") {
|
|
1037
|
+
$el = that.selector($el) as HTMLElement;
|
|
1062
1038
|
}
|
|
1063
|
-
if (
|
|
1039
|
+
if ($el == null) {
|
|
1064
1040
|
return;
|
|
1065
1041
|
}
|
|
1066
1042
|
|
|
1067
|
-
const rect =
|
|
1043
|
+
const rect = $el.getBoundingClientRect();
|
|
1068
1044
|
return {
|
|
1069
1045
|
/** y轴偏移 */
|
|
1070
|
-
top: rect.top +
|
|
1046
|
+
top: rect.top + that.windowApi.globalThis.scrollY,
|
|
1071
1047
|
/** x轴偏移 */
|
|
1072
|
-
left: rect.left +
|
|
1048
|
+
left: rect.left + that.windowApi.globalThis.scrollX,
|
|
1073
1049
|
};
|
|
1074
1050
|
}
|
|
1075
1051
|
/**
|
|
1076
1052
|
* 获取元素的宽度
|
|
1077
|
-
* @param
|
|
1053
|
+
* @param $el 要获取宽度的元素
|
|
1078
1054
|
* @param value 宽度值
|
|
1079
1055
|
* @param isShow 是否已进行isShow,避免爆堆栈
|
|
1080
1056
|
* @returns 元素的宽度,单位为像素
|
|
@@ -1091,43 +1067,43 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1091
1067
|
* DOMUtils.width(document.querySelector("a.xx"),200)
|
|
1092
1068
|
* DOMUtils.width("a.xx",200)
|
|
1093
1069
|
*/
|
|
1094
|
-
width(
|
|
1095
|
-
width(
|
|
1096
|
-
const
|
|
1097
|
-
if (typeof
|
|
1098
|
-
|
|
1070
|
+
width($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
|
|
1071
|
+
width($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
|
|
1072
|
+
const that = this;
|
|
1073
|
+
if (typeof $el === "string") {
|
|
1074
|
+
$el = that.selector<HTMLElement>($el)!;
|
|
1099
1075
|
}
|
|
1100
|
-
if (
|
|
1101
|
-
return
|
|
1076
|
+
if (CommonUtils.isWin($el)) {
|
|
1077
|
+
return that.windowApi.window.document.documentElement.clientWidth;
|
|
1102
1078
|
}
|
|
1103
|
-
if ((
|
|
1079
|
+
if (($el as HTMLElement).nodeType === 9) {
|
|
1104
1080
|
/* Document文档节点 */
|
|
1105
|
-
|
|
1081
|
+
$el = $el as Document;
|
|
1106
1082
|
return Math.max(
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1083
|
+
$el.body.scrollWidth,
|
|
1084
|
+
$el.documentElement.scrollWidth,
|
|
1085
|
+
$el.body.offsetWidth,
|
|
1086
|
+
$el.documentElement.offsetWidth,
|
|
1087
|
+
$el.documentElement.clientWidth
|
|
1112
1088
|
);
|
|
1113
1089
|
}
|
|
1114
|
-
if (isShow || (!isShow &&
|
|
1090
|
+
if (isShow || (!isShow && CommonUtils.isShow($el as HTMLElement))) {
|
|
1115
1091
|
/* 已显示 */
|
|
1116
1092
|
/* 不从style中获取对应的宽度,因为可能使用了class定义了width !important */
|
|
1117
|
-
|
|
1093
|
+
$el = $el as HTMLElement;
|
|
1118
1094
|
/* 如果element.style.width为空 则从css里面获取是否定义了width信息如果定义了 则读取css里面定义的宽度width */
|
|
1119
|
-
if (parseFloat(
|
|
1120
|
-
return parseFloat(
|
|
1095
|
+
if (parseFloat(CommonUtils.getStyleValue($el, "width").toString()) > 0) {
|
|
1096
|
+
return parseFloat(CommonUtils.getStyleValue($el, "width").toString());
|
|
1121
1097
|
}
|
|
1122
1098
|
|
|
1123
1099
|
/* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetWidth来进行计算 */
|
|
1124
|
-
if (
|
|
1125
|
-
const borderLeftWidth =
|
|
1126
|
-
const borderRightWidth =
|
|
1127
|
-
const paddingLeft =
|
|
1128
|
-
const paddingRight =
|
|
1100
|
+
if ($el.offsetWidth > 0) {
|
|
1101
|
+
const borderLeftWidth = CommonUtils.getStyleValue($el, "borderLeftWidth");
|
|
1102
|
+
const borderRightWidth = CommonUtils.getStyleValue($el, "borderRightWidth");
|
|
1103
|
+
const paddingLeft = CommonUtils.getStyleValue($el, "paddingLeft");
|
|
1104
|
+
const paddingRight = CommonUtils.getStyleValue($el, "paddingRight");
|
|
1129
1105
|
const backHeight =
|
|
1130
|
-
parseFloat(
|
|
1106
|
+
parseFloat($el.offsetWidth.toString()) -
|
|
1131
1107
|
parseFloat(borderLeftWidth.toString()) -
|
|
1132
1108
|
parseFloat(borderRightWidth.toString()) -
|
|
1133
1109
|
parseFloat(paddingLeft.toString()) -
|
|
@@ -1137,9 +1113,9 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1137
1113
|
return 0;
|
|
1138
1114
|
} else {
|
|
1139
1115
|
/* 未显示 */
|
|
1140
|
-
|
|
1141
|
-
const { recovery } =
|
|
1142
|
-
const width =
|
|
1116
|
+
$el = $el as HTMLElement;
|
|
1117
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
1118
|
+
const width = that.width($el, true);
|
|
1143
1119
|
recovery();
|
|
1144
1120
|
return width;
|
|
1145
1121
|
}
|
|
@@ -1147,7 +1123,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1147
1123
|
|
|
1148
1124
|
/**
|
|
1149
1125
|
* 获取元素的高度
|
|
1150
|
-
* @param
|
|
1126
|
+
* @param $el 要获取高度的元素
|
|
1151
1127
|
* @param isShow 是否已进行isShow,避免爆堆栈
|
|
1152
1128
|
* @returns 元素的高度,单位为像素
|
|
1153
1129
|
* @example
|
|
@@ -1163,43 +1139,43 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1163
1139
|
* DOMUtils.height(document.querySelector("a.xx"),200)
|
|
1164
1140
|
* DOMUtils.height("a.xx",200)
|
|
1165
1141
|
*/
|
|
1166
|
-
height(
|
|
1167
|
-
height(
|
|
1168
|
-
const
|
|
1169
|
-
if (
|
|
1170
|
-
return
|
|
1142
|
+
height($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
|
|
1143
|
+
height($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
|
|
1144
|
+
const that = this;
|
|
1145
|
+
if (CommonUtils.isWin($el)) {
|
|
1146
|
+
return that.windowApi.window.document.documentElement.clientHeight;
|
|
1171
1147
|
}
|
|
1172
|
-
if (typeof
|
|
1173
|
-
|
|
1148
|
+
if (typeof $el === "string") {
|
|
1149
|
+
$el = that.selector($el) as HTMLElement;
|
|
1174
1150
|
}
|
|
1175
|
-
if ((
|
|
1176
|
-
|
|
1151
|
+
if (($el as Document).nodeType === 9) {
|
|
1152
|
+
$el = $el as Document;
|
|
1177
1153
|
/* Document文档节点 */
|
|
1178
1154
|
return Math.max(
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1155
|
+
$el.body.scrollHeight,
|
|
1156
|
+
$el.documentElement.scrollHeight,
|
|
1157
|
+
$el.body.offsetHeight,
|
|
1158
|
+
$el.documentElement.offsetHeight,
|
|
1159
|
+
$el.documentElement.clientHeight
|
|
1184
1160
|
);
|
|
1185
1161
|
}
|
|
1186
|
-
if (isShow || (!isShow &&
|
|
1187
|
-
|
|
1162
|
+
if (isShow || (!isShow && CommonUtils.isShow($el as HTMLElement))) {
|
|
1163
|
+
$el = $el as HTMLElement;
|
|
1188
1164
|
/* 已显示 */
|
|
1189
1165
|
/* 从style中获取对应的高度,因为可能使用了class定义了width !important */
|
|
1190
1166
|
/* 如果element.style.height为空 则从css里面获取是否定义了height信息如果定义了 则读取css里面定义的高度height */
|
|
1191
|
-
if (parseFloat(
|
|
1192
|
-
return parseFloat(
|
|
1167
|
+
if (parseFloat(CommonUtils.getStyleValue($el, "height").toString()) > 0) {
|
|
1168
|
+
return parseFloat(CommonUtils.getStyleValue($el, "height").toString());
|
|
1193
1169
|
}
|
|
1194
1170
|
|
|
1195
1171
|
/* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetHeight来进行计算 */
|
|
1196
|
-
if (
|
|
1197
|
-
const borderTopWidth =
|
|
1198
|
-
const borderBottomWidth =
|
|
1199
|
-
const paddingTop =
|
|
1200
|
-
const paddingBottom =
|
|
1172
|
+
if ($el.offsetHeight > 0) {
|
|
1173
|
+
const borderTopWidth = CommonUtils.getStyleValue($el, "borderTopWidth");
|
|
1174
|
+
const borderBottomWidth = CommonUtils.getStyleValue($el, "borderBottomWidth");
|
|
1175
|
+
const paddingTop = CommonUtils.getStyleValue($el, "paddingTop");
|
|
1176
|
+
const paddingBottom = CommonUtils.getStyleValue($el, "paddingBottom");
|
|
1201
1177
|
const backHeight =
|
|
1202
|
-
parseFloat(
|
|
1178
|
+
parseFloat($el.offsetHeight.toString()) -
|
|
1203
1179
|
parseFloat(borderTopWidth.toString()) -
|
|
1204
1180
|
parseFloat(borderBottomWidth.toString()) -
|
|
1205
1181
|
parseFloat(paddingTop.toString()) -
|
|
@@ -1209,16 +1185,16 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1209
1185
|
return 0;
|
|
1210
1186
|
} else {
|
|
1211
1187
|
/* 未显示 */
|
|
1212
|
-
|
|
1213
|
-
const { recovery } =
|
|
1214
|
-
const height =
|
|
1188
|
+
$el = $el as HTMLElement;
|
|
1189
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
1190
|
+
const height = that.height($el, true);
|
|
1215
1191
|
recovery();
|
|
1216
1192
|
return height;
|
|
1217
1193
|
}
|
|
1218
1194
|
}
|
|
1219
1195
|
/**
|
|
1220
1196
|
* 获取元素的外部宽度(包括边框和外边距)
|
|
1221
|
-
* @param
|
|
1197
|
+
* @param $el 要获取外部宽度的元素
|
|
1222
1198
|
* @param [isShow=false] 是否已进行isShow,避免爆堆栈
|
|
1223
1199
|
* @returns 元素的外部宽度,单位为像素
|
|
1224
1200
|
* @example
|
|
@@ -1230,31 +1206,31 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1230
1206
|
* DOMUtils.outerWidth(window)
|
|
1231
1207
|
* > 400
|
|
1232
1208
|
*/
|
|
1233
|
-
outerWidth(
|
|
1234
|
-
outerWidth(
|
|
1235
|
-
const
|
|
1236
|
-
if (
|
|
1237
|
-
return
|
|
1238
|
-
}
|
|
1239
|
-
if (typeof
|
|
1240
|
-
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
if (isShow || (!isShow &&
|
|
1244
|
-
const style =
|
|
1245
|
-
const marginLeft =
|
|
1246
|
-
const marginRight =
|
|
1247
|
-
return
|
|
1209
|
+
outerWidth($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
|
|
1210
|
+
outerWidth($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
|
|
1211
|
+
const that = this;
|
|
1212
|
+
if (CommonUtils.isWin($el)) {
|
|
1213
|
+
return that.windowApi.window.innerWidth;
|
|
1214
|
+
}
|
|
1215
|
+
if (typeof $el === "string") {
|
|
1216
|
+
$el = that.selector($el) as HTMLElement;
|
|
1217
|
+
}
|
|
1218
|
+
$el = $el as HTMLElement;
|
|
1219
|
+
if (isShow || (!isShow && CommonUtils.isShow($el))) {
|
|
1220
|
+
const style = that.windowApi.globalThis.getComputedStyle($el, null);
|
|
1221
|
+
const marginLeft = CommonUtils.getStyleValue(style, "marginLeft");
|
|
1222
|
+
const marginRight = CommonUtils.getStyleValue(style, "marginRight");
|
|
1223
|
+
return $el.offsetWidth + marginLeft + marginRight;
|
|
1248
1224
|
} else {
|
|
1249
|
-
const { recovery } =
|
|
1250
|
-
const outerWidth =
|
|
1225
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
1226
|
+
const outerWidth = that.outerWidth($el, true);
|
|
1251
1227
|
recovery();
|
|
1252
1228
|
return outerWidth;
|
|
1253
1229
|
}
|
|
1254
1230
|
}
|
|
1255
1231
|
/**
|
|
1256
1232
|
* 获取元素的外部高度(包括边框和外边距)
|
|
1257
|
-
* @param {HTMLElement|string}
|
|
1233
|
+
* @param {HTMLElement|string} $el 要获取外部高度的元素
|
|
1258
1234
|
* @param {boolean} [isShow=false] 是否已进行isShow,避免爆堆栈
|
|
1259
1235
|
* @returns {number} 元素的外部高度,单位为像素
|
|
1260
1236
|
* @example
|
|
@@ -1266,139 +1242,102 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1266
1242
|
* DOMUtils.outerHeight(window)
|
|
1267
1243
|
* > 700
|
|
1268
1244
|
*/
|
|
1269
|
-
outerHeight(
|
|
1270
|
-
outerHeight(
|
|
1271
|
-
const
|
|
1272
|
-
if (
|
|
1273
|
-
return
|
|
1274
|
-
}
|
|
1275
|
-
if (typeof
|
|
1276
|
-
|
|
1277
|
-
}
|
|
1278
|
-
|
|
1279
|
-
if (isShow || (!isShow &&
|
|
1280
|
-
const style =
|
|
1281
|
-
const marginTop =
|
|
1282
|
-
const marginBottom =
|
|
1283
|
-
return
|
|
1245
|
+
outerHeight($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
|
|
1246
|
+
outerHeight($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
|
|
1247
|
+
const that = this;
|
|
1248
|
+
if (CommonUtils.isWin($el)) {
|
|
1249
|
+
return that.windowApi.window.innerHeight;
|
|
1250
|
+
}
|
|
1251
|
+
if (typeof $el === "string") {
|
|
1252
|
+
$el = that.selector($el) as HTMLElement;
|
|
1253
|
+
}
|
|
1254
|
+
$el = $el as HTMLElement;
|
|
1255
|
+
if (isShow || (!isShow && CommonUtils.isShow($el))) {
|
|
1256
|
+
const style = that.windowApi.globalThis.getComputedStyle($el, null);
|
|
1257
|
+
const marginTop = CommonUtils.getStyleValue(style, "marginTop");
|
|
1258
|
+
const marginBottom = CommonUtils.getStyleValue(style, "marginBottom");
|
|
1259
|
+
return $el.offsetHeight + marginTop + marginBottom;
|
|
1284
1260
|
} else {
|
|
1285
|
-
const { recovery } =
|
|
1286
|
-
const outerHeight =
|
|
1261
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
1262
|
+
const outerHeight = that.outerHeight($el, true);
|
|
1287
1263
|
recovery();
|
|
1288
1264
|
return outerHeight;
|
|
1289
1265
|
}
|
|
1290
1266
|
}
|
|
1291
1267
|
/**
|
|
1292
|
-
*
|
|
1293
|
-
* @param
|
|
1294
|
-
* @param
|
|
1295
|
-
* @param duration 动画持续时间,单位为毫秒
|
|
1296
|
-
* @param callback 动画结束后执行的函数
|
|
1268
|
+
* 将一个元素替换为另一个元素
|
|
1269
|
+
* @param $el 目标元素
|
|
1270
|
+
* @param $newEl 新元素
|
|
1297
1271
|
* @example
|
|
1298
|
-
* //
|
|
1299
|
-
* DOMUtils.
|
|
1300
|
-
*
|
|
1301
|
-
* })
|
|
1272
|
+
* // 替换元素a.xx为b.xx
|
|
1273
|
+
* DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
1274
|
+
* DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
|
|
1302
1275
|
*/
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
callback: (() => void) | undefined | null = null
|
|
1308
|
-
) {
|
|
1309
|
-
const DOMUtilsContext = this;
|
|
1310
|
-
if (typeof element === "string") {
|
|
1311
|
-
element = DOMUtilsContext.selectorAll(element);
|
|
1276
|
+
replaceWith($el: DOMUtilsTargetElementType, $newEl: HTMLElement | string | Node) {
|
|
1277
|
+
const that = this;
|
|
1278
|
+
if (typeof $el === "string") {
|
|
1279
|
+
$el = that.selectorAll($el);
|
|
1312
1280
|
}
|
|
1313
|
-
if (
|
|
1281
|
+
if ($el == null) {
|
|
1314
1282
|
return;
|
|
1315
1283
|
}
|
|
1316
|
-
if (
|
|
1284
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1317
1285
|
// 设置
|
|
1318
|
-
|
|
1319
|
-
|
|
1286
|
+
$el.forEach(($elItem) => {
|
|
1287
|
+
that.replaceWith($elItem as HTMLElement, $newEl);
|
|
1320
1288
|
});
|
|
1321
1289
|
return;
|
|
1322
1290
|
}
|
|
1323
|
-
if (typeof
|
|
1324
|
-
|
|
1325
|
-
}
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
throw new Error("styles must contain at least one property");
|
|
1334
|
-
}
|
|
1335
|
-
const start = performance.now();
|
|
1336
|
-
const from: {
|
|
1337
|
-
[prop: string]: any;
|
|
1338
|
-
} = {};
|
|
1339
|
-
const to: {
|
|
1340
|
-
[prop: string]: any;
|
|
1341
|
-
} = {};
|
|
1342
|
-
for (const prop in styles) {
|
|
1343
|
-
from[prop] = element.style[prop] || DOMUtilsContext.windowApi.globalThis.getComputedStyle(element)[prop];
|
|
1344
|
-
to[prop] = styles[prop];
|
|
1345
|
-
}
|
|
1346
|
-
const timer = DOMUtilsCommonUtils.setInterval(function () {
|
|
1347
|
-
const timePassed = performance.now() - start;
|
|
1348
|
-
let progress = timePassed / duration;
|
|
1349
|
-
if (progress > 1) {
|
|
1350
|
-
progress = 1;
|
|
1351
|
-
}
|
|
1352
|
-
for (const prop in styles) {
|
|
1353
|
-
element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
|
|
1354
|
-
}
|
|
1355
|
-
if (progress === 1) {
|
|
1356
|
-
DOMUtilsCommonUtils.clearInterval(timer);
|
|
1357
|
-
if (callback) {
|
|
1358
|
-
callback();
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
}, 10);
|
|
1291
|
+
if (typeof $newEl === "string") {
|
|
1292
|
+
$newEl = that.toElement($newEl, false, false);
|
|
1293
|
+
}
|
|
1294
|
+
const $parent = $el.parentElement;
|
|
1295
|
+
if ($parent) {
|
|
1296
|
+
$parent.replaceChild($newEl as Node, $el);
|
|
1297
|
+
} else {
|
|
1298
|
+
that.after($el, $newEl as HTMLElement);
|
|
1299
|
+
$el.remove();
|
|
1300
|
+
}
|
|
1362
1301
|
}
|
|
1363
1302
|
/**
|
|
1364
1303
|
* 将一个元素包裹在指定的HTML元素中
|
|
1365
|
-
* @param
|
|
1304
|
+
* @param $el 要包裹的元素
|
|
1366
1305
|
* @param wrapperHTML 要包裹的HTML元素的字符串表示形式
|
|
1367
1306
|
* @example
|
|
1368
1307
|
* // 将a.xx元素外面包裹一层div
|
|
1369
1308
|
* DOMUtils.wrap(document.querySelector("a.xx"),"<div></div>")
|
|
1370
1309
|
*/
|
|
1371
|
-
wrap(
|
|
1372
|
-
const
|
|
1373
|
-
if (typeof
|
|
1374
|
-
|
|
1310
|
+
wrap($el: DOMUtilsTargetElementType, wrapperHTML: string) {
|
|
1311
|
+
const that = this;
|
|
1312
|
+
if (typeof $el === "string") {
|
|
1313
|
+
$el = that.selectorAll($el);
|
|
1375
1314
|
}
|
|
1376
|
-
if (
|
|
1315
|
+
if ($el == null) {
|
|
1377
1316
|
return;
|
|
1378
1317
|
}
|
|
1379
|
-
if (
|
|
1318
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1380
1319
|
// 设置
|
|
1381
|
-
|
|
1382
|
-
|
|
1320
|
+
$el.forEach(($elItem) => {
|
|
1321
|
+
that.wrap($elItem as HTMLElement, wrapperHTML);
|
|
1383
1322
|
});
|
|
1384
1323
|
return;
|
|
1385
1324
|
}
|
|
1386
|
-
|
|
1325
|
+
$el = $el as HTMLElement;
|
|
1387
1326
|
// 创建一个新的div元素,并将wrapperHTML作为其innerHTML
|
|
1388
|
-
const wrapper =
|
|
1389
|
-
|
|
1327
|
+
const $wrapper = that.windowApi.document.createElement("div");
|
|
1328
|
+
that.html($wrapper, wrapperHTML);
|
|
1390
1329
|
|
|
1391
|
-
const wrapperFirstChild = wrapper.firstChild as HTMLElement;
|
|
1330
|
+
const wrapperFirstChild = $wrapper.firstChild as HTMLElement;
|
|
1392
1331
|
// 将要包裹的元素插入目标元素前面
|
|
1393
|
-
const parentElement =
|
|
1394
|
-
parentElement.insertBefore(wrapperFirstChild,
|
|
1332
|
+
const parentElement = $el.parentElement as HTMLElement;
|
|
1333
|
+
parentElement.insertBefore(wrapperFirstChild, $el);
|
|
1395
1334
|
|
|
1396
1335
|
// 将要包裹的元素移动到wrapper中
|
|
1397
|
-
wrapperFirstChild.appendChild(
|
|
1336
|
+
wrapperFirstChild.appendChild($el);
|
|
1398
1337
|
}
|
|
1399
1338
|
/**
|
|
1400
1339
|
* 获取当前元素的前一个兄弟元素
|
|
1401
|
-
* @param
|
|
1340
|
+
* @param $el 当前元素
|
|
1402
1341
|
* @returns 前一个兄弟元素
|
|
1403
1342
|
* @example
|
|
1404
1343
|
* // 获取a.xx元素前一个兄弟元素
|
|
@@ -1406,21 +1345,20 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1406
1345
|
* DOMUtils.prev("a.xx")
|
|
1407
1346
|
* > <div ...>....</div>
|
|
1408
1347
|
*/
|
|
1409
|
-
prev(
|
|
1410
|
-
prev(
|
|
1411
|
-
const
|
|
1412
|
-
if (typeof
|
|
1413
|
-
|
|
1348
|
+
prev($el: HTMLElement | string): HTMLElement;
|
|
1349
|
+
prev($el: HTMLElement | string) {
|
|
1350
|
+
const that = this;
|
|
1351
|
+
if (typeof $el === "string") {
|
|
1352
|
+
$el = that.selector($el) as HTMLElement;
|
|
1414
1353
|
}
|
|
1415
|
-
if (
|
|
1354
|
+
if ($el == null) {
|
|
1416
1355
|
return;
|
|
1417
1356
|
}
|
|
1418
|
-
return
|
|
1357
|
+
return $el.previousElementSibling as HTMLElement;
|
|
1419
1358
|
}
|
|
1420
|
-
|
|
1421
1359
|
/**
|
|
1422
1360
|
* 获取当前元素的后一个兄弟元素
|
|
1423
|
-
* @param
|
|
1361
|
+
* @param $el 当前元素
|
|
1424
1362
|
* @returns 后一个兄弟元素
|
|
1425
1363
|
* @example
|
|
1426
1364
|
* // 获取a.xx元素前一个兄弟元素
|
|
@@ -1428,29 +1366,16 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1428
1366
|
* DOMUtils.next("a.xx")
|
|
1429
1367
|
* > <div ...>....</div>
|
|
1430
1368
|
*/
|
|
1431
|
-
next(
|
|
1432
|
-
next(
|
|
1433
|
-
const
|
|
1434
|
-
if (typeof
|
|
1435
|
-
|
|
1369
|
+
next($el: HTMLElement | string): HTMLElement;
|
|
1370
|
+
next($el: HTMLElement | string) {
|
|
1371
|
+
const that = this;
|
|
1372
|
+
if (typeof $el === "string") {
|
|
1373
|
+
$el = that.selector($el) as HTMLElement;
|
|
1436
1374
|
}
|
|
1437
|
-
if (
|
|
1375
|
+
if ($el == null) {
|
|
1438
1376
|
return;
|
|
1439
1377
|
}
|
|
1440
|
-
return
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
|
-
* 取消挂载在window下的DOMUtils并返回DOMUtils
|
|
1444
|
-
* @example
|
|
1445
|
-
* let DOMUtils = window.DOMUtils.noConflict()
|
|
1446
|
-
*/
|
|
1447
|
-
noConflict() {
|
|
1448
|
-
const DOMUtilsContext = this;
|
|
1449
|
-
if ((DOMUtilsContext.windowApi.window as any).DOMUtils) {
|
|
1450
|
-
DOMUtilsCommonUtils.delete(window, "DOMUtils");
|
|
1451
|
-
}
|
|
1452
|
-
(DOMUtilsContext.windowApi.window as any).DOMUtils = this;
|
|
1453
|
-
return this;
|
|
1378
|
+
return $el.nextElementSibling as HTMLElement;
|
|
1454
1379
|
}
|
|
1455
1380
|
/**
|
|
1456
1381
|
* 获取当前元素的所有兄弟元素
|
|
@@ -1463,21 +1388,21 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1463
1388
|
* > (3)[div.logo-wrapper, div.forum-block, div.more-btn-desc]
|
|
1464
1389
|
*/
|
|
1465
1390
|
siblings(element: HTMLElement | string): HTMLElement[];
|
|
1466
|
-
siblings(
|
|
1467
|
-
const
|
|
1468
|
-
if (typeof
|
|
1469
|
-
|
|
1391
|
+
siblings($el: HTMLElement | string) {
|
|
1392
|
+
const that = this;
|
|
1393
|
+
if (typeof $el === "string") {
|
|
1394
|
+
$el = that.selector($el) as HTMLElement;
|
|
1470
1395
|
}
|
|
1471
|
-
if (
|
|
1396
|
+
if ($el == null) {
|
|
1472
1397
|
return;
|
|
1473
1398
|
}
|
|
1474
|
-
return Array.from((
|
|
1475
|
-
(child) => child !==
|
|
1399
|
+
return Array.from(($el.parentElement as HTMLElement).children as HTMLCollectionOf<HTMLElement>).filter(
|
|
1400
|
+
($child) => $child !== $el
|
|
1476
1401
|
);
|
|
1477
1402
|
}
|
|
1478
1403
|
/**
|
|
1479
1404
|
* 获取当前元素的父元素
|
|
1480
|
-
* @param
|
|
1405
|
+
* @param $el 当前元素
|
|
1481
1406
|
* @returns 父元素
|
|
1482
1407
|
* @example
|
|
1483
1408
|
* // 获取a.xx元素的父元素
|
|
@@ -1485,10 +1410,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1485
1410
|
* DOMUtils.parent("a.xx")
|
|
1486
1411
|
* > <div ...>....</div>
|
|
1487
1412
|
*/
|
|
1488
|
-
parent(
|
|
1413
|
+
parent($el: HTMLElement | string): HTMLElement;
|
|
1489
1414
|
/**
|
|
1490
1415
|
* 获取当前元素的父元素
|
|
1491
|
-
* @param
|
|
1416
|
+
* @param $el 当前元素
|
|
1492
1417
|
* @returns 父元素
|
|
1493
1418
|
* @example
|
|
1494
1419
|
* // 获取a.xx元素的父元素
|
|
@@ -1496,10 +1421,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1496
1421
|
* DOMUtils.parent("a.xx")
|
|
1497
1422
|
* > <div ...>....</div>
|
|
1498
1423
|
*/
|
|
1499
|
-
parent(
|
|
1424
|
+
parent($el: HTMLElement[] | NodeList): HTMLElement[];
|
|
1500
1425
|
/**
|
|
1501
1426
|
* 获取当前元素的父元素
|
|
1502
|
-
* @param
|
|
1427
|
+
* @param $el 当前元素
|
|
1503
1428
|
* @returns 父元素
|
|
1504
1429
|
* @example
|
|
1505
1430
|
* // 获取a.xx元素的父元素
|
|
@@ -1507,22 +1432,22 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1507
1432
|
* DOMUtils.parent("a.xx")
|
|
1508
1433
|
* > <div ...>....</div>
|
|
1509
1434
|
*/
|
|
1510
|
-
parent(
|
|
1511
|
-
const
|
|
1512
|
-
if (typeof
|
|
1513
|
-
|
|
1435
|
+
parent($el: HTMLElement | Element | Node | NodeList | string | HTMLElement[]) {
|
|
1436
|
+
const that = this;
|
|
1437
|
+
if (typeof $el === "string") {
|
|
1438
|
+
$el = that.selector<HTMLElement>($el)!;
|
|
1514
1439
|
}
|
|
1515
|
-
if (
|
|
1440
|
+
if ($el == null) {
|
|
1516
1441
|
return;
|
|
1517
1442
|
}
|
|
1518
|
-
if (
|
|
1443
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1519
1444
|
const resultArray: HTMLElement[] = [];
|
|
1520
|
-
|
|
1521
|
-
resultArray.push(
|
|
1445
|
+
$el.forEach(($elItem) => {
|
|
1446
|
+
resultArray.push(that.parent($elItem as HTMLElement));
|
|
1522
1447
|
});
|
|
1523
1448
|
return resultArray;
|
|
1524
1449
|
} else {
|
|
1525
|
-
return
|
|
1450
|
+
return $el.parentElement;
|
|
1526
1451
|
}
|
|
1527
1452
|
}
|
|
1528
1453
|
/**
|
|
@@ -1538,28 +1463,28 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1538
1463
|
* 如果useParser为false,返回一个DIV元素的firstChild
|
|
1539
1464
|
* @example
|
|
1540
1465
|
* // 将字符串转为Element元素
|
|
1541
|
-
* DOMUtils.
|
|
1466
|
+
* DOMUtils.toElement("<a href='xxxx'></a>")
|
|
1542
1467
|
* > <a href="xxxx"></a>
|
|
1543
1468
|
* @example
|
|
1544
1469
|
* // 使用DOMParser将字符串转为Element元素
|
|
1545
|
-
* DOMUtils.
|
|
1470
|
+
* DOMUtils.toElement("<a href='xxxx'></a>",true)
|
|
1546
1471
|
* > <a href="xxxx"></a>
|
|
1547
1472
|
* @example
|
|
1548
1473
|
* // 由于需要转换的元素是多个元素,将字符串转为完整的Element元素
|
|
1549
|
-
* DOMUtils.
|
|
1474
|
+
* DOMUtils.toElement("<a href='xxxx'></a><a href='xxxx'></a>",false, true)
|
|
1550
1475
|
* > <div><a href="xxxx"></a><a href='xxxx'></a></div>
|
|
1551
1476
|
* @example
|
|
1552
1477
|
* // 由于需要转换的元素是多个元素,使用DOMParser将字符串转为完整的Element元素
|
|
1553
|
-
* DOMUtils.
|
|
1478
|
+
* DOMUtils.toElement("<a href='xxxx'></a><a href='xxxx'></a>",true, true)
|
|
1554
1479
|
* > #document
|
|
1555
1480
|
*/
|
|
1556
|
-
|
|
1481
|
+
toElement<T1 extends boolean, T2 extends boolean>(
|
|
1557
1482
|
html: string,
|
|
1558
1483
|
useParser?: T1,
|
|
1559
1484
|
isComplete?: T2
|
|
1560
|
-
):
|
|
1561
|
-
|
|
1562
|
-
const
|
|
1485
|
+
): T1 extends true ? (T2 extends true ? Document : HTMLElement) : HTMLElement;
|
|
1486
|
+
toElement(html: string, useParser = false, isComplete = false) {
|
|
1487
|
+
const that = this;
|
|
1563
1488
|
// 去除html前后的空格
|
|
1564
1489
|
html = html.trim();
|
|
1565
1490
|
function parseHTMLByDOMParser() {
|
|
@@ -1571,12 +1496,12 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1571
1496
|
}
|
|
1572
1497
|
}
|
|
1573
1498
|
function parseHTMLByCreateDom() {
|
|
1574
|
-
const
|
|
1575
|
-
|
|
1499
|
+
const $el = that.windowApi.document.createElement("div");
|
|
1500
|
+
that.html($el, html);
|
|
1576
1501
|
if (isComplete) {
|
|
1577
|
-
return
|
|
1502
|
+
return $el;
|
|
1578
1503
|
} else {
|
|
1579
|
-
return
|
|
1504
|
+
return $el.firstElementChild ?? $el.firstChild;
|
|
1580
1505
|
}
|
|
1581
1506
|
}
|
|
1582
1507
|
if (useParser) {
|
|
@@ -1624,206 +1549,6 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1624
1549
|
.map((item) => `${encodeURIComponent(item.name)}=${encodeURIComponent(item.value)}`)
|
|
1625
1550
|
.join("&");
|
|
1626
1551
|
}
|
|
1627
|
-
/**
|
|
1628
|
-
* 显示元素
|
|
1629
|
-
* @param target 当前元素
|
|
1630
|
-
* @param checkVisiblie 是否检测元素是否显示
|
|
1631
|
-
* + true (默认)如果检测到还未显示,则强制使用display: unset !important;
|
|
1632
|
-
* + false 不检测,直接设置display属性为空
|
|
1633
|
-
* @example
|
|
1634
|
-
* // 显示a.xx元素
|
|
1635
|
-
* DOMUtils.show(document.querySelector("a.xx"))
|
|
1636
|
-
* DOMUtils.show(document.querySelectorAll("a.xx"))
|
|
1637
|
-
* DOMUtils.show("a.xx")
|
|
1638
|
-
*/
|
|
1639
|
-
show(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
|
|
1640
|
-
const DOMUtilsContext = this;
|
|
1641
|
-
if (target == null) {
|
|
1642
|
-
return;
|
|
1643
|
-
}
|
|
1644
|
-
if (typeof target === "string") {
|
|
1645
|
-
target = DOMUtilsContext.selectorAll(target);
|
|
1646
|
-
}
|
|
1647
|
-
if (target instanceof NodeList || target instanceof Array) {
|
|
1648
|
-
target = target as HTMLElement[];
|
|
1649
|
-
for (const element of target) {
|
|
1650
|
-
DOMUtilsContext.show(element, checkVisiblie);
|
|
1651
|
-
}
|
|
1652
|
-
} else {
|
|
1653
|
-
target = target as HTMLElement;
|
|
1654
|
-
target.style.display = "";
|
|
1655
|
-
if (checkVisiblie) {
|
|
1656
|
-
if (!DOMUtilsCommonUtils.isShow(target)) {
|
|
1657
|
-
/* 仍然是不显示,尝试使用强覆盖 */
|
|
1658
|
-
target.style.setProperty("display", "unset", "important");
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
}
|
|
1662
|
-
}
|
|
1663
|
-
/**
|
|
1664
|
-
* 隐藏元素
|
|
1665
|
-
* @param target 当前元素
|
|
1666
|
-
* @param checkVisiblie 是否检测元素是否显示
|
|
1667
|
-
* + true (默认)如果检测到显示,则强制使用display: none !important;
|
|
1668
|
-
* + false 不检测,直接设置display属性为none
|
|
1669
|
-
* @example
|
|
1670
|
-
* // 隐藏a.xx元素
|
|
1671
|
-
* DOMUtils.hide(document.querySelector("a.xx"))
|
|
1672
|
-
* DOMUtils.hide(document.querySelectorAll("a.xx"))
|
|
1673
|
-
* DOMUtils.hide("a.xx")
|
|
1674
|
-
*/
|
|
1675
|
-
hide(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
|
|
1676
|
-
const DOMUtilsContext = this;
|
|
1677
|
-
if (target == null) {
|
|
1678
|
-
return;
|
|
1679
|
-
}
|
|
1680
|
-
if (typeof target === "string") {
|
|
1681
|
-
target = DOMUtilsContext.selectorAll(target);
|
|
1682
|
-
}
|
|
1683
|
-
if (target instanceof NodeList || target instanceof Array) {
|
|
1684
|
-
target = target as HTMLElement[];
|
|
1685
|
-
for (const element of target) {
|
|
1686
|
-
DOMUtilsContext.hide(element, checkVisiblie);
|
|
1687
|
-
}
|
|
1688
|
-
} else {
|
|
1689
|
-
target = target as HTMLElement;
|
|
1690
|
-
target.style.display = "none";
|
|
1691
|
-
if (checkVisiblie) {
|
|
1692
|
-
if (DOMUtilsCommonUtils.isShow(target)) {
|
|
1693
|
-
/* 仍然是显示,尝试使用强覆盖 */
|
|
1694
|
-
target.style.setProperty("display", "none", "important");
|
|
1695
|
-
}
|
|
1696
|
-
}
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
/**
|
|
1700
|
-
* 淡入元素
|
|
1701
|
-
* @param element 当前元素
|
|
1702
|
-
* @param duration 动画持续时间(毫秒),默认400毫秒
|
|
1703
|
-
* @param callback 动画结束的回调
|
|
1704
|
-
* @example
|
|
1705
|
-
* // 元素a.xx淡入
|
|
1706
|
-
* DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
|
|
1707
|
-
* console.log("淡入完毕");
|
|
1708
|
-
* })
|
|
1709
|
-
* DOMUtils.fadeIn("a.xx",undefined,()=>{
|
|
1710
|
-
* console.log("淡入完毕");
|
|
1711
|
-
* })
|
|
1712
|
-
*/
|
|
1713
|
-
fadeIn(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
|
|
1714
|
-
if (element == null) {
|
|
1715
|
-
return;
|
|
1716
|
-
}
|
|
1717
|
-
const DOMUtilsContext = this;
|
|
1718
|
-
if (typeof element === "string") {
|
|
1719
|
-
element = DOMUtilsContext.selectorAll(element);
|
|
1720
|
-
}
|
|
1721
|
-
if (DOMUtilsCommonUtils.isNodeList(element)) {
|
|
1722
|
-
// 设置
|
|
1723
|
-
element.forEach(($ele) => {
|
|
1724
|
-
DOMUtilsContext.fadeIn($ele as HTMLElement, duration, callback);
|
|
1725
|
-
});
|
|
1726
|
-
return;
|
|
1727
|
-
}
|
|
1728
|
-
element.style.opacity = "0";
|
|
1729
|
-
element.style.display = "";
|
|
1730
|
-
let start: number = null as any;
|
|
1731
|
-
let timer: number = null as any;
|
|
1732
|
-
function step(timestamp: number) {
|
|
1733
|
-
if (!start) start = timestamp;
|
|
1734
|
-
const progress = timestamp - start;
|
|
1735
|
-
element = element as HTMLElement;
|
|
1736
|
-
element.style.opacity = Math.min(progress / duration, 1).toString();
|
|
1737
|
-
if (progress < duration) {
|
|
1738
|
-
DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1739
|
-
} else {
|
|
1740
|
-
if (callback && typeof callback === "function") {
|
|
1741
|
-
callback();
|
|
1742
|
-
}
|
|
1743
|
-
DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
|
|
1744
|
-
}
|
|
1745
|
-
}
|
|
1746
|
-
timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1747
|
-
}
|
|
1748
|
-
/**
|
|
1749
|
-
* 淡出元素
|
|
1750
|
-
* @param element 当前元素
|
|
1751
|
-
* @param duration 动画持续时间(毫秒),默认400毫秒
|
|
1752
|
-
* @param callback 动画结束的回调
|
|
1753
|
-
* @example
|
|
1754
|
-
* // 元素a.xx淡出
|
|
1755
|
-
* DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
|
|
1756
|
-
* console.log("淡出完毕");
|
|
1757
|
-
* })
|
|
1758
|
-
* DOMUtils.fadeOut("a.xx",undefined,()=>{
|
|
1759
|
-
* console.log("淡出完毕");
|
|
1760
|
-
* })
|
|
1761
|
-
*/
|
|
1762
|
-
fadeOut(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
|
|
1763
|
-
const DOMUtilsContext = this;
|
|
1764
|
-
if (element == null) {
|
|
1765
|
-
return;
|
|
1766
|
-
}
|
|
1767
|
-
if (typeof element === "string") {
|
|
1768
|
-
element = DOMUtilsContext.selectorAll(element);
|
|
1769
|
-
}
|
|
1770
|
-
if (DOMUtilsCommonUtils.isNodeList(element)) {
|
|
1771
|
-
// 设置
|
|
1772
|
-
element.forEach(($ele) => {
|
|
1773
|
-
DOMUtilsContext.fadeOut($ele as HTMLElement, duration, callback);
|
|
1774
|
-
});
|
|
1775
|
-
return;
|
|
1776
|
-
}
|
|
1777
|
-
element.style.opacity = "1";
|
|
1778
|
-
let start: number = null as any;
|
|
1779
|
-
let timer: number = null as any;
|
|
1780
|
-
function step(timestamp: number) {
|
|
1781
|
-
if (!start) start = timestamp;
|
|
1782
|
-
const progress = timestamp - start;
|
|
1783
|
-
element = element as HTMLElement;
|
|
1784
|
-
element.style.opacity = Math.max(1 - progress / duration, 0).toString();
|
|
1785
|
-
if (progress < duration) {
|
|
1786
|
-
DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1787
|
-
} else {
|
|
1788
|
-
element.style.display = "none";
|
|
1789
|
-
if (typeof callback === "function") {
|
|
1790
|
-
callback();
|
|
1791
|
-
}
|
|
1792
|
-
DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
|
|
1793
|
-
}
|
|
1794
|
-
}
|
|
1795
|
-
timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1796
|
-
}
|
|
1797
|
-
/**
|
|
1798
|
-
* 切换元素的显示和隐藏状态
|
|
1799
|
-
* @param element 当前元素
|
|
1800
|
-
* @param checkVisiblie 是否检测元素是否显示
|
|
1801
|
-
* @example
|
|
1802
|
-
* // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
|
|
1803
|
-
* DOMUtils.toggle(document.querySelector("a.xx"))
|
|
1804
|
-
* DOMUtils.toggle("a.xx")
|
|
1805
|
-
*/
|
|
1806
|
-
toggle(element: DOMUtilsTargetElementType, checkVisiblie?: boolean) {
|
|
1807
|
-
const DOMUtilsContext = this;
|
|
1808
|
-
if (typeof element === "string") {
|
|
1809
|
-
element = DOMUtilsContext.selectorAll(element);
|
|
1810
|
-
}
|
|
1811
|
-
if (element == null) {
|
|
1812
|
-
return;
|
|
1813
|
-
}
|
|
1814
|
-
if (DOMUtilsCommonUtils.isNodeList(element)) {
|
|
1815
|
-
// 设置
|
|
1816
|
-
element.forEach(($ele) => {
|
|
1817
|
-
DOMUtilsContext.toggle($ele as HTMLElement);
|
|
1818
|
-
});
|
|
1819
|
-
return;
|
|
1820
|
-
}
|
|
1821
|
-
if (DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
|
|
1822
|
-
DOMUtilsContext.show(element, checkVisiblie);
|
|
1823
|
-
} else {
|
|
1824
|
-
DOMUtilsContext.hide(element, checkVisiblie);
|
|
1825
|
-
}
|
|
1826
|
-
}
|
|
1827
1552
|
/**
|
|
1828
1553
|
* 创建一个新的DOMUtils实例
|
|
1829
1554
|
* @param option
|
|
@@ -1845,7 +1570,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1845
1570
|
selectionStart?: number | string,
|
|
1846
1571
|
selectionEnd?: number | string
|
|
1847
1572
|
): DOMRect {
|
|
1848
|
-
const
|
|
1573
|
+
const that = this;
|
|
1849
1574
|
// Basic parameter validation
|
|
1850
1575
|
if (!$input || !("value" in $input)) return $input;
|
|
1851
1576
|
if (selectionStart == null) {
|
|
@@ -1914,7 +1639,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1914
1639
|
// 不能为空,不然获取不到高度
|
|
1915
1640
|
const text = $input.value || "G",
|
|
1916
1641
|
textLen = text.length,
|
|
1917
|
-
fakeClone =
|
|
1642
|
+
fakeClone = that.windowApi.document.createElement("div");
|
|
1918
1643
|
if (selectionStart > 0) appendPart(0, selectionStart);
|
|
1919
1644
|
const fakeRange = appendPart(selectionStart, selectionEnd);
|
|
1920
1645
|
if (textLen > selectionEnd) appendPart(selectionEnd, textLen);
|
|
@@ -1928,7 +1653,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1928
1653
|
fakeClone.style.left = leftPos + "px";
|
|
1929
1654
|
fakeClone.style.width = width + "px";
|
|
1930
1655
|
fakeClone.style.height = height + "px";
|
|
1931
|
-
|
|
1656
|
+
that.windowApi.document.body.appendChild(fakeClone);
|
|
1932
1657
|
const returnValue = fakeRange.getBoundingClientRect(); //Get rect
|
|
1933
1658
|
|
|
1934
1659
|
fakeClone?.parentNode?.removeChild(fakeClone); //Remove temp
|
|
@@ -1942,7 +1667,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1942
1667
|
* @returns
|
|
1943
1668
|
*/
|
|
1944
1669
|
function appendPart(start: number, end: number) {
|
|
1945
|
-
const span =
|
|
1670
|
+
const span = that.windowApi.document.createElement("span");
|
|
1946
1671
|
span.style.cssText = cssDefaultStyles; //Force styles to prevent unexpected results
|
|
1947
1672
|
span.textContent = text.substring(start, end);
|
|
1948
1673
|
fakeClone.appendChild(span);
|
|
@@ -1950,10 +1675,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1950
1675
|
}
|
|
1951
1676
|
// Computing offset position
|
|
1952
1677
|
function getInputOffset() {
|
|
1953
|
-
const body =
|
|
1954
|
-
win =
|
|
1955
|
-
docElem =
|
|
1956
|
-
$box =
|
|
1678
|
+
const body = that.windowApi.document.body,
|
|
1679
|
+
win = that.windowApi.document.defaultView!,
|
|
1680
|
+
docElem = that.windowApi.document.documentElement,
|
|
1681
|
+
$box = that.windowApi.document.createElement("div");
|
|
1957
1682
|
$box.style.paddingLeft = $box.style.width = "1px";
|
|
1958
1683
|
body.appendChild($box);
|
|
1959
1684
|
const isBoxModel = $box.offsetWidth == 2;
|
|
@@ -1975,7 +1700,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1975
1700
|
* @returns
|
|
1976
1701
|
*/
|
|
1977
1702
|
function getInputCSS<T extends boolean>(prop: string, isNumber: T): T extends true ? number : string {
|
|
1978
|
-
const val =
|
|
1703
|
+
const val = that.windowApi.document.defaultView!.getComputedStyle($input, null).getPropertyValue(prop);
|
|
1979
1704
|
if (isNumber) {
|
|
1980
1705
|
return parseFloat(val) as T extends true ? number : string;
|
|
1981
1706
|
} else {
|
|
@@ -1983,13 +1708,209 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1983
1708
|
}
|
|
1984
1709
|
}
|
|
1985
1710
|
}
|
|
1986
|
-
/**
|
|
1987
|
-
|
|
1988
|
-
|
|
1711
|
+
/**
|
|
1712
|
+
* 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
|
|
1713
|
+
* @param cssText css字符串
|
|
1714
|
+
* @returns 返回添加的CSS标签
|
|
1715
|
+
* @example
|
|
1716
|
+
* DOMUtils.addStyle("html{}");
|
|
1717
|
+
* > <style type="text/css">html{}</style>
|
|
1718
|
+
*/
|
|
1719
|
+
addStyle(cssText: string): HTMLStyleElement;
|
|
1720
|
+
addStyle(cssText: string): HTMLStyleElement {
|
|
1721
|
+
if (typeof cssText !== "string") {
|
|
1722
|
+
throw new Error("DOMUtils.addStyle 参数cssText 必须为String类型");
|
|
1723
|
+
}
|
|
1724
|
+
const $css = this.createElement("style", {
|
|
1725
|
+
type: "text/css",
|
|
1726
|
+
innerHTML: cssText,
|
|
1727
|
+
});
|
|
1728
|
+
if (this.windowApi.document.head) {
|
|
1729
|
+
/* 插入head最后 */
|
|
1730
|
+
this.windowApi.document.head.appendChild($css);
|
|
1731
|
+
} else if (this.windowApi.document.documentElement.childNodes.length === 0) {
|
|
1732
|
+
/* 插入#html后 */
|
|
1733
|
+
this.windowApi.document.documentElement.appendChild($css);
|
|
1734
|
+
} else {
|
|
1735
|
+
/* 插入#html第一个元素前 */
|
|
1736
|
+
this.windowApi.document.documentElement.insertBefore($css, this.windowApi.document.documentElement.childNodes[0]);
|
|
1737
|
+
}
|
|
1738
|
+
return $css;
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* 检测点击的地方是否在该元素区域内
|
|
1742
|
+
* @param $el 需要检测的元素
|
|
1743
|
+
* @returns
|
|
1744
|
+
* + true 点击在元素上
|
|
1745
|
+
* + false 未点击在元素上
|
|
1746
|
+
* @example
|
|
1747
|
+
* DOMUtils.checkUserClickInNode(document.querySelector(".xxx"));
|
|
1748
|
+
* > false
|
|
1749
|
+
**/
|
|
1750
|
+
checkUserClickInNode($el: Element | Node | HTMLElement) {
|
|
1751
|
+
const that = this;
|
|
1752
|
+
if (!CommonUtils.isDOM($el)) {
|
|
1753
|
+
throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
|
|
1754
|
+
}
|
|
1755
|
+
const clickEvent = that.windowApi.window.event as PointerEvent;
|
|
1756
|
+
const touchEvent = that.windowApi.window.event as TouchEvent;
|
|
1757
|
+
const $click = clickEvent?.composedPath()?.[0] as HTMLElement;
|
|
1758
|
+
|
|
1759
|
+
// 点击的x坐标
|
|
1760
|
+
const clickPosX = clickEvent?.clientX != null ? clickEvent.clientX : touchEvent.touches[0].clientX;
|
|
1761
|
+
// 点击的y坐标
|
|
1762
|
+
const clickPosY = clickEvent?.clientY != null ? clickEvent.clientY : touchEvent.touches[0].clientY;
|
|
1763
|
+
const {
|
|
1764
|
+
/* 要检测的元素的相对屏幕的横坐标最左边 */
|
|
1765
|
+
left: elementPosXLeft,
|
|
1766
|
+
/* 要检测的元素的相对屏幕的横坐标最右边 */
|
|
1767
|
+
right: elementPosXRight,
|
|
1768
|
+
/* 要检测的元素的相对屏幕的纵坐标最上边 */
|
|
1769
|
+
top: elementPosYTop,
|
|
1770
|
+
/* 要检测的元素的相对屏幕的纵坐标最下边 */
|
|
1771
|
+
bottom: elementPosYBottom,
|
|
1772
|
+
} = (<HTMLElement>$el).getBoundingClientRect();
|
|
1773
|
+
if (
|
|
1774
|
+
clickPosX >= elementPosXLeft &&
|
|
1775
|
+
clickPosX <= elementPosXRight &&
|
|
1776
|
+
clickPosY >= elementPosYTop &&
|
|
1777
|
+
clickPosY <= elementPosYBottom
|
|
1778
|
+
) {
|
|
1779
|
+
return true;
|
|
1780
|
+
} else if (($click && $el.contains($click)) || $click == $el) {
|
|
1781
|
+
/* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
|
|
1782
|
+
return true;
|
|
1783
|
+
} else {
|
|
1784
|
+
return false;
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* 删除某个父元素,父元素可能在上层或上上层或上上上层...
|
|
1789
|
+
* @param $el 当前元素
|
|
1790
|
+
* @param parentSelector 判断是否满足父元素,参数为当前处理的父元素,满足返回true,否则false
|
|
1791
|
+
* @returns
|
|
1792
|
+
* + true 已删除
|
|
1793
|
+
* + false 未删除
|
|
1794
|
+
* @example
|
|
1795
|
+
* DOMUtils.deleteParentNode(document.querySelector("a"),".xxx");
|
|
1796
|
+
* > true
|
|
1797
|
+
**/
|
|
1798
|
+
deleteParentNode($el: Node | HTMLElement | Element | null, parentSelector: string): boolean;
|
|
1799
|
+
deleteParentNode($el: Node | HTMLElement | Element | null, parentSelector: string) {
|
|
1800
|
+
if ($el == null) {
|
|
1801
|
+
return;
|
|
1802
|
+
}
|
|
1803
|
+
if (!CommonUtils.isDOM($el)) {
|
|
1804
|
+
throw new Error("DOMUtils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
|
|
1805
|
+
}
|
|
1806
|
+
if (typeof parentSelector !== "string") {
|
|
1807
|
+
throw new Error("DOMUtils.deleteParentNode 参数 targetSelector 必须为 string 类型");
|
|
1808
|
+
}
|
|
1809
|
+
let result = false;
|
|
1810
|
+
const $parent = domUtils.closest($el as HTMLElement, parentSelector);
|
|
1811
|
+
if ($parent) {
|
|
1812
|
+
this.remove($parent);
|
|
1813
|
+
result = true;
|
|
1814
|
+
}
|
|
1815
|
+
return result;
|
|
1989
1816
|
}
|
|
1990
|
-
/**
|
|
1991
|
-
|
|
1992
|
-
|
|
1817
|
+
/**
|
|
1818
|
+
* 定位元素上的字符串,返回一个迭代器
|
|
1819
|
+
* @param $el 目标元素
|
|
1820
|
+
* @param text 需要定位的字符串
|
|
1821
|
+
* @param filter (可选)过滤器函数,返回值为true是排除该元素
|
|
1822
|
+
* @example
|
|
1823
|
+
* let textIterator = DOMUtils.findElementsWithText(document.documentElement,"xxxx");
|
|
1824
|
+
* textIterator.next();
|
|
1825
|
+
* > {value: ?HTMLElement, done: boolean, next: Function}
|
|
1826
|
+
*/
|
|
1827
|
+
findElementsWithText<T extends HTMLElement | Element | Node>(
|
|
1828
|
+
$el: T,
|
|
1829
|
+
text: string,
|
|
1830
|
+
filter?: (element: T) => boolean
|
|
1831
|
+
): Generator<HTMLElement | ChildNode, void, any>;
|
|
1832
|
+
*findElementsWithText<T extends HTMLElement | Element | Node>(
|
|
1833
|
+
$el: T,
|
|
1834
|
+
text: string,
|
|
1835
|
+
filter?: (element: T) => boolean
|
|
1836
|
+
) {
|
|
1837
|
+
const that = this;
|
|
1838
|
+
if ((<HTMLElement>$el).outerHTML.includes(text)) {
|
|
1839
|
+
if ((<HTMLElement>$el).children.length === 0) {
|
|
1840
|
+
const filterResult = typeof filter === "function" ? filter($el) : false;
|
|
1841
|
+
if (!filterResult) {
|
|
1842
|
+
yield $el as any;
|
|
1843
|
+
}
|
|
1844
|
+
} else {
|
|
1845
|
+
const $text = Array.from($el.childNodes).filter(($child) => $child.nodeType === Node.TEXT_NODE);
|
|
1846
|
+
for (const $child of $text) {
|
|
1847
|
+
if ((<HTMLElement>$child).textContent.includes(text)) {
|
|
1848
|
+
const filterResult = typeof filter === "function" ? filter($el) : false;
|
|
1849
|
+
if (!filterResult) {
|
|
1850
|
+
yield $child;
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
for (let index = 0; index < (<HTMLElement>$el).children.length; index++) {
|
|
1858
|
+
const $child = (<HTMLElement>$el).children[index] as T;
|
|
1859
|
+
yield* that.findElementsWithText($child, text, filter);
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
/**
|
|
1863
|
+
* 寻找可见元素,如果元素不可见,则向上找它的父元素直至找到,如果父元素不存在则返回null
|
|
1864
|
+
* @param $el
|
|
1865
|
+
* @example
|
|
1866
|
+
* let visibleElement = DOMUtils.findVisibleElement(document.querySelector("a.xx"));
|
|
1867
|
+
* > <HTMLElement>
|
|
1868
|
+
*/
|
|
1869
|
+
findVisibleElement($el: HTMLElement | Element | Node) {
|
|
1870
|
+
let $current = $el as HTMLElement;
|
|
1871
|
+
while ($current) {
|
|
1872
|
+
const rect = $current.getBoundingClientRect();
|
|
1873
|
+
if ((rect as any).length) {
|
|
1874
|
+
return $current;
|
|
1875
|
+
}
|
|
1876
|
+
$current = $current.parentElement as HTMLElement;
|
|
1877
|
+
}
|
|
1878
|
+
return null;
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* 将元素上的文本或元素使用光标进行选中
|
|
1882
|
+
*
|
|
1883
|
+
* 注意,如果设置startIndex和endIndex,且元素上并无可选则的坐标,那么会报错
|
|
1884
|
+
* @param $el 目标元素
|
|
1885
|
+
* @param childTextNode 目标元素下的#text元素
|
|
1886
|
+
* @param startIndex (可选)开始坐标,可为空
|
|
1887
|
+
* @param endIndex (可选)结束坐标,可为空
|
|
1888
|
+
* @example
|
|
1889
|
+
* DOMUtils.setElementSelection(document.querySelector("span"));
|
|
1890
|
+
*/
|
|
1891
|
+
setElementSelection(
|
|
1892
|
+
$el: HTMLElement | Element | Node,
|
|
1893
|
+
childTextNode?: ChildNode,
|
|
1894
|
+
startIndex?: number,
|
|
1895
|
+
endIndex?: number
|
|
1896
|
+
): void {
|
|
1897
|
+
const range = this.windowApi.document.createRange();
|
|
1898
|
+
range.selectNodeContents($el);
|
|
1899
|
+
if (childTextNode) {
|
|
1900
|
+
if (childTextNode.nodeType !== Node.TEXT_NODE) {
|
|
1901
|
+
throw new TypeError("childTextNode必须是#text元素");
|
|
1902
|
+
}
|
|
1903
|
+
if (startIndex != null && endIndex != null) {
|
|
1904
|
+
range.setStart(childTextNode, startIndex);
|
|
1905
|
+
range.setEnd(childTextNode, endIndex);
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
const selection = this.windowApi.globalThis.getSelection();
|
|
1910
|
+
if (selection) {
|
|
1911
|
+
selection.removeAllRanges();
|
|
1912
|
+
selection.addRange(range);
|
|
1913
|
+
}
|
|
1993
1914
|
}
|
|
1994
1915
|
}
|
|
1995
1916
|
|