@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.
@@ -1,7 +1,7 @@
1
1
  import { DOMUtilsCommonUtils } from "./DOMUtilsCommonUtils";
2
- import { DOMUtilsCore } from "./DOMUtilsCore";
3
2
  import { DOMUtilsData } from "./DOMUtilsData";
4
3
  import { OriginPrototype } from "./DOMUtilsOriginPrototype";
4
+ import { UtilsWindowApiOption, WindowApi } from "./WindowApi";
5
5
 
6
6
  export type DOMUtilsEventObject<T extends Node> = Event & {
7
7
  target: T;
@@ -242,7 +242,11 @@ export declare type DOMUtilsElementEventType =
242
242
  | ChildNode
243
243
  | Node;
244
244
 
245
- class DOMUtilsEvent {
245
+ export class DOMUtilsEvent {
246
+ windowApi: UtilsWindowApiOption;
247
+ constructor(windowApiOption?: UtilsWindowApiOption) {
248
+ this.windowApi = new WindowApi(windowApiOption);
249
+ }
246
250
  /**
247
251
  * 绑定事件
248
252
  * @param element 需要绑定的元素|元素数组|window
@@ -404,7 +408,7 @@ class DOMUtilsEvent {
404
408
  let DOMUtilsContext = this;
405
409
  let args = arguments;
406
410
  if (typeof element === "string") {
407
- element = DOMUtilsCore.document.querySelectorAll(element);
411
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
408
412
  }
409
413
  if (element == null) {
410
414
  return;
@@ -459,7 +463,7 @@ class DOMUtilsEvent {
459
463
  if (_selector_) {
460
464
  /* 存在自定义子元素选择器 */
461
465
  let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
462
- ? DOMUtilsCore.document.documentElement
466
+ ? DOMUtilsContext.windowApi.document.documentElement
463
467
  : elementItem;
464
468
  if (target.matches(_selector_)) {
465
469
  /* 当前目标可以被selector所匹配到 */
@@ -658,10 +662,10 @@ class DOMUtilsEvent {
658
662
  }
659
663
  return option;
660
664
  }
661
-
665
+ let DOMUtilsContext = this;
662
666
  let args = arguments;
663
667
  if (typeof element === "string") {
664
- element = DOMUtilsCore.document.querySelectorAll(element);
668
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
665
669
  }
666
670
  if (element == null) {
667
671
  return;
@@ -768,8 +772,9 @@ class DOMUtilsEvent {
768
772
  element: DOMUtilsElementEventType,
769
773
  eventType?: DOMUtils_EventType | DOMUtils_EventType[] | string
770
774
  ) {
775
+ let DOMUtilsContext = this;
771
776
  if (typeof element === "string") {
772
- element = DOMUtilsCore.document.querySelectorAll(element);
777
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
773
778
  }
774
779
  if (element == null) {
775
780
  return;
@@ -827,15 +832,17 @@ class DOMUtilsEvent {
827
832
  if (typeof callback !== "function") {
828
833
  return;
829
834
  }
835
+ let DOMUtilsContext = this;
830
836
  /**
831
837
  * 检测文档是否加载完毕
832
838
  */
833
839
  function checkDOMReadyState() {
834
840
  try {
835
841
  if (
836
- DOMUtilsCore.document.readyState === "complete" ||
837
- (DOMUtilsCore.document.readyState !== "loading" &&
838
- !(DOMUtilsCore.document.documentElement as any).doScroll)
842
+ DOMUtilsContext.windowApi.document.readyState === "complete" ||
843
+ (DOMUtilsContext.windowApi.document.readyState !== "loading" &&
844
+ !(DOMUtilsContext.windowApi.document.documentElement as any)
845
+ .doScroll)
839
846
  ) {
840
847
  return true;
841
848
  } else {
@@ -855,12 +862,12 @@ class DOMUtilsEvent {
855
862
 
856
863
  let targetList = [
857
864
  {
858
- target: DOMUtilsCore.document,
865
+ target: DOMUtilsContext.windowApi.document,
859
866
  eventType: "DOMContentLoaded",
860
867
  callback: completed,
861
868
  },
862
869
  {
863
- target: DOMUtilsCore.window,
870
+ target: DOMUtilsContext.windowApi.window,
864
871
  eventType: "load",
865
872
  callback: completed,
866
873
  },
@@ -951,8 +958,11 @@ class DOMUtilsEvent {
951
958
  details?: object,
952
959
  useDispatchToTriggerEvent: boolean = true
953
960
  ) {
961
+ let DOMUtilsContext = this;
954
962
  if (typeof element === "string") {
955
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
963
+ element = DOMUtilsContext.windowApi.document.querySelector(
964
+ element
965
+ ) as HTMLElement;
956
966
  }
957
967
  if (element == null) {
958
968
  return;
@@ -1019,7 +1029,9 @@ class DOMUtilsEvent {
1019
1029
  ) {
1020
1030
  let DOMUtilsContext = this;
1021
1031
  if (typeof element === "string") {
1022
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1032
+ element = DOMUtilsContext.windowApi.document.querySelector(
1033
+ element
1034
+ ) as HTMLElement;
1023
1035
  }
1024
1036
  if (element == null) {
1025
1037
  return;
@@ -1057,7 +1069,9 @@ class DOMUtilsEvent {
1057
1069
  ) {
1058
1070
  let DOMUtilsContext = this;
1059
1071
  if (typeof element === "string") {
1060
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1072
+ element = DOMUtilsContext.windowApi.document.querySelector(
1073
+ element
1074
+ ) as HTMLElement;
1061
1075
  }
1062
1076
  if (element == null) {
1063
1077
  return;
@@ -1100,7 +1114,9 @@ class DOMUtilsEvent {
1100
1114
  ) {
1101
1115
  let DOMUtilsContext = this;
1102
1116
  if (typeof element === "string") {
1103
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1117
+ element = DOMUtilsContext.windowApi.document.querySelector(
1118
+ element
1119
+ ) as HTMLElement;
1104
1120
  }
1105
1121
  if (element == null) {
1106
1122
  return;
@@ -1137,7 +1153,9 @@ class DOMUtilsEvent {
1137
1153
  ) {
1138
1154
  let DOMUtilsContext = this;
1139
1155
  if (typeof element === "string") {
1140
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1156
+ element = DOMUtilsContext.windowApi.document.querySelector(
1157
+ element
1158
+ ) as HTMLElement;
1141
1159
  }
1142
1160
  if (element == null) {
1143
1161
  return;
@@ -1170,7 +1188,9 @@ class DOMUtilsEvent {
1170
1188
  return;
1171
1189
  }
1172
1190
  if (typeof target === "string") {
1173
- target = DOMUtilsCore.document.querySelector(target) as HTMLElement;
1191
+ target = DOMUtilsContext.windowApi.document.querySelector(
1192
+ target
1193
+ ) as HTMLElement;
1174
1194
  }
1175
1195
  DOMUtilsContext.on(target, "keyup", null, handler, option);
1176
1196
  }
@@ -1199,7 +1219,9 @@ class DOMUtilsEvent {
1199
1219
  return;
1200
1220
  }
1201
1221
  if (typeof target === "string") {
1202
- target = DOMUtilsCore.document.querySelector(target) as HTMLElement;
1222
+ target = DOMUtilsContext.windowApi.document.querySelector(
1223
+ target
1224
+ ) as HTMLElement;
1203
1225
  }
1204
1226
  DOMUtilsContext.on(target, "keydown", null, handler, option);
1205
1227
  }
@@ -1228,10 +1250,10 @@ class DOMUtilsEvent {
1228
1250
  return;
1229
1251
  }
1230
1252
  if (typeof target === "string") {
1231
- target = DOMUtilsCore.document.querySelector(target) as HTMLElement;
1253
+ target = DOMUtilsContext.windowApi.document.querySelector(
1254
+ target
1255
+ ) as HTMLElement;
1232
1256
  }
1233
1257
  DOMUtilsContext.on(target, "keypress", null, handler, option);
1234
1258
  }
1235
1259
  }
1236
-
1237
- export { DOMUtilsEvent };
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 配置类型
3
+ */
4
+ export type UtilsWindowApiOption = {
5
+ document: Document;
6
+ window: Window & typeof globalThis;
7
+ globalThis: typeof globalThis | Window;
8
+ self: Window & typeof globalThis;
9
+ top: Window;
10
+ };
11
+
12
+ export class WindowApi {
13
+ /** 默认的配置 */
14
+ private defaultApi: UtilsWindowApiOption = {
15
+ document: document,
16
+ window: window,
17
+ globalThis: globalThis,
18
+ self: self,
19
+ top: top!,
20
+ };
21
+ /** 使用的配置 */
22
+ private api: UtilsWindowApiOption;
23
+ constructor(option?: UtilsWindowApiOption) {
24
+ if (!option) {
25
+ option = Object.assign({}, this.defaultApi);
26
+ }
27
+ this.api = Object.assign({}, option);
28
+ }
29
+ get document() {
30
+ return this.api.document;
31
+ }
32
+ get window() {
33
+ return this.api.window;
34
+ }
35
+ get globalThis() {
36
+ return this.api.globalThis;
37
+ }
38
+ get self() {
39
+ return this.api.self;
40
+ }
41
+ get top() {
42
+ return this.api.top;
43
+ }
44
+ }
@@ -1,15 +0,0 @@
1
- export interface DOMUtilsCoreOption {
2
- document: Document;
3
- window: Window | typeof globalThis;
4
- globalThis: Window | typeof globalThis;
5
- self: Window | typeof globalThis;
6
- top: Window | typeof globalThis;
7
- }
8
- declare const DOMUtilsCore: {
9
- init(option?: DOMUtilsCoreOption): void;
10
- readonly document: Document;
11
- readonly window: typeof globalThis | Window;
12
- readonly globalThis: typeof globalThis | Window;
13
- readonly self: typeof globalThis | Window;
14
- };
15
- export { DOMUtilsCore };
@@ -1,45 +0,0 @@
1
- export interface DOMUtilsCoreOption {
2
- document: Document;
3
- window: Window | typeof globalThis;
4
- globalThis: Window | typeof globalThis;
5
- self: Window | typeof globalThis;
6
- top: Window | typeof globalThis;
7
- }
8
-
9
- const DOMUtilsCoreDefaultEnv: DOMUtilsCoreOption = {
10
- document: document,
11
- window: window,
12
- globalThis: globalThis,
13
- self: self,
14
- top: top!,
15
- };
16
- const DOMUtilsCoreEnv: DOMUtilsCoreOption = {
17
- document: document,
18
- window: window,
19
- globalThis: globalThis,
20
- self: self,
21
- top: top!,
22
- };
23
-
24
- const DOMUtilsCore = {
25
- init(option?: DOMUtilsCoreOption) {
26
- if (!option) {
27
- option = Object.assign({}, DOMUtilsCoreDefaultEnv);
28
- }
29
- Object.assign(DOMUtilsCoreEnv, option);
30
- },
31
- get document() {
32
- return DOMUtilsCoreEnv.document;
33
- },
34
- get window() {
35
- return DOMUtilsCoreEnv.window;
36
- },
37
- get globalThis() {
38
- return DOMUtilsCoreEnv.globalThis;
39
- },
40
- get self() {
41
- return DOMUtilsCoreEnv.self;
42
- },
43
- };
44
-
45
- export { DOMUtilsCore };