@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.
Files changed (55) hide show
  1. package/dist/index.amd.js +1928 -1047
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.amd.min.js +2 -0
  4. package/dist/index.amd.min.js.map +1 -0
  5. package/dist/index.cjs.js +1928 -1047
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/index.cjs.min.js +2 -0
  8. package/dist/index.cjs.min.js.map +1 -0
  9. package/dist/index.esm.js +1928 -1047
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.esm.min.js +2 -0
  12. package/dist/index.esm.min.js.map +1 -0
  13. package/dist/index.iife.js +1928 -1047
  14. package/dist/index.iife.js.map +1 -1
  15. package/dist/index.iife.min.js +2 -0
  16. package/dist/index.iife.min.js.map +1 -0
  17. package/dist/index.system.js +1928 -1047
  18. package/dist/index.system.js.map +1 -1
  19. package/dist/index.system.min.js +2 -0
  20. package/dist/index.system.min.js.map +1 -0
  21. package/dist/index.umd.js +1928 -1047
  22. package/dist/index.umd.js.map +1 -1
  23. package/dist/index.umd.min.js +2 -0
  24. package/dist/index.umd.min.js.map +1 -0
  25. package/dist/types/index.d.ts +1 -1
  26. package/dist/types/src/{DOMUtilsCommonUtils.d.ts → CommonUtils.d.ts} +20 -8
  27. package/dist/types/src/ElementAnimate.d.ts +89 -0
  28. package/dist/types/src/{DOMUtilsEvent.d.ts → ElementEvent.d.ts} +19 -84
  29. package/dist/types/src/ElementHandler.d.ts +17 -0
  30. package/dist/types/src/ElementSelector.d.ts +96 -0
  31. package/dist/types/src/ElementWait.d.ts +278 -0
  32. package/dist/types/src/GlobalData.d.ts +4 -0
  33. package/dist/types/src/{DOMUtilsOriginPrototype.d.ts → OriginPrototype.d.ts} +1 -2
  34. package/dist/types/src/Utils.d.ts +68 -0
  35. package/dist/types/src/{DOMUtils.d.ts → index.d.ts} +157 -177
  36. package/dist/types/src/types/env.d.ts +9 -0
  37. package/dist/types/src/types/global.d.ts +0 -2
  38. package/dist/types/src/types/gm.d.ts +0 -4
  39. package/index.ts +1 -1
  40. package/package.json +6 -2
  41. package/src/{DOMUtilsCommonUtils.ts → CommonUtils.ts} +25 -11
  42. package/src/ElementAnimate.ts +290 -0
  43. package/src/{DOMUtilsEvent.ts → ElementEvent.ts} +166 -361
  44. package/src/ElementHandler.ts +43 -0
  45. package/src/ElementSelector.ts +260 -0
  46. package/src/ElementWait.ts +699 -0
  47. package/src/GlobalData.ts +5 -0
  48. package/src/{DOMUtilsOriginPrototype.ts → OriginPrototype.ts} +1 -3
  49. package/src/Utils.ts +386 -0
  50. package/src/{DOMUtils.ts → index.ts} +678 -757
  51. package/src/types/env.d.ts +9 -0
  52. package/src/types/global.d.ts +0 -2
  53. package/src/types/gm.d.ts +0 -4
  54. package/dist/types/src/DOMUtilsData.d.ts +0 -5
  55. package/src/DOMUtilsData.ts +0 -7
package/dist/index.umd.js CHANGED
@@ -308,7 +308,7 @@
308
308
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
309
309
 
310
310
  /** 通用工具类 */
