@whitesev/domutils 1.2.0 → 1.3.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 +155 -115
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +155 -115
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +155 -115
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +155 -115
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +155 -115
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +155 -115
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtils.d.ts +3 -3
- package/dist/types/src/DOMUtilsCommonUtils.d.ts +2 -0
- package/dist/types/src/DOMUtilsEvent.d.ts +4 -2
- package/dist/types/src/WindowApi.d.ts +22 -0
- package/package.json +1 -1
- package/src/DOMUtils.ts +152 -66
- package/src/DOMUtilsCommonUtils.ts +9 -5
- package/src/DOMUtilsEvent.ts +44 -22
- package/src/WindowApi.ts +44 -0
- package/dist/types/src/DOMUtilsCore.d.ts +0 -15
- package/src/DOMUtilsCore.ts +0 -45
package/src/DOMUtils.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { DOMUtilsCommonUtils } from "./DOMUtilsCommonUtils";
|
|
2
|
-
import { DOMUtilsCore } from "./DOMUtilsCore";
|
|
3
|
-
import type { DOMUtilsCoreOption } from "./DOMUtilsCore";
|
|
4
2
|
import {
|
|
5
3
|
type DOMUtilsCreateElementAttributesMap,
|
|
6
4
|
DOMUtilsEvent,
|
|
7
5
|
} from "./DOMUtilsEvent";
|
|
8
6
|
import { ParseHTMLReturnType } from "./types/global";
|
|
7
|
+
import { type UtilsWindowApiOption } from "./WindowApi";
|
|
9
8
|
|
|
10
9
|
class DOMUtils extends DOMUtilsEvent {
|
|
11
|
-
constructor(option?:
|
|
12
|
-
|
|
13
|
-
super();
|
|
10
|
+
constructor(option?: UtilsWindowApiOption) {
|
|
11
|
+
super(option);
|
|
14
12
|
}
|
|
15
13
|
/** 版本号 */
|
|
16
|
-
version = "2024.
|
|
14
|
+
version = "2024.7.24";
|
|
17
15
|
/**
|
|
18
16
|
* 获取元素的属性值
|
|
19
17
|
* @param element 目标元素
|
|
@@ -41,8 +39,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
41
39
|
attrValue: string
|
|
42
40
|
): void;
|
|
43
41
|
attr(element: HTMLElement | string, attrName: string, attrValue?: string) {
|
|
42
|
+
let DOMUtilsContext = this;
|
|
44
43
|
if (typeof element === "string") {
|
|
45
|
-
element =
|
|
44
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
45
|
+
element
|
|
46
|
+
) as HTMLElement;
|
|
46
47
|
}
|
|
47
48
|
if (element == null) {
|
|
48
49
|
return;
|
|
@@ -85,7 +86,8 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
85
86
|
/** 自定义属性 */
|
|
86
87
|
attributes?: DOMUtilsCreateElementAttributesMap
|
|
87
88
|
): HTMLElementTagNameMap[K] {
|
|
88
|
-
let
|
|
89
|
+
let DOMUtilsContext = this;
|
|
90
|
+
let tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
|
|
89
91
|
if (typeof property === "string") {
|
|
90
92
|
tempElement.innerHTML = property;
|
|
91
93
|
return tempElement;
|
|
@@ -197,6 +199,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
197
199
|
},
|
|
198
200
|
value?: string | number
|
|
199
201
|
) {
|
|
202
|
+
let DOMUtilsContext = this;
|
|
200
203
|
/**
|
|
201
204
|
* 把纯数字没有px的加上
|
|
202
205
|
*/
|
|
@@ -223,7 +226,9 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
223
226
|
return propertyValue;
|
|
224
227
|
}
|
|
225
228
|
if (typeof element === "string") {
|
|
226
|
-
element =
|
|
229
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
230
|
+
element
|
|
231
|
+
) as HTMLElement;
|
|
227
232
|
}
|
|
228
233
|
if (element == null) {
|
|
229
234
|
return;
|
|
@@ -285,8 +290,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
285
290
|
text: string | HTMLElement | Element
|
|
286
291
|
): void;
|
|
287
292
|
text(element: HTMLElement | string, text?: string | HTMLElement | Element) {
|
|
293
|
+
let DOMUtilsContext = this;
|
|
288
294
|
if (typeof element === "string") {
|
|
289
|
-
element =
|
|
295
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
296
|
+
element
|
|
297
|
+
) as HTMLElement;
|
|
290
298
|
}
|
|
291
299
|
if (element == null) {
|
|
292
300
|
return;
|
|
@@ -333,8 +341,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
333
341
|
* */
|
|
334
342
|
html(element: HTMLElement | string): string;
|
|
335
343
|
html(element: HTMLElement | string, html?: string | HTMLElement | Element) {
|
|
344
|
+
let DOMUtilsContext = this;
|
|
336
345
|
if (typeof element === "string") {
|
|
337
|
-
element =
|
|
346
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
347
|
+
element
|
|
348
|
+
) as HTMLElement;
|
|
338
349
|
}
|
|
339
350
|
if (element == null) {
|
|
340
351
|
return;
|
|
@@ -423,8 +434,9 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
423
434
|
* */
|
|
424
435
|
val(element: HTMLInputElement): boolean | string;
|
|
425
436
|
val(element: HTMLInputElement | string, value?: string | boolean) {
|
|
437
|
+
let DOMUtilsContext = this;
|
|
426
438
|
if (typeof element === "string") {
|
|
427
|
-
element =
|
|
439
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
428
440
|
element
|
|
429
441
|
) as HTMLInputElement;
|
|
430
442
|
}
|
|
@@ -480,11 +492,14 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
480
492
|
propValue: T
|
|
481
493
|
): void;
|
|
482
494
|
prop(element: HTMLElement | string, propName: string, propValue?: any) {
|
|
495
|
+
let DOMUtilsContext = this;
|
|
483
496
|
if (element == null) {
|
|
484
497
|
return;
|
|
485
498
|
}
|
|
486
499
|
if (typeof element === "string") {
|
|
487
|
-
element =
|
|
500
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
501
|
+
element
|
|
502
|
+
) as HTMLElement;
|
|
488
503
|
}
|
|
489
504
|
if (propValue == null) {
|
|
490
505
|
return (element as any)[propName];
|
|
@@ -502,8 +517,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
502
517
|
* DOMUtils.removeAttr("a.xx","data-value")
|
|
503
518
|
* */
|
|
504
519
|
removeAttr(element: HTMLElement | string, attrName: string) {
|
|
520
|
+
let DOMUtilsContext = this;
|
|
505
521
|
if (typeof element === "string") {
|
|
506
|
-
element =
|
|
522
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
523
|
+
element
|
|
524
|
+
) as HTMLElement;
|
|
507
525
|
}
|
|
508
526
|
if (element == null) {
|
|
509
527
|
return;
|
|
@@ -520,8 +538,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
520
538
|
* DOMUtils.removeClass("a.xx","xx")
|
|
521
539
|
*/
|
|
522
540
|
removeClass(element: HTMLElement | string, className: string) {
|
|
541
|
+
let DOMUtilsContext = this;
|
|
523
542
|
if (typeof element === "string") {
|
|
524
|
-
element =
|
|
543
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
544
|
+
element
|
|
545
|
+
) as HTMLElement;
|
|
525
546
|
}
|
|
526
547
|
if (element == null) {
|
|
527
548
|
return;
|
|
@@ -541,8 +562,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
541
562
|
* DOMUtils.removeProp("a.xx","href")
|
|
542
563
|
* */
|
|
543
564
|
removeProp(element: HTMLElement | string, propName: string) {
|
|
565
|
+
let DOMUtilsContext = this;
|
|
544
566
|
if (typeof element === "string") {
|
|
545
|
-
element =
|
|
567
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
568
|
+
element
|
|
569
|
+
) as HTMLElement;
|
|
546
570
|
}
|
|
547
571
|
if (element == null) {
|
|
548
572
|
return;
|
|
@@ -564,7 +588,9 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
564
588
|
) {
|
|
565
589
|
let DOMUtilsContext = this;
|
|
566
590
|
if (typeof element === "string") {
|
|
567
|
-
element =
|
|
591
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
592
|
+
element
|
|
593
|
+
) as HTMLElement;
|
|
568
594
|
}
|
|
569
595
|
if (element == null) {
|
|
570
596
|
return;
|
|
@@ -590,8 +616,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
590
616
|
* DOMUtils.addClass("a.xx","_vue_")
|
|
591
617
|
* */
|
|
592
618
|
addClass(element: HTMLElement | string, className: string) {
|
|
619
|
+
let DOMUtilsContext = this;
|
|
593
620
|
if (typeof element === "string") {
|
|
594
|
-
element =
|
|
621
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
622
|
+
element
|
|
623
|
+
) as HTMLElement;
|
|
595
624
|
}
|
|
596
625
|
if (element == null) {
|
|
597
626
|
return;
|
|
@@ -615,8 +644,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
615
644
|
| (HTMLElement | string | Element)[]
|
|
616
645
|
| NodeList
|
|
617
646
|
) {
|
|
647
|
+
let DOMUtilsContext = this;
|
|
618
648
|
if (typeof element === "string") {
|
|
619
|
-
element =
|
|
649
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
650
|
+
element
|
|
651
|
+
) as HTMLElement;
|
|
620
652
|
}
|
|
621
653
|
if (element == null) {
|
|
622
654
|
return;
|
|
@@ -630,7 +662,8 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
630
662
|
}
|
|
631
663
|
if (Array.isArray(content) || content instanceof NodeList) {
|
|
632
664
|
/* 数组 */
|
|
633
|
-
let fragment =
|
|
665
|
+
let fragment =
|
|
666
|
+
DOMUtilsContext.windowApi.document.createDocumentFragment();
|
|
634
667
|
content.forEach((ele) => {
|
|
635
668
|
if (typeof ele === "string") {
|
|
636
669
|
ele = this.parseHTML(ele, true, false);
|
|
@@ -652,8 +685,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
652
685
|
* DOMUtils.prepend("a.xx","'<b class="xx"></b>")
|
|
653
686
|
* */
|
|
654
687
|
prepend(element: HTMLElement | string, content: HTMLElement | string) {
|
|
688
|
+
let DOMUtilsContext = this;
|
|
655
689
|
if (typeof element === "string") {
|
|
656
|
-
element =
|
|
690
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
691
|
+
element
|
|
692
|
+
) as HTMLElement;
|
|
657
693
|
}
|
|
658
694
|
if (element == null) {
|
|
659
695
|
return;
|
|
@@ -674,8 +710,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
674
710
|
* DOMUtils.after("a.xx","'<b class="xx"></b>")
|
|
675
711
|
* */
|
|
676
712
|
after(element: HTMLElement | string, content: HTMLElement | string) {
|
|
713
|
+
let DOMUtilsContext = this;
|
|
677
714
|
if (typeof element === "string") {
|
|
678
|
-
element =
|
|
715
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
716
|
+
element
|
|
717
|
+
) as HTMLElement;
|
|
679
718
|
}
|
|
680
719
|
if (element == null) {
|
|
681
720
|
return;
|
|
@@ -696,8 +735,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
696
735
|
* DOMUtils.before("a.xx","'<b class="xx"></b>")
|
|
697
736
|
* */
|
|
698
737
|
before(element: HTMLElement | string, content: HTMLElement | string) {
|
|
738
|
+
let DOMUtilsContext = this;
|
|
699
739
|
if (typeof element === "string") {
|
|
700
|
-
element =
|
|
740
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
741
|
+
element
|
|
742
|
+
) as HTMLElement;
|
|
701
743
|
}
|
|
702
744
|
if (element == null) {
|
|
703
745
|
return;
|
|
@@ -720,7 +762,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
720
762
|
remove(target: HTMLElement | string | NodeList | HTMLElement[]) {
|
|
721
763
|
let DOMUtilsContext = this;
|
|
722
764
|
if (typeof target === "string") {
|
|
723
|
-
target =
|
|
765
|
+
target = DOMUtilsContext.windowApi.document.querySelectorAll(
|
|
724
766
|
target
|
|
725
767
|
) as NodeListOf<HTMLElement>;
|
|
726
768
|
}
|
|
@@ -745,8 +787,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
745
787
|
* DOMUtils.empty("a.xx")
|
|
746
788
|
* */
|
|
747
789
|
empty(element: HTMLElement | string) {
|
|
790
|
+
let DOMUtilsContext = this;
|
|
748
791
|
if (typeof element === "string") {
|
|
749
|
-
element =
|
|
792
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
793
|
+
element
|
|
794
|
+
) as HTMLElement;
|
|
750
795
|
}
|
|
751
796
|
if (element == null) {
|
|
752
797
|
return;
|
|
@@ -763,8 +808,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
763
808
|
* > 0
|
|
764
809
|
*/
|
|
765
810
|
offset(element: HTMLElement | string) {
|
|
811
|
+
let DOMUtilsContext = this;
|
|
766
812
|
if (typeof element === "string") {
|
|
767
|
-
element =
|
|
813
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
814
|
+
element
|
|
815
|
+
) as HTMLElement;
|
|
768
816
|
}
|
|
769
817
|
if (element == null) {
|
|
770
818
|
return;
|
|
@@ -772,9 +820,9 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
772
820
|
let rect = element.getBoundingClientRect();
|
|
773
821
|
return {
|
|
774
822
|
/** y轴偏移 */
|
|
775
|
-
top: rect.top +
|
|
823
|
+
top: rect.top + DOMUtilsContext.windowApi.globalThis.scrollY,
|
|
776
824
|
/** x轴偏移 */
|
|
777
|
-
left: rect.left +
|
|
825
|
+
left: rect.left + DOMUtilsContext.windowApi.globalThis.scrollX,
|
|
778
826
|
};
|
|
779
827
|
}
|
|
780
828
|
/**
|
|
@@ -805,13 +853,16 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
805
853
|
) {
|
|
806
854
|
let DOMUtilsContext = this;
|
|
807
855
|
if (typeof element === "string") {
|
|
808
|
-
element =
|
|
856
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
857
|
+
element
|
|
858
|
+
) as HTMLElement;
|
|
809
859
|
}
|
|
810
860
|
if (element == null) {
|
|
811
861
|
return;
|
|
812
862
|
}
|
|
813
863
|
if (DOMUtilsCommonUtils.isWin(element)) {
|
|
814
|
-
return
|
|
864
|
+
return DOMUtilsContext.windowApi.window.document.documentElement
|
|
865
|
+
.clientWidth;
|
|
815
866
|
}
|
|
816
867
|
if ((element as HTMLElement).nodeType === 9) {
|
|
817
868
|
/* Document文档节点 */
|
|
@@ -907,10 +958,13 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
907
958
|
) {
|
|
908
959
|
let DOMUtilsContext = this;
|
|
909
960
|
if (DOMUtilsCommonUtils.isWin(element)) {
|
|
910
|
-
return
|
|
961
|
+
return DOMUtilsContext.windowApi.window.document.documentElement
|
|
962
|
+
.clientHeight;
|
|
911
963
|
}
|
|
912
964
|
if (typeof element === "string") {
|
|
913
|
-
element =
|
|
965
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
966
|
+
element
|
|
967
|
+
) as HTMLElement;
|
|
914
968
|
}
|
|
915
969
|
if (element == null) {
|
|
916
970
|
// @ts-ignore
|
|
@@ -1005,10 +1059,12 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1005
1059
|
) {
|
|
1006
1060
|
let DOMUtilsContext = this;
|
|
1007
1061
|
if (DOMUtilsCommonUtils.isWin(element)) {
|
|
1008
|
-
return
|
|
1062
|
+
return DOMUtilsContext.windowApi.window.innerWidth;
|
|
1009
1063
|
}
|
|
1010
1064
|
if (typeof element === "string") {
|
|
1011
|
-
element =
|
|
1065
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1066
|
+
element
|
|
1067
|
+
) as HTMLElement;
|
|
1012
1068
|
}
|
|
1013
1069
|
if (element == null) {
|
|
1014
1070
|
// @ts-ignore
|
|
@@ -1048,10 +1104,12 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1048
1104
|
): number {
|
|
1049
1105
|
let DOMUtilsContext = this;
|
|
1050
1106
|
if (DOMUtilsCommonUtils.isWin(element)) {
|
|
1051
|
-
return
|
|
1107
|
+
return DOMUtilsContext.windowApi.window.innerHeight;
|
|
1052
1108
|
}
|
|
1053
1109
|
if (typeof element === "string") {
|
|
1054
|
-
element =
|
|
1110
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1111
|
+
element
|
|
1112
|
+
) as HTMLElement;
|
|
1055
1113
|
}
|
|
1056
1114
|
if (element == null) {
|
|
1057
1115
|
// @ts-ignore
|
|
@@ -1091,8 +1149,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1091
1149
|
duration: number = 1000,
|
|
1092
1150
|
callback: (() => void) | undefined | null = null
|
|
1093
1151
|
) {
|
|
1152
|
+
let DOMUtilsContext = this;
|
|
1094
1153
|
if (typeof element === "string") {
|
|
1095
|
-
element =
|
|
1154
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1155
|
+
element
|
|
1156
|
+
) as HTMLElement;
|
|
1096
1157
|
}
|
|
1097
1158
|
if (element == null) {
|
|
1098
1159
|
return;
|
|
@@ -1147,15 +1208,18 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1147
1208
|
* DOMUtils.wrap(document.querySelector("a.xx"),"<div></div>")
|
|
1148
1209
|
*/
|
|
1149
1210
|
wrap(element: HTMLElement | string | Node, wrapperHTML: string) {
|
|
1211
|
+
let DOMUtilsContext = this;
|
|
1150
1212
|
if (typeof element === "string") {
|
|
1151
|
-
element =
|
|
1213
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1214
|
+
element
|
|
1215
|
+
) as HTMLElement;
|
|
1152
1216
|
}
|
|
1153
1217
|
if (element == null) {
|
|
1154
1218
|
return;
|
|
1155
1219
|
}
|
|
1156
1220
|
element = element as HTMLElement;
|
|
1157
1221
|
// 创建一个新的div元素,并将wrapperHTML作为其innerHTML
|
|
1158
|
-
let wrapper =
|
|
1222
|
+
let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
|
|
1159
1223
|
wrapper.innerHTML = wrapperHTML;
|
|
1160
1224
|
|
|
1161
1225
|
let wrapperFirstChild = wrapper.firstChild as HTMLElement;
|
|
@@ -1180,8 +1244,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1180
1244
|
*/
|
|
1181
1245
|
prev(element: HTMLElement | string): HTMLElement;
|
|
1182
1246
|
prev(element: HTMLElement | string) {
|
|
1247
|
+
let DOMUtilsContext = this;
|
|
1183
1248
|
if (typeof element === "string") {
|
|
1184
|
-
element =
|
|
1249
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1250
|
+
element
|
|
1251
|
+
) as HTMLElement;
|
|
1185
1252
|
}
|
|
1186
1253
|
if (element == null) {
|
|
1187
1254
|
return;
|
|
@@ -1201,8 +1268,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1201
1268
|
*/
|
|
1202
1269
|
next(element: HTMLElement | string): HTMLElement;
|
|
1203
1270
|
next(element: HTMLElement | string) {
|
|
1271
|
+
let DOMUtilsContext = this;
|
|
1204
1272
|
if (typeof element === "string") {
|
|
1205
|
-
element =
|
|
1273
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1274
|
+
element
|
|
1275
|
+
) as HTMLElement;
|
|
1206
1276
|
}
|
|
1207
1277
|
if (element == null) {
|
|
1208
1278
|
return;
|
|
@@ -1215,10 +1285,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1215
1285
|
* let DOMUtils = window.DOMUtils.noConflict()
|
|
1216
1286
|
*/
|
|
1217
1287
|
noConflict() {
|
|
1218
|
-
|
|
1288
|
+
let DOMUtilsContext = this;
|
|
1289
|
+
if ((DOMUtilsContext.windowApi.window as any).DOMUtils) {
|
|
1219
1290
|
DOMUtilsCommonUtils.delete(window, "DOMUtils");
|
|
1220
1291
|
}
|
|
1221
|
-
(
|
|
1292
|
+
(DOMUtilsContext.windowApi.window as any).DOMUtils = this;
|
|
1222
1293
|
return this;
|
|
1223
1294
|
}
|
|
1224
1295
|
/**
|
|
@@ -1233,8 +1304,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1233
1304
|
*/
|
|
1234
1305
|
siblings(element: HTMLElement | string): HTMLElement[];
|
|
1235
1306
|
siblings(element: HTMLElement | string) {
|
|
1307
|
+
let DOMUtilsContext = this;
|
|
1236
1308
|
if (typeof element === "string") {
|
|
1237
|
-
element =
|
|
1309
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1310
|
+
element
|
|
1311
|
+
) as HTMLElement;
|
|
1238
1312
|
}
|
|
1239
1313
|
if (element == null) {
|
|
1240
1314
|
return;
|
|
@@ -1279,7 +1353,9 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1279
1353
|
parent(element: HTMLElement | NodeList | string | HTMLElement[]) {
|
|
1280
1354
|
let DOMUtilsContext = this;
|
|
1281
1355
|
if (typeof element === "string") {
|
|
1282
|
-
element =
|
|
1356
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1357
|
+
element
|
|
1358
|
+
) as HTMLElement;
|
|
1283
1359
|
}
|
|
1284
1360
|
if (element == null) {
|
|
1285
1361
|
return;
|
|
@@ -1329,6 +1405,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1329
1405
|
isComplete?: T2
|
|
1330
1406
|
): ParseHTMLReturnType<T1, T2>;
|
|
1331
1407
|
parseHTML(html: string, useParser = false, isComplete = false) {
|
|
1408
|
+
let DOMUtilsContext = this;
|
|
1332
1409
|
function parseHTMLByDOMParser() {
|
|
1333
1410
|
let parser = new DOMParser();
|
|
1334
1411
|
if (isComplete) {
|
|
@@ -1338,7 +1415,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1338
1415
|
}
|
|
1339
1416
|
}
|
|
1340
1417
|
function parseHTMLByCreateDom() {
|
|
1341
|
-
let tempDIV =
|
|
1418
|
+
let tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
|
|
1342
1419
|
tempDIV.innerHTML = html;
|
|
1343
1420
|
if (isComplete) {
|
|
1344
1421
|
return tempDIV;
|
|
@@ -1367,7 +1444,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1367
1444
|
return;
|
|
1368
1445
|
}
|
|
1369
1446
|
if (typeof target === "string") {
|
|
1370
|
-
target =
|
|
1447
|
+
target = DOMUtilsContext.windowApi.document.querySelectorAll(target);
|
|
1371
1448
|
}
|
|
1372
1449
|
if (target instanceof NodeList || target instanceof Array) {
|
|
1373
1450
|
target = target as HTMLElement[];
|
|
@@ -1398,7 +1475,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1398
1475
|
return;
|
|
1399
1476
|
}
|
|
1400
1477
|
if (typeof target === "string") {
|
|
1401
|
-
target =
|
|
1478
|
+
target = DOMUtilsContext.windowApi.document.querySelectorAll(target);
|
|
1402
1479
|
}
|
|
1403
1480
|
if (target instanceof NodeList || target instanceof Array) {
|
|
1404
1481
|
target = target as HTMLElement[];
|
|
@@ -1436,8 +1513,11 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1436
1513
|
if (element == null) {
|
|
1437
1514
|
return;
|
|
1438
1515
|
}
|
|
1516
|
+
let DOMUtilsContext = this;
|
|
1439
1517
|
if (typeof element === "string") {
|
|
1440
|
-
element =
|
|
1518
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1519
|
+
element
|
|
1520
|
+
) as HTMLElement;
|
|
1441
1521
|
}
|
|
1442
1522
|
element = element as HTMLElement;
|
|
1443
1523
|
element.style.opacity = "0";
|
|
@@ -1450,15 +1530,15 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1450
1530
|
element = element as HTMLElement;
|
|
1451
1531
|
element.style.opacity = Math.min(progress / duration, 1).toString();
|
|
1452
1532
|
if (progress < duration) {
|
|
1453
|
-
|
|
1533
|
+
DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1454
1534
|
} else {
|
|
1455
1535
|
if (callback && typeof callback === "function") {
|
|
1456
1536
|
callback();
|
|
1457
1537
|
}
|
|
1458
|
-
|
|
1538
|
+
DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
|
|
1459
1539
|
}
|
|
1460
1540
|
}
|
|
1461
|
-
timer =
|
|
1541
|
+
timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1462
1542
|
}
|
|
1463
1543
|
/**
|
|
1464
1544
|
* 淡出元素
|
|
@@ -1479,11 +1559,14 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1479
1559
|
duration: number = 400,
|
|
1480
1560
|
callback?: () => void
|
|
1481
1561
|
) {
|
|
1562
|
+
let DOMUtilsContext = this;
|
|
1482
1563
|
if (element == null) {
|
|
1483
1564
|
return;
|
|
1484
1565
|
}
|
|
1485
1566
|
if (typeof element === "string") {
|
|
1486
|
-
element =
|
|
1567
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1568
|
+
element
|
|
1569
|
+
) as HTMLElement;
|
|
1487
1570
|
}
|
|
1488
1571
|
element = element as HTMLElement;
|
|
1489
1572
|
element.style.opacity = "1";
|
|
@@ -1495,16 +1578,16 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1495
1578
|
element = element as HTMLElement;
|
|
1496
1579
|
element.style.opacity = Math.max(1 - progress / duration, 0).toString();
|
|
1497
1580
|
if (progress < duration) {
|
|
1498
|
-
|
|
1581
|
+
DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1499
1582
|
} else {
|
|
1500
1583
|
element.style.display = "none";
|
|
1501
1584
|
if (typeof callback === "function") {
|
|
1502
1585
|
callback();
|
|
1503
1586
|
}
|
|
1504
|
-
|
|
1587
|
+
DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
|
|
1505
1588
|
}
|
|
1506
1589
|
}
|
|
1507
|
-
timer =
|
|
1590
|
+
timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
|
|
1508
1591
|
}
|
|
1509
1592
|
/**
|
|
1510
1593
|
* 切换元素的显示和隐藏状态
|
|
@@ -1517,7 +1600,9 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1517
1600
|
toggle(element: HTMLElement | string) {
|
|
1518
1601
|
let DOMUtilsContext = this;
|
|
1519
1602
|
if (typeof element === "string") {
|
|
1520
|
-
element =
|
|
1603
|
+
element = DOMUtilsContext.windowApi.document.querySelector(
|
|
1604
|
+
element
|
|
1605
|
+
) as HTMLElement;
|
|
1521
1606
|
}
|
|
1522
1607
|
if (element == null) {
|
|
1523
1608
|
return;
|
|
@@ -1533,7 +1618,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1533
1618
|
* @param option
|
|
1534
1619
|
* @returns
|
|
1535
1620
|
*/
|
|
1536
|
-
createDOMUtils(option?:
|
|
1621
|
+
createDOMUtils(option?: UtilsWindowApiOption) {
|
|
1537
1622
|
return new DOMUtils(option);
|
|
1538
1623
|
}
|
|
1539
1624
|
/**
|
|
@@ -1549,6 +1634,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1549
1634
|
selectionStart?: number | string,
|
|
1550
1635
|
selectionEnd?: number | string
|
|
1551
1636
|
): DOMRect {
|
|
1637
|
+
let DOMUtilsContext = this;
|
|
1552
1638
|
// Basic parameter validation
|
|
1553
1639
|
if (!$input || !("value" in $input)) return $input;
|
|
1554
1640
|
if (selectionStart == null) {
|
|
@@ -1625,7 +1711,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1625
1711
|
// 不能为空,不然获取不到高度
|
|
1626
1712
|
let text = $input.value || "G",
|
|
1627
1713
|
textLen = text.length,
|
|
1628
|
-
fakeClone =
|
|
1714
|
+
fakeClone = DOMUtilsContext.windowApi.document.createElement("div");
|
|
1629
1715
|
if (selectionStart > 0) appendPart(0, selectionStart);
|
|
1630
1716
|
var fakeRange = appendPart(selectionStart, selectionEnd);
|
|
1631
1717
|
if (textLen > selectionEnd) appendPart(selectionEnd, textLen);
|
|
@@ -1639,7 +1725,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1639
1725
|
fakeClone.style.left = leftPos + "px";
|
|
1640
1726
|
fakeClone.style.width = width + "px";
|
|
1641
1727
|
fakeClone.style.height = height + "px";
|
|
1642
|
-
|
|
1728
|
+
DOMUtilsContext.windowApi.document.body.appendChild(fakeClone);
|
|
1643
1729
|
var returnValue = fakeRange.getBoundingClientRect(); //Get rect
|
|
1644
1730
|
|
|
1645
1731
|
fakeClone?.parentNode?.removeChild(fakeClone); //Remove temp
|
|
@@ -1653,7 +1739,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1653
1739
|
* @returns
|
|
1654
1740
|
*/
|
|
1655
1741
|
function appendPart(start: number, end: number) {
|
|
1656
|
-
var span =
|
|
1742
|
+
var span = DOMUtilsContext.windowApi.document.createElement("span");
|
|
1657
1743
|
span.style.cssText = cssDefaultStyles; //Force styles to prevent unexpected results
|
|
1658
1744
|
span.textContent = text.substring(start, end);
|
|
1659
1745
|
fakeClone.appendChild(span);
|
|
@@ -1661,10 +1747,10 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1661
1747
|
}
|
|
1662
1748
|
// Computing offset position
|
|
1663
1749
|
function getInputOffset() {
|
|
1664
|
-
let body =
|
|
1665
|
-
win =
|
|
1666
|
-
docElem =
|
|
1667
|
-
$box =
|
|
1750
|
+
let body = DOMUtilsContext.windowApi.document.body,
|
|
1751
|
+
win = DOMUtilsContext.windowApi.document.defaultView!,
|
|
1752
|
+
docElem = DOMUtilsContext.windowApi.document.documentElement,
|
|
1753
|
+
$box = DOMUtilsContext.windowApi.document.createElement("div");
|
|
1668
1754
|
$box.style.paddingLeft = $box.style.width = "1px";
|
|
1669
1755
|
body.appendChild($box);
|
|
1670
1756
|
var isBoxModel = $box.offsetWidth == 2;
|
|
@@ -1692,7 +1778,7 @@ class DOMUtils extends DOMUtilsEvent {
|
|
|
1692
1778
|
* @returns
|
|
1693
1779
|
*/
|
|
1694
1780
|
function getInputCSS(prop: string, isNumber: boolean) {
|
|
1695
|
-
var val =
|
|
1781
|
+
var val = DOMUtilsContext.windowApi.document
|
|
1696
1782
|
.defaultView!.getComputedStyle($input, null)
|
|
1697
1783
|
.getPropertyValue(prop);
|
|
1698
1784
|
return isNumber ? parseFloat(val) : val;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UtilsWindowApiOption } from "./WindowApi";
|
|
2
2
|
|
|
3
3
|
/** 通用工具类 */
|
|
4
4
|
const DOMUtilsCommonUtils = {
|
|
5
|
+
windowApi: {
|
|
6
|
+
window: window,
|
|
7
|
+
document: document,
|
|
8
|
+
} as UtilsWindowApiOption,
|
|
5
9
|
/**
|
|
6
10
|
* 判断元素是否已显示或已连接
|
|
7
11
|
* @param element
|
|
@@ -19,7 +23,7 @@ const DOMUtilsCommonUtils = {
|
|
|
19
23
|
"style",
|
|
20
24
|
"visibility: hidden !important;display:block !important;"
|
|
21
25
|
);
|
|
22
|
-
|
|
26
|
+
this.windowApi.document.documentElement.appendChild(dupNode);
|
|
23
27
|
return {
|
|
24
28
|
/**
|
|
25
29
|
* 恢复修改的style
|
|
@@ -74,13 +78,13 @@ const DOMUtilsCommonUtils = {
|
|
|
74
78
|
if (target === self) {
|
|
75
79
|
return true;
|
|
76
80
|
}
|
|
77
|
-
if (target ===
|
|
81
|
+
if (target === globalThis) {
|
|
78
82
|
return true;
|
|
79
83
|
}
|
|
80
|
-
if (target ===
|
|
84
|
+
if (target === window) {
|
|
81
85
|
return true;
|
|
82
86
|
}
|
|
83
|
-
if (target ===
|
|
87
|
+
if (target === self) {
|
|
84
88
|
return true;
|
|
85
89
|
}
|
|
86
90
|
if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
|