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