311
- const DOMUtilsCommonUtils = {
311
+ const CommonUtils = {
312
312
  windowApi: new WindowApi({
313
313
  document: document,
314
314
  window: window,
@@ -320,15 +320,16 @@
320
320
  }),
321
321
  /**
322
322
  * 判断元素是否已显示或已连接
323
- * @param element
323
+ * @param $el
324
324
  */
325
- isShow(element) {
326
- return Boolean(element.getClientRects().length);
325
+ isShow($el) {
326
+ return Boolean($el.getClientRects().length);
327
327
  },
328
328
  /**
329
- * 获取安全的html
329
+ * 创建安全的html
330
+ * @param text 字符串
330
331
  */
331
- getSafeHTML(text) {
332
+ createSafeHTML(text) {
332
333
  if (window.trustedTypes) {
333
334
  const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
334
335
  createHTML: (html) => html,
@@ -346,14 +347,14 @@
346
347
  */
347
348
  setSafeHTML($el, text) {
348
349
  // 创建 TrustedHTML 策略(需 CSP 允许)
349
- $el.innerHTML = this.getSafeHTML(text);
350
+ $el.innerHTML = this.createSafeHTML(text);
350
351
  },
351
352
  /**
352
- * 用于显示元素并获取它的高度宽度等其它属性
353
- * @param element
353
+ * 用于强制显示元素并获取它的高度宽度等其它属性
354
+ * @param $el
354
355
  */
355
- showElement(element) {
356
- const dupNode = element.cloneNode(true);
356
+ forceShow($el) {
357
+ const dupNode = $el.cloneNode(true);
357
358
  dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
358
359
  this.windowApi.document.documentElement.appendChild(dupNode);
359
360
  return {
@@ -429,6 +430,19 @@
429
430
  }
430
431
  return true;
431
432
  },
433
+ /**
434
+ * 判断对象是否是元素
435
+ * @param $el
436
+ * @returns
437
+ * + true 是元素
438
+ * + false 不是元素
439
+ * @example
440
+ * DOMUtilsCommonUtils.isDOM(document.querySelector("a"))
441
+ * > true
442
+ */
443
+ isDOM($el) {
444
+ return $el instanceof Node;
445
+ },
432
446
  /**
433
447
  * 删除对象上的属性
434
448
  * @param target
@@ -452,72 +466,1159 @@
452
466
  catch {
453
467
  return this.windowApi.setTimeout(callback, timeout);
454
468
  }
455
- },
469
+ },
470
+ /**
471
+ * 配合 .setTimeout 使用
472
+ */
473
+ clearTimeout(timeId) {
474
+ try {
475
+ if (timeId != null) {
476
+ clearTimeout$1(timeId);
477
+ }
478
+ }
479
+ catch {
480
+ // TODO
481
+ }
482
+ finally {
483
+ this.windowApi.clearTimeout(timeId);
484
+ }
485
+ },
486
+ /**
487
+ * 自动使用 Worker 执行 setInterval
488
+ */
489
+ setInterval(callback, timeout = 0) {
490
+ try {
491
+ return setInterval$1(callback, timeout);
492
+ }
493
+ catch {
494
+ return this.windowApi.setInterval(callback, timeout);
495
+ }
496
+ },
497
+ /**
498
+ * 配合 .setInterval 使用
499
+ */
500
+ clearInterval(timeId) {
501
+ try {
502
+ if (timeId != null) {
503
+ clearInterval$1(timeId);
504
+ }
505
+ }
506
+ catch {
507
+ // TODO
508
+ }
509
+ finally {
510
+ this.windowApi.clearInterval(timeId);
511
+ }
512
+ },
513
+ /**
514
+ * 判断是否是元素列表
515
+ * @param $ele
516
+ */
517
+ isNodeList($ele) {
518
+ return Array.isArray($ele) || $ele instanceof NodeList;
519
+ },
520
+ /** 获取 animationend 在各个浏览器的兼容名 */
521
+ getAnimationEndNameList() {
522
+ return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
523
+ },
524
+ /** 获取 transitionend 在各个浏览器的兼容名 */
525
+ getTransitionEndNameList() {
526
+ return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
527
+ },
528
+ };
529
+
530
+ const version = "1.7.0";
531
+
532
+ /* 数据 */
533
+ const GlobalData = {
534
+ /** .on添加在元素存储的事件 */
535
+ domEventSymbol: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
536
+ };
537
+
538
+ class ElementSelector {
539
+ windowApi;
540
+ constructor(windowApiOption) {
541
+ this.windowApi = new WindowApi(windowApiOption);
542
+ }
543
+ selector(selector, parent) {
544
+ return this.selectorAll(selector, parent)[0];
545
+ }
546
+ selectorAll(selector, parent) {
547
+ const context = this;
548
+ parent = parent || context.windowApi.document;
549
+ selector = selector.trim();
550
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
551
+ // empty 语法
552
+ selector = selector.replace(/:empty$/gi, "");
553
+ return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
554
+ return $ele?.innerHTML?.trim() === "";
555
+ });
556
+ }
557
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
558
+ // contains 语法
559
+ const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
560
+ const text = textMatch[2];
561
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
562
+ return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
563
+ // @ts-ignore
564
+ return ($ele?.textContent || $ele?.innerText)?.includes(text);
565
+ });
566
+ }
567
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
568
+ // regexp 语法
569
+ const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
570
+ let pattern = textMatch[2];
571
+ const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
572
+ let flags = "";
573
+ if (flagMatch) {
574
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
575
+ flags = flagMatch[3];
576
+ }
577
+ const regexp = new RegExp(pattern, flags);
578
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
579
+ return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
580
+ // @ts-ignore
581
+ return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
582
+ });
583
+ }
584
+ else {
585
+ // 普通语法
586
+ return Array.from(parent.querySelectorAll(selector));
587
+ }
588
+ }
589
+ /**
590
+ * 匹配元素,可使用以下的额外语法
591
+ *
592
+ * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
593
+ * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
594
+ * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
595
+ * @param $el 元素
596
+ * @param selector 选择器
597
+ * @example
598
+ * DOMUtils.matches("div:contains('测试')")
599
+ * > true
600
+ * @example
601
+ * DOMUtils.matches("div:empty")
602
+ * > true
603
+ * @example
604
+ * DOMUtils.matches("div:regexp('^xxxx$')")
605
+ * > true
606
+ * @example
607
+ * DOMUtils.matches("div:regexp(/^xxx/ig)")
608
+ * > false
609
+ */
610
+ matches($el, selector) {
611
+ selector = selector.trim();
612
+ if ($el == null) {
613
+ return false;
614
+ }
615
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
616
+ // empty 语法
617
+ selector = selector.replace(/:empty$/gi, "");
618
+ return $el.matches(selector) && $el?.innerHTML?.trim() === "";
619
+ }
620
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
621
+ // contains 语法
622
+ const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
623
+ const text = textMatch[2];
624
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
625
+ // @ts-ignore
626
+ let content = $el?.textContent || $el?.innerText;
627
+ if (typeof content !== "string") {
628
+ content = "";
629
+ }
630
+ return $el.matches(selector) && content?.includes(text);
631
+ }
632
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
633
+ // regexp 语法
634
+ const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
635
+ let pattern = textMatch[2];
636
+ const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
637
+ let flags = "";
638
+ if (flagMatch) {
639
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
640
+ flags = flagMatch[3];
641
+ }
642
+ const regexp = new RegExp(pattern, flags);
643
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
644
+ // @ts-ignore
645
+ let content = $el?.textContent || $el?.innerText;
646
+ if (typeof content !== "string") {
647
+ content = "";
648
+ }
649
+ return $el.matches(selector) && Boolean(content?.match(regexp));
650
+ }
651
+ else {
652
+ // 普通语法
653
+ return $el.matches(selector);
654
+ }
655
+ }
656
+ closest($el, selector) {
657
+ selector = selector.trim();
658
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
659
+ // empty 语法
660
+ selector = selector.replace(/:empty$/gi, "");
661
+ const $closest = $el?.closest(selector);
662
+ if ($closest && $closest?.innerHTML?.trim() === "") {
663
+ return $closest;
664
+ }
665
+ return null;
666
+ }
667
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
668
+ // contains 语法
669
+ const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
670
+ const text = textMatch[2];
671
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
672
+ const $closest = $el?.closest(selector);
673
+ if ($closest) {
674
+ // @ts-ignore
675
+ const content = $el?.textContent || $el?.innerText;
676
+ if (typeof content === "string" && content.includes(text)) {
677
+ return $closest;
678
+ }
679
+ }
680
+ return null;
681
+ }
682
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
683
+ // regexp 语法
684
+ const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
685
+ let pattern = textMatch[2];
686
+ const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
687
+ let flags = "";
688
+ if (flagMatch) {
689
+ pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
690
+ flags = flagMatch[3];
691
+ }
692
+ const regexp = new RegExp(pattern, flags);
693
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
694
+ const $closest = $el?.closest(selector);
695
+ if ($closest) {
696
+ // @ts-ignore
697
+ const content = $el?.textContent || $el?.innerText;
698
+ if (typeof content === "string" && content.match(regexp)) {
699
+ return $closest;
700
+ }
701
+ }
702
+ return null;
703
+ }
704
+ else {
705
+ // 普通语法
706
+ const $closest = $el?.closest(selector);
707
+ return $closest;
708
+ }
709
+ }
710
+ }
711
+ const elementSelector = new ElementSelector();
712
+
713
+ /**
714
+ * 判断对象是否是元素
715
+ * @param $el
716
+ * @returns
717
+ * + true 是元素
718
+ * + false 不是元素
719
+ * @example
720
+ * DOMUtilsCommonUtils.isDOM(document.querySelector("a"))
721
+ * > true
722
+ */
723
+ const isDOM = function ($el) {
724
+ return $el instanceof Node;
725
+ };
726
+ class Utils {
727
+ windowApi;
728
+ constructor(option) {
729
+ this.windowApi = new WindowApi(option);
730
+ }
731
+ /**
732
+ * 判断对象是否是jQuery对象
733
+ * @param target
734
+ * @returns
735
+ * + true 是jQuery对象
736
+ * + false 不是jQuery对象
737
+ * @example
738
+ * Utils.isJQuery($("a"))
739
+ * > true
740
+ */
741
+ isJQuery(target) {
742
+ let result = false;
743
+ if (typeof jQuery === "object" && target instanceof jQuery) {
744
+ result = true;
745
+ }
746
+ if (target == null) {
747
+ return false;
748
+ }
749
+ if (typeof target === "object") {
750
+ /* 也有种可能,这个jQuery对象是1.8.3版本的,页面中的jQuery是3.4.1版本的 */
751
+ const jQueryProps = [
752
+ "add",
753
+ "addBack",
754
+ "addClass",
755
+ "after",
756
+ "ajaxComplete",
757
+ "ajaxError",
758
+ "ajaxSend",
759
+ "ajaxStart",
760
+ "ajaxStop",
761
+ "ajaxSuccess",
762
+ "animate",
763
+ "append",
764
+ "appendTo",
765
+ "attr",
766
+ "before",
767
+ "bind",
768
+ "blur",
769
+ "change",
770
+ "children",
771
+ "clearQueue",
772
+ "click",
773
+ "clone",
774
+ "closest",
775
+ "constructor",
776
+ "contents",
777
+ "contextmenu",
778
+ "css",
779
+ "data",
780
+ "dblclick",
781
+ "delay",
782
+ "delegate",
783
+ "dequeue",
784
+ "each",
785
+ "empty",
786
+ "end",
787
+ "eq",
788
+ "extend",
789
+ "fadeIn",
790
+ "fadeOut",
791
+ "fadeTo",
792
+ "fadeToggle",
793
+ "filter",
794
+ "find",
795
+ "first",
796
+ "focus",
797
+ "focusin",
798
+ "focusout",
799
+ "get",
800
+ "has",
801
+ "hasClass",
802
+ "height",
803
+ "hide",
804
+ "hover",
805
+ "html",
806
+ "index",
807
+ "init",
808
+ "innerHeight",
809
+ "innerWidth",
810
+ "insertAfter",
811
+ "insertBefore",
812
+ "is",
813
+ "jquery",
814
+ "keydown",
815
+ "keypress",
816
+ "keyup",
817
+ "last",
818
+ "load",
819
+ "map",
820
+ "mousedown",
821
+ "mouseenter",
822
+ "mouseleave",
823
+ "mousemove",
824
+ "mouseout",
825
+ "mouseover",
826
+ "mouseup",
827
+ "next",
828
+ "nextAll",
829
+ "not",
830
+ "off",
831
+ "offset",
832
+ "offsetParent",
833
+ "on",
834
+ "one",
835
+ "outerHeight",
836
+ "outerWidth",
837
+ "parent",
838
+ "parents",
839
+ "position",
840
+ "prepend",
841
+ "prependTo",
842
+ "prev",
843
+ "prevAll",
844
+ "prevUntil",
845
+ "promise",
846
+ "prop",
847
+ "pushStack",
848
+ "queue",
849
+ "ready",
850
+ "remove",
851
+ "removeAttr",
852
+ "removeClass",
853
+ "removeData",
854
+ "removeProp",
855
+ "replaceAll",
856
+ "replaceWith",
857
+ "resize",
858
+ "scroll",
859
+ "scrollLeft",
860
+ "scrollTop",
861
+ "select",
862
+ "show",
863
+ "siblings",
864
+ "slice",
865
+ "slideDown",
866
+ "slideToggle",
867
+ "slideUp",
868
+ "sort",
869
+ "splice",
870
+ "text",
871
+ "toArray",
872
+ "toggle",
873
+ "toggleClass",
874
+ "trigger",
875
+ "triggerHandler",
876
+ "unbind",
877
+ "width",
878
+ "wrap",
879
+ ];
880
+ for (const jQueryPropsName of jQueryProps) {
881
+ if (!(jQueryPropsName in target)) {
882
+ result = false;
883
+ break;
884
+ }
885
+ else {
886
+ result = true;
887
+ }
888
+ }
889
+ }
890
+ return result;
891
+ }
892
+ assign(target = {}, source = {}, isAdd = false) {
893
+ const UtilsContext = this;
894
+ if (Array.isArray(source)) {
895
+ const canTraverse = source.filter((item) => {
896
+ return typeof item === "object";
897
+ });
898
+ if (!canTraverse.length) {
899
+ return source;
900
+ }
901
+ }
902
+ if (source == null) {
903
+ return target;
904
+ }
905
+ if (target == null) {
906
+ target = {};
907
+ }
908
+ if (isAdd) {
909
+ for (const sourceKeyName in source) {
910
+ const targetKeyName = sourceKeyName;
911
+ const targetValue = Reflect.get(target, targetKeyName);
912
+ const sourceValue = Reflect.get(source, sourceKeyName);
913
+ if (typeof sourceValue === "object" && sourceValue != null && sourceKeyName in target && !isDOM(sourceValue)) {
914
+ /* 源端的值是object类型,且不是元素节点 */
915
+ Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
916
+ continue;
917
+ }
918
+ Reflect.set(target, sourceKeyName, sourceValue);
919
+ }
920
+ }
921
+ else {
922
+ for (const targetKeyName in target) {
923
+ if (targetKeyName in source) {
924
+ const targetValue = Reflect.get(target, targetKeyName);
925
+ const sourceValue = Reflect.get(source, targetKeyName);
926
+ if (typeof sourceValue === "object" &&
927
+ sourceValue != null &&
928
+ !isDOM(sourceValue) &&
929
+ Object.keys(sourceValue).length) {
930
+ /* 源端的值是object类型,且不是元素节点 */
931
+ Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
932
+ continue;
933
+ }
934
+ /* 直接赋值 */
935
+ Reflect.set(target, targetKeyName, sourceValue);
936
+ }
937
+ }
938
+ }
939
+ return target;
940
+ }
941
+ mutationObserver(target, observer_config) {
942
+ const that = this;
943
+ const default_obverser_config = {
944
+ /* 监听到元素有反馈,需执行的函数 */
945
+ callback: () => { },
946
+ config: {
947
+ /**
948
+ * + true 监听以 target 为根节点的整个子树。包括子树中所有节点的属性,而不仅仅是针对 target
949
+ * + false (默认) 不生效
950
+ */
951
+ subtree: void 0,
952
+ /**
953
+ * + true 监听 target 节点中发生的节点的新增与删除(同时,如果 subtree 为 true,会针对整个子树生效)
954
+ * + false (默认) 不生效
955
+ */
956
+ childList: void 0,
957
+ /**
958
+ * + true 观察所有监听的节点属性值的变化。默认值为 true,当声明了 attributeFilter 或 attributeOldValue
959
+ * + false (默认) 不生效
960
+ */
961
+ attributes: void 0,
962
+ /**
963
+ * 一个用于声明哪些属性名会被监听的数组。如果不声明该属性,所有属性的变化都将触发通知
964
+ */
965
+ attributeFilter: void 0,
966
+ /**
967
+ * + true 记录上一次被监听的节点的属性变化;可查阅 MutationObserver 中的 Monitoring attribute values 了解关于观察属性变化和属性值记录的详情
968
+ * + false (默认) 不生效
969
+ */
970
+ attributeOldValue: void 0,
971
+ /**
972
+ * + true 监听声明的 target 节点上所有字符的变化。默认值为 true,如果声明了 characterDataOldValue
973
+ * + false (默认) 不生效
974
+ */
975
+ characterData: void 0,
976
+ /**
977
+ * + true 记录前一个被监听的节点中发生的文本变化
978
+ * + false (默认) 不生效
979
+ */
980
+ characterDataOldValue: void 0,
981
+ },
982
+ immediate: false,
983
+ };
984
+ observer_config = that.assign(default_obverser_config, observer_config);
985
+ const windowMutationObserver = this.windowApi.window.MutationObserver ||
986
+ this.windowApi.window.webkitMutationObserver ||
987
+ this.windowApi.window.MozMutationObserver;
988
+ // 观察者对象
989
+ const mutationObserver = new windowMutationObserver(function (mutations, observer) {
990
+ if (typeof observer_config.callback === "function") {
991
+ observer_config.callback(mutations, observer);
992
+ }
993
+ });
994
+ if (Array.isArray(target) || target instanceof NodeList) {
995
+ // 传入的是数组或者元素数组
996
+ target.forEach((item) => {
997
+ mutationObserver.observe(item, observer_config.config);
998
+ });
999
+ }
1000
+ else if (that.isJQuery(target)) {
1001
+ /* 传入的参数是jQuery对象 */
1002
+ target.each((_, item) => {
1003
+ mutationObserver.observe(item, observer_config.config);
1004
+ });
1005
+ }
1006
+ else {
1007
+ mutationObserver.observe(target, observer_config.config);
1008
+ }
1009
+ if (observer_config.immediate) {
1010
+ /* 主动触发一次 */
1011
+ if (typeof observer_config.callback === "function") {
1012
+ observer_config.callback([], mutationObserver);
1013
+ }
1014
+ }
1015
+ return mutationObserver;
1016
+ }
1017
+ }
1018
+ const utils = new Utils();
1019
+
1020
+ class ElementWait extends ElementSelector {
1021
+ windowApi;
1022
+ constructor(windowApiOption) {
1023
+ super(windowApiOption);
1024
+ this.windowApi = new WindowApi(windowApiOption);
1025
+ }
1026
+ wait(checkFn, timeout, parent) {
1027
+ const UtilsContext = this;
1028
+ const __timeout__ = typeof timeout === "number" ? timeout : 0;
1029
+ return new Promise((resolve) => {
1030
+ const observer = utils.mutationObserver(parent || UtilsContext.windowApi.document, {
1031
+ config: {
1032
+ subtree: true,
1033
+ childList: true,
1034
+ attributes: true,
1035
+ },
1036
+ immediate: true,
1037
+ callback(_, __observer__) {
1038
+ const result = checkFn();
1039
+ if (result.success) {
1040
+ // 取消观察器
1041
+ if (typeof __observer__?.disconnect === "function") {
1042
+ __observer__.disconnect();
1043
+ }
1044
+ resolve(result.data);
1045
+ }
1046
+ },
1047
+ });
1048
+ if (__timeout__ > 0) {
1049
+ CommonUtils.setTimeout(() => {
1050
+ // 取消观察器
1051
+ if (typeof observer?.disconnect === "function") {
1052
+ observer.disconnect();
1053
+ }
1054
+ resolve(null);
1055
+ }, __timeout__);
1056
+ }
1057
+ });
1058
+ }
1059
+ waitNode(...args) {
1060
+ // 过滤掉undefined
1061
+ args = args.filter((arg) => arg !== void 0);
1062
+ const UtilsContext = this;
1063
+ // 选择器
1064
+ const selector = args[0];
1065
+ // 父元素(监听的元素)
1066
+ let parent = UtilsContext.windowApi.document;
1067
+ // 超时时间
1068
+ let timeout = 0;
1069
+ if (typeof args[0] !== "string" && !Array.isArray(args[0]) && typeof args[0] !== "function") {
1070
+ throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]|Function");
1071
+ }
1072
+ if (args.length === 1) ;
1073
+ else if (args.length === 2) {
1074
+ const secondParam = args[1];
1075
+ if (typeof secondParam === "number") {
1076
+ // "div",10000
1077
+ timeout = secondParam;
1078
+ }
1079
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1080
+ // "div",document
1081
+ parent = secondParam;
1082
+ }
1083
+ else {
1084
+ throw new TypeError("Utils.waitNode 第二个参数必须是number|Node");
1085
+ }
1086
+ }
1087
+ else if (args.length === 3) {
1088
+ // "div",document,10000
1089
+ // 第二个参数,parent
1090
+ const secondParam = args[1];
1091
+ // 第三个参数,timeout
1092
+ const thirdParam = args[2];
1093
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1094
+ parent = secondParam;
1095
+ if (typeof thirdParam === "number") {
1096
+ timeout = thirdParam;
1097
+ }
1098
+ else {
1099
+ throw new TypeError("Utils.waitNode 第三个参数必须是number");
1100
+ }
1101
+ }
1102
+ else {
1103
+ throw new TypeError("Utils.waitNode 第二个参数必须是Node");
1104
+ }
1105
+ }
1106
+ else {
1107
+ throw new TypeError("Utils.waitNode 参数个数错误");
1108
+ }
1109
+ function getNode() {
1110
+ if (Array.isArray(selector)) {
1111
+ const result = [];
1112
+ for (let index = 0; index < selector.length; index++) {
1113
+ const node = elementSelector.selector(selector[index]);
1114
+ if (node) {
1115
+ result.push(node);
1116
+ }
1117
+ }
1118
+ if (result.length === selector.length) {
1119
+ return result;
1120
+ }
1121
+ }
1122
+ else if (typeof selector === "function") {
1123
+ return selector();
1124
+ }
1125
+ else {
1126
+ return elementSelector.selector(selector, parent);
1127
+ }
1128
+ }
1129
+ return UtilsContext.wait(() => {
1130
+ const node = getNode();
1131
+ if (node) {
1132
+ return {
1133
+ success: true,
1134
+ data: node,
1135
+ };
1136
+ }
1137
+ else {
1138
+ return {
1139
+ success: false,
1140
+ data: node,
1141
+ };
1142
+ }
1143
+ }, timeout, parent);
1144
+ }
1145
+ waitAnyNode(...args) {
1146
+ // 过滤掉undefined
1147
+ args = args.filter((arg) => arg !== void 0);
1148
+ const UtilsContext = this;
1149
+ // 选择器
1150
+ const selectorList = args[0];
1151
+ // 父元素(监听的元素)
1152
+ let parent = UtilsContext.windowApi.document;
1153
+ // 超时时间
1154
+ let timeout = 0;
1155
+ if (typeof args[0] !== "object" && !Array.isArray(args[0])) {
1156
+ throw new TypeError("Utils.waitAnyNode 第一个参数必须是string[]");
1157
+ }
1158
+ if (args.length === 1) ;
1159
+ else if (args.length === 2) {
1160
+ const secondParam = args[1];
1161
+ if (typeof secondParam === "number") {
1162
+ // "div",10000
1163
+ timeout = secondParam;
1164
+ }
1165
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1166
+ // "div",document
1167
+ parent = secondParam;
1168
+ }
1169
+ else {
1170
+ throw new TypeError("Utils.waitAnyNode 第二个参数必须是number|Node");
1171
+ }
1172
+ }
1173
+ else if (args.length === 3) {
1174
+ // "div",document,10000
1175
+ // 第二个参数,parent
1176
+ const secondParam = args[1];
1177
+ // 第三个参数,timeout
1178
+ const thirdParam = args[2];
1179
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1180
+ parent = secondParam;
1181
+ if (typeof thirdParam === "number") {
1182
+ timeout = thirdParam;
1183
+ }
1184
+ else {
1185
+ throw new TypeError("Utils.waitAnyNode 第三个参数必须是number");
1186
+ }
1187
+ }
1188
+ else {
1189
+ throw new TypeError("Utils.waitAnyNode 第二个参数必须是Node");
1190
+ }
1191
+ }
1192
+ else {
1193
+ throw new TypeError("Utils.waitAnyNode 参数个数错误");
1194
+ }
1195
+ const promiseList = selectorList.map((selector) => {
1196
+ return UtilsContext.waitNode(selector, parent, timeout);
1197
+ });
1198
+ return Promise.any(promiseList);
1199
+ }
1200
+ waitNodeList(...args) {
1201
+ // 过滤掉undefined
1202
+ args = args.filter((arg) => arg !== void 0);
1203
+ const UtilsContext = this;
1204
+ // 选择器数组
1205
+ const selector = args[0];
1206
+ // 父元素(监听的元素)
1207
+ let parent = UtilsContext.windowApi.document;
1208
+ // 超时时间
1209
+ let timeout = 0;
1210
+ if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
1211
+ throw new TypeError("Utils.waitNodeList 第一个参数必须是string|string[]");
1212
+ }
1213
+ if (args.length === 1) ;
1214
+ else if (args.length === 2) {
1215
+ const secondParam = args[1];
1216
+ if (typeof secondParam === "number") {
1217
+ // "div",10000
1218
+ timeout = secondParam;
1219
+ }
1220
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1221
+ // "div",document
1222
+ parent = secondParam;
1223
+ }
1224
+ else {
1225
+ throw new TypeError("Utils.waitNodeList 第二个参数必须是number|Node");
1226
+ }
1227
+ }
1228
+ else if (args.length === 3) {
1229
+ // "div",document,10000
1230
+ // 第二个参数,parent
1231
+ const secondParam = args[1];
1232
+ // 第三个参数,timeout
1233
+ const thirdParam = args[2];
1234
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1235
+ parent = secondParam;
1236
+ if (typeof thirdParam === "number") {
1237
+ timeout = thirdParam;
1238
+ }
1239
+ else {
1240
+ throw new TypeError("Utils.waitNodeList 第三个参数必须是number");
1241
+ }
1242
+ }
1243
+ else {
1244
+ throw new TypeError("Utils.waitNodeList 第二个参数必须是Node");
1245
+ }
1246
+ }
1247
+ else {
1248
+ throw new TypeError("Utils.waitNodeList 参数个数错误");
1249
+ }
1250
+ function getNodeList() {
1251
+ if (Array.isArray(selector)) {
1252
+ const result = [];
1253
+ for (let index = 0; index < selector.length; index++) {
1254
+ const nodeList = elementSelector.selectorAll(selector[index], parent);
1255
+ if (nodeList.length) {
1256
+ result.push(nodeList);
1257
+ }
1258
+ }
1259
+ if (result.length === selector.length) {
1260
+ return result;
1261
+ }
1262
+ }
1263
+ else {
1264
+ const nodeList = elementSelector.selectorAll(selector, parent);
1265
+ if (nodeList.length) {
1266
+ return nodeList;
1267
+ }
1268
+ }
1269
+ }
1270
+ return UtilsContext.wait(() => {
1271
+ const node = getNodeList();
1272
+ if (node) {
1273
+ return {
1274
+ success: true,
1275
+ data: node,
1276
+ };
1277
+ }
1278
+ else {
1279
+ return {
1280
+ success: false,
1281
+ data: node,
1282
+ };
1283
+ }
1284
+ }, timeout, parent);
1285
+ }
1286
+ waitAnyNodeList(...args) {
1287
+ // 过滤掉undefined
1288
+ args = args.filter((arg) => arg !== void 0);
1289
+ const UtilsContext = this;
1290
+ // 选择器数组
1291
+ const selectorList = args[0];
1292
+ // 父元素(监听的元素)
1293
+ let parent = UtilsContext.windowApi.document;
1294
+ // 超时时间
1295
+ let timeout = 0;
1296
+ if (!Array.isArray(args[0])) {
1297
+ throw new TypeError("Utils.waitAnyNodeList 第一个参数必须是string[]");
1298
+ }
1299
+ if (args.length === 1) ;
1300
+ else if (args.length === 2) {
1301
+ const secondParam = args[1];
1302
+ if (typeof secondParam === "number") {
1303
+ // "div",10000
1304
+ timeout = secondParam;
1305
+ }
1306
+ else if (typeof secondParam === "object" && secondParam instanceof Node) {
1307
+ // "div",document
1308
+ parent = secondParam;
1309
+ }
1310
+ else {
1311
+ throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是number|Node");
1312
+ }
1313
+ }
1314
+ else if (args.length === 3) {
1315
+ // "div",document,10000
1316
+ // 第二个参数,parent
1317
+ const secondParam = args[1];
1318
+ // 第三个参数,timeout
1319
+ const thirdParam = args[2];
1320
+ if (typeof secondParam === "object" && secondParam instanceof Node) {
1321
+ parent = secondParam;
1322
+ if (typeof thirdParam === "number") {
1323
+ timeout = thirdParam;
1324
+ }
1325
+ else {
1326
+ throw new TypeError("Utils.waitAnyNodeList 第三个参数必须是number");
1327
+ }
1328
+ }
1329
+ else {
1330
+ throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是Node");
1331
+ }
1332
+ }
1333
+ else {
1334
+ throw new TypeError("Utils.waitAnyNodeList 参数个数错误");
1335
+ }
1336
+ const promiseList = selectorList.map((selector) => {
1337
+ return UtilsContext.waitNodeList(selector, parent, timeout);
1338
+ });
1339
+ return Promise.any(promiseList);
1340
+ }
1341
+ }
1342
+ new ElementWait();
1343
+
1344
+ class ElementAnimate extends ElementWait {
1345
+ windowApi;
1346
+ constructor(windowApiOption) {
1347
+ super(windowApiOption);
1348
+ this.windowApi = new WindowApi(windowApiOption);
1349
+ }
1350
+ /**
1351
+ * 在一定时间内改变元素的样式属性,实现动画效果
1352
+ * @param element 需要进行动画的元素
1353
+ * @param styles 动画结束时元素的样式属性
1354
+ * @param duration 动画持续时间,单位为毫秒
1355
+ * @param callback 动画结束后执行的函数
1356
+ * @example
1357
+ * // 监听元素a.xx的从显示变为隐藏
1358
+ * DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
1359
+ * console.log("已往上位移100px")
1360
+ * })
1361
+ */
1362
+ animate(element, styles, duration = 1000, callback = null) {
1363
+ const context = this;
1364
+ if (typeof element === "string") {
1365
+ element = elementSelector.selectorAll(element);
1366
+ }
1367
+ if (element == null) {
1368
+ return;
1369
+ }
1370
+ if (CommonUtils.isNodeList(element)) {
1371
+ // 设置
1372
+ element.forEach(($ele) => {
1373
+ context.animate($ele, styles, duration, callback);
1374
+ });
1375
+ return;
1376
+ }
1377
+ if (typeof duration !== "number" || duration <= 0) {
1378
+ throw new TypeError("duration must be a positive number");
1379
+ }
1380
+ if (typeof callback !== "function" && callback !== void 0) {
1381
+ throw new TypeError("callback must be a function or null");
1382
+ }
1383
+ if (typeof styles !== "object" || styles === void 0) {
1384
+ throw new TypeError("styles must be an object");
1385
+ }
1386
+ if (Object.keys(styles).length === 0) {
1387
+ throw new Error("styles must contain at least one property");
1388
+ }
1389
+ const start = performance.now();
1390
+ const from = {};
1391
+ const to = {};
1392
+ for (const prop in styles) {
1393
+ from[prop] = element.style[prop] || context.windowApi.globalThis.getComputedStyle(element)[prop];
1394
+ to[prop] = styles[prop];
1395
+ }
1396
+ const timer = CommonUtils.setInterval(function () {
1397
+ const timePassed = performance.now() - start;
1398
+ let progress = timePassed / duration;
1399
+ if (progress > 1) {
1400
+ progress = 1;
1401
+ }
1402
+ for (const prop in styles) {
1403
+ element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
1404
+ }
1405
+ if (progress === 1) {
1406
+ CommonUtils.clearInterval(timer);
1407
+ if (callback) {
1408
+ callback();
1409
+ }
1410
+ }
1411
+ }, 10);
1412
+ }
1413
+ /**
1414
+ * 显示元素
1415
+ * @param target 当前元素
1416
+ * @param checkVisiblie 是否检测元素是否显示
1417
+ * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
1418
+ * + false 不检测,直接设置display属性为空
1419
+ * @example
1420
+ * // 显示a.xx元素
1421
+ * DOMUtils.show(document.querySelector("a.xx"))
1422
+ * DOMUtils.show(document.querySelectorAll("a.xx"))
1423
+ * DOMUtils.show("a.xx")
1424
+ */
1425
+ show(target, checkVisiblie = true) {
1426
+ const context = this;
1427
+ if (target == null) {
1428
+ return;
1429
+ }
1430
+ if (typeof target === "string") {
1431
+ target = elementSelector.selectorAll(target);
1432
+ }
1433
+ if (target instanceof NodeList || target instanceof Array) {
1434
+ target = target;
1435
+ for (const element of target) {
1436
+ context.show(element, checkVisiblie);
1437
+ }
1438
+ }
1439
+ else {
1440
+ target = target;
1441
+ target.style.display = "";
1442
+ if (checkVisiblie) {
1443
+ if (!CommonUtils.isShow(target)) {
1444
+ /* 仍然是不显示,尝试使用强覆盖 */
1445
+ target.style.setProperty("display", "unset", "important");
1446
+ }
1447
+ }
1448
+ }
1449
+ }
1450
+ /**
1451
+ * 隐藏元素
1452
+ * @param target 当前元素
1453
+ * @param checkVisiblie 是否检测元素是否显示
1454
+ * + true (默认)如果检测到显示,则强制使用display: none !important;
1455
+ * + false 不检测,直接设置display属性为none
1456
+ * @example
1457
+ * // 隐藏a.xx元素
1458
+ * DOMUtils.hide(document.querySelector("a.xx"))
1459
+ * DOMUtils.hide(document.querySelectorAll("a.xx"))
1460
+ * DOMUtils.hide("a.xx")
1461
+ */
1462
+ hide(target, checkVisiblie = true) {
1463
+ const context = this;
1464
+ if (target == null) {
1465
+ return;
1466
+ }
1467
+ if (typeof target === "string") {
1468
+ target = elementSelector.selectorAll(target);
1469
+ }
1470
+ if (target instanceof NodeList || target instanceof Array) {
1471
+ target = target;
1472
+ for (const element of target) {
1473
+ context.hide(element, checkVisiblie);
1474
+ }
1475
+ }
1476
+ else {
1477
+ target = target;
1478
+ target.style.display = "none";
1479
+ if (checkVisiblie) {
1480
+ if (CommonUtils.isShow(target)) {
1481
+ /* 仍然是显示,尝试使用强覆盖 */
1482
+ target.style.setProperty("display", "none", "important");
1483
+ }
1484
+ }
1485
+ }
1486
+ }
1487
+ /**
1488
+ * 淡入元素
1489
+ * @param element 当前元素
1490
+ * @param duration 动画持续时间(毫秒),默认400毫秒
1491
+ * @param callback 动画结束的回调
1492
+ * @example
1493
+ * // 元素a.xx淡入
1494
+ * DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
1495
+ * console.log("淡入完毕");
1496
+ * })
1497
+ * DOMUtils.fadeIn("a.xx",undefined,()=>{
1498
+ * console.log("淡入完毕");
1499
+ * })
1500
+ */
1501
+ fadeIn(element, duration = 400, callback) {
1502
+ if (element == null) {
1503
+ return;
1504
+ }
1505
+ const context = this;
1506
+ if (typeof element === "string") {
1507
+ element = elementSelector.selectorAll(element);
1508
+ }
1509
+ if (CommonUtils.isNodeList(element)) {
1510
+ // 设置
1511
+ element.forEach(($ele) => {
1512
+ context.fadeIn($ele, duration, callback);
1513
+ });
1514
+ return;
1515
+ }
1516
+ element.style.opacity = "0";
1517
+ element.style.display = "";
1518
+ let start = null;
1519
+ let timer = null;
1520
+ function step(timestamp) {
1521
+ if (!start)
1522
+ start = timestamp;
1523
+ const progress = timestamp - start;
1524
+ element = element;
1525
+ element.style.opacity = Math.min(progress / duration, 1).toString();
1526
+ if (progress < duration) {
1527
+ context.windowApi.window.requestAnimationFrame(step);
1528
+ }
1529
+ else {
1530
+ if (callback && typeof callback === "function") {
1531
+ callback();
1532
+ }
1533
+ context.windowApi.window.cancelAnimationFrame(timer);
1534
+ }
1535
+ }
1536
+ timer = context.windowApi.window.requestAnimationFrame(step);
1537
+ }
456
1538
  /**
457
- * 配合 .setTimeout 使用
1539
+ * 淡出元素
1540
+ * @param element 当前元素
1541
+ * @param duration 动画持续时间(毫秒),默认400毫秒
1542
+ * @param callback 动画结束的回调
1543
+ * @example
1544
+ * // 元素a.xx淡出
1545
+ * DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
1546
+ * console.log("淡出完毕");
1547
+ * })
1548
+ * DOMUtils.fadeOut("a.xx",undefined,()=>{
1549
+ * console.log("淡出完毕");
1550
+ * })
458
1551
  */
459
- clearTimeout(timeId) {
460
- try {
461
- if (timeId != null) {
462
- clearTimeout$1(timeId);
463
- }
1552
+ fadeOut(element, duration = 400, callback) {
1553
+ const context = this;
1554
+ if (element == null) {
1555
+ return;
464
1556
  }
465
- catch {
466
- // TODO
1557
+ if (typeof element === "string") {
1558
+ element = elementSelector.selectorAll(element);
467
1559
  }
468
- finally {
469
- this.windowApi.clearTimeout(timeId);
1560
+ if (CommonUtils.isNodeList(element)) {
1561
+ // 设置
1562
+ element.forEach(($ele) => {
1563
+ context.fadeOut($ele, duration, callback);
1564
+ });
1565
+ return;
470
1566
  }
471
- },
1567
+ element.style.opacity = "1";
1568
+ let start = null;
1569
+ let timer = null;
1570
+ function step(timestamp) {
1571
+ if (!start)
1572
+ start = timestamp;
1573
+ const progress = timestamp - start;
1574
+ element = element;
1575
+ element.style.opacity = Math.max(1 - progress / duration, 0).toString();
1576
+ if (progress < duration) {
1577
+ context.windowApi.window.requestAnimationFrame(step);
1578
+ }
1579
+ else {
1580
+ element.style.display = "none";
1581
+ if (typeof callback === "function") {
1582
+ callback();
1583
+ }
1584
+ context.windowApi.window.cancelAnimationFrame(timer);
1585
+ }
1586
+ }
1587
+ timer = context.windowApi.window.requestAnimationFrame(step);
1588
+ }
472
1589
  /**
473
- * 自动使用 Worker 执行 setInterval
1590
+ * 切换元素的显示和隐藏状态
1591
+ * @param element 当前元素
1592
+ * @param checkVisiblie 是否检测元素是否显示
1593
+ * @example
1594
+ * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
1595
+ * DOMUtils.toggle(document.querySelector("a.xx"))
1596
+ * DOMUtils.toggle("a.xx")
474
1597
  */
475
- setInterval(callback, timeout = 0) {
476
- try {
477
- return setInterval$1(callback, timeout);
1598
+ toggle(element, checkVisiblie) {
1599
+ const context = this;
1600
+ if (typeof element === "string") {
1601
+ element = elementSelector.selectorAll(element);
478
1602
  }
479
- catch {
480
- return this.windowApi.setInterval(callback, timeout);
1603
+ if (element == null) {
1604
+ return;
481
1605
  }
482
- },
483
- /**
484
- * 配合 .setInterval 使用
485
- */
486
- clearInterval(timeId) {
487
- try {
488
- if (timeId != null) {
489
- clearInterval$1(timeId);
490
- }
1606
+ if (CommonUtils.isNodeList(element)) {
1607
+ // 设置
1608
+ element.forEach(($ele) => {
1609
+ context.toggle($ele);
1610
+ });
1611
+ return;
491
1612
  }
492
- catch {
493
- // TODO
1613
+ if (context.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
1614
+ context.show(element, checkVisiblie);
494
1615
  }
495
- finally {
496
- this.windowApi.clearInterval(timeId);
1616
+ else {
1617
+ context.hide(element, checkVisiblie);
497
1618
  }
498
- },
499
- /**
500
- * 判断是否是元素列表
501
- * @param $ele
502
- */
503
- isNodeList($ele) {
504
- return Array.isArray($ele) || $ele instanceof NodeList;
505
- },
506
- /** 获取 animationend 在各个浏览器的兼容名 */
507
- getAnimationEndNameList() {
508
- return ["webkitAnimationEnd", "mozAnimationEnd", "MSAnimationEnd", "oanimationend", "animationend"];
509
- },
510
- /** 获取 transitionend 在各个浏览器的兼容名 */
511
- getTransitionEndNameList() {
512
- return ["webkitTransitionEnd", "mozTransitionEnd", "MSTransitionEnd", "otransitionend", "transitionend"];
513
- },
514
- };
515
-
516
- /* 数据 */
517
- const DOMUtilsData = {
518
- /** .on添加在元素存储的事件 */
519
- SymbolEvents: Symbol("events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)),
520
- };
1619
+ }
1620
+ }
1621
+ new ElementAnimate();
521
1622
 
522
1623
  const OriginPrototype = {
523
1624
  Object: {
@@ -525,11 +1626,20 @@
525
1626
  },
526
1627
  };
527
1628
 
528
- class DOMUtilsEvent {
1629
+ class ElementEvent extends ElementAnimate {
529
1630
  windowApi;
530
1631
  constructor(windowApiOption) {
1632
+ super(windowApiOption);
531
1633
  this.windowApi = new WindowApi(windowApiOption);
532
1634
  }
1635
+ /** 获取 animationend 在各个浏览器的兼容名 */
1636
+ getAnimationEndNameList() {
1637
+ return CommonUtils.getAnimationEndNameList();
1638
+ }
1639
+ /** 获取 transitionend 在各个浏览器的兼容名 */
1640
+ getTransitionEndNameList() {
1641
+ return CommonUtils.getTransitionEndNameList();
1642
+ }
533
1643
  on(element, eventType, selector, callback, option) {
534
1644
  /**
535
1645
  * 获取option配置
@@ -560,11 +1670,11 @@
560
1670
  }
561
1671
  return option;
562
1672
  }
563
- const DOMUtilsContext = this;
1673
+ const that = this;
564
1674
  // eslint-disable-next-line prefer-rest-params
565
1675
  const args = arguments;
566
1676
  if (typeof element === "string") {
567
- element = DOMUtilsContext.selectorAll(element);
1677
+ element = that.selectorAll(element);
568
1678
  }
569
1679
  if (element == null) {
570
1680
  return;
@@ -617,7 +1727,7 @@
617
1727
  */
618
1728
  function checkOptionOnceToRemoveEventListener() {
619
1729
  if (listenerOption.once) {
620
- DOMUtilsContext.off(element, eventType, selector, callback, option);
1730
+ that.off(element, eventType, selector, callback, option);
621
1731
  }
622
1732
  }
623
1733
  elementList.forEach((elementItem) => {
@@ -633,19 +1743,19 @@
633
1743
  ? event.composedPath()[0]
634
1744
  : event.target;
635
1745
  let totalParent = elementItem;
636
- if (DOMUtilsCommonUtils.isWin(totalParent)) {
637
- if (totalParent === DOMUtilsContext.windowApi.document) {
638
- totalParent = DOMUtilsContext.windowApi.document.documentElement;
1746
+ if (CommonUtils.isWin(totalParent)) {
1747
+ if (totalParent === that.windowApi.document) {
1748
+ totalParent = that.windowApi.document.documentElement;
639
1749
  }
640
1750
  }
641
1751
  const findValue = selectorList.find((selectorItem) => {
642
1752
  // 判断目标元素是否匹配选择器
643
- if (DOMUtilsContext.matches(eventTarget, selectorItem)) {
1753
+ if (that.matches(eventTarget, selectorItem)) {
644
1754
  /* 当前目标可以被selector所匹配到 */
645
1755
  return true;
646
1756
  }
647
1757
  /* 在上层与主元素之间寻找可以被selector所匹配到的 */
648
- const $closestMatches = DOMUtilsContext.closest(eventTarget, selectorItem);
1758
+ const $closestMatches = that.closest(eventTarget, selectorItem);
649
1759
  if ($closestMatches && totalParent?.contains($closestMatches)) {
650
1760
  eventTarget = $closestMatches;
651
1761
  return true;
@@ -678,7 +1788,7 @@
678
1788
  eventTypeList.forEach((eventName) => {
679
1789
  elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
680
1790
  /* 获取对象上的事件 */
681
- const elementEvents = Reflect.get(elementItem, DOMUtilsData.SymbolEvents) || {};
1791
+ const elementEvents = Reflect.get(elementItem, GlobalData.domEventSymbol) || {};
682
1792
  /* 初始化对象上的xx事件 */
683
1793
  elementEvents[eventName] = elementEvents[eventName] || [];
684
1794
  elementEvents[eventName].push({
@@ -688,7 +1798,7 @@
688
1798
  originCallBack: listenerCallBack,
689
1799
  });
690
1800
  /* 覆盖事件 */
691
- Reflect.set(elementItem, DOMUtilsData.SymbolEvents, elementEvents);
1801
+ Reflect.set(elementItem, GlobalData.domEventSymbol, elementEvents);
692
1802
  });
693
1803
  });
694
1804
  }
@@ -709,22 +1819,22 @@
709
1819
  }
710
1820
  return option;
711
1821
  }
712
- const DOMUtilsContext = this;
1822
+ const that = this;
713
1823
  // eslint-disable-next-line prefer-rest-params
714
1824
  const args = arguments;
715
1825
  if (typeof element === "string") {
716
- element = DOMUtilsContext.selectorAll(element);
1826
+ element = that.selectorAll(element);
717
1827
  }
718
1828
  if (element == null) {
719
1829
  return;
720
1830
  }
721
- let elementList = [];
1831
+ let $elList = [];
722
1832
  if (element instanceof NodeList || Array.isArray(element)) {
723
1833
  element = element;
724
- elementList = [...element];
1834
+ $elList = [...element];
725
1835
  }
726
1836
  else {
727
- elementList.push(element);
1837
+ $elList.push(element);
728
1838
  }
729
1839
  let eventTypeList = [];
730
1840
  if (Array.isArray(eventType)) {
@@ -775,9 +1885,9 @@
775
1885
  // 目标函数、事件名、回调函数、事件配置、过滤函数
776
1886
  filter = option;
777
1887
  }
778
- elementList.forEach((elementItem) => {
1888
+ $elList.forEach(($elItem) => {
779
1889
  /* 获取对象上的事件 */
780
- const elementEvents = Reflect.get(elementItem, DOMUtilsData.SymbolEvents) || {};
1890
+ const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
781
1891
  eventTypeList.forEach((eventName) => {
782
1892
  const handlers = elementEvents[eventName] || [];
783
1893
  const filterHandler = typeof filter === "function" ? handlers.filter(filter) : handlers;
@@ -801,7 +1911,7 @@
801
1911
  flag = false;
802
1912
  }
803
1913
  if (flag || isRemoveAll) {
804
- elementItem.removeEventListener(eventName, handler.callback, handler.option);
1914
+ $elItem.removeEventListener(eventName, handler.callback, handler.option);
805
1915
  const findIndex = handlers.findIndex((item) => item === handler);
806
1916
  if (findIndex !== -1) {
807
1917
  handlers.splice(findIndex, 1);
@@ -810,10 +1920,10 @@
810
1920
  }
811
1921
  if (handlers.length === 0) {
812
1922
  /* 如果没有任意的handler,那么删除该属性 */
813
- DOMUtilsCommonUtils.delete(elementEvents, eventType);
1923
+ CommonUtils.delete(elementEvents, eventType);
814
1924
  }
815
1925
  });
816
- Reflect.set(elementItem, DOMUtilsData.SymbolEvents, elementEvents);
1926
+ Reflect.set($elItem, GlobalData.domEventSymbol, elementEvents);
817
1927
  });
818
1928
  }
819
1929
  /**
@@ -822,19 +1932,19 @@
822
1932
  * @param eventType (可选)需要取消监听的事件
823
1933
  */
824
1934
  offAll(element, eventType) {
825
- const DOMUtilsContext = this;
1935
+ const that = this;
826
1936
  if (typeof element === "string") {
827
- element = DOMUtilsContext.selectorAll(element);
1937
+ element = that.selectorAll(element);
828
1938
  }
829
1939
  if (element == null) {
830
1940
  return;
831
1941
  }
832
- let elementList = [];
1942
+ let $elList = [];
833
1943
  if (element instanceof NodeList || Array.isArray(element)) {
834
- elementList = [...element];
1944
+ $elList = [...element];
835
1945
  }
836
1946
  else {
837
- elementList.push(element);
1947
+ $elList.push(element);
838
1948
  }
839
1949
  let eventTypeList = [];
840
1950
  if (Array.isArray(eventType)) {
@@ -843,12 +1953,13 @@
843
1953
  else if (typeof eventType === "string") {
844
1954
  eventTypeList = eventTypeList.concat(eventType.split(" "));
845
1955
  }
846
- elementList.forEach((elementItem) => {
847
- Object.getOwnPropertySymbols(elementItem).forEach((symbolEvents) => {
848
- if (!symbolEvents.toString().startsWith("Symbol(events_")) {
1956
+ $elList.forEach(($elItem) => {
1957
+ const symbolList = [...new Set([...Object.getOwnPropertySymbols($elItem), GlobalData.domEventSymbol])];
1958
+ symbolList.forEach((symbolItem) => {
1959
+ if (!symbolItem.toString().startsWith("Symbol(events_")) {
849
1960
  return;
850
1961
  }
851
- const elementEvents = elementItem[symbolEvents] || {};
1962
+ const elementEvents = Reflect.get($elItem, symbolItem) || {};
852
1963
  const iterEventNameList = eventTypeList.length ? eventTypeList : Object.keys(elementEvents);
853
1964
  iterEventNameList.forEach((eventName) => {
854
1965
  const handlers = elementEvents[eventName];
@@ -856,12 +1967,12 @@
856
1967
  return;
857
1968
  }
858
1969
  for (const handler of handlers) {
859
- elementItem.removeEventListener(eventName, handler.callback, {
1970
+ $elItem.removeEventListener(eventName, handler.callback, {
860
1971
  capture: handler["option"]["capture"],
861
1972
  });
862
1973
  }
863
- const events = Reflect.get(elementItem, symbolEvents);
864
- DOMUtilsCommonUtils.delete(events, eventName);
1974
+ const events = Reflect.get($elItem, symbolItem);
1975
+ CommonUtils.delete(events, eventName);
865
1976
  });
866
1977
  });
867
1978
  });
@@ -878,15 +1989,15 @@
878
1989
  if (typeof callback !== "function") {
879
1990
  return;
880
1991
  }
881
- const DOMUtilsContext = this;
1992
+ const that = this;
882
1993
  /**
883
1994
  * 检测文档是否加载完毕
884
1995
  */
885
1996
  function checkDOMReadyState() {
886
1997
  try {
887
- if (DOMUtilsContext.windowApi.document.readyState === "complete" ||
888
- (DOMUtilsContext.windowApi.document.readyState !== "loading" &&
889
- !DOMUtilsContext.windowApi.document.documentElement.doScroll)) {
1998
+ if (that.windowApi.document.readyState === "complete" ||
1999
+ (that.windowApi.document.readyState !== "loading" &&
2000
+ !that.windowApi.document.documentElement.doScroll)) {
890
2001
  return true;
891
2002
  }
892
2003
  else {
@@ -906,12 +2017,12 @@
906
2017
  }
907
2018
  const targetList = [
908
2019
  {
909
- target: DOMUtilsContext.windowApi.document,
2020
+ target: that.windowApi.document,
910
2021
  eventType: "DOMContentLoaded",
911
2022
  callback: completed,
912
2023
  },
913
2024
  {
914
- target: DOMUtilsContext.windowApi.window,
2025
+ target: that.windowApi.window,
915
2026
  eventType: "load",
916
2027
  callback: completed,
917
2028
  },
@@ -936,7 +2047,7 @@
936
2047
  }
937
2048
  if (checkDOMReadyState()) {
938
2049
  /* 检查document状态 */
939
- DOMUtilsCommonUtils.setTimeout(callback);
2050
+ CommonUtils.setTimeout(callback);
940
2051
  }
941
2052
  else {
942
2053
  /* 添加监听 */
@@ -948,7 +2059,7 @@
948
2059
  * @param element 需要触发的元素|元素数组|window
949
2060
  * @param eventType 需要触发的事件
950
2061
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
951
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true
2062
+ * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true
952
2063
  * @example
953
2064
  * // 触发元素a.xx的click事件
954
2065
  * DOMUtils.trigger(document.querySelector("a.xx"),"click")
@@ -958,20 +2069,20 @@
958
2069
  * DOMUtils.trigger("a.xx",["click","tap","hover"])
959
2070
  */
960
2071
  trigger(element, eventType, details, useDispatchToTriggerEvent = true) {
961
- const DOMUtilsContext = this;
2072
+ const that = this;
962
2073
  if (typeof element === "string") {
963
- element = DOMUtilsContext.selectorAll(element);
2074
+ element = that.selectorAll(element);
964
2075
  }
965
2076
  if (element == null) {
966
2077
  return;
967
2078
  }
968
- let elementList = [];
2079
+ let $elList = [];
969
2080
  if (element instanceof NodeList || Array.isArray(element)) {
970
2081
  element = element;
971
- elementList = [...element];
2082
+ $elList = [...element];
972
2083
  }
973
2084
  else {
974
- elementList = [element];
2085
+ $elList = [element];
975
2086
  }
976
2087
  let eventTypeList = [];
977
2088
  if (Array.isArray(eventType)) {
@@ -980,29 +2091,33 @@
980
2091
  else if (typeof eventType === "string") {
981
2092
  eventTypeList = eventType.split(" ");
982
2093
  }
983
- elementList.forEach((elementItem) => {
2094
+ $elList.forEach(($elItem) => {
984
2095
  /* 获取对象上的事件 */
985
- const events = elementItem[DOMUtilsData.SymbolEvents] || {};
986
- eventTypeList.forEach((_eventType_) => {
2096
+ const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
2097
+ eventTypeList.forEach((__eventType) => {
987
2098
  let event = null;
988
2099
  if (details && details instanceof Event) {
989
2100
  event = details;
990
2101
  }
991
2102
  else {
992
- event = new Event(_eventType_);
2103
+ // 构造事件
2104
+ event = new Event(__eventType);
993
2105
  if (details) {
994
2106
  Object.keys(details).forEach((keyName) => {
995
- event[keyName] = details[keyName];
2107
+ const value = Reflect.get(details, keyName);
2108
+ // 在event上添加属性
2109
+ Reflect.set(event, keyName, value);
996
2110
  });
997
2111
  }
998
2112
  }
999
- if (useDispatchToTriggerEvent == false && _eventType_ in events) {
1000
- events[_eventType_].forEach((eventsItem) => {
2113
+ if (useDispatchToTriggerEvent == false && __eventType in elementEvents) {
2114
+ // 直接调用监听的事件
2115
+ elementEvents[__eventType].forEach((eventsItem) => {
1001
2116
  eventsItem.callback(event);
1002
2117
  });
1003
2118
  }
1004
2119
  else {
1005
- elementItem.dispatchEvent(event);
2120
+ $elItem.dispatchEvent(event);
1006
2121
  }
1007
2122
  });
1008
2123
  });
@@ -1022,25 +2137,25 @@
1022
2137
  * })
1023
2138
  * */
1024
2139
  click(element, handler, details, useDispatchToTriggerEvent) {
1025
- const DOMUtilsContext = this;
2140
+ const that = this;
1026
2141
  if (typeof element === "string") {
1027
- element = DOMUtilsContext.selectorAll(element);
2142
+ element = that.selectorAll(element);
1028
2143
  }
1029
2144
  if (element == null) {
1030
2145
  return;
1031
2146
  }
1032
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2147
+ if (CommonUtils.isNodeList(element)) {
1033
2148
  // 设置
1034
2149
  element.forEach(($ele) => {
1035
- DOMUtilsContext.click($ele, handler, details, useDispatchToTriggerEvent);
2150
+ that.click($ele, handler, details, useDispatchToTriggerEvent);
1036
2151
  });
1037
2152
  return;
1038
2153
  }
1039
2154
  if (handler == null) {
1040
- DOMUtilsContext.trigger(element, "click", details, useDispatchToTriggerEvent);
2155
+ that.trigger(element, "click", details, useDispatchToTriggerEvent);
1041
2156
  }
1042
2157
  else {
1043
- DOMUtilsContext.on(element, "click", null, handler);
2158
+ that.on(element, "click", null, handler);
1044
2159
  }
1045
2160
  }
1046
2161
  /**
@@ -1058,25 +2173,25 @@
1058
2173
  * })
1059
2174
  * */
1060
2175
  blur(element, handler, details, useDispatchToTriggerEvent) {
1061
- const DOMUtilsContext = this;
2176
+ const that = this;
1062
2177
  if (typeof element === "string") {
1063
- element = DOMUtilsContext.selectorAll(element);
2178
+ element = that.selectorAll(element);
1064
2179
  }
1065
2180
  if (element == null) {
1066
2181
  return;
1067
2182
  }
1068
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2183
+ if (CommonUtils.isNodeList(element)) {
1069
2184
  // 设置
1070
2185
  element.forEach(($ele) => {
1071
- DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
2186
+ that.focus($ele, handler, details, useDispatchToTriggerEvent);
1072
2187
  });
1073
2188
  return;
1074
2189
  }
1075
2190
  if (handler === null) {
1076
- DOMUtilsContext.trigger(element, "blur", details, useDispatchToTriggerEvent);
2191
+ that.trigger(element, "blur", details, useDispatchToTriggerEvent);
1077
2192
  }
1078
2193
  else {
1079
- DOMUtilsContext.on(element, "blur", null, handler);
2194
+ that.on(element, "blur", null, handler);
1080
2195
  }
1081
2196
  }
1082
2197
  /**
@@ -1094,25 +2209,25 @@
1094
2209
  * })
1095
2210
  * */
1096
2211
  focus(element, handler, details, useDispatchToTriggerEvent) {
1097
- const DOMUtilsContext = this;
2212
+ const that = this;
1098
2213
  if (typeof element === "string") {
1099
- element = DOMUtilsContext.selectorAll(element);
2214
+ element = that.selectorAll(element);
1100
2215
  }
1101
2216
  if (element == null) {
1102
2217
  return;
1103
2218
  }
1104
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2219
+ if (CommonUtils.isNodeList(element)) {
1105
2220
  // 设置
1106
2221
  element.forEach(($ele) => {
1107
- DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
2222
+ that.focus($ele, handler, details, useDispatchToTriggerEvent);
1108
2223
  });
1109
2224
  return;
1110
2225
  }
1111
2226
  if (handler == null) {
1112
- DOMUtilsContext.trigger(element, "focus", details, useDispatchToTriggerEvent);
2227
+ that.trigger(element, "focus", details, useDispatchToTriggerEvent);
1113
2228
  }
1114
2229
  else {
1115
- DOMUtilsContext.on(element, "focus", null, handler);
2230
+ that.on(element, "focus", null, handler);
1116
2231
  }
1117
2232
  }
1118
2233
  /**
@@ -1130,22 +2245,22 @@
1130
2245
  * })
1131
2246
  */
1132
2247
  hover(element, handler, option) {
1133
- const DOMUtilsContext = this;
2248
+ const that = this;
1134
2249
  if (typeof element === "string") {
1135
- element = DOMUtilsContext.selectorAll(element);
2250
+ element = that.selectorAll(element);
1136
2251
  }
1137
2252
  if (element == null) {
1138
2253
  return;
1139
2254
  }
1140
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2255
+ if (CommonUtils.isNodeList(element)) {
1141
2256
  // 设置
1142
2257
  element.forEach(($ele) => {
1143
- DOMUtilsContext.hover($ele, handler, option);
2258
+ that.hover($ele, handler, option);
1144
2259
  });
1145
2260
  return;
1146
2261
  }
1147
- DOMUtilsContext.on(element, "mouseenter", null, handler, option);
1148
- DOMUtilsContext.on(element, "mouseleave", null, handler, option);
2262
+ that.on(element, "mouseenter", null, handler, option);
2263
+ that.on(element, "mouseleave", null, handler, option);
1149
2264
  }
1150
2265
  /**
1151
2266
  * 当动画结束时触发事件
@@ -1154,30 +2269,23 @@
1154
2269
  * @param option 配置项,这里默认配置once为true
1155
2270
  */
1156
2271
  animationend(element, handler, option) {
1157
- const DOMUtilsContext = this;
2272
+ const that = this;
1158
2273
  if (typeof element === "string") {
1159
- element = DOMUtilsContext.selector(element);
2274
+ element = that.selector(element);
1160
2275
  }
1161
2276
  if (element == null) {
1162
2277
  return;
1163
2278
  }
1164
- // if (DOMUtilsCommonUtils.isNodeList(element)) {
1165
- // // 设置
1166
- // element.forEach(($ele) => {
1167
- // DOMUtilsContext.animationend($ele as HTMLElement, handler, option);
1168
- // });
1169
- // return;
1170
- // }
1171
2279
  const defaultOption = {
1172
2280
  once: true,
1173
2281
  };
1174
2282
  Object.assign(defaultOption, option || {});
1175
- const eventNameList = DOMUtilsCommonUtils.getAnimationEndNameList();
1176
- DOMUtilsContext.on(element, eventNameList, null, handler, defaultOption);
2283
+ const eventNameList = CommonUtils.getAnimationEndNameList();
2284
+ that.on(element, eventNameList, null, handler, defaultOption);
1177
2285
  if (!defaultOption.once) {
1178
2286
  return {
1179
2287
  off() {
1180
- DOMUtilsContext.off(element, eventNameList, null, handler, defaultOption);
2288
+ that.off(element, eventNameList, null, handler, defaultOption);
1181
2289
  },
1182
2290
  };
1183
2291
  }
@@ -1189,30 +2297,23 @@
1189
2297
  * @param option 配置项,这里默认配置once为true
1190
2298
  */
1191
2299
  transitionend(element, handler, option) {
1192
- const DOMUtilsContext = this;
2300
+ const that = this;
1193
2301
  if (typeof element === "string") {
1194
- element = DOMUtilsContext.selector(element);
2302
+ element = that.selector(element);
1195
2303
  }
1196
2304
  if (element == null) {
1197
2305
  return;
1198
2306
  }
1199
- // if (DOMUtilsCommonUtils.isNodeList(element)) {
1200
- // // 设置
1201
- // element.forEach(($ele) => {
1202
- // DOMUtilsContext.transitionend($ele as HTMLElement, handler, option);
1203
- // });
1204
- // return;
1205
- // }
1206
2307
  const defaultOption = {
1207
2308
  once: true,
1208
2309
  };
1209
2310
  Object.assign(defaultOption, option || {});
1210
- const eventNameList = DOMUtilsCommonUtils.getTransitionEndNameList();
1211
- DOMUtilsContext.on(element, eventNameList, null, handler, defaultOption);
2311
+ const eventNameList = CommonUtils.getTransitionEndNameList();
2312
+ that.on(element, eventNameList, null, handler, defaultOption);
1212
2313
  if (!defaultOption.once) {
1213
2314
  return {
1214
2315
  off() {
1215
- DOMUtilsContext.off(element, eventNameList, null, handler, defaultOption);
2316
+ that.off(element, eventNameList, null, handler, defaultOption);
1216
2317
  },
1217
2318
  };
1218
2319
  }
@@ -1233,21 +2334,21 @@
1233
2334
  * })
1234
2335
  */
1235
2336
  keyup(element, handler, option) {
1236
- const DOMUtilsContext = this;
2337
+ const that = this;
1237
2338
  if (element == null) {
1238
2339
  return;
1239
2340
  }
1240
2341
  if (typeof element === "string") {
1241
- element = DOMUtilsContext.selectorAll(element);
2342
+ element = that.selectorAll(element);
1242
2343
  }
1243
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2344
+ if (CommonUtils.isNodeList(element)) {
1244
2345
  // 设置
1245
2346
  element.forEach(($ele) => {
1246
- DOMUtilsContext.keyup($ele, handler, option);
2347
+ that.keyup($ele, handler, option);
1247
2348
  });
1248
2349
  return;
1249
2350
  }
1250
- DOMUtilsContext.on(element, "keyup", null, handler, option);
2351
+ that.on(element, "keyup", null, handler, option);
1251
2352
  }
1252
2353
  /**
1253
2354
  * 当按键按下时触发事件
@@ -1265,21 +2366,21 @@
1265
2366
  * })
1266
2367
  */
1267
2368
  keydown(element, handler, option) {
1268
- const DOMUtilsContext = this;
2369
+ const that = this;
1269
2370
  if (element == null) {
1270
2371
  return;
1271
2372
  }
1272
2373
  if (typeof element === "string") {
1273
- element = DOMUtilsContext.selectorAll(element);
2374
+ element = that.selectorAll(element);
1274
2375
  }
1275
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2376
+ if (CommonUtils.isNodeList(element)) {
1276
2377
  // 设置
1277
2378
  element.forEach(($ele) => {
1278
- DOMUtilsContext.keydown($ele, handler, option);
2379
+ that.keydown($ele, handler, option);
1279
2380
  });
1280
2381
  return;
1281
2382
  }
1282
- DOMUtilsContext.on(element, "keydown", null, handler, option);
2383
+ that.on(element, "keydown", null, handler, option);
1283
2384
  }
1284
2385
  /**
1285
2386
  * 当按键按下时触发事件
@@ -1297,21 +2398,21 @@
1297
2398
  * })
1298
2399
  */
1299
2400
  keypress(element, handler, option) {
1300
- const DOMUtilsContext = this;
2401
+ const that = this;
1301
2402
  if (element == null) {
1302
2403
  return;
1303
2404
  }
1304
2405
  if (typeof element === "string") {
1305
- element = DOMUtilsContext.selectorAll(element);
2406
+ element = that.selectorAll(element);
1306
2407
  }
1307
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2408
+ if (CommonUtils.isNodeList(element)) {
1308
2409
  // 设置
1309
2410
  element.forEach(($ele) => {
1310
- DOMUtilsContext.keypress($ele, handler, option);
2411
+ that.keypress($ele, handler, option);
1311
2412
  });
1312
2413
  return;
1313
2414
  }
1314
- DOMUtilsContext.on(element, "keypress", null, handler, option);
2415
+ that.on(element, "keypress", null, handler, option);
1315
2416
  }
1316
2417
  /**
1317
2418
  * 监听某个元素键盘按键事件或window全局按键事件
@@ -1377,9 +2478,9 @@
1377
2478
  收藏 171
1378
2479
  **/
1379
2480
  listenKeyboard(element, eventName = "keypress", callback, options) {
1380
- const DOMUtilsContext = this;
2481
+ const that = this;
1381
2482
  if (typeof element === "string") {
1382
- element = DOMUtilsContext.selectorAll(element);
2483
+ element = that.selectorAll(element);
1383
2484
  }
1384
2485
  const keyboardEventCallBack = function (event) {
1385
2486
  /** 键名 */
@@ -1387,233 +2488,146 @@
1387
2488
  /** 键值 */
1388
2489
  const keyValue = event.charCode || event.keyCode || event.which;
1389
2490
  /** 组合键列表 */
1390
- const otherCodeList = [];
1391
- if (event.ctrlKey) {
1392
- otherCodeList.push("ctrl");
1393
- }
1394
- if (event.altKey) {
1395
- otherCodeList.push("alt");
1396
- }
1397
- if (event.metaKey) {
1398
- otherCodeList.push("meta");
1399
- }
1400
- if (event.shiftKey) {
1401
- otherCodeList.push("shift");
1402
- }
1403
- if (typeof callback === "function") {
1404
- callback(keyName, keyValue, otherCodeList, event);
1405
- }
1406
- };
1407
- DOMUtilsContext.on(element, eventName, keyboardEventCallBack, options);
1408
- return {
1409
- removeListen: () => {
1410
- DOMUtilsContext.off(element, eventName, keyboardEventCallBack, options);
1411
- },
1412
- };
1413
- }
1414
- selector(selector, parent) {
1415
- return this.selectorAll(selector, parent)[0];
1416
- }
1417
- selectorAll(selector, parent) {
1418
- const context = this;
1419
- parent = parent || context.windowApi.document;
1420
- selector = selector.trim();
1421
- if (selector.match(/[^\s]{1}:empty$/gi)) {
1422
- // empty 语法
1423
- selector = selector.replace(/:empty$/gi, "");
1424
- return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
1425
- return $ele?.innerHTML?.trim() === "";
1426
- });
1427
- }
1428
- else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1429
- // contains 语法
1430
- const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1431
- const text = textMatch[2];
1432
- selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1433
- return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
1434
- // @ts-ignore
1435
- return ($ele?.textContent || $ele?.innerText)?.includes(text);
1436
- });
1437
- }
1438
- else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1439
- // regexp 语法
1440
- const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1441
- let pattern = textMatch[2];
1442
- const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1443
- let flags = "";
1444
- if (flagMatch) {
1445
- pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1446
- flags = flagMatch[3];
1447
- }
1448
- const regexp = new RegExp(pattern, flags);
1449
- selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1450
- return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
1451
- // @ts-ignore
1452
- return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
1453
- });
1454
- }
1455
- else {
1456
- // 普通语法
1457
- return Array.from(parent.querySelectorAll(selector));
1458
- }
1459
- }
1460
- /**
1461
- * 匹配元素,可使用以下的额外语法
1462
- *
1463
- * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
1464
- * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
1465
- * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
1466
- * @param $el 元素
1467
- * @param selector 选择器
1468
- * @example
1469
- * DOMUtils.matches("div:contains('测试')")
1470
- * > true
1471
- * @example
1472
- * DOMUtils.matches("div:empty")
1473
- * > true
1474
- * @example
1475
- * DOMUtils.matches("div:regexp('^xxxx$')")
1476
- * > true
1477
- * @example
1478
- * DOMUtils.matches("div:regexp(/^xxx/ig)")
1479
- * > false
1480
- */
1481
- matches($el, selector) {
1482
- selector = selector.trim();
1483
- if ($el == null) {
1484
- return false;
1485
- }
1486
- if (selector.match(/[^\s]{1}:empty$/gi)) {
1487
- // empty 语法
1488
- selector = selector.replace(/:empty$/gi, "");
1489
- return $el.matches(selector) && $el?.innerHTML?.trim() === "";
1490
- }
1491
- else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1492
- // contains 语法
1493
- const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1494
- const text = textMatch[2];
1495
- selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1496
- // @ts-ignore
1497
- let content = $el?.textContent || $el?.innerText;
1498
- if (typeof content !== "string") {
1499
- content = "";
1500
- }
1501
- return $el.matches(selector) && content?.includes(text);
1502
- }
1503
- else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1504
- // regexp 语法
1505
- const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1506
- let pattern = textMatch[2];
1507
- const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1508
- let flags = "";
1509
- if (flagMatch) {
1510
- pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1511
- flags = flagMatch[3];
1512
- }
1513
- const regexp = new RegExp(pattern, flags);
1514
- selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1515
- // @ts-ignore
1516
- let content = $el?.textContent || $el?.innerText;
1517
- if (typeof content !== "string") {
1518
- content = "";
2491
+ const otherCodeList = [];
2492
+ if (event.ctrlKey) {
2493
+ otherCodeList.push("ctrl");
1519
2494
  }
1520
- return $el.matches(selector) && Boolean(content?.match(regexp));
1521
- }
1522
- else {
1523
- // 普通语法
1524
- return $el.matches(selector);
1525
- }
1526
- }
1527
- closest($el, selector) {
1528
- selector = selector.trim();
1529
- if (selector.match(/[^\s]{1}:empty$/gi)) {
1530
- // empty 语法
1531
- selector = selector.replace(/:empty$/gi, "");
1532
- const $closest = $el?.closest(selector);
1533
- if ($closest && $closest?.innerHTML?.trim() === "") {
1534
- return $closest;
2495
+ if (event.altKey) {
2496
+ otherCodeList.push("alt");
1535
2497
  }
1536
- return null;
1537
- }
1538
- else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) || selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
1539
- // contains 语法
1540
- const textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
1541
- const text = textMatch[2];
1542
- selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
1543
- const $closest = $el?.closest(selector);
1544
- if ($closest) {
1545
- // @ts-ignore
1546
- const content = $el?.textContent || $el?.innerText;
1547
- if (typeof content === "string" && content.includes(text)) {
1548
- return $closest;
1549
- }
2498
+ if (event.metaKey) {
2499
+ otherCodeList.push("meta");
1550
2500
  }
1551
- return null;
1552
- }
1553
- else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) || selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
1554
- // regexp 语法
1555
- const textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
1556
- let pattern = textMatch[2];
1557
- const flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
1558
- let flags = "";
1559
- if (flagMatch) {
1560
- pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
1561
- flags = flagMatch[3];
2501
+ if (event.shiftKey) {
2502
+ otherCodeList.push("shift");
1562
2503
  }
1563
- const regexp = new RegExp(pattern, flags);
1564
- selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
1565
- const $closest = $el?.closest(selector);
1566
- if ($closest) {
1567
- // @ts-ignore
1568
- const content = $el?.textContent || $el?.innerText;
1569
- if (typeof content === "string" && content.match(regexp)) {
1570
- return $closest;
1571
- }
2504
+ if (typeof callback === "function") {
2505
+ callback(keyName, keyValue, otherCodeList, event);
1572
2506
  }
1573
- return null;
2507
+ };
2508
+ that.on(element, eventName, keyboardEventCallBack, options);
2509
+ return {
2510
+ removeListen: () => {
2511
+ that.off(element, eventName, keyboardEventCallBack, options);
2512
+ },
2513
+ };
2514
+ }
2515
+ preventEvent(...args) {
2516
+ /**
2517
+ * 阻止事件的默认行为发生,并阻止事件传播
2518
+ */
2519
+ const stopEvent = (event) => {
2520
+ /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
2521
+ event?.preventDefault();
2522
+ /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
2523
+ event?.stopPropagation();
2524
+ /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
2525
+ event?.stopImmediatePropagation();
2526
+ return false;
2527
+ };
2528
+ if (args.length === 1) {
2529
+ /* 直接阻止事件 */
2530
+ return stopEvent(args[0]);
1574
2531
  }
1575
2532
  else {
1576
- // 普通语法
1577
- const $closest = $el?.closest(selector);
1578
- return $closest;
2533
+ const $el = args[0];
2534
+ let eventNameList = args[1];
2535
+ const capture = args[2];
2536
+ /* 添加对应的事件来阻止触发 */
2537
+ if (typeof eventNameList === "string") {
2538
+ eventNameList = [eventNameList];
2539
+ }
2540
+ this.on($el, eventNameList, stopEvent, { capture: Boolean(capture) });
1579
2541
  }
1580
2542
  }
1581
2543
  }
2544
+ new ElementEvent();
1582
2545
 
1583
- const version = "1.6.8";
2546
+ class ElementHandler extends ElementEvent {
2547
+ windowApi;
2548
+ constructor(windowApiOption) {
2549
+ super(windowApiOption);
2550
+ this.windowApi = new WindowApi(windowApiOption);
2551
+ }
2552
+ /**
2553
+ * 获取元素的选择器字符串
2554
+ * @param $el
2555
+ * @example
2556
+ * DOMUtils.getElementSelector(document.querySelector("a"))
2557
+ * > '.....'
2558
+ */
2559
+ getElementSelector($el) {
2560
+ const that = this;
2561
+ if (!$el)
2562
+ return void 0;
2563
+ if (!$el.parentElement)
2564
+ return void 0;
2565
+ /* 如果元素有id属性,则直接返回id选择器 */
2566
+ if ($el.id)
2567
+ return `#${$el.id}`;
2568
+ /* 递归地获取父元素的选择器 */
2569
+ let selector = that.getElementSelector($el.parentElement);
2570
+ if (!selector) {
2571
+ return $el.tagName.toLowerCase();
2572
+ }
2573
+ /* 如果有多个相同类型的兄弟元素,则需要添加索引 */
2574
+ if ($el.parentElement.querySelectorAll($el.tagName).length > 1) {
2575
+ const index = Array.prototype.indexOf.call($el.parentElement.children, $el) + 1;
2576
+ selector += ` > ${$el.tagName.toLowerCase()}:nth-child(${index})`;
2577
+ }
2578
+ else {
2579
+ selector += ` > ${$el.tagName.toLowerCase()}`;
2580
+ }
2581
+ return selector;
2582
+ }
2583
+ }
2584
+ new ElementHandler();
1584
2585
 
1585
- class DOMUtils extends DOMUtilsEvent {
2586
+ class DOMUtils extends ElementHandler {
1586
2587
  constructor(option) {
1587
2588
  super(option);
1588
2589
  }
1589
2590
  /** 版本号 */
1590
2591
  version = version;
1591
- attr(element, attrName, attrValue) {
1592
- const DOMUtilsContext = this;
1593
- if (typeof element === "string") {
1594
- element = DOMUtilsContext.selectorAll(element);
2592
+ /**
2593
+ * 取消挂载在window下的DOMUtils并返回DOMUtils
2594
+ * @example
2595
+ * let DOMUtils = window.DOMUtils.noConflict()
2596
+ */
2597
+ noConflict() {
2598
+ const that = this;
2599
+ if (that.windowApi.window.DOMUtils) {
2600
+ CommonUtils.delete(window, "DOMUtils");
1595
2601
  }
1596
- if (element == null) {
2602
+ that.windowApi.window.DOMUtils = this;
2603
+ return this;
2604
+ }
2605
+ attr($el, attrName, attrValue) {
2606
+ const that = this;
2607
+ if (typeof $el === "string") {
2608
+ $el = that.selectorAll($el);
2609
+ }
2610
+ if ($el == null) {
1597
2611
  return;
1598
2612
  }
1599
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2613
+ if (CommonUtils.isNodeList($el)) {
1600
2614
  if (attrValue == null) {
1601
2615
  // 获取属性
1602
- return DOMUtilsContext.attr(element[0], attrName, attrValue);
2616
+ return that.attr($el[0], attrName, attrValue);
1603
2617
  }
1604
2618
  else {
1605
2619
  // 设置属性
1606
- element.forEach(($ele) => {
1607
- DOMUtilsContext.attr($ele, attrName, attrValue);
2620
+ $el.forEach(($elItem) => {
2621
+ that.attr($elItem, attrName, attrValue);
1608
2622
  });
1609
2623
  return;
1610
2624
  }
1611
2625
  }
1612
2626
  if (attrValue == null) {
1613
- return element.getAttribute(attrName);
2627
+ return $el.getAttribute(attrName);
1614
2628
  }
1615
2629
  else {
1616
- element.setAttribute(attrName, attrValue);
2630
+ $el.setAttribute(attrName, attrValue);
1617
2631
  }
1618
2632
  }
1619
2633
  createElement(
@@ -1623,11 +2637,11 @@
1623
2637
  property,
1624
2638
  /** 自定义属性 */
1625
2639
  attributes) {
1626
- const DOMUtilsContext = this;
1627
- const tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
2640
+ const that = this;
2641
+ const $el = that.windowApi.document.createElement(tagName);
1628
2642
  if (typeof property === "string") {
1629
- DOMUtilsContext.html(tempElement, property);
1630
- return tempElement;
2643
+ that.html($el, property);
2644
+ return $el;
1631
2645
  }
1632
2646
  if (property == null) {
1633
2647
  property = {};
@@ -1638,10 +2652,10 @@
1638
2652
  Object.keys(property).forEach((key) => {
1639
2653
  const value = property[key];
1640
2654
  if (key === "innerHTML") {
1641
- DOMUtilsContext.html(tempElement, value);
2655
+ that.html($el, value);
1642
2656
  return;
1643
2657
  }
1644
- tempElement[key] = value;
2658
+ $el[key] = value;
1645
2659
  });
1646
2660
  Object.keys(attributes).forEach((key) => {
1647
2661
  let value = attributes[key];
@@ -1653,12 +2667,12 @@
1653
2667
  /* function转字符串 */
1654
2668
  value = value.toString();
1655
2669
  }
1656
- tempElement.setAttribute(key, value);
2670
+ $el.setAttribute(key, value);
1657
2671
  });
1658
- return tempElement;
2672
+ return $el;
1659
2673
  }
1660
- css(element, property, value) {
1661
- const DOMUtilsContext = this;
2674
+ css($el, property, value) {
2675
+ const that = this;
1662
2676
  /**
1663
2677
  * 把纯数字没有px的加上
1664
2678
  */
@@ -1672,30 +2686,30 @@
1672
2686
  }
1673
2687
  return propertyValue;
1674
2688
  }
1675
- if (typeof element === "string") {
1676
- element = DOMUtilsContext.selectorAll(element);
2689
+ if (typeof $el === "string") {
2690
+ $el = that.selectorAll($el);
1677
2691
  }
1678
- if (element == null) {
2692
+ if ($el == null) {
1679
2693
  return;
1680
2694
  }
1681
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2695
+ if (CommonUtils.isNodeList($el)) {
1682
2696
  if (typeof property === "string") {
1683
2697
  if (value == null) {
1684
2698
  // 获取属性
1685
- return DOMUtilsContext.css(element[0], property);
2699
+ return that.css($el[0], property);
1686
2700
  }
1687
2701
  else {
1688
2702
  // 设置属性
1689
- element.forEach(($ele) => {
1690
- DOMUtilsContext.css($ele, property);
2703
+ $el.forEach(($elItem) => {
2704
+ that.css($elItem, property);
1691
2705
  });
1692
2706
  return;
1693
2707
  }
1694
2708
  }
1695
2709
  else if (typeof property === "object") {
1696
2710
  // 设置属性
1697
- element.forEach(($ele) => {
1698
- DOMUtilsContext.css($ele, property);
2711
+ $el.forEach(($elItem) => {
2712
+ that.css($elItem, property);
1699
2713
  });
1700
2714
  return;
1701
2715
  }
@@ -1707,16 +2721,16 @@
1707
2721
  .trim()
1708
2722
  .replace(/!important$/gi, "")
1709
2723
  .trim();
1710
- element.style.setProperty(propertyName, propertyValue, "important");
2724
+ $el.style.setProperty(propertyName, propertyValue, "important");
1711
2725
  }
1712
2726
  else {
1713
2727
  propertyValue = handlePixe(propertyName, propertyValue);
1714
- element.style.setProperty(propertyName, propertyValue);
2728
+ $el.style.setProperty(propertyName, propertyValue);
1715
2729
  }
1716
2730
  };
1717
2731
  if (typeof property === "string") {
1718
2732
  if (value == null) {
1719
- return DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).getPropertyValue(property);
2733
+ return that.windowApi.globalThis.getComputedStyle($el).getPropertyValue(property);
1720
2734
  }
1721
2735
  else {
1722
2736
  setStyleProperty(property, value);
@@ -1733,92 +2747,92 @@
1733
2747
  throw new TypeError("property must be string or object");
1734
2748
  }
1735
2749
  }
1736
- text(element, text) {
1737
- const DOMUtilsContext = this;
1738
- if (typeof element === "string") {
1739
- element = DOMUtilsContext.selectorAll(element);
2750
+ text($el, text) {
2751
+ const that = this;
2752
+ if (typeof $el === "string") {
2753
+ $el = that.selectorAll($el);
1740
2754
  }
1741
- if (element == null) {
2755
+ if ($el == null) {
1742
2756
  return;
1743
2757
  }
1744
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2758
+ if (CommonUtils.isNodeList($el)) {
1745
2759
  if (text == null) {
1746
2760
  // 获取
1747
- return DOMUtilsContext.text(element[0]);
2761
+ return that.text($el[0]);
1748
2762
  }
1749
2763
  else {
1750
2764
  // 设置
1751
- element.forEach(($ele) => {
1752
- DOMUtilsContext.text($ele, text);
2765
+ $el.forEach(($elItem) => {
2766
+ that.text($elItem, text);
1753
2767
  });
1754
2768
  }
1755
2769
  return;
1756
2770
  }
1757
2771
  if (text == null) {
1758
- return element.textContent || element.innerText;
2772
+ return $el.textContent || $el.innerText;
1759
2773
  }
1760
2774
  else {
1761
2775
  if (text instanceof Node) {
1762
2776
  text = text.textContent || text.innerText;
1763
2777
  }
1764
- if ("textContent" in element) {
1765
- element.textContent = text;
2778
+ if ("textContent" in $el) {
2779
+ $el.textContent = text;
1766
2780
  }
1767
- else if ("innerText" in element) {
1768
- element.innerText = text;
2781
+ else if ("innerText" in $el) {
2782
+ $el.innerText = text;
1769
2783
  }
1770
2784
  }
1771
2785
  }
1772
- html(element, html) {
1773
- const DOMUtilsContext = this;
1774
- if (typeof element === "string") {
1775
- element = DOMUtilsContext.selectorAll(element);
2786
+ html($el, html) {
2787
+ const that = this;
2788
+ if (typeof $el === "string") {
2789
+ $el = that.selectorAll($el);
1776
2790
  }
1777
- if (element == null) {
2791
+ if ($el == null) {
1778
2792
  return;
1779
2793
  }
1780
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2794
+ if (CommonUtils.isNodeList($el)) {
1781
2795
  if (html == null) {
1782
2796
  // 获取
1783
- return DOMUtilsContext.html(element[0]);
2797
+ return that.html($el[0]);
1784
2798
  }
1785
2799
  else {
1786
2800
  // 设置
1787
- element.forEach(($ele) => {
1788
- DOMUtilsContext.html($ele, html);
2801
+ $el.forEach(($elItem) => {
2802
+ that.html($elItem, html);
1789
2803
  });
1790
2804
  }
1791
2805
  return;
1792
2806
  }
1793
2807
  if (html == null) {
1794
2808
  // 获取
1795
- return element.innerHTML;
2809
+ return $el.innerHTML;
1796
2810
  }
1797
2811
  else {
1798
2812
  // 设置
1799
2813
  if (html instanceof Element) {
1800
2814
  html = html.innerHTML;
1801
2815
  }
1802
- if ("innerHTML" in element) {
1803
- DOMUtilsCommonUtils.setSafeHTML(element, html);
2816
+ if ("innerHTML" in $el) {
2817
+ CommonUtils.setSafeHTML($el, html);
1804
2818
  }
1805
2819
  }
1806
2820
  }
1807
2821
  /**
1808
2822
  * 获取移动元素的transform偏移
1809
2823
  */
1810
- getTransform(element, isShow = false) {
1811
- const DOMUtilsContext = this;
2824
+ getTransform($el, isShow = false) {
2825
+ const that = this;
1812
2826
  let transform_left = 0;
1813
2827
  let transform_top = 0;
1814
- if (!(isShow || (!isShow && DOMUtilsCommonUtils.isShow(element)))) {
2828
+ if (!(isShow || (!isShow && CommonUtils.isShow($el)))) {
1815
2829
  /* 未显示 */
1816
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
1817
- const transformInfo = DOMUtilsContext.getTransform(element, true);
2830
+ const { recovery } = CommonUtils.forceShow($el);
2831
+ const transformInfo = that.getTransform($el, true);
1818
2832
  recovery();
1819
2833
  return transformInfo;
1820
2834
  }
1821
- const elementTransform = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).transform;
2835
+ const elementTransform = that.windowApi.globalThis.getComputedStyle($el).transform;
1822
2836
  if (elementTransform != null && elementTransform !== "none" && elementTransform !== "") {
1823
2837
  const elementTransformSplit = elementTransform.match(/\((.+)\)/)?.[1].split(",");
1824
2838
  if (elementTransformSplit) {
@@ -1835,225 +2849,189 @@
1835
2849
  transformTop: transform_top,
1836
2850
  };
1837
2851
  }
1838
- val(element, value) {
1839
- const DOMUtilsContext = this;
1840
- if (typeof element === "string") {
1841
- element = DOMUtilsContext.selectorAll(element);
2852
+ val($el, value) {
2853
+ const that = this;
2854
+ if (typeof $el === "string") {
2855
+ $el = that.selectorAll($el);
1842
2856
  }
1843
- if (element == null) {
2857
+ if ($el == null) {
1844
2858
  return;
1845
2859
  }
1846
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2860
+ if (CommonUtils.isNodeList($el)) {
1847
2861
  if (value == null) {
1848
2862
  // 获取
1849
- return DOMUtilsContext.val(element[0]);
2863
+ return that.val($el[0]);
1850
2864
  }
1851
2865
  else {
1852
2866
  // 设置
1853
- element.forEach(($ele) => {
1854
- DOMUtilsContext.val($ele, value);
2867
+ $el.forEach(($elItem) => {
2868
+ that.val($elItem, value);
1855
2869
  });
1856
2870
  }
1857
2871
  return;
1858
2872
  }
1859
2873
  if (value == null) {
1860
2874
  // 获取
1861
- if (element.localName === "input" && (element.type === "checkbox" || element.type === "radio")) {
1862
- return element.checked;
2875
+ if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
2876
+ return $el.checked;
1863
2877
  }
1864
2878
  else {
1865
- return element.value;
2879
+ return $el.value;
1866
2880
  }
1867
2881
  }
1868
2882
  else {
1869
2883
  // 设置
1870
- if (element.localName === "input" && (element.type === "checkbox" || element.type === "radio")) {
1871
- element.checked = !!value;
2884
+ if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
2885
+ $el.checked = !!value;
1872
2886
  }
1873
2887
  else {
1874
- element.value = value;
2888
+ $el.value = value;
1875
2889
  }
1876
2890
  }
1877
2891
  }
1878
- prop(element, propName, propValue) {
1879
- const DOMUtilsContext = this;
1880
- if (typeof element === "string") {
1881
- element = DOMUtilsContext.selectorAll(element);
2892
+ prop($el, propName, propValue) {
2893
+ const that = this;
2894
+ if (typeof $el === "string") {
2895
+ $el = that.selectorAll($el);
1882
2896
  }
1883
- if (element == null) {
2897
+ if ($el == null) {
1884
2898
  return;
1885
2899
  }
1886
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2900
+ if (CommonUtils.isNodeList($el)) {
1887
2901
  if (propValue == null) {
1888
2902
  // 获取
1889
- return DOMUtilsContext.prop(element[0], propName);
2903
+ return that.prop($el[0], propName);
1890
2904
  }
1891
2905
  else {
1892
2906
  // 设置
1893
- element.forEach(($ele) => {
1894
- DOMUtilsContext.prop($ele, propName, propValue);
2907
+ $el.forEach(($elItem) => {
2908
+ that.prop($elItem, propName, propValue);
1895
2909
  });
1896
2910
  }
1897
2911
  return;
1898
2912
  }
1899
2913
  if (propValue == null) {
1900
- return Reflect.get(element, propName);
2914
+ return Reflect.get($el, propName);
1901
2915
  }
1902
2916
  else {
1903
- if (element instanceof Element && propName === "innerHTML") {
1904
- DOMUtilsContext.html(element, propValue);
2917
+ if ($el instanceof Element && propName === "innerHTML") {
2918
+ that.html($el, propValue);
1905
2919
  }
1906
2920
  else {
1907
- Reflect.set(element, propName, propValue);
2921
+ Reflect.set($el, propName, propValue);
1908
2922
  }
1909
2923
  }
1910
2924
  }
1911
2925
  /**
1912
2926
  * 移除元素的属性
1913
- * @param element 目标元素
2927
+ * @param $el 目标元素
1914
2928
  * @param attrName 属性名
1915
2929
  * @example
1916
2930
  * // 移除元素a.xx的属性data-value
1917
2931
  * DOMUtils.removeAttr(document.querySelector("a.xx"),"data-value")
1918
2932
  * DOMUtils.removeAttr("a.xx","data-value")
1919
2933
  * */
1920
- removeAttr(element, attrName) {
1921
- const DOMUtilsContext = this;
1922
- if (typeof element === "string") {
1923
- element = DOMUtilsContext.selectorAll(element);
2934
+ removeAttr($el, attrName) {
2935
+ const that = this;
2936
+ if (typeof $el === "string") {
2937
+ $el = that.selectorAll($el);
1924
2938
  }
1925
- if (element == null) {
2939
+ if ($el == null) {
1926
2940
  return;
1927
2941
  }
1928
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2942
+ if (CommonUtils.isNodeList($el)) {
1929
2943
  // 设置
1930
- element.forEach(($ele) => {
1931
- DOMUtilsContext.removeAttr($ele, attrName);
2944
+ $el.forEach(($elItem) => {
2945
+ that.removeAttr($elItem, attrName);
1932
2946
  });
1933
2947
  return;
1934
2948
  }
1935
- element.removeAttribute(attrName);
2949
+ $el.removeAttribute(attrName);
1936
2950
  }
1937
2951
  /**
1938
2952
  * 移除元素class名
1939
- * @param element 目标元素
2953
+ * @param $el 目标元素
1940
2954
  * @param className 类名
1941
2955
  * @example
1942
2956
  * // 移除元素a.xx的className为xx
1943
2957
  * DOMUtils.removeClass(document.querySelector("a.xx"),"xx")
1944
2958
  * DOMUtils.removeClass("a.xx","xx")
1945
2959
  */
1946
- removeClass(element, className) {
1947
- const DOMUtilsContext = this;
1948
- if (typeof element === "string") {
1949
- element = DOMUtilsContext.selectorAll(element);
2960
+ removeClass($el, className) {
2961
+ const that = this;
2962
+ if (typeof $el === "string") {
2963
+ $el = that.selectorAll($el);
1950
2964
  }
1951
- if (element == null) {
2965
+ if ($el == null) {
1952
2966
  return;
1953
2967
  }
1954
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2968
+ if (CommonUtils.isNodeList($el)) {
1955
2969
  // 设置
1956
- element.forEach(($ele) => {
1957
- DOMUtilsContext.removeClass($ele, className);
2970
+ $el.forEach(($elItem) => {
2971
+ that.removeClass($elItem, className);
1958
2972
  });
1959
2973
  return;
1960
2974
  }
1961
2975
  if (className == null) {
1962
2976
  // 清空全部className
1963
- element.className = "";
2977
+ $el.className = "";
1964
2978
  }
1965
2979
  else {
1966
2980
  if (!Array.isArray(className)) {
1967
2981
  className = className.trim().split(" ");
1968
2982
  }
1969
2983
  className.forEach((itemClassName) => {
1970
- element.classList.remove(itemClassName);
2984
+ $el.classList.remove(itemClassName);
1971
2985
  });
1972
2986
  }
1973
2987
  }
1974
2988
  /**
1975
2989
  * 移除元素的属性
1976
- * @param element 目标元素
2990
+ * @param $el 目标元素
1977
2991
  * @param propName 属性名
1978
2992
  * @example
1979
2993
  * // 移除元素a.xx的href属性
1980
2994
  * DOMUtils.removeProp(document.querySelector("a.xx"),"href")
1981
2995
  * DOMUtils.removeProp("a.xx","href")
1982
2996
  * */
1983
- removeProp(element, propName) {
1984
- const DOMUtilsContext = this;
1985
- if (typeof element === "string") {
1986
- element = DOMUtilsContext.selectorAll(element);
1987
- }
1988
- if (element == null) {
1989
- return;
1990
- }
1991
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1992
- // 设置
1993
- element.forEach(($ele) => {
1994
- DOMUtilsContext.removeProp($ele, propName);
1995
- });
1996
- return;
1997
- }
1998
- DOMUtilsCommonUtils.delete(element, propName);
1999
- }
2000
- /**
2001
- * 将一个元素替换为另一个元素
2002
- * @param element 目标元素
2003
- * @param newElement 新元素
2004
- * @example
2005
- * // 替换元素a.xx为b.xx
2006
- * DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
2007
- * DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
2008
- */
2009
- replaceWith(element, newElement) {
2010
- const DOMUtilsContext = this;
2011
- if (typeof element === "string") {
2012
- element = DOMUtilsContext.selectorAll(element);
2997
+ removeProp($el, propName) {
2998
+ const that = this;
2999
+ if (typeof $el === "string") {
3000
+ $el = that.selectorAll($el);
2013
3001
  }
2014
- if (element == null) {
3002
+ if ($el == null) {
2015
3003
  return;
2016
3004
  }
2017
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3005
+ if (CommonUtils.isNodeList($el)) {
2018
3006
  // 设置
2019
- element.forEach(($ele) => {
2020
- DOMUtilsContext.replaceWith($ele, newElement);
3007
+ $el.forEach(($elItem) => {
3008
+ that.removeProp($elItem, propName);
2021
3009
  });
2022
3010
  return;
2023
3011
  }
2024
- if (typeof newElement === "string") {
2025
- newElement = DOMUtilsContext.parseHTML(newElement, false, false);
2026
- }
2027
- const $parent = element.parentElement;
2028
- if ($parent) {
2029
- $parent.replaceChild(newElement, element);
2030
- }
2031
- else {
2032
- DOMUtilsContext.after(element, newElement);
2033
- element.remove();
2034
- }
3012
+ CommonUtils.delete($el, propName);
2035
3013
  }
2036
3014
  /**
2037
3015
  * 给元素添加class
2038
- * @param element 目标元素
3016
+ * @param $el 目标元素
2039
3017
  * @param className class名
2040
3018
  * @example
2041
3019
  * // 元素a.xx的className添加_vue_
2042
3020
  * DOMUtils.addClass(document.querySelector("a.xx"),"_vue_")
2043
3021
  * DOMUtils.addClass("a.xx","_vue_")
2044
3022
  * */
2045
- addClass(element, className) {
2046
- const DOMUtilsContext = this;
2047
- if (typeof element === "string") {
2048
- element = DOMUtilsContext.selectorAll(element);
3023
+ addClass($el, className) {
3024
+ const that = this;
3025
+ if (typeof $el === "string") {
3026
+ $el = that.selectorAll($el);
2049
3027
  }
2050
- if (element == null) {
3028
+ if ($el == null) {
2051
3029
  return;
2052
3030
  }
2053
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3031
+ if (CommonUtils.isNodeList($el)) {
2054
3032
  // 设置
2055
- element.forEach(($ele) => {
2056
- DOMUtilsContext.addClass($ele, className);
3033
+ $el.forEach(($elItem) => {
3034
+ that.addClass($elItem, className);
2057
3035
  });
2058
3036
  return;
2059
3037
  }
@@ -2064,31 +3042,31 @@
2064
3042
  if (itemClassName.trim() == "") {
2065
3043
  return;
2066
3044
  }
2067
- element.classList.add(itemClassName);
3045
+ $el.classList.add(itemClassName);
2068
3046
  });
2069
3047
  }
2070
3048
  /**
2071
3049
  * 判断元素是否存在className
2072
- * @param element
3050
+ * @param $el
2073
3051
  * @param className
2074
3052
  */
2075
- hasClass(element, className) {
2076
- const DOMUtilsContext = this;
2077
- if (typeof element === "string") {
2078
- element = DOMUtilsContext.selectorAll(element);
3053
+ hasClass($el, className) {
3054
+ const that = this;
3055
+ if (typeof $el === "string") {
3056
+ $el = that.selectorAll($el);
2079
3057
  }
2080
- if (element == null) {
3058
+ if ($el == null) {
2081
3059
  return false;
2082
3060
  }
2083
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3061
+ if (CommonUtils.isNodeList($el)) {
2084
3062
  let flag = true;
2085
- for (let index = 0; index < element.length; index++) {
2086
- const $ele = element[index];
2087
- flag = flag && DOMUtilsContext.hasClass($ele, className);
3063
+ for (let index = 0; index < $el.length; index++) {
3064
+ const $elItem = $el[index];
3065
+ flag = flag && that.hasClass($elItem, className);
2088
3066
  }
2089
3067
  return flag;
2090
3068
  }
2091
- if (!element?.classList) {
3069
+ if (!$el?.classList) {
2092
3070
  return false;
2093
3071
  }
2094
3072
  if (!Array.isArray(className)) {
@@ -2096,7 +3074,7 @@
2096
3074
  }
2097
3075
  for (let index = 0; index < className.length; index++) {
2098
3076
  const item = className[index].trim();
2099
- if (!element.classList.contains(item)) {
3077
+ if (!$el.classList.contains(item)) {
2100
3078
  return false;
2101
3079
  }
2102
3080
  }
@@ -2104,25 +3082,25 @@
2104
3082
  }
2105
3083
  /**
2106
3084
  * 函数在元素内部末尾添加子元素或HTML字符串
2107
- * @param element 目标元素
3085
+ * @param $el 目标元素
2108
3086
  * @param content 子元素或HTML字符串
2109
3087
  * @example
2110
3088
  * // 元素a.xx的内部末尾添加一个元素
2111
3089
  * DOMUtils.append(document.querySelector("a.xx"),document.querySelector("b.xx"))
2112
3090
  * DOMUtils.append("a.xx","'<b class="xx"></b>")
2113
3091
  * */
2114
- append(element, content) {
2115
- const DOMUtilsContext = this;
2116
- if (typeof element === "string") {
2117
- element = DOMUtilsContext.selectorAll(element);
3092
+ append($el, content) {
3093
+ const that = this;
3094
+ if (typeof $el === "string") {
3095
+ $el = that.selectorAll($el);
2118
3096
  }
2119
- if (element == null) {
3097
+ if ($el == null) {
2120
3098
  return;
2121
3099
  }
2122
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3100
+ if (CommonUtils.isNodeList($el)) {
2123
3101
  // 设置
2124
- element.forEach(($ele) => {
2125
- DOMUtilsContext.append($ele, content);
3102
+ $el.forEach(($elItem) => {
3103
+ that.append($elItem, content);
2126
3104
  });
2127
3105
  return;
2128
3106
  }
@@ -2130,12 +3108,12 @@
2130
3108
  if (typeof content === "string") {
2131
3109
  if (ele instanceof DocumentFragment) {
2132
3110
  if (typeof text === "string") {
2133
- text = DOMUtilsContext.parseHTML(text, true, false);
3111
+ text = that.toElement(text, true, false);
2134
3112
  }
2135
3113
  ele.appendChild(text);
2136
3114
  }
2137
3115
  else {
2138
- ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text));
3116
+ ele.insertAdjacentHTML("beforeend", CommonUtils.createSafeHTML(text));
2139
3117
  }
2140
3118
  }
2141
3119
  else {
@@ -2144,96 +3122,96 @@
2144
3122
  }
2145
3123
  if (Array.isArray(content) || content instanceof NodeList) {
2146
3124
  /* 数组 */
2147
- const fragment = DOMUtilsContext.windowApi.document.createDocumentFragment();
3125
+ const fragment = that.windowApi.document.createDocumentFragment();
2148
3126
  content.forEach((ele) => {
2149
3127
  if (typeof ele === "string") {
2150
3128
  // 转为元素
2151
- ele = DOMUtilsContext.parseHTML(ele, true, false);
3129
+ ele = that.toElement(ele, true, false);
2152
3130
  }
2153
3131
  fragment.appendChild(ele);
2154
3132
  });
2155
- element.appendChild(fragment);
3133
+ $el.appendChild(fragment);
2156
3134
  }
2157
3135
  else {
2158
- elementAppendChild(element, content);
3136
+ elementAppendChild($el, content);
2159
3137
  }
2160
3138
  }
2161
3139
  /**
2162
3140
  * 函数 在元素内部开头添加子元素或HTML字符串
2163
- * @param element 目标元素
3141
+ * @param $el 目标元素
2164
3142
  * @param content 子元素或HTML字符串
2165
3143
  * @example
2166
3144
  * // 元素a.xx内部开头添加一个元素
2167
3145
  * DOMUtils.prepend(document.querySelector("a.xx"),document.querySelector("b.xx"))
2168
3146
  * DOMUtils.prepend("a.xx","'<b class="xx"></b>")
2169
3147
  * */
2170
- prepend(element, content) {
2171
- const DOMUtilsContext = this;
2172
- if (typeof element === "string") {
2173
- element = DOMUtilsContext.selectorAll(element);
3148
+ prepend($el, content) {
3149
+ const that = this;
3150
+ if (typeof $el === "string") {
3151
+ $el = that.selectorAll($el);
2174
3152
  }
2175
- if (element == null) {
3153
+ if ($el == null) {
2176
3154
  return;
2177
3155
  }
2178
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3156
+ if (CommonUtils.isNodeList($el)) {
2179
3157
  // 设置
2180
- element.forEach(($ele) => {
2181
- DOMUtilsContext.prepend($ele, content);
3158
+ $el.forEach(($elItem) => {
3159
+ that.prepend($elItem, content);
2182
3160
  });
2183
3161
  return;
2184
3162
  }
2185
3163
  if (typeof content === "string") {
2186
- if (element instanceof DocumentFragment) {
2187
- content = DOMUtilsContext.parseHTML(content, true, false);
2188
- element.prepend(content);
3164
+ if ($el instanceof DocumentFragment) {
3165
+ content = that.toElement(content, true, false);
3166
+ $el.prepend(content);
2189
3167
  }
2190
3168
  else {
2191
- element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
3169
+ $el.insertAdjacentHTML("afterbegin", CommonUtils.createSafeHTML(content));
2192
3170
  }
2193
3171
  }
2194
3172
  else {
2195
- const $firstChild = element.firstChild;
3173
+ const $firstChild = $el.firstChild;
2196
3174
  if ($firstChild == null) {
2197
- element.prepend(content);
3175
+ $el.prepend(content);
2198
3176
  }
2199
3177
  else {
2200
- element.insertBefore(content, $firstChild);
3178
+ $el.insertBefore(content, $firstChild);
2201
3179
  }
2202
3180
  }
2203
3181
  }
2204
3182
  /**
2205
3183
  * 在元素后面添加兄弟元素或HTML字符串
2206
- * @param element 目标元素
3184
+ * @param $el 目标元素
2207
3185
  * @param content 兄弟元素或HTML字符串
2208
3186
  * @example
2209
3187
  * // 元素a.xx后面添加一个元素
2210
3188
  * DOMUtils.after(document.querySelector("a.xx"),document.querySelector("b.xx"))
2211
3189
  * DOMUtils.after("a.xx","'<b class="xx"></b>")
2212
3190
  * */
2213
- after(element, content) {
2214
- const DOMUtilsContext = this;
2215
- if (typeof element === "string") {
2216
- element = DOMUtilsContext.selectorAll(element);
3191
+ after($el, content) {
3192
+ const that = this;
3193
+ if (typeof $el === "string") {
3194
+ $el = that.selectorAll($el);
2217
3195
  }
2218
- if (element == null) {
3196
+ if ($el == null) {
2219
3197
  return;
2220
3198
  }
2221
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3199
+ if (CommonUtils.isNodeList($el)) {
2222
3200
  // 设置
2223
- element.forEach(($ele) => {
2224
- DOMUtilsContext.after($ele, content);
3201
+ $el.forEach(($elItem) => {
3202
+ that.after($elItem, content);
2225
3203
  });
2226
3204
  return;
2227
3205
  }
2228
3206
  if (typeof content === "string") {
2229
- element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
3207
+ $el.insertAdjacentHTML("afterend", CommonUtils.createSafeHTML(content));
2230
3208
  }
2231
3209
  else {
2232
- const $parent = element.parentElement;
2233
- const $nextSlibling = element.nextSibling;
3210
+ const $parent = $el.parentElement;
3211
+ const $nextSlibling = $el.nextSibling;
2234
3212
  if (!$parent || $nextSlibling) {
2235
3213
  // 任意一个不行
2236
- element.after(content);
3214
+ $el.after(content);
2237
3215
  }
2238
3216
  else {
2239
3217
  $parent.insertBefore(content, $nextSlibling);
@@ -2242,157 +3220,157 @@
2242
3220
  }
2243
3221
  /**
2244
3222
  * 在元素前面添加兄弟元素或HTML字符串
2245
- * @param element 目标元素
3223
+ * @param $el 目标元素
2246
3224
  * @param content 兄弟元素或HTML字符串
2247
3225
  * @example
2248
3226
  * // 元素a.xx前面添加一个元素
2249
3227
  * DOMUtils.before(document.querySelector("a.xx"),document.querySelector("b.xx"))
2250
3228
  * DOMUtils.before("a.xx","'<b class="xx"></b>")
2251
3229
  * */
2252
- before(element, content) {
2253
- const DOMUtilsContext = this;
2254
- if (typeof element === "string") {
2255
- element = DOMUtilsContext.selectorAll(element);
3230
+ before($el, content) {
3231
+ const that = this;
3232
+ if (typeof $el === "string") {
3233
+ $el = that.selectorAll($el);
2256
3234
  }
2257
- if (element == null) {
3235
+ if ($el == null) {
2258
3236
  return;
2259
3237
  }
2260
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3238
+ if (CommonUtils.isNodeList($el)) {
2261
3239
  // 设置
2262
- element.forEach(($ele) => {
2263
- DOMUtilsContext.before($ele, content);
3240
+ $el.forEach(($elItem) => {
3241
+ that.before($elItem, content);
2264
3242
  });
2265
3243
  return;
2266
3244
  }
2267
3245
  if (typeof content === "string") {
2268
- element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
3246
+ $el.insertAdjacentHTML("beforebegin", CommonUtils.createSafeHTML(content));
2269
3247
  }
2270
3248
  else {
2271
- const $parent = element.parentElement;
3249
+ const $parent = $el.parentElement;
2272
3250
  if (!$parent) {
2273
- element.before(content);
3251
+ $el.before(content);
2274
3252
  }
2275
3253
  else {
2276
- $parent.insertBefore(content, element);
3254
+ $parent.insertBefore(content, $el);
2277
3255
  }
2278
3256
  }
2279
3257
  }
2280
3258
  /**
2281
3259
  * 移除元素
2282
- * @param element 目标元素
3260
+ * @param $el 目标元素
2283
3261
  * @example
2284
3262
  * // 元素a.xx前面添加一个元素
2285
3263
  * DOMUtils.remove(document.querySelector("a.xx"))
2286
3264
  * DOMUtils.remove(document.querySelectorAll("a.xx"))
2287
3265
  * DOMUtils.remove("a.xx")
2288
3266
  * */
2289
- remove(element) {
2290
- const DOMUtilsContext = this;
2291
- if (typeof element === "string") {
2292
- element = DOMUtilsContext.selectorAll(element);
3267
+ remove($el) {
3268
+ const that = this;
3269
+ if (typeof $el === "string") {
3270
+ $el = that.selectorAll($el);
2293
3271
  }
2294
- if (element == null) {
3272
+ if ($el == null) {
2295
3273
  return;
2296
3274
  }
2297
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2298
- element.forEach(($ele) => {
2299
- DOMUtilsContext.remove($ele);
3275
+ if (CommonUtils.isNodeList($el)) {
3276
+ $el.forEach(($elItem) => {
3277
+ that.remove($elItem);
2300
3278
  });
2301
3279
  return;
2302
3280
  }
2303
- if (typeof element.remove === "function") {
2304
- element.remove();
3281
+ if (typeof $el.remove === "function") {
3282
+ $el.remove();
2305
3283
  }
2306
- else if (element.parentElement) {
2307
- element.parentElement.removeChild(element);
3284
+ else if ($el.parentElement) {
3285
+ $el.parentElement.removeChild($el);
2308
3286
  }
2309
- else if (element.parentNode) {
2310
- element.parentNode.removeChild(element);
3287
+ else if ($el.parentNode) {
3288
+ $el.parentNode.removeChild($el);
2311
3289
  }
2312
3290
  }
2313
3291
  /**
2314
3292
  * 移除元素的所有子元素
2315
- * @param element 目标元素
3293
+ * @param $el 目标元素
2316
3294
  * @example
2317
3295
  * // 移除元素a.xx元素的所有子元素
2318
3296
  * DOMUtils.empty(document.querySelector("a.xx"))
2319
3297
  * DOMUtils.empty("a.xx")
2320
3298
  * */
2321
- empty(element) {
2322
- const DOMUtilsContext = this;
2323
- if (typeof element === "string") {
2324
- element = DOMUtilsContext.selectorAll(element);
3299
+ empty($el) {
3300
+ const that = this;
3301
+ if (typeof $el === "string") {
3302
+ $el = that.selectorAll($el);
2325
3303
  }
2326
- if (element == null) {
3304
+ if ($el == null) {
2327
3305
  return;
2328
3306
  }
2329
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3307
+ if (CommonUtils.isNodeList($el)) {
2330
3308
  // 设置
2331
- element.forEach(($ele) => {
2332
- DOMUtilsContext.empty($ele);
3309
+ $el.forEach(($elItem) => {
3310
+ that.empty($elItem);
2333
3311
  });
2334
3312
  return;
2335
3313
  }
2336
- if (element.innerHTML) {
2337
- element.innerHTML = "";
3314
+ if ($el.innerHTML) {
3315
+ $el.innerHTML = "";
2338
3316
  }
2339
- else if (element.textContent) {
2340
- element.textContent = "";
3317
+ else if ($el.textContent) {
3318
+ $el.textContent = "";
2341
3319
  }
2342
3320
  }
2343
3321
  /**
2344
3322
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
2345
- * @param element 目标元素
3323
+ * @param $el 目标元素
2346
3324
  * @example
2347
3325
  * // 获取元素a.xx的对于文档的偏移坐标
2348
3326
  * DOMUtils.offset(document.querySelector("a.xx"))
2349
3327
  * DOMUtils.offset("a.xx")
2350
3328
  * > 0
2351
3329
  */
2352
- offset(element) {
2353
- const DOMUtilsContext = this;
2354
- if (typeof element === "string") {
2355
- element = DOMUtilsContext.selector(element);
3330
+ offset($el) {
3331
+ const that = this;
3332
+ if (typeof $el === "string") {
3333
+ $el = that.selector($el);
2356
3334
  }
2357
- if (element == null) {
3335
+ if ($el == null) {
2358
3336
  return;
2359
3337
  }
2360
- const rect = element.getBoundingClientRect();
3338
+ const rect = $el.getBoundingClientRect();
2361
3339
  return {
2362
3340
  /** y轴偏移 */
2363
- top: rect.top + DOMUtilsContext.windowApi.globalThis.scrollY,
3341
+ top: rect.top + that.windowApi.globalThis.scrollY,
2364
3342
  /** x轴偏移 */
2365
- left: rect.left + DOMUtilsContext.windowApi.globalThis.scrollX,
3343
+ left: rect.left + that.windowApi.globalThis.scrollX,
2366
3344
  };
2367
3345
  }
2368
- width(element, isShow = false) {
2369
- const DOMUtilsContext = this;
2370
- if (typeof element === "string") {
2371
- element = DOMUtilsContext.selector(element);
3346
+ width($el, isShow = false) {
3347
+ const that = this;
3348
+ if (typeof $el === "string") {
3349
+ $el = that.selector($el);
2372
3350
  }
2373
- if (DOMUtilsCommonUtils.isWin(element)) {
2374
- return DOMUtilsContext.windowApi.window.document.documentElement.clientWidth;
3351
+ if (CommonUtils.isWin($el)) {
3352
+ return that.windowApi.window.document.documentElement.clientWidth;
2375
3353
  }
2376
- if (element.nodeType === 9) {
3354
+ if ($el.nodeType === 9) {
2377
3355
  /* Document文档节点 */
2378
- element = element;
2379
- return Math.max(element.body.scrollWidth, element.documentElement.scrollWidth, element.body.offsetWidth, element.documentElement.offsetWidth, element.documentElement.clientWidth);
3356
+ $el = $el;
3357
+ return Math.max($el.body.scrollWidth, $el.documentElement.scrollWidth, $el.body.offsetWidth, $el.documentElement.offsetWidth, $el.documentElement.clientWidth);
2380
3358
  }
2381
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
3359
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
2382
3360
  /* 已显示 */
2383
3361
  /* 不从style中获取对应的宽度,因为可能使用了class定义了width !important */
2384
- element = element;
3362
+ $el = $el;
2385
3363
  /* 如果element.style.width为空 则从css里面获取是否定义了width信息如果定义了 则读取css里面定义的宽度width */
2386
- if (parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "width").toString()) > 0) {
2387
- return parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "width").toString());
3364
+ if (parseFloat(CommonUtils.getStyleValue($el, "width").toString()) > 0) {
3365
+ return parseFloat(CommonUtils.getStyleValue($el, "width").toString());
2388
3366
  }
2389
3367
  /* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetWidth来进行计算 */
2390
- if (element.offsetWidth > 0) {
2391
- const borderLeftWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderLeftWidth");
2392
- const borderRightWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderRightWidth");
2393
- const paddingLeft = DOMUtilsCommonUtils.getStyleValue(element, "paddingLeft");
2394
- const paddingRight = DOMUtilsCommonUtils.getStyleValue(element, "paddingRight");
2395
- const backHeight = parseFloat(element.offsetWidth.toString()) -
3368
+ if ($el.offsetWidth > 0) {
3369
+ const borderLeftWidth = CommonUtils.getStyleValue($el, "borderLeftWidth");
3370
+ const borderRightWidth = CommonUtils.getStyleValue($el, "borderRightWidth");
3371
+ const paddingLeft = CommonUtils.getStyleValue($el, "paddingLeft");
3372
+ const paddingRight = CommonUtils.getStyleValue($el, "paddingRight");
3373
+ const backHeight = parseFloat($el.offsetWidth.toString()) -
2396
3374
  parseFloat(borderLeftWidth.toString()) -
2397
3375
  parseFloat(borderRightWidth.toString()) -
2398
3376
  parseFloat(paddingLeft.toString()) -
@@ -2403,41 +3381,41 @@
2403
3381
  }
2404
3382
  else {
2405
3383
  /* 未显示 */
2406
- element = element;
2407
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2408
- const width = DOMUtilsContext.width(element, true);
3384
+ $el = $el;
3385
+ const { recovery } = CommonUtils.forceShow($el);
3386
+ const width = that.width($el, true);
2409
3387
  recovery();
2410
3388
  return width;
2411
3389
  }
2412
3390
  }
2413
- height(element, isShow = false) {
2414
- const DOMUtilsContext = this;
2415
- if (DOMUtilsCommonUtils.isWin(element)) {
2416
- return DOMUtilsContext.windowApi.window.document.documentElement.clientHeight;
3391
+ height($el, isShow = false) {
3392
+ const that = this;
3393
+ if (CommonUtils.isWin($el)) {
3394
+ return that.windowApi.window.document.documentElement.clientHeight;
2417
3395
  }
2418
- if (typeof element === "string") {
2419
- element = DOMUtilsContext.selector(element);
3396
+ if (typeof $el === "string") {
3397
+ $el = that.selector($el);
2420
3398
  }
2421
- if (element.nodeType === 9) {
2422
- element = element;
3399
+ if ($el.nodeType === 9) {
3400
+ $el = $el;
2423
3401
  /* Document文档节点 */
2424
- return Math.max(element.body.scrollHeight, element.documentElement.scrollHeight, element.body.offsetHeight, element.documentElement.offsetHeight, element.documentElement.clientHeight);
3402
+ return Math.max($el.body.scrollHeight, $el.documentElement.scrollHeight, $el.body.offsetHeight, $el.documentElement.offsetHeight, $el.documentElement.clientHeight);
2425
3403
  }
2426
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
2427
- element = element;
3404
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
3405
+ $el = $el;
2428
3406
  /* 已显示 */
2429
3407
  /* 从style中获取对应的高度,因为可能使用了class定义了width !important */
2430
3408
  /* 如果element.style.height为空 则从css里面获取是否定义了height信息如果定义了 则读取css里面定义的高度height */
2431
- if (parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "height").toString()) > 0) {
2432
- return parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "height").toString());
3409
+ if (parseFloat(CommonUtils.getStyleValue($el, "height").toString()) > 0) {
3410
+ return parseFloat(CommonUtils.getStyleValue($el, "height").toString());
2433
3411
  }
2434
3412
  /* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetHeight来进行计算 */
2435
- if (element.offsetHeight > 0) {
2436
- const borderTopWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderTopWidth");
2437
- const borderBottomWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderBottomWidth");
2438
- const paddingTop = DOMUtilsCommonUtils.getStyleValue(element, "paddingTop");
2439
- const paddingBottom = DOMUtilsCommonUtils.getStyleValue(element, "paddingBottom");
2440
- const backHeight = parseFloat(element.offsetHeight.toString()) -
3413
+ if ($el.offsetHeight > 0) {
3414
+ const borderTopWidth = CommonUtils.getStyleValue($el, "borderTopWidth");
3415
+ const borderBottomWidth = CommonUtils.getStyleValue($el, "borderBottomWidth");
3416
+ const paddingTop = CommonUtils.getStyleValue($el, "paddingTop");
3417
+ const paddingBottom = CommonUtils.getStyleValue($el, "paddingBottom");
3418
+ const backHeight = parseFloat($el.offsetHeight.toString()) -
2441
3419
  parseFloat(borderTopWidth.toString()) -
2442
3420
  parseFloat(borderBottomWidth.toString()) -
2443
3421
  parseFloat(paddingTop.toString()) -
@@ -2448,200 +3426,160 @@
2448
3426
  }
2449
3427
  else {
2450
3428
  /* 未显示 */
2451
- element = element;
2452
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2453
- const height = DOMUtilsContext.height(element, true);
3429
+ $el = $el;
3430
+ const { recovery } = CommonUtils.forceShow($el);
3431
+ const height = that.height($el, true);
2454
3432
  recovery();
2455
3433
  return height;
2456
3434
  }
2457
3435
  }
2458
- outerWidth(element, isShow = false) {
2459
- const DOMUtilsContext = this;
2460
- if (DOMUtilsCommonUtils.isWin(element)) {
2461
- return DOMUtilsContext.windowApi.window.innerWidth;
3436
+ outerWidth($el, isShow = false) {
3437
+ const that = this;
3438
+ if (CommonUtils.isWin($el)) {
3439
+ return that.windowApi.window.innerWidth;
2462
3440
  }
2463
- if (typeof element === "string") {
2464
- element = DOMUtilsContext.selector(element);
3441
+ if (typeof $el === "string") {
3442
+ $el = that.selector($el);
2465
3443
  }
2466
- element = element;
2467
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
2468
- const style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
2469
- const marginLeft = DOMUtilsCommonUtils.getStyleValue(style, "marginLeft");
2470
- const marginRight = DOMUtilsCommonUtils.getStyleValue(style, "marginRight");
2471
- return element.offsetWidth + marginLeft + marginRight;
3444
+ $el = $el;
3445
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
3446
+ const style = that.windowApi.globalThis.getComputedStyle($el, null);
3447
+ const marginLeft = CommonUtils.getStyleValue(style, "marginLeft");
3448
+ const marginRight = CommonUtils.getStyleValue(style, "marginRight");
3449
+ return $el.offsetWidth + marginLeft + marginRight;
2472
3450
  }
2473
3451
  else {
2474
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2475
- const outerWidth = DOMUtilsContext.outerWidth(element, true);
3452
+ const { recovery } = CommonUtils.forceShow($el);
3453
+ const outerWidth = that.outerWidth($el, true);
2476
3454
  recovery();
2477
3455
  return outerWidth;
2478
3456
  }
2479
3457
  }
2480
- outerHeight(element, isShow = false) {
2481
- const DOMUtilsContext = this;
2482
- if (DOMUtilsCommonUtils.isWin(element)) {
2483
- return DOMUtilsContext.windowApi.window.innerHeight;
3458
+ outerHeight($el, isShow = false) {
3459
+ const that = this;
3460
+ if (CommonUtils.isWin($el)) {
3461
+ return that.windowApi.window.innerHeight;
2484
3462
  }
2485
- if (typeof element === "string") {
2486
- element = DOMUtilsContext.selector(element);
3463
+ if (typeof $el === "string") {
3464
+ $el = that.selector($el);
2487
3465
  }
2488
- element = element;
2489
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
2490
- const style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
2491
- const marginTop = DOMUtilsCommonUtils.getStyleValue(style, "marginTop");
2492
- const marginBottom = DOMUtilsCommonUtils.getStyleValue(style, "marginBottom");
2493
- return element.offsetHeight + marginTop + marginBottom;
3466
+ $el = $el;
3467
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
3468
+ const style = that.windowApi.globalThis.getComputedStyle($el, null);
3469
+ const marginTop = CommonUtils.getStyleValue(style, "marginTop");
3470
+ const marginBottom = CommonUtils.getStyleValue(style, "marginBottom");
3471
+ return $el.offsetHeight + marginTop + marginBottom;
2494
3472
  }
2495
3473
  else {
2496
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
2497
- const outerHeight = DOMUtilsContext.outerHeight(element, true);
3474
+ const { recovery } = CommonUtils.forceShow($el);
3475
+ const outerHeight = that.outerHeight($el, true);
2498
3476
  recovery();
2499
3477
  return outerHeight;
2500
3478
  }
2501
3479
  }
2502
3480
  /**
2503
- * 在一定时间内改变元素的样式属性,实现动画效果
2504
- * @param element 需要进行动画的元素
2505
- * @param styles 动画结束时元素的样式属性
2506
- * @param duration 动画持续时间,单位为毫秒
2507
- * @param callback 动画结束后执行的函数
3481
+ * 将一个元素替换为另一个元素
3482
+ * @param $el 目标元素
3483
+ * @param $newEl 新元素
2508
3484
  * @example
2509
- * // 监听元素a.xx的从显示变为隐藏
2510
- * DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
2511
- * console.log("已往上位移100px")
2512
- * })
3485
+ * // 替换元素a.xx为b.xx
3486
+ * DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
3487
+ * DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
2513
3488
  */
2514
- animate(element, styles, duration = 1000, callback = null) {
2515
- const DOMUtilsContext = this;
2516
- if (typeof element === "string") {
2517
- element = DOMUtilsContext.selectorAll(element);
3489
+ replaceWith($el, $newEl) {
3490
+ const that = this;
3491
+ if (typeof $el === "string") {
3492
+ $el = that.selectorAll($el);
2518
3493
  }
2519
- if (element == null) {
3494
+ if ($el == null) {
2520
3495
  return;
2521
3496
  }
2522
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3497
+ if (CommonUtils.isNodeList($el)) {
2523
3498
  // 设置
2524
- element.forEach(($ele) => {
2525
- DOMUtilsContext.animate($ele, styles, duration, callback);
3499
+ $el.forEach(($elItem) => {
3500
+ that.replaceWith($elItem, $newEl);
2526
3501
  });
2527
3502
  return;
2528
3503
  }
2529
- if (typeof duration !== "number" || duration <= 0) {
2530
- throw new TypeError("duration must be a positive number");
2531
- }
2532
- if (typeof callback !== "function" && callback !== void 0) {
2533
- throw new TypeError("callback must be a function or null");
2534
- }
2535
- if (typeof styles !== "object" || styles === void 0) {
2536
- throw new TypeError("styles must be an object");
3504
+ if (typeof $newEl === "string") {
3505
+ $newEl = that.toElement($newEl, false, false);
2537
3506
  }
2538
- if (Object.keys(styles).length === 0) {
2539
- throw new Error("styles must contain at least one property");
3507
+ const $parent = $el.parentElement;
3508
+ if ($parent) {
3509
+ $parent.replaceChild($newEl, $el);
2540
3510
  }
2541
- const start = performance.now();
2542
- const from = {};
2543
- const to = {};
2544
- for (const prop in styles) {
2545
- from[prop] = element.style[prop] || DOMUtilsContext.windowApi.globalThis.getComputedStyle(element)[prop];
2546
- to[prop] = styles[prop];
3511
+ else {
3512
+ that.after($el, $newEl);
3513
+ $el.remove();
2547
3514
  }
2548
- const timer = DOMUtilsCommonUtils.setInterval(function () {
2549
- const timePassed = performance.now() - start;
2550
- let progress = timePassed / duration;
2551
- if (progress > 1) {
2552
- progress = 1;
2553
- }
2554
- for (const prop in styles) {
2555
- element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
2556
- }
2557
- if (progress === 1) {
2558
- DOMUtilsCommonUtils.clearInterval(timer);
2559
- if (callback) {
2560
- callback();
2561
- }
2562
- }
2563
- }, 10);
2564
3515
  }
2565
3516
  /**
2566
3517
  * 将一个元素包裹在指定的HTML元素中
2567
- * @param element 要包裹的元素
3518
+ * @param $el 要包裹的元素
2568
3519
  * @param wrapperHTML 要包裹的HTML元素的字符串表示形式
2569
3520
  * @example
2570
3521
  * // 将a.xx元素外面包裹一层div
2571
3522
  * DOMUtils.wrap(document.querySelector("a.xx"),"<div></div>")
2572
3523
  */
2573
- wrap(element, wrapperHTML) {
2574
- const DOMUtilsContext = this;
2575
- if (typeof element === "string") {
2576
- element = DOMUtilsContext.selectorAll(element);
3524
+ wrap($el, wrapperHTML) {
3525
+ const that = this;
3526
+ if (typeof $el === "string") {
3527
+ $el = that.selectorAll($el);
2577
3528
  }
2578
- if (element == null) {
3529
+ if ($el == null) {
2579
3530
  return;
2580
3531
  }
2581
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3532
+ if (CommonUtils.isNodeList($el)) {
2582
3533
  // 设置
2583
- element.forEach(($ele) => {
2584
- DOMUtilsContext.wrap($ele, wrapperHTML);
3534
+ $el.forEach(($elItem) => {
3535
+ that.wrap($elItem, wrapperHTML);
2585
3536
  });
2586
3537
  return;
2587
3538
  }
2588
- element = element;
3539
+ $el = $el;
2589
3540
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
2590
- const wrapper = DOMUtilsContext.windowApi.document.createElement("div");
2591
- DOMUtilsContext.html(wrapper, wrapperHTML);
2592
- const wrapperFirstChild = wrapper.firstChild;
3541
+ const $wrapper = that.windowApi.document.createElement("div");
3542
+ that.html($wrapper, wrapperHTML);
3543
+ const wrapperFirstChild = $wrapper.firstChild;
2593
3544
  // 将要包裹的元素插入目标元素前面
2594
- const parentElement = element.parentElement;
2595
- parentElement.insertBefore(wrapperFirstChild, element);
3545
+ const parentElement = $el.parentElement;
3546
+ parentElement.insertBefore(wrapperFirstChild, $el);
2596
3547
  // 将要包裹的元素移动到wrapper中
2597
- wrapperFirstChild.appendChild(element);
3548
+ wrapperFirstChild.appendChild($el);
2598
3549
  }
2599
- prev(element) {
2600
- const DOMUtilsContext = this;
2601
- if (typeof element === "string") {
2602
- element = DOMUtilsContext.selector(element);
3550
+ prev($el) {
3551
+ const that = this;
3552
+ if (typeof $el === "string") {
3553
+ $el = that.selector($el);
2603
3554
  }
2604
- if (element == null) {
3555
+ if ($el == null) {
2605
3556
  return;
2606
3557
  }
2607
- return element.previousElementSibling;
3558
+ return $el.previousElementSibling;
2608
3559
  }
2609
- next(element) {
2610
- const DOMUtilsContext = this;
2611
- if (typeof element === "string") {
2612
- element = DOMUtilsContext.selector(element);
3560
+ next($el) {
3561
+ const that = this;
3562
+ if (typeof $el === "string") {
3563
+ $el = that.selector($el);
2613
3564
  }
2614
- if (element == null) {
3565
+ if ($el == null) {
2615
3566
  return;
2616
3567
  }
2617
- return element.nextElementSibling;
2618
- }
2619
- /**
2620
- * 取消挂载在window下的DOMUtils并返回DOMUtils
2621
- * @example
2622
- * let DOMUtils = window.DOMUtils.noConflict()
2623
- */
2624
- noConflict() {
2625
- const DOMUtilsContext = this;
2626
- if (DOMUtilsContext.windowApi.window.DOMUtils) {
2627
- DOMUtilsCommonUtils.delete(window, "DOMUtils");
2628
- }
2629
- DOMUtilsContext.windowApi.window.DOMUtils = this;
2630
- return this;
3568
+ return $el.nextElementSibling;
2631
3569
  }
2632
- siblings(element) {
2633
- const DOMUtilsContext = this;
2634
- if (typeof element === "string") {
2635
- element = DOMUtilsContext.selector(element);
3570
+ siblings($el) {
3571
+ const that = this;
3572
+ if (typeof $el === "string") {
3573
+ $el = that.selector($el);
2636
3574
  }
2637
- if (element == null) {
3575
+ if ($el == null) {
2638
3576
  return;
2639
3577
  }
2640
- return Array.from(element.parentElement.children).filter((child) => child !== element);
3578
+ return Array.from($el.parentElement.children).filter(($child) => $child !== $el);
2641
3579
  }
2642
3580
  /**
2643
3581
  * 获取当前元素的父元素
2644
- * @param element 当前元素
3582
+ * @param $el 当前元素
2645
3583
  * @returns 父元素
2646
3584
  * @example
2647
3585
  * // 获取a.xx元素的父元素
@@ -2649,27 +3587,27 @@
2649
3587
  * DOMUtils.parent("a.xx")
2650
3588
  * > <div ...>....</div>
2651
3589
  */
2652
- parent(element) {
2653
- const DOMUtilsContext = this;
2654
- if (typeof element === "string") {
2655
- element = DOMUtilsContext.selector(element);
3590
+ parent($el) {
3591
+ const that = this;
3592
+ if (typeof $el === "string") {
3593
+ $el = that.selector($el);
2656
3594
  }
2657
- if (element == null) {
3595
+ if ($el == null) {
2658
3596
  return;
2659
3597
  }
2660
- if (DOMUtilsCommonUtils.isNodeList(element)) {
3598
+ if (CommonUtils.isNodeList($el)) {
2661
3599
  const resultArray = [];
2662
- element.forEach(($ele) => {
2663
- resultArray.push(DOMUtilsContext.parent($ele));
3600
+ $el.forEach(($elItem) => {
3601
+ resultArray.push(that.parent($elItem));
2664
3602
  });
2665
3603
  return resultArray;
2666
3604
  }
2667
3605
  else {
2668
- return element.parentElement;
3606
+ return $el.parentElement;
2669
3607
  }
2670
3608
  }
2671
- parseHTML(html, useParser = false, isComplete = false) {
2672
- const DOMUtilsContext = this;
3609
+ toElement(html, useParser = false, isComplete = false) {
3610
+ const that = this;
2673
3611
  // 去除html前后的空格
2674
3612
  html = html.trim();
2675
3613
  function parseHTMLByDOMParser() {
@@ -2682,13 +3620,13 @@
2682
3620
  }
2683
3621
  }
2684
3622
  function parseHTMLByCreateDom() {
2685
- const tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
2686
- DOMUtilsContext.html(tempDIV, html);
3623
+ const $el = that.windowApi.document.createElement("div");
3624
+ that.html($el, html);
2687
3625
  if (isComplete) {
2688
- return tempDIV;
3626
+ return $el;
2689
3627
  }
2690
3628
  else {
2691
- return tempDIV.firstChild;
3629
+ return $el.firstElementChild ?? $el.firstChild;
2692
3630
  }
2693
3631
  }
2694
3632
  if (useParser) {
@@ -2733,213 +3671,6 @@
2733
3671
  .map((item) => `${encodeURIComponent(item.name)}=${encodeURIComponent(item.value)}`)
2734
3672
  .join("&");
2735
3673
  }
2736
- /**
2737
- * 显示元素
2738
- * @param target 当前元素
2739
- * @param checkVisiblie 是否检测元素是否显示
2740
- * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
2741
- * + false 不检测,直接设置display属性为空
2742
- * @example
2743
- * // 显示a.xx元素
2744
- * DOMUtils.show(document.querySelector("a.xx"))
2745
- * DOMUtils.show(document.querySelectorAll("a.xx"))
2746
- * DOMUtils.show("a.xx")
2747
- */
2748
- show(target, checkVisiblie = true) {
2749
- const DOMUtilsContext = this;
2750
- if (target == null) {
2751
- return;
2752
- }
2753
- if (typeof target === "string") {
2754
- target = DOMUtilsContext.selectorAll(target);
2755
- }
2756
- if (target instanceof NodeList || target instanceof Array) {
2757
- target = target;
2758
- for (const element of target) {
2759
- DOMUtilsContext.show(element, checkVisiblie);
2760
- }
2761
- }
2762
- else {
2763
- target = target;
2764
- target.style.display = "";
2765
- if (checkVisiblie) {
2766
- if (!DOMUtilsCommonUtils.isShow(target)) {
2767
- /* 仍然是不显示,尝试使用强覆盖 */
2768
- target.style.setProperty("display", "unset", "important");
2769
- }
2770
- }
2771
- }
2772
- }
2773
- /**
2774
- * 隐藏元素
2775
- * @param target 当前元素
2776
- * @param checkVisiblie 是否检测元素是否显示
2777
- * + true (默认)如果检测到显示,则强制使用display: none !important;
2778
- * + false 不检测,直接设置display属性为none
2779
- * @example
2780
- * // 隐藏a.xx元素
2781
- * DOMUtils.hide(document.querySelector("a.xx"))
2782
- * DOMUtils.hide(document.querySelectorAll("a.xx"))
2783
- * DOMUtils.hide("a.xx")
2784
- */
2785
- hide(target, checkVisiblie = true) {
2786
- const DOMUtilsContext = this;
2787
- if (target == null) {
2788
- return;
2789
- }
2790
- if (typeof target === "string") {
2791
- target = DOMUtilsContext.selectorAll(target);
2792
- }
2793
- if (target instanceof NodeList || target instanceof Array) {
2794
- target = target;
2795
- for (const element of target) {
2796
- DOMUtilsContext.hide(element, checkVisiblie);
2797
- }
2798
- }
2799
- else {
2800
- target = target;
2801
- target.style.display = "none";
2802
- if (checkVisiblie) {
2803
- if (DOMUtilsCommonUtils.isShow(target)) {
2804
- /* 仍然是显示,尝试使用强覆盖 */
2805
- target.style.setProperty("display", "none", "important");
2806
- }
2807
- }
2808
- }
2809
- }
2810
- /**
2811
- * 淡入元素
2812
- * @param element 当前元素
2813
- * @param duration 动画持续时间(毫秒),默认400毫秒
2814
- * @param callback 动画结束的回调
2815
- * @example
2816
- * // 元素a.xx淡入
2817
- * DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
2818
- * console.log("淡入完毕");
2819
- * })
2820
- * DOMUtils.fadeIn("a.xx",undefined,()=>{
2821
- * console.log("淡入完毕");
2822
- * })
2823
- */
2824
- fadeIn(element, duration = 400, callback) {
2825
- if (element == null) {
2826
- return;
2827
- }
2828
- const DOMUtilsContext = this;
2829
- if (typeof element === "string") {
2830
- element = DOMUtilsContext.selectorAll(element);
2831
- }
2832
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2833
- // 设置
2834
- element.forEach(($ele) => {
2835
- DOMUtilsContext.fadeIn($ele, duration, callback);
2836
- });
2837
- return;
2838
- }
2839
- element.style.opacity = "0";
2840
- element.style.display = "";
2841
- let start = null;
2842
- let timer = null;
2843
- function step(timestamp) {
2844
- if (!start)
2845
- start = timestamp;
2846
- const progress = timestamp - start;
2847
- element = element;
2848
- element.style.opacity = Math.min(progress / duration, 1).toString();
2849
- if (progress < duration) {
2850
- DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2851
- }
2852
- else {
2853
- if (callback && typeof callback === "function") {
2854
- callback();
2855
- }
2856
- DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
2857
- }
2858
- }
2859
- timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2860
- }
2861
- /**
2862
- * 淡出元素
2863
- * @param element 当前元素
2864
- * @param duration 动画持续时间(毫秒),默认400毫秒
2865
- * @param callback 动画结束的回调
2866
- * @example
2867
- * // 元素a.xx淡出
2868
- * DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
2869
- * console.log("淡出完毕");
2870
- * })
2871
- * DOMUtils.fadeOut("a.xx",undefined,()=>{
2872
- * console.log("淡出完毕");
2873
- * })
2874
- */
2875
- fadeOut(element, duration = 400, callback) {
2876
- const DOMUtilsContext = this;
2877
- if (element == null) {
2878
- return;
2879
- }
2880
- if (typeof element === "string") {
2881
- element = DOMUtilsContext.selectorAll(element);
2882
- }
2883
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2884
- // 设置
2885
- element.forEach(($ele) => {
2886
- DOMUtilsContext.fadeOut($ele, duration, callback);
2887
- });
2888
- return;
2889
- }
2890
- element.style.opacity = "1";
2891
- let start = null;
2892
- let timer = null;
2893
- function step(timestamp) {
2894
- if (!start)
2895
- start = timestamp;
2896
- const progress = timestamp - start;
2897
- element = element;
2898
- element.style.opacity = Math.max(1 - progress / duration, 0).toString();
2899
- if (progress < duration) {
2900
- DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2901
- }
2902
- else {
2903
- element.style.display = "none";
2904
- if (typeof callback === "function") {
2905
- callback();
2906
- }
2907
- DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
2908
- }
2909
- }
2910
- timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
2911
- }
2912
- /**
2913
- * 切换元素的显示和隐藏状态
2914
- * @param element 当前元素
2915
- * @param checkVisiblie 是否检测元素是否显示
2916
- * @example
2917
- * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
2918
- * DOMUtils.toggle(document.querySelector("a.xx"))
2919
- * DOMUtils.toggle("a.xx")
2920
- */
2921
- toggle(element, checkVisiblie) {
2922
- const DOMUtilsContext = this;
2923
- if (typeof element === "string") {
2924
- element = DOMUtilsContext.selectorAll(element);
2925
- }
2926
- if (element == null) {
2927
- return;
2928
- }
2929
- if (DOMUtilsCommonUtils.isNodeList(element)) {
2930
- // 设置
2931
- element.forEach(($ele) => {
2932
- DOMUtilsContext.toggle($ele);
2933
- });
2934
- return;
2935
- }
2936
- if (DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
2937
- DOMUtilsContext.show(element, checkVisiblie);
2938
- }
2939
- else {
2940
- DOMUtilsContext.hide(element, checkVisiblie);
2941
- }
2942
- }
2943
3674
  /**
2944
3675
  * 创建一个新的DOMUtils实例
2945
3676
  * @param option
@@ -2957,7 +3688,7 @@
2957
3688
  * DOMUtils.getTextBoundingRect(document.querySelector("input"));
2958
3689
  */
2959
3690
  getTextBoundingRect($input, selectionStart, selectionEnd) {
2960
- const DOMUtilsContext = this;
3691
+ const that = this;
2961
3692
  // Basic parameter validation
2962
3693
  if (!$input || !("value" in $input))
2963
3694
  return $input;
@@ -3026,7 +3757,7 @@
3026
3757
  }
3027
3758
  // End of CSS variable checks
3028
3759
  // 不能为空,不然获取不到高度
3029
- const text = $input.value || "G", textLen = text.length, fakeClone = DOMUtilsContext.windowApi.document.createElement("div");
3760
+ const text = $input.value || "G", textLen = text.length, fakeClone = that.windowApi.document.createElement("div");
3030
3761
  if (selectionStart > 0)
3031
3762
  appendPart(0, selectionStart);
3032
3763
  const fakeRange = appendPart(selectionStart, selectionEnd);
@@ -3040,7 +3771,7 @@
3040
3771
  fakeClone.style.left = leftPos + "px";
3041
3772
  fakeClone.style.width = width + "px";
3042
3773
  fakeClone.style.height = height + "px";
3043
- DOMUtilsContext.windowApi.document.body.appendChild(fakeClone);
3774
+ that.windowApi.document.body.appendChild(fakeClone);
3044
3775
  const returnValue = fakeRange.getBoundingClientRect(); //Get rect
3045
3776
  fakeClone?.parentNode?.removeChild(fakeClone); //Remove temp
3046
3777
  return returnValue;
@@ -3052,7 +3783,7 @@
3052
3783
  * @returns
3053
3784
  */
3054
3785
  function appendPart(start, end) {
3055
- const span = DOMUtilsContext.windowApi.document.createElement("span");
3786
+ const span = that.windowApi.document.createElement("span");
3056
3787
  span.style.cssText = cssDefaultStyles; //Force styles to prevent unexpected results
3057
3788
  span.textContent = text.substring(start, end);
3058
3789
  fakeClone.appendChild(span);
@@ -3060,7 +3791,7 @@
3060
3791
  }
3061
3792
  // Computing offset position
3062
3793
  function getInputOffset() {
3063
- const body = DOMUtilsContext.windowApi.document.body, win = DOMUtilsContext.windowApi.document.defaultView, docElem = DOMUtilsContext.windowApi.document.documentElement, $box = DOMUtilsContext.windowApi.document.createElement("div");
3794
+ const body = that.windowApi.document.body, win = that.windowApi.document.defaultView, docElem = that.windowApi.document.documentElement, $box = that.windowApi.document.createElement("div");
3064
3795
  $box.style.paddingLeft = $box.style.width = "1px";
3065
3796
  body.appendChild($box);
3066
3797
  const isBoxModel = $box.offsetWidth == 2;
@@ -3079,7 +3810,7 @@
3079
3810
  * @returns
3080
3811
  */
3081
3812
  function getInputCSS(prop, isNumber) {
3082
- const val = DOMUtilsContext.windowApi.document.defaultView.getComputedStyle($input, null).getPropertyValue(prop);
3813
+ const val = that.windowApi.document.defaultView.getComputedStyle($input, null).getPropertyValue(prop);
3083
3814
  if (isNumber) {
3084
3815
  return parseFloat(val);
3085
3816
  }
@@ -3088,13 +3819,163 @@
3088
3819
  }
3089
3820
  }
3090
3821
  }
3091
- /** 获取 animationend 在各个浏览器的兼容名 */
3092
- getAnimationEndNameList() {
3093
- return DOMUtilsCommonUtils.getAnimationEndNameList();
3822
+ addStyle(cssText) {
3823
+ if (typeof cssText !== "string") {
3824
+ throw new Error("DOMUtils.addStyle 参数cssText 必须为String类型");
3825
+ }
3826
+ const $css = this.createElement("style", {
3827
+ type: "text/css",
3828
+ innerHTML: cssText,
3829
+ });
3830
+ if (this.windowApi.document.head) {
3831
+ /* 插入head最后 */
3832
+ this.windowApi.document.head.appendChild($css);
3833
+ }
3834
+ else if (this.windowApi.document.documentElement.childNodes.length === 0) {
3835
+ /* 插入#html后 */
3836
+ this.windowApi.document.documentElement.appendChild($css);
3837
+ }
3838
+ else {
3839
+ /* 插入#html第一个元素前 */
3840
+ this.windowApi.document.documentElement.insertBefore($css, this.windowApi.document.documentElement.childNodes[0]);
3841
+ }
3842
+ return $css;
3094
3843
  }
3095
- /** 获取 transitionend 在各个浏览器的兼容名 */
3096
- getTransitionEndNameList() {
3097
- return DOMUtilsCommonUtils.getTransitionEndNameList();
3844
+ /**
3845
+ * 检测点击的地方是否在该元素区域内
3846
+ * @param $el 需要检测的元素
3847
+ * @returns
3848
+ * + true 点击在元素上
3849
+ * + false 未点击在元素上
3850
+ * @example
3851
+ * DOMUtils.checkUserClickInNode(document.querySelector(".xxx"));
3852
+ * > false
3853
+ **/
3854
+ checkUserClickInNode($el) {
3855
+ const that = this;
3856
+ if (!CommonUtils.isDOM($el)) {
3857
+ throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
3858
+ }
3859
+ const clickEvent = that.windowApi.window.event;
3860
+ const touchEvent = that.windowApi.window.event;
3861
+ const $click = clickEvent?.composedPath()?.[0];
3862
+ // 点击的x坐标
3863
+ const clickPosX = clickEvent?.clientX != null ? clickEvent.clientX : touchEvent.touches[0].clientX;
3864
+ // 点击的y坐标
3865
+ const clickPosY = clickEvent?.clientY != null ? clickEvent.clientY : touchEvent.touches[0].clientY;
3866
+ const {
3867
+ /* 要检测的元素的相对屏幕的横坐标最左边 */
3868
+ left: elementPosXLeft,
3869
+ /* 要检测的元素的相对屏幕的横坐标最右边 */
3870
+ right: elementPosXRight,
3871
+ /* 要检测的元素的相对屏幕的纵坐标最上边 */
3872
+ top: elementPosYTop,
3873
+ /* 要检测的元素的相对屏幕的纵坐标最下边 */
3874
+ bottom: elementPosYBottom, } = $el.getBoundingClientRect();
3875
+ if (clickPosX >= elementPosXLeft &&
3876
+ clickPosX <= elementPosXRight &&
3877
+ clickPosY >= elementPosYTop &&
3878
+ clickPosY <= elementPosYBottom) {
3879
+ return true;
3880
+ }
3881
+ else if (($click && $el.contains($click)) || $click == $el) {
3882
+ /* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
3883
+ return true;
3884
+ }
3885
+ else {
3886
+ return false;
3887
+ }
3888
+ }
3889
+ deleteParentNode($el, parentSelector) {
3890
+ if ($el == null) {
3891
+ return;
3892
+ }
3893
+ if (!CommonUtils.isDOM($el)) {
3894
+ throw new Error("DOMUtils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
3895
+ }
3896
+ if (typeof parentSelector !== "string") {
3897
+ throw new Error("DOMUtils.deleteParentNode 参数 targetSelector 必须为 string 类型");
3898
+ }
3899
+ let result = false;
3900
+ const $parent = domUtils.closest($el, parentSelector);
3901
+ if ($parent) {
3902
+ this.remove($parent);
3903
+ result = true;
3904
+ }
3905
+ return result;
3906
+ }
3907
+ *findElementsWithText($el, text, filter) {
3908
+ const that = this;
3909
+ if ($el.outerHTML.includes(text)) {
3910
+ if ($el.children.length === 0) {
3911
+ const filterResult = typeof filter === "function" ? filter($el) : false;
3912
+ if (!filterResult) {
3913
+ yield $el;
3914
+ }
3915
+ }
3916
+ else {
3917
+ const $text = Array.from($el.childNodes).filter(($child) => $child.nodeType === Node.TEXT_NODE);
3918
+ for (const $child of $text) {
3919
+ if ($child.textContent.includes(text)) {
3920
+ const filterResult = typeof filter === "function" ? filter($el) : false;
3921
+ if (!filterResult) {
3922
+ yield $child;
3923
+ }
3924
+ }
3925
+ }
3926
+ }
3927
+ }
3928
+ for (let index = 0; index < $el.children.length; index++) {
3929
+ const $child = $el.children[index];
3930
+ yield* that.findElementsWithText($child, text, filter);
3931
+ }
3932
+ }
3933
+ /**
3934
+ * 寻找可见元素,如果元素不可见,则向上找它的父元素直至找到,如果父元素不存在则返回null
3935
+ * @param $el
3936
+ * @example
3937
+ * let visibleElement = DOMUtils.findVisibleElement(document.querySelector("a.xx"));
3938
+ * > <HTMLElement>
3939
+ */
3940
+ findVisibleElement($el) {
3941
+ let $current = $el;
3942
+ while ($current) {
3943
+ const rect = $current.getBoundingClientRect();
3944
+ if (rect.length) {
3945
+ return $current;
3946
+ }
3947
+ $current = $current.parentElement;
3948
+ }
3949
+ return null;
3950
+ }
3951
+ /**
3952
+ * 将元素上的文本或元素使用光标进行选中
3953
+ *
3954
+ * 注意,如果设置startIndex和endIndex,且元素上并无可选则的坐标,那么会报错
3955
+ * @param $el 目标元素
3956
+ * @param childTextNode 目标元素下的#text元素
3957
+ * @param startIndex (可选)开始坐标,可为空
3958
+ * @param endIndex (可选)结束坐标,可为空
3959
+ * @example
3960
+ * DOMUtils.setElementSelection(document.querySelector("span"));
3961
+ */
3962
+ setElementSelection($el, childTextNode, startIndex, endIndex) {
3963
+ const range = this.windowApi.document.createRange();
3964
+ range.selectNodeContents($el);
3965
+ if (childTextNode) {
3966
+ if (childTextNode.nodeType !== Node.TEXT_NODE) {
3967
+ throw new TypeError("childTextNode必须是#text元素");
3968
+ }
3969
+ if (startIndex != null && endIndex != null) {
3970
+ range.setStart(childTextNode, startIndex);
3971
+ range.setEnd(childTextNode, endIndex);
3972
+ }
3973
+ }
3974
+ const selection = this.windowApi.globalThis.getSelection();
3975
+ if (selection) {
3976
+ selection.removeAllRanges();
3977
+ selection.addRange(range);
3978
+ }
3098
3979
  }
3099
3980
  }
3100
3981
  const domUtils = new DOMUtils();