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