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