@whitesev/domutils 1.6.8 → 1.7.1
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 +1962 -1062
- 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 +1962 -1062
- 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 +1962 -1062
- 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 +1962 -1062
- 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 +1962 -1062
- 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 +1962 -1062
- 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} +27 -92
- 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/DOMUtilsEvent.d.ts +23 -0
- 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 +13 -12
- package/src/{DOMUtilsCommonUtils.ts → CommonUtils.ts} +25 -11
- package/src/ElementAnimate.ts +290 -0
- package/src/{DOMUtilsEvent.ts → ElementEvent.ts} +220 -396
- 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/DOMUtilsEvent.d.ts +23 -0
- 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
|
|
@@ -451,70 +465,1157 @@ var DOMUtils = (function () {
|
|
|
451
465
|
}
|
|
452
466
|
},
|
|
453
467
|
/**
|
|
454
|
-
* 配合 .setTimeout 使用
|
|
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.1";
|
|
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
|
+
}
|
|
1535
|
+
/**
|
|
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,35 +1667,37 @@ 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
|
-
return
|
|
1677
|
+
return {
|
|
1678
|
+
off() { },
|
|
1679
|
+
trigger() { },
|
|
1680
|
+
};
|
|
568
1681
|
}
|
|
569
|
-
let
|
|
1682
|
+
let $elList = [];
|
|
570
1683
|
if (element instanceof NodeList || Array.isArray(element)) {
|
|
571
|
-
|
|
572
|
-
elementList = [...element];
|
|
1684
|
+
$elList = $elList.concat(Array.from(element));
|
|
573
1685
|
}
|
|
574
1686
|
else {
|
|
575
|
-
|
|
1687
|
+
$elList.push(element);
|
|
576
1688
|
}
|
|
577
1689
|
// 事件名
|
|
578
1690
|
let eventTypeList = [];
|
|
579
1691
|
if (Array.isArray(eventType)) {
|
|
580
|
-
eventTypeList = eventTypeList.concat(eventType.filter((
|
|
1692
|
+
eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
|
|
581
1693
|
}
|
|
582
1694
|
else if (typeof eventType === "string") {
|
|
583
|
-
eventTypeList = eventTypeList.concat(eventType.split(" ").filter((
|
|
1695
|
+
eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
|
|
584
1696
|
}
|
|
585
1697
|
// 子元素选择器
|
|
586
1698
|
let selectorList = [];
|
|
587
1699
|
if (Array.isArray(selector)) {
|
|
588
|
-
selectorList = selectorList.concat(selector.filter((
|
|
1700
|
+
selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
|
|
589
1701
|
}
|
|
590
1702
|
else if (typeof selector === "string") {
|
|
591
1703
|
selectorList.push(selector);
|
|
@@ -614,10 +1726,10 @@ var DOMUtils = (function () {
|
|
|
614
1726
|
*/
|
|
615
1727
|
function checkOptionOnceToRemoveEventListener() {
|
|
616
1728
|
if (listenerOption.once) {
|
|
617
|
-
|
|
1729
|
+
that.off(element, eventType, selector, callback, option);
|
|
618
1730
|
}
|
|
619
1731
|
}
|
|
620
|
-
|
|
1732
|
+
$elList.forEach((elementItem) => {
|
|
621
1733
|
/**
|
|
622
1734
|
* 事件回调
|
|
623
1735
|
* @param event
|
|
@@ -630,20 +1742,20 @@ var DOMUtils = (function () {
|
|
|
630
1742
|
? event.composedPath()[0]
|
|
631
1743
|
: event.target;
|
|
632
1744
|
let totalParent = elementItem;
|
|
633
|
-
if (
|
|
634
|
-
if (totalParent ===
|
|
635
|
-
totalParent =
|
|
1745
|
+
if (CommonUtils.isWin(totalParent)) {
|
|
1746
|
+
if (totalParent === that.windowApi.document) {
|
|
1747
|
+
totalParent = that.windowApi.document.documentElement;
|
|
636
1748
|
}
|
|
637
1749
|
}
|
|
638
1750
|
const findValue = selectorList.find((selectorItem) => {
|
|
639
1751
|
// 判断目标元素是否匹配选择器
|
|
640
|
-
if (
|
|
1752
|
+
if (that.matches(eventTarget, selectorItem)) {
|
|
641
1753
|
/* 当前目标可以被selector所匹配到 */
|
|
642
1754
|
return true;
|
|
643
1755
|
}
|
|
644
1756
|
/* 在上层与主元素之间寻找可以被selector所匹配到的 */
|
|
645
|
-
const $closestMatches =
|
|
646
|
-
if ($closestMatches && totalParent?.contains($closestMatches)) {
|
|
1757
|
+
const $closestMatches = that.closest(eventTarget, selectorItem);
|
|
1758
|
+
if ($closestMatches && totalParent?.contains?.($closestMatches)) {
|
|
647
1759
|
eventTarget = $closestMatches;
|
|
648
1760
|
return true;
|
|
649
1761
|
}
|
|
@@ -675,7 +1787,7 @@ var DOMUtils = (function () {
|
|
|
675
1787
|
eventTypeList.forEach((eventName) => {
|
|
676
1788
|
elementItem.addEventListener(eventName, domUtilsEventCallBack, listenerOption);
|
|
677
1789
|
/* 获取对象上的事件 */
|
|
678
|
-
const elementEvents = Reflect.get(elementItem,
|
|
1790
|
+
const elementEvents = Reflect.get(elementItem, GlobalData.domEventSymbol) || {};
|
|
679
1791
|
/* 初始化对象上的xx事件 */
|
|
680
1792
|
elementEvents[eventName] = elementEvents[eventName] || [];
|
|
681
1793
|
elementEvents[eventName].push({
|
|
@@ -685,9 +1797,26 @@ var DOMUtils = (function () {
|
|
|
685
1797
|
originCallBack: listenerCallBack,
|
|
686
1798
|
});
|
|
687
1799
|
/* 覆盖事件 */
|
|
688
|
-
Reflect.set(elementItem,
|
|
1800
|
+
Reflect.set(elementItem, GlobalData.domEventSymbol, elementEvents);
|
|
689
1801
|
});
|
|
690
1802
|
});
|
|
1803
|
+
return {
|
|
1804
|
+
/**
|
|
1805
|
+
* 取消绑定的监听事件
|
|
1806
|
+
* @param filter (可选)过滤函数,对元素属性上的事件进行过滤出想要删除的事件
|
|
1807
|
+
*/
|
|
1808
|
+
off(filter) {
|
|
1809
|
+
that.off($elList, eventTypeList, selectorList, listenerCallBack, listenerOption, filter);
|
|
1810
|
+
},
|
|
1811
|
+
/**
|
|
1812
|
+
* 主动触发事件
|
|
1813
|
+
* @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
|
|
1814
|
+
* @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
|
|
1815
|
+
*/
|
|
1816
|
+
trigger(details, useDispatchToTriggerEvent) {
|
|
1817
|
+
that.trigger($elList, eventTypeList, details, useDispatchToTriggerEvent);
|
|
1818
|
+
},
|
|
1819
|
+
};
|
|
691
1820
|
}
|
|
692
1821
|
off(element, eventType, selector, callback, option, filter) {
|
|
693
1822
|
/**
|
|
@@ -706,34 +1835,34 @@ var DOMUtils = (function () {
|
|
|
706
1835
|
}
|
|
707
1836
|
return option;
|
|
708
1837
|
}
|
|
709
|
-
const
|
|
1838
|
+
const that = this;
|
|
710
1839
|
// eslint-disable-next-line prefer-rest-params
|
|
711
1840
|
const args = arguments;
|
|
712
1841
|
if (typeof element === "string") {
|
|
713
|
-
element =
|
|
1842
|
+
element = that.selectorAll(element);
|
|
714
1843
|
}
|
|
715
1844
|
if (element == null) {
|
|
716
1845
|
return;
|
|
717
1846
|
}
|
|
718
|
-
let
|
|
1847
|
+
let $elList = [];
|
|
719
1848
|
if (element instanceof NodeList || Array.isArray(element)) {
|
|
720
1849
|
element = element;
|
|
721
|
-
|
|
1850
|
+
$elList = $elList.concat(Array.from(element));
|
|
722
1851
|
}
|
|
723
1852
|
else {
|
|
724
|
-
|
|
1853
|
+
$elList.push(element);
|
|
725
1854
|
}
|
|
726
1855
|
let eventTypeList = [];
|
|
727
1856
|
if (Array.isArray(eventType)) {
|
|
728
|
-
eventTypeList = eventTypeList.concat(eventType.filter((
|
|
1857
|
+
eventTypeList = eventTypeList.concat(eventType.filter((it) => typeof it === "string" && it.toString() !== ""));
|
|
729
1858
|
}
|
|
730
1859
|
else if (typeof eventType === "string") {
|
|
731
|
-
eventTypeList = eventTypeList.concat(eventType.split(" ").filter((
|
|
1860
|
+
eventTypeList = eventTypeList.concat(eventType.split(" ").filter((it) => it !== ""));
|
|
732
1861
|
}
|
|
733
1862
|
// 子元素选择器
|
|
734
1863
|
let selectorList = [];
|
|
735
1864
|
if (Array.isArray(selector)) {
|
|
736
|
-
selectorList = selectorList.concat(selector.filter((
|
|
1865
|
+
selectorList = selectorList.concat(selector.filter((it) => typeof it === "string" && it.toString() !== ""));
|
|
737
1866
|
}
|
|
738
1867
|
else if (typeof selector === "string") {
|
|
739
1868
|
selectorList.push(selector);
|
|
@@ -772,9 +1901,9 @@ var DOMUtils = (function () {
|
|
|
772
1901
|
// 目标函数、事件名、回调函数、事件配置、过滤函数
|
|
773
1902
|
filter = option;
|
|
774
1903
|
}
|
|
775
|
-
|
|
1904
|
+
$elList.forEach(($elItem) => {
|
|
776
1905
|
/* 获取对象上的事件 */
|
|
777
|
-
const elementEvents = Reflect.get(
|
|
1906
|
+
const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
|
|
778
1907
|
eventTypeList.forEach((eventName) => {
|
|
779
1908
|
const handlers = elementEvents[eventName] || [];
|
|
780
1909
|
const filterHandler = typeof filter === "function" ? handlers.filter(filter) : handlers;
|
|
@@ -798,7 +1927,7 @@ var DOMUtils = (function () {
|
|
|
798
1927
|
flag = false;
|
|
799
1928
|
}
|
|
800
1929
|
if (flag || isRemoveAll) {
|
|
801
|
-
|
|
1930
|
+
$elItem.removeEventListener(eventName, handler.callback, handler.option);
|
|
802
1931
|
const findIndex = handlers.findIndex((item) => item === handler);
|
|
803
1932
|
if (findIndex !== -1) {
|
|
804
1933
|
handlers.splice(findIndex, 1);
|
|
@@ -807,10 +1936,10 @@ var DOMUtils = (function () {
|
|
|
807
1936
|
}
|
|
808
1937
|
if (handlers.length === 0) {
|
|
809
1938
|
/* 如果没有任意的handler,那么删除该属性 */
|
|
810
|
-
|
|
1939
|
+
CommonUtils.delete(elementEvents, eventType);
|
|
811
1940
|
}
|
|
812
1941
|
});
|
|
813
|
-
Reflect.set(
|
|
1942
|
+
Reflect.set($elItem, GlobalData.domEventSymbol, elementEvents);
|
|
814
1943
|
});
|
|
815
1944
|
}
|
|
816
1945
|
/**
|
|
@@ -819,19 +1948,19 @@ var DOMUtils = (function () {
|
|
|
819
1948
|
* @param eventType (可选)需要取消监听的事件
|
|
820
1949
|
*/
|
|
821
1950
|
offAll(element, eventType) {
|
|
822
|
-
const
|
|
1951
|
+
const that = this;
|
|
823
1952
|
if (typeof element === "string") {
|
|
824
|
-
element =
|
|
1953
|
+
element = that.selectorAll(element);
|
|
825
1954
|
}
|
|
826
1955
|
if (element == null) {
|
|
827
1956
|
return;
|
|
828
1957
|
}
|
|
829
|
-
let
|
|
1958
|
+
let $elList = [];
|
|
830
1959
|
if (element instanceof NodeList || Array.isArray(element)) {
|
|
831
|
-
|
|
1960
|
+
$elList = $elList.concat(Array.from(element));
|
|
832
1961
|
}
|
|
833
1962
|
else {
|
|
834
|
-
|
|
1963
|
+
$elList.push(element);
|
|
835
1964
|
}
|
|
836
1965
|
let eventTypeList = [];
|
|
837
1966
|
if (Array.isArray(eventType)) {
|
|
@@ -840,12 +1969,13 @@ var DOMUtils = (function () {
|
|
|
840
1969
|
else if (typeof eventType === "string") {
|
|
841
1970
|
eventTypeList = eventTypeList.concat(eventType.split(" "));
|
|
842
1971
|
}
|
|
843
|
-
|
|
844
|
-
Object.getOwnPropertySymbols(
|
|
845
|
-
|
|
1972
|
+
$elList.forEach(($elItem) => {
|
|
1973
|
+
const symbolList = [...new Set([...Object.getOwnPropertySymbols($elItem), GlobalData.domEventSymbol])];
|
|
1974
|
+
symbolList.forEach((symbolItem) => {
|
|
1975
|
+
if (!symbolItem.toString().startsWith("Symbol(events_")) {
|
|
846
1976
|
return;
|
|
847
1977
|
}
|
|
848
|
-
const elementEvents =
|
|
1978
|
+
const elementEvents = Reflect.get($elItem, symbolItem) || {};
|
|
849
1979
|
const iterEventNameList = eventTypeList.length ? eventTypeList : Object.keys(elementEvents);
|
|
850
1980
|
iterEventNameList.forEach((eventName) => {
|
|
851
1981
|
const handlers = elementEvents[eventName];
|
|
@@ -853,12 +1983,12 @@ var DOMUtils = (function () {
|
|
|
853
1983
|
return;
|
|
854
1984
|
}
|
|
855
1985
|
for (const handler of handlers) {
|
|
856
|
-
|
|
1986
|
+
$elItem.removeEventListener(eventName, handler.callback, {
|
|
857
1987
|
capture: handler["option"]["capture"],
|
|
858
1988
|
});
|
|
859
1989
|
}
|
|
860
|
-
const events = Reflect.get(
|
|
861
|
-
|
|
1990
|
+
const events = Reflect.get($elItem, symbolItem);
|
|
1991
|
+
CommonUtils.delete(events, eventName);
|
|
862
1992
|
});
|
|
863
1993
|
});
|
|
864
1994
|
});
|
|
@@ -875,15 +2005,15 @@ var DOMUtils = (function () {
|
|
|
875
2005
|
if (typeof callback !== "function") {
|
|
876
2006
|
return;
|
|
877
2007
|
}
|
|
878
|
-
const
|
|
2008
|
+
const that = this;
|
|
879
2009
|
/**
|
|
880
2010
|
* 检测文档是否加载完毕
|
|
881
2011
|
*/
|
|
882
2012
|
function checkDOMReadyState() {
|
|
883
2013
|
try {
|
|
884
|
-
if (
|
|
885
|
-
(
|
|
886
|
-
!
|
|
2014
|
+
if (that.windowApi.document.readyState === "complete" ||
|
|
2015
|
+
(that.windowApi.document.readyState !== "loading" &&
|
|
2016
|
+
!that.windowApi.document.documentElement.doScroll)) {
|
|
887
2017
|
return true;
|
|
888
2018
|
}
|
|
889
2019
|
else {
|
|
@@ -903,12 +2033,12 @@ var DOMUtils = (function () {
|
|
|
903
2033
|
}
|
|
904
2034
|
const targetList = [
|
|
905
2035
|
{
|
|
906
|
-
target:
|
|
2036
|
+
target: that.windowApi.document,
|
|
907
2037
|
eventType: "DOMContentLoaded",
|
|
908
2038
|
callback: completed,
|
|
909
2039
|
},
|
|
910
2040
|
{
|
|
911
|
-
target:
|
|
2041
|
+
target: that.windowApi.window,
|
|
912
2042
|
eventType: "load",
|
|
913
2043
|
callback: completed,
|
|
914
2044
|
},
|
|
@@ -933,7 +2063,7 @@ var DOMUtils = (function () {
|
|
|
933
2063
|
}
|
|
934
2064
|
if (checkDOMReadyState()) {
|
|
935
2065
|
/* 检查document状态 */
|
|
936
|
-
|
|
2066
|
+
CommonUtils.setTimeout(callback, 0);
|
|
937
2067
|
}
|
|
938
2068
|
else {
|
|
939
2069
|
/* 添加监听 */
|
|
@@ -945,7 +2075,7 @@ var DOMUtils = (function () {
|
|
|
945
2075
|
* @param element 需要触发的元素|元素数组|window
|
|
946
2076
|
* @param eventType 需要触发的事件
|
|
947
2077
|
* @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
|
|
948
|
-
* @param useDispatchToTriggerEvent 是否使用dispatchEvent
|
|
2078
|
+
* @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
|
|
949
2079
|
* @example
|
|
950
2080
|
* // 触发元素a.xx的click事件
|
|
951
2081
|
* DOMUtils.trigger(document.querySelector("a.xx"),"click")
|
|
@@ -955,51 +2085,55 @@ var DOMUtils = (function () {
|
|
|
955
2085
|
* DOMUtils.trigger("a.xx",["click","tap","hover"])
|
|
956
2086
|
*/
|
|
957
2087
|
trigger(element, eventType, details, useDispatchToTriggerEvent = true) {
|
|
958
|
-
const
|
|
2088
|
+
const that = this;
|
|
959
2089
|
if (typeof element === "string") {
|
|
960
|
-
element =
|
|
2090
|
+
element = that.selectorAll(element);
|
|
961
2091
|
}
|
|
962
2092
|
if (element == null) {
|
|
963
2093
|
return;
|
|
964
2094
|
}
|
|
965
|
-
let
|
|
2095
|
+
let $elList = [];
|
|
966
2096
|
if (element instanceof NodeList || Array.isArray(element)) {
|
|
967
|
-
|
|
968
|
-
elementList = [...element];
|
|
2097
|
+
$elList = $elList.concat(Array.from(element));
|
|
969
2098
|
}
|
|
970
2099
|
else {
|
|
971
|
-
|
|
2100
|
+
$elList.push(element);
|
|
972
2101
|
}
|
|
973
2102
|
let eventTypeList = [];
|
|
974
2103
|
if (Array.isArray(eventType)) {
|
|
975
|
-
eventTypeList = eventType;
|
|
2104
|
+
eventTypeList = eventType.filter((it) => typeof it === "string" && it.trim() !== "");
|
|
976
2105
|
}
|
|
977
2106
|
else if (typeof eventType === "string") {
|
|
978
2107
|
eventTypeList = eventType.split(" ");
|
|
979
2108
|
}
|
|
980
|
-
|
|
2109
|
+
$elList.forEach(($elItem) => {
|
|
981
2110
|
/* 获取对象上的事件 */
|
|
982
|
-
const
|
|
983
|
-
eventTypeList.forEach((
|
|
2111
|
+
const elementEvents = Reflect.get($elItem, GlobalData.domEventSymbol) || {};
|
|
2112
|
+
eventTypeList.forEach((eventTypeItem) => {
|
|
984
2113
|
let event = null;
|
|
985
2114
|
if (details && details instanceof Event) {
|
|
986
2115
|
event = details;
|
|
987
2116
|
}
|
|
988
2117
|
else {
|
|
989
|
-
|
|
2118
|
+
// 构造事件
|
|
2119
|
+
event = new Event(eventTypeItem);
|
|
990
2120
|
if (details) {
|
|
991
|
-
Object.keys(details)
|
|
992
|
-
|
|
2121
|
+
const detailKeys = Object.keys(details);
|
|
2122
|
+
detailKeys.forEach((keyName) => {
|
|
2123
|
+
const value = Reflect.get(details, keyName);
|
|
2124
|
+
// 在event上添加属性
|
|
2125
|
+
Reflect.set(event, keyName, value);
|
|
993
2126
|
});
|
|
994
2127
|
}
|
|
995
2128
|
}
|
|
996
|
-
if (useDispatchToTriggerEvent == false &&
|
|
997
|
-
|
|
2129
|
+
if (useDispatchToTriggerEvent == false && eventTypeItem in elementEvents) {
|
|
2130
|
+
// 直接调用监听的事件
|
|
2131
|
+
elementEvents[eventTypeItem].forEach((eventsItem) => {
|
|
998
2132
|
eventsItem.callback(event);
|
|
999
2133
|
});
|
|
1000
2134
|
}
|
|
1001
2135
|
else {
|
|
1002
|
-
|
|
2136
|
+
$elItem.dispatchEvent(event);
|
|
1003
2137
|
}
|
|
1004
2138
|
});
|
|
1005
2139
|
});
|
|
@@ -1019,25 +2153,25 @@ var DOMUtils = (function () {
|
|
|
1019
2153
|
* })
|
|
1020
2154
|
* */
|
|
1021
2155
|
click(element, handler, details, useDispatchToTriggerEvent) {
|
|
1022
|
-
const
|
|
2156
|
+
const that = this;
|
|
1023
2157
|
if (typeof element === "string") {
|
|
1024
|
-
element =
|
|
2158
|
+
element = that.selectorAll(element);
|
|
1025
2159
|
}
|
|
1026
2160
|
if (element == null) {
|
|
1027
2161
|
return;
|
|
1028
2162
|
}
|
|
1029
|
-
if (
|
|
2163
|
+
if (CommonUtils.isNodeList(element)) {
|
|
1030
2164
|
// 设置
|
|
1031
2165
|
element.forEach(($ele) => {
|
|
1032
|
-
|
|
2166
|
+
that.click($ele, handler, details, useDispatchToTriggerEvent);
|
|
1033
2167
|
});
|
|
1034
2168
|
return;
|
|
1035
2169
|
}
|
|
1036
2170
|
if (handler == null) {
|
|
1037
|
-
|
|
2171
|
+
that.trigger(element, "click", details, useDispatchToTriggerEvent);
|
|
1038
2172
|
}
|
|
1039
2173
|
else {
|
|
1040
|
-
|
|
2174
|
+
that.on(element, "click", null, handler);
|
|
1041
2175
|
}
|
|
1042
2176
|
}
|
|
1043
2177
|
/**
|
|
@@ -1055,25 +2189,25 @@ var DOMUtils = (function () {
|
|
|
1055
2189
|
* })
|
|
1056
2190
|
* */
|
|
1057
2191
|
blur(element, handler, details, useDispatchToTriggerEvent) {
|
|
1058
|
-
const
|
|
2192
|
+
const that = this;
|
|
1059
2193
|
if (typeof element === "string") {
|
|
1060
|
-
element =
|
|
2194
|
+
element = that.selectorAll(element);
|
|
1061
2195
|
}
|
|
1062
2196
|
if (element == null) {
|
|
1063
2197
|
return;
|
|
1064
2198
|
}
|
|
1065
|
-
if (
|
|
2199
|
+
if (CommonUtils.isNodeList(element)) {
|
|
1066
2200
|
// 设置
|
|
1067
2201
|
element.forEach(($ele) => {
|
|
1068
|
-
|
|
2202
|
+
that.focus($ele, handler, details, useDispatchToTriggerEvent);
|
|
1069
2203
|
});
|
|
1070
2204
|
return;
|
|
1071
2205
|
}
|
|
1072
2206
|
if (handler === null) {
|
|
1073
|
-
|
|
2207
|
+
that.trigger(element, "blur", details, useDispatchToTriggerEvent);
|
|
1074
2208
|
}
|
|
1075
2209
|
else {
|
|
1076
|
-
|
|
2210
|
+
that.on(element, "blur", null, handler);
|
|
1077
2211
|
}
|
|
1078
2212
|
}
|
|
1079
2213
|
/**
|
|
@@ -1091,25 +2225,25 @@ var DOMUtils = (function () {
|
|
|
1091
2225
|
* })
|
|
1092
2226
|
* */
|
|
1093
2227
|
focus(element, handler, details, useDispatchToTriggerEvent) {
|
|
1094
|
-
const
|
|
2228
|
+
const that = this;
|
|
1095
2229
|
if (typeof element === "string") {
|
|
1096
|
-
element =
|
|
2230
|
+
element = that.selectorAll(element);
|
|
1097
2231
|
}
|
|
1098
2232
|
if (element == null) {
|
|
1099
2233
|
return;
|
|
1100
2234
|
}
|
|
1101
|
-
if (
|
|
2235
|
+
if (CommonUtils.isNodeList(element)) {
|
|
1102
2236
|
// 设置
|
|
1103
2237
|
element.forEach(($ele) => {
|
|
1104
|
-
|
|
2238
|
+
that.focus($ele, handler, details, useDispatchToTriggerEvent);
|
|
1105
2239
|
});
|
|
1106
2240
|
return;
|
|
1107
2241
|
}
|
|
1108
2242
|
if (handler == null) {
|
|
1109
|
-
|
|
2243
|
+
that.trigger(element, "focus", details, useDispatchToTriggerEvent);
|
|
1110
2244
|
}
|
|
1111
2245
|
else {
|
|
1112
|
-
|
|
2246
|
+
that.on(element, "focus", null, handler);
|
|
1113
2247
|
}
|
|
1114
2248
|
}
|
|
1115
2249
|
/**
|
|
@@ -1127,22 +2261,22 @@ var DOMUtils = (function () {
|
|
|
1127
2261
|
* })
|
|
1128
2262
|
*/
|
|
1129
2263
|
hover(element, handler, option) {
|
|
1130
|
-
const
|
|
2264
|
+
const that = this;
|
|
1131
2265
|
if (typeof element === "string") {
|
|
1132
|
-
element =
|
|
2266
|
+
element = that.selectorAll(element);
|
|
1133
2267
|
}
|
|
1134
2268
|
if (element == null) {
|
|
1135
2269
|
return;
|
|
1136
2270
|
}
|
|
1137
|
-
if (
|
|
2271
|
+
if (CommonUtils.isNodeList(element)) {
|
|
1138
2272
|
// 设置
|
|
1139
2273
|
element.forEach(($ele) => {
|
|
1140
|
-
|
|
2274
|
+
that.hover($ele, handler, option);
|
|
1141
2275
|
});
|
|
1142
2276
|
return;
|
|
1143
2277
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
2278
|
+
that.on(element, "mouseenter", null, handler, option);
|
|
2279
|
+
that.on(element, "mouseleave", null, handler, option);
|
|
1146
2280
|
}
|
|
1147
2281
|
/**
|
|
1148
2282
|
* 当动画结束时触发事件
|
|
@@ -1151,30 +2285,23 @@ var DOMUtils = (function () {
|
|
|
1151
2285
|
* @param option 配置项,这里默认配置once为true
|
|
1152
2286
|
*/
|
|
1153
2287
|
animationend(element, handler, option) {
|
|
1154
|
-
const
|
|
2288
|
+
const that = this;
|
|
1155
2289
|
if (typeof element === "string") {
|
|
1156
|
-
element =
|
|
2290
|
+
element = that.selector(element);
|
|
1157
2291
|
}
|
|
1158
2292
|
if (element == null) {
|
|
1159
2293
|
return;
|
|
1160
2294
|
}
|
|
1161
|
-
// if (DOMUtilsCommonUtils.isNodeList(element)) {
|
|
1162
|
-
// // 设置
|
|
1163
|
-
// element.forEach(($ele) => {
|
|
1164
|
-
// DOMUtilsContext.animationend($ele as HTMLElement, handler, option);
|
|
1165
|
-
// });
|
|
1166
|
-
// return;
|
|
1167
|
-
// }
|
|
1168
2295
|
const defaultOption = {
|
|
1169
2296
|
once: true,
|
|
1170
2297
|
};
|
|
1171
2298
|
Object.assign(defaultOption, option || {});
|
|
1172
|
-
const eventNameList =
|
|
1173
|
-
|
|
2299
|
+
const eventNameList = CommonUtils.getAnimationEndNameList();
|
|
2300
|
+
that.on(element, eventNameList, null, handler, defaultOption);
|
|
1174
2301
|
if (!defaultOption.once) {
|
|
1175
2302
|
return {
|
|
1176
2303
|
off() {
|
|
1177
|
-
|
|
2304
|
+
that.off(element, eventNameList, null, handler, defaultOption);
|
|
1178
2305
|
},
|
|
1179
2306
|
};
|
|
1180
2307
|
}
|
|
@@ -1186,30 +2313,23 @@ var DOMUtils = (function () {
|
|
|
1186
2313
|
* @param option 配置项,这里默认配置once为true
|
|
1187
2314
|
*/
|
|
1188
2315
|
transitionend(element, handler, option) {
|
|
1189
|
-
const
|
|
2316
|
+
const that = this;
|
|
1190
2317
|
if (typeof element === "string") {
|
|
1191
|
-
element =
|
|
2318
|
+
element = that.selector(element);
|
|
1192
2319
|
}
|
|
1193
2320
|
if (element == null) {
|
|
1194
2321
|
return;
|
|
1195
2322
|
}
|
|
1196
|
-
// if (DOMUtilsCommonUtils.isNodeList(element)) {
|
|
1197
|
-
// // 设置
|
|
1198
|
-
// element.forEach(($ele) => {
|
|
1199
|
-
// DOMUtilsContext.transitionend($ele as HTMLElement, handler, option);
|
|
1200
|
-
// });
|
|
1201
|
-
// return;
|
|
1202
|
-
// }
|
|
1203
2323
|
const defaultOption = {
|
|
1204
2324
|
once: true,
|
|
1205
2325
|
};
|
|
1206
2326
|
Object.assign(defaultOption, option || {});
|
|
1207
|
-
const eventNameList =
|
|
1208
|
-
|
|
2327
|
+
const eventNameList = CommonUtils.getTransitionEndNameList();
|
|
2328
|
+
that.on(element, eventNameList, null, handler, defaultOption);
|
|
1209
2329
|
if (!defaultOption.once) {
|
|
1210
2330
|
return {
|
|
1211
2331
|
off() {
|
|
1212
|
-
|
|
2332
|
+
that.off(element, eventNameList, null, handler, defaultOption);
|
|
1213
2333
|
},
|
|
1214
2334
|
};
|
|
1215
2335
|
}
|
|
@@ -1230,21 +2350,21 @@ var DOMUtils = (function () {
|
|
|
1230
2350
|
* })
|
|
1231
2351
|
*/
|
|
1232
2352
|
keyup(element, handler, option) {
|
|
1233
|
-
const
|
|
2353
|
+
const that = this;
|
|
1234
2354
|
if (element == null) {
|
|
1235
2355
|
return;
|
|
1236
2356
|
}
|
|
1237
2357
|
if (typeof element === "string") {
|
|
1238
|
-
element =
|
|
2358
|
+
element = that.selectorAll(element);
|
|
1239
2359
|
}
|
|
1240
|
-
if (
|
|
2360
|
+
if (CommonUtils.isNodeList(element)) {
|
|
1241
2361
|
// 设置
|
|
1242
2362
|
element.forEach(($ele) => {
|
|
1243
|
-
|
|
2363
|
+
that.keyup($ele, handler, option);
|
|
1244
2364
|
});
|
|
1245
2365
|
return;
|
|
1246
2366
|
}
|
|
1247
|
-
|
|
2367
|
+
that.on(element, "keyup", null, handler, option);
|
|
1248
2368
|
}
|
|
1249
2369
|
/**
|
|
1250
2370
|
* 当按键按下时触发事件
|
|
@@ -1262,21 +2382,21 @@ var DOMUtils = (function () {
|
|
|
1262
2382
|
* })
|
|
1263
2383
|
*/
|
|
1264
2384
|
keydown(element, handler, option) {
|
|
1265
|
-
const
|
|
2385
|
+
const that = this;
|
|
1266
2386
|
if (element == null) {
|
|
1267
2387
|
return;
|
|
1268
2388
|
}
|
|
1269
2389
|
if (typeof element === "string") {
|
|
1270
|
-
element =
|
|
2390
|
+
element = that.selectorAll(element);
|
|
1271
2391
|
}
|
|
1272
|
-
if (
|
|
2392
|
+
if (CommonUtils.isNodeList(element)) {
|
|
1273
2393
|
// 设置
|
|
1274
2394
|
element.forEach(($ele) => {
|
|
1275
|
-
|
|
2395
|
+
that.keydown($ele, handler, option);
|
|
1276
2396
|
});
|
|
1277
2397
|
return;
|
|
1278
2398
|
}
|
|
1279
|
-
|
|
2399
|
+
that.on(element, "keydown", null, handler, option);
|
|
1280
2400
|
}
|
|
1281
2401
|
/**
|
|
1282
2402
|
* 当按键按下时触发事件
|
|
@@ -1294,21 +2414,21 @@ var DOMUtils = (function () {
|
|
|
1294
2414
|
* })
|
|
1295
2415
|
*/
|
|
1296
2416
|
keypress(element, handler, option) {
|
|
1297
|
-
const
|
|
2417
|
+
const that = this;
|
|
1298
2418
|
if (element == null) {
|
|
1299
2419
|
return;
|
|
1300
2420
|
}
|
|
1301
2421
|
if (typeof element === "string") {
|
|
1302
|
-
element =
|
|
2422
|
+
element = that.selectorAll(element);
|
|
1303
2423
|
}
|
|
1304
|
-
if (
|
|
2424
|
+
if (CommonUtils.isNodeList(element)) {
|
|
1305
2425
|
// 设置
|
|
1306
2426
|
element.forEach(($ele) => {
|
|
1307
|
-
|
|
2427
|
+
that.keypress($ele, handler, option);
|
|
1308
2428
|
});
|
|
1309
2429
|
return;
|
|
1310
2430
|
}
|
|
1311
|
-
|
|
2431
|
+
that.on(element, "keypress", null, handler, option);
|
|
1312
2432
|
}
|
|
1313
2433
|
/**
|
|
1314
2434
|
* 监听某个元素键盘按键事件或window全局按键事件
|
|
@@ -1374,9 +2494,9 @@ var DOMUtils = (function () {
|
|
|
1374
2494
|
收藏 171
|
|
1375
2495
|
**/
|
|
1376
2496
|
listenKeyboard(element, eventName = "keypress", callback, options) {
|
|
1377
|
-
const
|
|
2497
|
+
const that = this;
|
|
1378
2498
|
if (typeof element === "string") {
|
|
1379
|
-
element =
|
|
2499
|
+
element = that.selectorAll(element);
|
|
1380
2500
|
}
|
|
1381
2501
|
const keyboardEventCallBack = function (event) {
|
|
1382
2502
|
/** 键名 */
|
|
@@ -1384,233 +2504,146 @@ var DOMUtils = (function () {
|
|
|
1384
2504
|
/** 键值 */
|
|
1385
2505
|
const keyValue = event.charCode || event.keyCode || event.which;
|
|
1386
2506
|
/** 组合键列表 */
|
|
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 = "";
|
|
2507
|
+
const otherCodeList = [];
|
|
2508
|
+
if (event.ctrlKey) {
|
|
2509
|
+
otherCodeList.push("ctrl");
|
|
1516
2510
|
}
|
|
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;
|
|
2511
|
+
if (event.altKey) {
|
|
2512
|
+
otherCodeList.push("alt");
|
|
1532
2513
|
}
|
|
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
|
-
}
|
|
2514
|
+
if (event.metaKey) {
|
|
2515
|
+
otherCodeList.push("meta");
|
|
1547
2516
|
}
|
|
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];
|
|
2517
|
+
if (event.shiftKey) {
|
|
2518
|
+
otherCodeList.push("shift");
|
|
1559
2519
|
}
|
|
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
|
-
}
|
|
2520
|
+
if (typeof callback === "function") {
|
|
2521
|
+
callback(keyName, keyValue, otherCodeList, event);
|
|
1569
2522
|
}
|
|
1570
|
-
|
|
2523
|
+
};
|
|
2524
|
+
that.on(element, eventName, keyboardEventCallBack, options);
|
|
2525
|
+
return {
|
|
2526
|
+
removeListen: () => {
|
|
2527
|
+
that.off(element, eventName, keyboardEventCallBack, options);
|
|
2528
|
+
},
|
|
2529
|
+
};
|
|
2530
|
+
}
|
|
2531
|
+
preventEvent(...args) {
|
|
2532
|
+
/**
|
|
2533
|
+
* 阻止事件的默认行为发生,并阻止事件传播
|
|
2534
|
+
*/
|
|
2535
|
+
const stopEvent = (event) => {
|
|
2536
|
+
/* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
|
|
2537
|
+
event?.preventDefault();
|
|
2538
|
+
/* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
|
|
2539
|
+
event?.stopPropagation();
|
|
2540
|
+
/* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
|
|
2541
|
+
event?.stopImmediatePropagation();
|
|
2542
|
+
return false;
|
|
2543
|
+
};
|
|
2544
|
+
if (args.length === 1) {
|
|
2545
|
+
/* 直接阻止事件 */
|
|
2546
|
+
return stopEvent(args[0]);
|
|
1571
2547
|
}
|
|
1572
2548
|
else {
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
2549
|
+
const $el = args[0];
|
|
2550
|
+
let eventNameList = args[1];
|
|
2551
|
+
const capture = args[2];
|
|
2552
|
+
/* 添加对应的事件来阻止触发 */
|
|
2553
|
+
if (typeof eventNameList === "string") {
|
|
2554
|
+
eventNameList = [eventNameList];
|
|
2555
|
+
}
|
|
2556
|
+
this.on($el, eventNameList, stopEvent, { capture: Boolean(capture) });
|
|
1576
2557
|
}
|
|
1577
2558
|
}
|
|
1578
2559
|
}
|
|
2560
|
+
new ElementEvent();
|
|
1579
2561
|
|
|
1580
|
-
|
|
2562
|
+
class ElementHandler extends ElementEvent {
|
|
2563
|
+
windowApi;
|
|
2564
|
+
constructor(windowApiOption) {
|
|
2565
|
+
super(windowApiOption);
|
|
2566
|
+
this.windowApi = new WindowApi(windowApiOption);
|
|
2567
|
+
}
|
|
2568
|
+
/**
|
|
2569
|
+
* 获取元素的选择器字符串
|
|
2570
|
+
* @param $el
|
|
2571
|
+
* @example
|
|
2572
|
+
* DOMUtils.getElementSelector(document.querySelector("a"))
|
|
2573
|
+
* > '.....'
|
|
2574
|
+
*/
|
|
2575
|
+
getElementSelector($el) {
|
|
2576
|
+
const that = this;
|
|
2577
|
+
if (!$el)
|
|
2578
|
+
return void 0;
|
|
2579
|
+
if (!$el.parentElement)
|
|
2580
|
+
return void 0;
|
|
2581
|
+
/* 如果元素有id属性,则直接返回id选择器 */
|
|
2582
|
+
if ($el.id)
|
|
2583
|
+
return `#${$el.id}`;
|
|
2584
|
+
/* 递归地获取父元素的选择器 */
|
|
2585
|
+
let selector = that.getElementSelector($el.parentElement);
|
|
2586
|
+
if (!selector) {
|
|
2587
|
+
return $el.tagName.toLowerCase();
|
|
2588
|
+
}
|
|
2589
|
+
/* 如果有多个相同类型的兄弟元素,则需要添加索引 */
|
|
2590
|
+
if ($el.parentElement.querySelectorAll($el.tagName).length > 1) {
|
|
2591
|
+
const index = Array.prototype.indexOf.call($el.parentElement.children, $el) + 1;
|
|
2592
|
+
selector += ` > ${$el.tagName.toLowerCase()}:nth-child(${index})`;
|
|
2593
|
+
}
|
|
2594
|
+
else {
|
|
2595
|
+
selector += ` > ${$el.tagName.toLowerCase()}`;
|
|
2596
|
+
}
|
|
2597
|
+
return selector;
|
|
2598
|
+
}
|
|
2599
|
+
}
|
|
2600
|
+
new ElementHandler();
|
|
1581
2601
|
|
|
1582
|
-
class DOMUtils extends
|
|
2602
|
+
class DOMUtils extends ElementHandler {
|
|
1583
2603
|
constructor(option) {
|
|
1584
2604
|
super(option);
|
|
1585
2605
|
}
|
|
1586
2606
|
/** 版本号 */
|
|
1587
2607
|
version = version;
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
2608
|
+
/**
|
|
2609
|
+
* 取消挂载在window下的DOMUtils并返回DOMUtils
|
|
2610
|
+
* @example
|
|
2611
|
+
* let DOMUtils = window.DOMUtils.noConflict()
|
|
2612
|
+
*/
|
|
2613
|
+
noConflict() {
|
|
2614
|
+
const that = this;
|
|
2615
|
+
if (that.windowApi.window.DOMUtils) {
|
|
2616
|
+
CommonUtils.delete(window, "DOMUtils");
|
|
1592
2617
|
}
|
|
1593
|
-
|
|
2618
|
+
that.windowApi.window.DOMUtils = this;
|
|
2619
|
+
return this;
|
|
2620
|
+
}
|
|
2621
|
+
attr($el, attrName, attrValue) {
|
|
2622
|
+
const that = this;
|
|
2623
|
+
if (typeof $el === "string") {
|
|
2624
|
+
$el = that.selectorAll($el);
|
|
2625
|
+
}
|
|
2626
|
+
if ($el == null) {
|
|
1594
2627
|
return;
|
|
1595
2628
|
}
|
|
1596
|
-
if (
|
|
2629
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1597
2630
|
if (attrValue == null) {
|
|
1598
2631
|
// 获取属性
|
|
1599
|
-
return
|
|
2632
|
+
return that.attr($el[0], attrName, attrValue);
|
|
1600
2633
|
}
|
|
1601
2634
|
else {
|
|
1602
2635
|
// 设置属性
|
|
1603
|
-
|
|
1604
|
-
|
|
2636
|
+
$el.forEach(($elItem) => {
|
|
2637
|
+
that.attr($elItem, attrName, attrValue);
|
|
1605
2638
|
});
|
|
1606
2639
|
return;
|
|
1607
2640
|
}
|
|
1608
2641
|
}
|
|
1609
2642
|
if (attrValue == null) {
|
|
1610
|
-
return
|
|
2643
|
+
return $el.getAttribute(attrName);
|
|
1611
2644
|
}
|
|
1612
2645
|
else {
|
|
1613
|
-
|
|
2646
|
+
$el.setAttribute(attrName, attrValue);
|
|
1614
2647
|
}
|
|
1615
2648
|
}
|
|
1616
2649
|
createElement(
|
|
@@ -1620,11 +2653,11 @@ var DOMUtils = (function () {
|
|
|
1620
2653
|
property,
|
|
1621
2654
|
/** 自定义属性 */
|
|
1622
2655
|
attributes) {
|
|
1623
|
-
const
|
|
1624
|
-
const
|
|
2656
|
+
const that = this;
|
|
2657
|
+
const $el = that.windowApi.document.createElement(tagName);
|
|
1625
2658
|
if (typeof property === "string") {
|
|
1626
|
-
|
|
1627
|
-
return
|
|
2659
|
+
that.html($el, property);
|
|
2660
|
+
return $el;
|
|
1628
2661
|
}
|
|
1629
2662
|
if (property == null) {
|
|
1630
2663
|
property = {};
|
|
@@ -1635,10 +2668,10 @@ var DOMUtils = (function () {
|
|
|
1635
2668
|
Object.keys(property).forEach((key) => {
|
|
1636
2669
|
const value = property[key];
|
|
1637
2670
|
if (key === "innerHTML") {
|
|
1638
|
-
|
|
2671
|
+
that.html($el, value);
|
|
1639
2672
|
return;
|
|
1640
2673
|
}
|
|
1641
|
-
|
|
2674
|
+
$el[key] = value;
|
|
1642
2675
|
});
|
|
1643
2676
|
Object.keys(attributes).forEach((key) => {
|
|
1644
2677
|
let value = attributes[key];
|
|
@@ -1650,12 +2683,12 @@ var DOMUtils = (function () {
|
|
|
1650
2683
|
/* function转字符串 */
|
|
1651
2684
|
value = value.toString();
|
|
1652
2685
|
}
|
|
1653
|
-
|
|
2686
|
+
$el.setAttribute(key, value);
|
|
1654
2687
|
});
|
|
1655
|
-
return
|
|
2688
|
+
return $el;
|
|
1656
2689
|
}
|
|
1657
|
-
css(
|
|
1658
|
-
const
|
|
2690
|
+
css($el, property, value) {
|
|
2691
|
+
const that = this;
|
|
1659
2692
|
/**
|
|
1660
2693
|
* 把纯数字没有px的加上
|
|
1661
2694
|
*/
|
|
@@ -1669,30 +2702,30 @@ var DOMUtils = (function () {
|
|
|
1669
2702
|
}
|
|
1670
2703
|
return propertyValue;
|
|
1671
2704
|
}
|
|
1672
|
-
if (typeof
|
|
1673
|
-
|
|
2705
|
+
if (typeof $el === "string") {
|
|
2706
|
+
$el = that.selectorAll($el);
|
|
1674
2707
|
}
|
|
1675
|
-
if (
|
|
2708
|
+
if ($el == null) {
|
|
1676
2709
|
return;
|
|
1677
2710
|
}
|
|
1678
|
-
if (
|
|
2711
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1679
2712
|
if (typeof property === "string") {
|
|
1680
2713
|
if (value == null) {
|
|
1681
2714
|
// 获取属性
|
|
1682
|
-
return
|
|
2715
|
+
return that.css($el[0], property);
|
|
1683
2716
|
}
|
|
1684
2717
|
else {
|
|
1685
2718
|
// 设置属性
|
|
1686
|
-
|
|
1687
|
-
|
|
2719
|
+
$el.forEach(($elItem) => {
|
|
2720
|
+
that.css($elItem, property);
|
|
1688
2721
|
});
|
|
1689
2722
|
return;
|
|
1690
2723
|
}
|
|
1691
2724
|
}
|
|
1692
2725
|
else if (typeof property === "object") {
|
|
1693
2726
|
// 设置属性
|
|
1694
|
-
|
|
1695
|
-
|
|
2727
|
+
$el.forEach(($elItem) => {
|
|
2728
|
+
that.css($elItem, property);
|
|
1696
2729
|
});
|
|
1697
2730
|
return;
|
|
1698
2731
|
}
|
|
@@ -1704,16 +2737,16 @@ var DOMUtils = (function () {
|
|
|
1704
2737
|
.trim()
|
|
1705
2738
|
.replace(/!important$/gi, "")
|
|
1706
2739
|
.trim();
|
|
1707
|
-
|
|
2740
|
+
$el.style.setProperty(propertyName, propertyValue, "important");
|
|
1708
2741
|
}
|
|
1709
2742
|
else {
|
|
1710
2743
|
propertyValue = handlePixe(propertyName, propertyValue);
|
|
1711
|
-
|
|
2744
|
+
$el.style.setProperty(propertyName, propertyValue);
|
|
1712
2745
|
}
|
|
1713
2746
|
};
|
|
1714
2747
|
if (typeof property === "string") {
|
|
1715
2748
|
if (value == null) {
|
|
1716
|
-
return
|
|
2749
|
+
return that.windowApi.globalThis.getComputedStyle($el).getPropertyValue(property);
|
|
1717
2750
|
}
|
|
1718
2751
|
else {
|
|
1719
2752
|
setStyleProperty(property, value);
|
|
@@ -1730,92 +2763,92 @@ var DOMUtils = (function () {
|
|
|
1730
2763
|
throw new TypeError("property must be string or object");
|
|
1731
2764
|
}
|
|
1732
2765
|
}
|
|
1733
|
-
text(
|
|
1734
|
-
const
|
|
1735
|
-
if (typeof
|
|
1736
|
-
|
|
2766
|
+
text($el, text) {
|
|
2767
|
+
const that = this;
|
|
2768
|
+
if (typeof $el === "string") {
|
|
2769
|
+
$el = that.selectorAll($el);
|
|
1737
2770
|
}
|
|
1738
|
-
if (
|
|
2771
|
+
if ($el == null) {
|
|
1739
2772
|
return;
|
|
1740
2773
|
}
|
|
1741
|
-
if (
|
|
2774
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1742
2775
|
if (text == null) {
|
|
1743
2776
|
// 获取
|
|
1744
|
-
return
|
|
2777
|
+
return that.text($el[0]);
|
|
1745
2778
|
}
|
|
1746
2779
|
else {
|
|
1747
2780
|
// 设置
|
|
1748
|
-
|
|
1749
|
-
|
|
2781
|
+
$el.forEach(($elItem) => {
|
|
2782
|
+
that.text($elItem, text);
|
|
1750
2783
|
});
|
|
1751
2784
|
}
|
|
1752
2785
|
return;
|
|
1753
2786
|
}
|
|
1754
2787
|
if (text == null) {
|
|
1755
|
-
return
|
|
2788
|
+
return $el.textContent || $el.innerText;
|
|
1756
2789
|
}
|
|
1757
2790
|
else {
|
|
1758
2791
|
if (text instanceof Node) {
|
|
1759
2792
|
text = text.textContent || text.innerText;
|
|
1760
2793
|
}
|
|
1761
|
-
if ("textContent" in
|
|
1762
|
-
|
|
2794
|
+
if ("textContent" in $el) {
|
|
2795
|
+
$el.textContent = text;
|
|
1763
2796
|
}
|
|
1764
|
-
else if ("innerText" in
|
|
1765
|
-
|
|
2797
|
+
else if ("innerText" in $el) {
|
|
2798
|
+
$el.innerText = text;
|
|
1766
2799
|
}
|
|
1767
2800
|
}
|
|
1768
2801
|
}
|
|
1769
|
-
html(
|
|
1770
|
-
const
|
|
1771
|
-
if (typeof
|
|
1772
|
-
|
|
2802
|
+
html($el, html) {
|
|
2803
|
+
const that = this;
|
|
2804
|
+
if (typeof $el === "string") {
|
|
2805
|
+
$el = that.selectorAll($el);
|
|
1773
2806
|
}
|
|
1774
|
-
if (
|
|
2807
|
+
if ($el == null) {
|
|
1775
2808
|
return;
|
|
1776
2809
|
}
|
|
1777
|
-
if (
|
|
2810
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1778
2811
|
if (html == null) {
|
|
1779
2812
|
// 获取
|
|
1780
|
-
return
|
|
2813
|
+
return that.html($el[0]);
|
|
1781
2814
|
}
|
|
1782
2815
|
else {
|
|
1783
2816
|
// 设置
|
|
1784
|
-
|
|
1785
|
-
|
|
2817
|
+
$el.forEach(($elItem) => {
|
|
2818
|
+
that.html($elItem, html);
|
|
1786
2819
|
});
|
|
1787
2820
|
}
|
|
1788
2821
|
return;
|
|
1789
2822
|
}
|
|
1790
2823
|
if (html == null) {
|
|
1791
2824
|
// 获取
|
|
1792
|
-
return
|
|
2825
|
+
return $el.innerHTML;
|
|
1793
2826
|
}
|
|
1794
2827
|
else {
|
|
1795
2828
|
// 设置
|
|
1796
2829
|
if (html instanceof Element) {
|
|
1797
2830
|
html = html.innerHTML;
|
|
1798
2831
|
}
|
|
1799
|
-
if ("innerHTML" in
|
|
1800
|
-
|
|
2832
|
+
if ("innerHTML" in $el) {
|
|
2833
|
+
CommonUtils.setSafeHTML($el, html);
|
|
1801
2834
|
}
|
|
1802
2835
|
}
|
|
1803
2836
|
}
|
|
1804
2837
|
/**
|
|
1805
2838
|
* 获取移动元素的transform偏移
|
|
1806
2839
|
*/
|
|
1807
|
-
getTransform(
|
|
1808
|
-
const
|
|
2840
|
+
getTransform($el, isShow = false) {
|
|
2841
|
+
const that = this;
|
|
1809
2842
|
let transform_left = 0;
|
|
1810
2843
|
let transform_top = 0;
|
|
1811
|
-
if (!(isShow || (!isShow &&
|
|
2844
|
+
if (!(isShow || (!isShow && CommonUtils.isShow($el)))) {
|
|
1812
2845
|
/* 未显示 */
|
|
1813
|
-
const { recovery } =
|
|
1814
|
-
const transformInfo =
|
|
2846
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
2847
|
+
const transformInfo = that.getTransform($el, true);
|
|
1815
2848
|
recovery();
|
|
1816
2849
|
return transformInfo;
|
|
1817
2850
|
}
|
|
1818
|
-
const elementTransform =
|
|
2851
|
+
const elementTransform = that.windowApi.globalThis.getComputedStyle($el).transform;
|
|
1819
2852
|
if (elementTransform != null && elementTransform !== "none" && elementTransform !== "") {
|
|
1820
2853
|
const elementTransformSplit = elementTransform.match(/\((.+)\)/)?.[1].split(",");
|
|
1821
2854
|
if (elementTransformSplit) {
|
|
@@ -1832,225 +2865,189 @@ var DOMUtils = (function () {
|
|
|
1832
2865
|
transformTop: transform_top,
|
|
1833
2866
|
};
|
|
1834
2867
|
}
|
|
1835
|
-
val(
|
|
1836
|
-
const
|
|
1837
|
-
if (typeof
|
|
1838
|
-
|
|
2868
|
+
val($el, value) {
|
|
2869
|
+
const that = this;
|
|
2870
|
+
if (typeof $el === "string") {
|
|
2871
|
+
$el = that.selectorAll($el);
|
|
1839
2872
|
}
|
|
1840
|
-
if (
|
|
2873
|
+
if ($el == null) {
|
|
1841
2874
|
return;
|
|
1842
2875
|
}
|
|
1843
|
-
if (
|
|
2876
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1844
2877
|
if (value == null) {
|
|
1845
2878
|
// 获取
|
|
1846
|
-
return
|
|
2879
|
+
return that.val($el[0]);
|
|
1847
2880
|
}
|
|
1848
2881
|
else {
|
|
1849
2882
|
// 设置
|
|
1850
|
-
|
|
1851
|
-
|
|
2883
|
+
$el.forEach(($elItem) => {
|
|
2884
|
+
that.val($elItem, value);
|
|
1852
2885
|
});
|
|
1853
2886
|
}
|
|
1854
2887
|
return;
|
|
1855
2888
|
}
|
|
1856
2889
|
if (value == null) {
|
|
1857
2890
|
// 获取
|
|
1858
|
-
if (
|
|
1859
|
-
return
|
|
2891
|
+
if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
|
|
2892
|
+
return $el.checked;
|
|
1860
2893
|
}
|
|
1861
2894
|
else {
|
|
1862
|
-
return
|
|
2895
|
+
return $el.value;
|
|
1863
2896
|
}
|
|
1864
2897
|
}
|
|
1865
2898
|
else {
|
|
1866
2899
|
// 设置
|
|
1867
|
-
if (
|
|
1868
|
-
|
|
2900
|
+
if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
|
|
2901
|
+
$el.checked = !!value;
|
|
1869
2902
|
}
|
|
1870
2903
|
else {
|
|
1871
|
-
|
|
2904
|
+
$el.value = value;
|
|
1872
2905
|
}
|
|
1873
2906
|
}
|
|
1874
2907
|
}
|
|
1875
|
-
prop(
|
|
1876
|
-
const
|
|
1877
|
-
if (typeof
|
|
1878
|
-
|
|
2908
|
+
prop($el, propName, propValue) {
|
|
2909
|
+
const that = this;
|
|
2910
|
+
if (typeof $el === "string") {
|
|
2911
|
+
$el = that.selectorAll($el);
|
|
1879
2912
|
}
|
|
1880
|
-
if (
|
|
2913
|
+
if ($el == null) {
|
|
1881
2914
|
return;
|
|
1882
2915
|
}
|
|
1883
|
-
if (
|
|
2916
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1884
2917
|
if (propValue == null) {
|
|
1885
2918
|
// 获取
|
|
1886
|
-
return
|
|
2919
|
+
return that.prop($el[0], propName);
|
|
1887
2920
|
}
|
|
1888
2921
|
else {
|
|
1889
2922
|
// 设置
|
|
1890
|
-
|
|
1891
|
-
|
|
2923
|
+
$el.forEach(($elItem) => {
|
|
2924
|
+
that.prop($elItem, propName, propValue);
|
|
1892
2925
|
});
|
|
1893
2926
|
}
|
|
1894
2927
|
return;
|
|
1895
2928
|
}
|
|
1896
2929
|
if (propValue == null) {
|
|
1897
|
-
return Reflect.get(
|
|
2930
|
+
return Reflect.get($el, propName);
|
|
1898
2931
|
}
|
|
1899
2932
|
else {
|
|
1900
|
-
if (
|
|
1901
|
-
|
|
2933
|
+
if ($el instanceof Element && propName === "innerHTML") {
|
|
2934
|
+
that.html($el, propValue);
|
|
1902
2935
|
}
|
|
1903
2936
|
else {
|
|
1904
|
-
Reflect.set(
|
|
2937
|
+
Reflect.set($el, propName, propValue);
|
|
1905
2938
|
}
|
|
1906
2939
|
}
|
|
1907
2940
|
}
|
|
1908
2941
|
/**
|
|
1909
2942
|
* 移除元素的属性
|
|
1910
|
-
* @param
|
|
2943
|
+
* @param $el 目标元素
|
|
1911
2944
|
* @param attrName 属性名
|
|
1912
2945
|
* @example
|
|
1913
2946
|
* // 移除元素a.xx的属性data-value
|
|
1914
2947
|
* DOMUtils.removeAttr(document.querySelector("a.xx"),"data-value")
|
|
1915
2948
|
* DOMUtils.removeAttr("a.xx","data-value")
|
|
1916
2949
|
* */
|
|
1917
|
-
removeAttr(
|
|
1918
|
-
const
|
|
1919
|
-
if (typeof
|
|
1920
|
-
|
|
2950
|
+
removeAttr($el, attrName) {
|
|
2951
|
+
const that = this;
|
|
2952
|
+
if (typeof $el === "string") {
|
|
2953
|
+
$el = that.selectorAll($el);
|
|
1921
2954
|
}
|
|
1922
|
-
if (
|
|
2955
|
+
if ($el == null) {
|
|
1923
2956
|
return;
|
|
1924
2957
|
}
|
|
1925
|
-
if (
|
|
2958
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1926
2959
|
// 设置
|
|
1927
|
-
|
|
1928
|
-
|
|
2960
|
+
$el.forEach(($elItem) => {
|
|
2961
|
+
that.removeAttr($elItem, attrName);
|
|
1929
2962
|
});
|
|
1930
2963
|
return;
|
|
1931
2964
|
}
|
|
1932
|
-
|
|
2965
|
+
$el.removeAttribute(attrName);
|
|
1933
2966
|
}
|
|
1934
2967
|
/**
|
|
1935
2968
|
* 移除元素class名
|
|
1936
|
-
* @param
|
|
2969
|
+
* @param $el 目标元素
|
|
1937
2970
|
* @param className 类名
|
|
1938
2971
|
* @example
|
|
1939
2972
|
* // 移除元素a.xx的className为xx
|
|
1940
2973
|
* DOMUtils.removeClass(document.querySelector("a.xx"),"xx")
|
|
1941
2974
|
* DOMUtils.removeClass("a.xx","xx")
|
|
1942
2975
|
*/
|
|
1943
|
-
removeClass(
|
|
1944
|
-
const
|
|
1945
|
-
if (typeof
|
|
1946
|
-
|
|
2976
|
+
removeClass($el, className) {
|
|
2977
|
+
const that = this;
|
|
2978
|
+
if (typeof $el === "string") {
|
|
2979
|
+
$el = that.selectorAll($el);
|
|
1947
2980
|
}
|
|
1948
|
-
if (
|
|
2981
|
+
if ($el == null) {
|
|
1949
2982
|
return;
|
|
1950
2983
|
}
|
|
1951
|
-
if (
|
|
2984
|
+
if (CommonUtils.isNodeList($el)) {
|
|
1952
2985
|
// 设置
|
|
1953
|
-
|
|
1954
|
-
|
|
2986
|
+
$el.forEach(($elItem) => {
|
|
2987
|
+
that.removeClass($elItem, className);
|
|
1955
2988
|
});
|
|
1956
2989
|
return;
|
|
1957
2990
|
}
|
|
1958
2991
|
if (className == null) {
|
|
1959
2992
|
// 清空全部className
|
|
1960
|
-
|
|
2993
|
+
$el.className = "";
|
|
1961
2994
|
}
|
|
1962
2995
|
else {
|
|
1963
2996
|
if (!Array.isArray(className)) {
|
|
1964
2997
|
className = className.trim().split(" ");
|
|
1965
2998
|
}
|
|
1966
2999
|
className.forEach((itemClassName) => {
|
|
1967
|
-
|
|
3000
|
+
$el.classList.remove(itemClassName);
|
|
1968
3001
|
});
|
|
1969
3002
|
}
|
|
1970
3003
|
}
|
|
1971
3004
|
/**
|
|
1972
3005
|
* 移除元素的属性
|
|
1973
|
-
* @param
|
|
3006
|
+
* @param $el 目标元素
|
|
1974
3007
|
* @param propName 属性名
|
|
1975
3008
|
* @example
|
|
1976
3009
|
* // 移除元素a.xx的href属性
|
|
1977
3010
|
* DOMUtils.removeProp(document.querySelector("a.xx"),"href")
|
|
1978
3011
|
* DOMUtils.removeProp("a.xx","href")
|
|
1979
3012
|
* */
|
|
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);
|
|
3013
|
+
removeProp($el, propName) {
|
|
3014
|
+
const that = this;
|
|
3015
|
+
if (typeof $el === "string") {
|
|
3016
|
+
$el = that.selectorAll($el);
|
|
2010
3017
|
}
|
|
2011
|
-
if (
|
|
3018
|
+
if ($el == null) {
|
|
2012
3019
|
return;
|
|
2013
3020
|
}
|
|
2014
|
-
if (
|
|
3021
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2015
3022
|
// 设置
|
|
2016
|
-
|
|
2017
|
-
|
|
3023
|
+
$el.forEach(($elItem) => {
|
|
3024
|
+
that.removeProp($elItem, propName);
|
|
2018
3025
|
});
|
|
2019
3026
|
return;
|
|
2020
3027
|
}
|
|
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
|
-
}
|
|
3028
|
+
CommonUtils.delete($el, propName);
|
|
2032
3029
|
}
|
|
2033
3030
|
/**
|
|
2034
3031
|
* 给元素添加class
|
|
2035
|
-
* @param
|
|
3032
|
+
* @param $el 目标元素
|
|
2036
3033
|
* @param className class名
|
|
2037
3034
|
* @example
|
|
2038
3035
|
* // 元素a.xx的className添加_vue_
|
|
2039
3036
|
* DOMUtils.addClass(document.querySelector("a.xx"),"_vue_")
|
|
2040
3037
|
* DOMUtils.addClass("a.xx","_vue_")
|
|
2041
3038
|
* */
|
|
2042
|
-
addClass(
|
|
2043
|
-
const
|
|
2044
|
-
if (typeof
|
|
2045
|
-
|
|
3039
|
+
addClass($el, className) {
|
|
3040
|
+
const that = this;
|
|
3041
|
+
if (typeof $el === "string") {
|
|
3042
|
+
$el = that.selectorAll($el);
|
|
2046
3043
|
}
|
|
2047
|
-
if (
|
|
3044
|
+
if ($el == null) {
|
|
2048
3045
|
return;
|
|
2049
3046
|
}
|
|
2050
|
-
if (
|
|
3047
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2051
3048
|
// 设置
|
|
2052
|
-
|
|
2053
|
-
|
|
3049
|
+
$el.forEach(($elItem) => {
|
|
3050
|
+
that.addClass($elItem, className);
|
|
2054
3051
|
});
|
|
2055
3052
|
return;
|
|
2056
3053
|
}
|
|
@@ -2061,31 +3058,31 @@ var DOMUtils = (function () {
|
|
|
2061
3058
|
if (itemClassName.trim() == "") {
|
|
2062
3059
|
return;
|
|
2063
3060
|
}
|
|
2064
|
-
|
|
3061
|
+
$el.classList.add(itemClassName);
|
|
2065
3062
|
});
|
|
2066
3063
|
}
|
|
2067
3064
|
/**
|
|
2068
3065
|
* 判断元素是否存在className
|
|
2069
|
-
* @param
|
|
3066
|
+
* @param $el
|
|
2070
3067
|
* @param className
|
|
2071
3068
|
*/
|
|
2072
|
-
hasClass(
|
|
2073
|
-
const
|
|
2074
|
-
if (typeof
|
|
2075
|
-
|
|
3069
|
+
hasClass($el, className) {
|
|
3070
|
+
const that = this;
|
|
3071
|
+
if (typeof $el === "string") {
|
|
3072
|
+
$el = that.selectorAll($el);
|
|
2076
3073
|
}
|
|
2077
|
-
if (
|
|
3074
|
+
if ($el == null) {
|
|
2078
3075
|
return false;
|
|
2079
3076
|
}
|
|
2080
|
-
if (
|
|
3077
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2081
3078
|
let flag = true;
|
|
2082
|
-
for (let index = 0; index <
|
|
2083
|
-
const $
|
|
2084
|
-
flag = flag &&
|
|
3079
|
+
for (let index = 0; index < $el.length; index++) {
|
|
3080
|
+
const $elItem = $el[index];
|
|
3081
|
+
flag = flag && that.hasClass($elItem, className);
|
|
2085
3082
|
}
|
|
2086
3083
|
return flag;
|
|
2087
3084
|
}
|
|
2088
|
-
if (
|
|
3085
|
+
if (!$el?.classList) {
|
|
2089
3086
|
return false;
|
|
2090
3087
|
}
|
|
2091
3088
|
if (!Array.isArray(className)) {
|
|
@@ -2093,7 +3090,7 @@ var DOMUtils = (function () {
|
|
|
2093
3090
|
}
|
|
2094
3091
|
for (let index = 0; index < className.length; index++) {
|
|
2095
3092
|
const item = className[index].trim();
|
|
2096
|
-
if (
|
|
3093
|
+
if (!$el.classList.contains(item)) {
|
|
2097
3094
|
return false;
|
|
2098
3095
|
}
|
|
2099
3096
|
}
|
|
@@ -2101,25 +3098,25 @@ var DOMUtils = (function () {
|
|
|
2101
3098
|
}
|
|
2102
3099
|
/**
|
|
2103
3100
|
* 函数在元素内部末尾添加子元素或HTML字符串
|
|
2104
|
-
* @param
|
|
3101
|
+
* @param $el 目标元素
|
|
2105
3102
|
* @param content 子元素或HTML字符串
|
|
2106
3103
|
* @example
|
|
2107
3104
|
* // 元素a.xx的内部末尾添加一个元素
|
|
2108
3105
|
* DOMUtils.append(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
2109
3106
|
* DOMUtils.append("a.xx","'<b class="xx"></b>")
|
|
2110
3107
|
* */
|
|
2111
|
-
append(
|
|
2112
|
-
const
|
|
2113
|
-
if (typeof
|
|
2114
|
-
|
|
3108
|
+
append($el, content) {
|
|
3109
|
+
const that = this;
|
|
3110
|
+
if (typeof $el === "string") {
|
|
3111
|
+
$el = that.selectorAll($el);
|
|
2115
3112
|
}
|
|
2116
|
-
if (
|
|
3113
|
+
if ($el == null) {
|
|
2117
3114
|
return;
|
|
2118
3115
|
}
|
|
2119
|
-
if (
|
|
3116
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2120
3117
|
// 设置
|
|
2121
|
-
|
|
2122
|
-
|
|
3118
|
+
$el.forEach(($elItem) => {
|
|
3119
|
+
that.append($elItem, content);
|
|
2123
3120
|
});
|
|
2124
3121
|
return;
|
|
2125
3122
|
}
|
|
@@ -2127,12 +3124,12 @@ var DOMUtils = (function () {
|
|
|
2127
3124
|
if (typeof content === "string") {
|
|
2128
3125
|
if (ele instanceof DocumentFragment) {
|
|
2129
3126
|
if (typeof text === "string") {
|
|
2130
|
-
text =
|
|
3127
|
+
text = that.toElement(text, true, false);
|
|
2131
3128
|
}
|
|
2132
3129
|
ele.appendChild(text);
|
|
2133
3130
|
}
|
|
2134
3131
|
else {
|
|
2135
|
-
ele.insertAdjacentHTML("beforeend",
|
|
3132
|
+
ele.insertAdjacentHTML("beforeend", CommonUtils.createSafeHTML(text));
|
|
2136
3133
|
}
|
|
2137
3134
|
}
|
|
2138
3135
|
else {
|
|
@@ -2141,96 +3138,96 @@ var DOMUtils = (function () {
|
|
|
2141
3138
|
}
|
|
2142
3139
|
if (Array.isArray(content) || content instanceof NodeList) {
|
|
2143
3140
|
/* 数组 */
|
|
2144
|
-
const fragment =
|
|
3141
|
+
const fragment = that.windowApi.document.createDocumentFragment();
|
|
2145
3142
|
content.forEach((ele) => {
|
|
2146
3143
|
if (typeof ele === "string") {
|
|
2147
3144
|
// 转为元素
|
|
2148
|
-
ele =
|
|
3145
|
+
ele = that.toElement(ele, true, false);
|
|
2149
3146
|
}
|
|
2150
3147
|
fragment.appendChild(ele);
|
|
2151
3148
|
});
|
|
2152
|
-
|
|
3149
|
+
$el.appendChild(fragment);
|
|
2153
3150
|
}
|
|
2154
3151
|
else {
|
|
2155
|
-
elementAppendChild(
|
|
3152
|
+
elementAppendChild($el, content);
|
|
2156
3153
|
}
|
|
2157
3154
|
}
|
|
2158
3155
|
/**
|
|
2159
3156
|
* 函数 在元素内部开头添加子元素或HTML字符串
|
|
2160
|
-
* @param
|
|
3157
|
+
* @param $el 目标元素
|
|
2161
3158
|
* @param content 子元素或HTML字符串
|
|
2162
3159
|
* @example
|
|
2163
3160
|
* // 元素a.xx内部开头添加一个元素
|
|
2164
3161
|
* DOMUtils.prepend(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
2165
3162
|
* DOMUtils.prepend("a.xx","'<b class="xx"></b>")
|
|
2166
3163
|
* */
|
|
2167
|
-
prepend(
|
|
2168
|
-
const
|
|
2169
|
-
if (typeof
|
|
2170
|
-
|
|
3164
|
+
prepend($el, content) {
|
|
3165
|
+
const that = this;
|
|
3166
|
+
if (typeof $el === "string") {
|
|
3167
|
+
$el = that.selectorAll($el);
|
|
2171
3168
|
}
|
|
2172
|
-
if (
|
|
3169
|
+
if ($el == null) {
|
|
2173
3170
|
return;
|
|
2174
3171
|
}
|
|
2175
|
-
if (
|
|
3172
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2176
3173
|
// 设置
|
|
2177
|
-
|
|
2178
|
-
|
|
3174
|
+
$el.forEach(($elItem) => {
|
|
3175
|
+
that.prepend($elItem, content);
|
|
2179
3176
|
});
|
|
2180
3177
|
return;
|
|
2181
3178
|
}
|
|
2182
3179
|
if (typeof content === "string") {
|
|
2183
|
-
if (
|
|
2184
|
-
content =
|
|
2185
|
-
|
|
3180
|
+
if ($el instanceof DocumentFragment) {
|
|
3181
|
+
content = that.toElement(content, true, false);
|
|
3182
|
+
$el.prepend(content);
|
|
2186
3183
|
}
|
|
2187
3184
|
else {
|
|
2188
|
-
|
|
3185
|
+
$el.insertAdjacentHTML("afterbegin", CommonUtils.createSafeHTML(content));
|
|
2189
3186
|
}
|
|
2190
3187
|
}
|
|
2191
3188
|
else {
|
|
2192
|
-
const $firstChild =
|
|
3189
|
+
const $firstChild = $el.firstChild;
|
|
2193
3190
|
if ($firstChild == null) {
|
|
2194
|
-
|
|
3191
|
+
$el.prepend(content);
|
|
2195
3192
|
}
|
|
2196
3193
|
else {
|
|
2197
|
-
|
|
3194
|
+
$el.insertBefore(content, $firstChild);
|
|
2198
3195
|
}
|
|
2199
3196
|
}
|
|
2200
3197
|
}
|
|
2201
3198
|
/**
|
|
2202
3199
|
* 在元素后面添加兄弟元素或HTML字符串
|
|
2203
|
-
* @param
|
|
3200
|
+
* @param $el 目标元素
|
|
2204
3201
|
* @param content 兄弟元素或HTML字符串
|
|
2205
3202
|
* @example
|
|
2206
3203
|
* // 元素a.xx后面添加一个元素
|
|
2207
3204
|
* DOMUtils.after(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
2208
3205
|
* DOMUtils.after("a.xx","'<b class="xx"></b>")
|
|
2209
3206
|
* */
|
|
2210
|
-
after(
|
|
2211
|
-
const
|
|
2212
|
-
if (typeof
|
|
2213
|
-
|
|
3207
|
+
after($el, content) {
|
|
3208
|
+
const that = this;
|
|
3209
|
+
if (typeof $el === "string") {
|
|
3210
|
+
$el = that.selectorAll($el);
|
|
2214
3211
|
}
|
|
2215
|
-
if (
|
|
3212
|
+
if ($el == null) {
|
|
2216
3213
|
return;
|
|
2217
3214
|
}
|
|
2218
|
-
if (
|
|
3215
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2219
3216
|
// 设置
|
|
2220
|
-
|
|
2221
|
-
|
|
3217
|
+
$el.forEach(($elItem) => {
|
|
3218
|
+
that.after($elItem, content);
|
|
2222
3219
|
});
|
|
2223
3220
|
return;
|
|
2224
3221
|
}
|
|
2225
3222
|
if (typeof content === "string") {
|
|
2226
|
-
|
|
3223
|
+
$el.insertAdjacentHTML("afterend", CommonUtils.createSafeHTML(content));
|
|
2227
3224
|
}
|
|
2228
3225
|
else {
|
|
2229
|
-
const $parent =
|
|
2230
|
-
const $nextSlibling =
|
|
3226
|
+
const $parent = $el.parentElement;
|
|
3227
|
+
const $nextSlibling = $el.nextSibling;
|
|
2231
3228
|
if (!$parent || $nextSlibling) {
|
|
2232
3229
|
// 任意一个不行
|
|
2233
|
-
|
|
3230
|
+
$el.after(content);
|
|
2234
3231
|
}
|
|
2235
3232
|
else {
|
|
2236
3233
|
$parent.insertBefore(content, $nextSlibling);
|
|
@@ -2239,157 +3236,157 @@ var DOMUtils = (function () {
|
|
|
2239
3236
|
}
|
|
2240
3237
|
/**
|
|
2241
3238
|
* 在元素前面添加兄弟元素或HTML字符串
|
|
2242
|
-
* @param
|
|
3239
|
+
* @param $el 目标元素
|
|
2243
3240
|
* @param content 兄弟元素或HTML字符串
|
|
2244
3241
|
* @example
|
|
2245
3242
|
* // 元素a.xx前面添加一个元素
|
|
2246
3243
|
* DOMUtils.before(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
2247
3244
|
* DOMUtils.before("a.xx","'<b class="xx"></b>")
|
|
2248
3245
|
* */
|
|
2249
|
-
before(
|
|
2250
|
-
const
|
|
2251
|
-
if (typeof
|
|
2252
|
-
|
|
3246
|
+
before($el, content) {
|
|
3247
|
+
const that = this;
|
|
3248
|
+
if (typeof $el === "string") {
|
|
3249
|
+
$el = that.selectorAll($el);
|
|
2253
3250
|
}
|
|
2254
|
-
if (
|
|
3251
|
+
if ($el == null) {
|
|
2255
3252
|
return;
|
|
2256
3253
|
}
|
|
2257
|
-
if (
|
|
3254
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2258
3255
|
// 设置
|
|
2259
|
-
|
|
2260
|
-
|
|
3256
|
+
$el.forEach(($elItem) => {
|
|
3257
|
+
that.before($elItem, content);
|
|
2261
3258
|
});
|
|
2262
3259
|
return;
|
|
2263
3260
|
}
|
|
2264
3261
|
if (typeof content === "string") {
|
|
2265
|
-
|
|
3262
|
+
$el.insertAdjacentHTML("beforebegin", CommonUtils.createSafeHTML(content));
|
|
2266
3263
|
}
|
|
2267
3264
|
else {
|
|
2268
|
-
const $parent =
|
|
3265
|
+
const $parent = $el.parentElement;
|
|
2269
3266
|
if (!$parent) {
|
|
2270
|
-
|
|
3267
|
+
$el.before(content);
|
|
2271
3268
|
}
|
|
2272
3269
|
else {
|
|
2273
|
-
$parent.insertBefore(content,
|
|
3270
|
+
$parent.insertBefore(content, $el);
|
|
2274
3271
|
}
|
|
2275
3272
|
}
|
|
2276
3273
|
}
|
|
2277
3274
|
/**
|
|
2278
3275
|
* 移除元素
|
|
2279
|
-
* @param
|
|
3276
|
+
* @param $el 目标元素
|
|
2280
3277
|
* @example
|
|
2281
3278
|
* // 元素a.xx前面添加一个元素
|
|
2282
3279
|
* DOMUtils.remove(document.querySelector("a.xx"))
|
|
2283
3280
|
* DOMUtils.remove(document.querySelectorAll("a.xx"))
|
|
2284
3281
|
* DOMUtils.remove("a.xx")
|
|
2285
3282
|
* */
|
|
2286
|
-
remove(
|
|
2287
|
-
const
|
|
2288
|
-
if (typeof
|
|
2289
|
-
|
|
3283
|
+
remove($el) {
|
|
3284
|
+
const that = this;
|
|
3285
|
+
if (typeof $el === "string") {
|
|
3286
|
+
$el = that.selectorAll($el);
|
|
2290
3287
|
}
|
|
2291
|
-
if (
|
|
3288
|
+
if ($el == null) {
|
|
2292
3289
|
return;
|
|
2293
3290
|
}
|
|
2294
|
-
if (
|
|
2295
|
-
|
|
2296
|
-
|
|
3291
|
+
if (CommonUtils.isNodeList($el)) {
|
|
3292
|
+
$el.forEach(($elItem) => {
|
|
3293
|
+
that.remove($elItem);
|
|
2297
3294
|
});
|
|
2298
3295
|
return;
|
|
2299
3296
|
}
|
|
2300
|
-
if (typeof
|
|
2301
|
-
|
|
3297
|
+
if (typeof $el.remove === "function") {
|
|
3298
|
+
$el.remove();
|
|
2302
3299
|
}
|
|
2303
|
-
else if (
|
|
2304
|
-
|
|
3300
|
+
else if ($el.parentElement) {
|
|
3301
|
+
$el.parentElement.removeChild($el);
|
|
2305
3302
|
}
|
|
2306
|
-
else if (
|
|
2307
|
-
|
|
3303
|
+
else if ($el.parentNode) {
|
|
3304
|
+
$el.parentNode.removeChild($el);
|
|
2308
3305
|
}
|
|
2309
3306
|
}
|
|
2310
3307
|
/**
|
|
2311
3308
|
* 移除元素的所有子元素
|
|
2312
|
-
* @param
|
|
3309
|
+
* @param $el 目标元素
|
|
2313
3310
|
* @example
|
|
2314
3311
|
* // 移除元素a.xx元素的所有子元素
|
|
2315
3312
|
* DOMUtils.empty(document.querySelector("a.xx"))
|
|
2316
3313
|
* DOMUtils.empty("a.xx")
|
|
2317
3314
|
* */
|
|
2318
|
-
empty(
|
|
2319
|
-
const
|
|
2320
|
-
if (typeof
|
|
2321
|
-
|
|
3315
|
+
empty($el) {
|
|
3316
|
+
const that = this;
|
|
3317
|
+
if (typeof $el === "string") {
|
|
3318
|
+
$el = that.selectorAll($el);
|
|
2322
3319
|
}
|
|
2323
|
-
if (
|
|
3320
|
+
if ($el == null) {
|
|
2324
3321
|
return;
|
|
2325
3322
|
}
|
|
2326
|
-
if (
|
|
3323
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2327
3324
|
// 设置
|
|
2328
|
-
|
|
2329
|
-
|
|
3325
|
+
$el.forEach(($elItem) => {
|
|
3326
|
+
that.empty($elItem);
|
|
2330
3327
|
});
|
|
2331
3328
|
return;
|
|
2332
3329
|
}
|
|
2333
|
-
if (
|
|
2334
|
-
|
|
3330
|
+
if ($el.innerHTML) {
|
|
3331
|
+
$el.innerHTML = "";
|
|
2335
3332
|
}
|
|
2336
|
-
else if (
|
|
2337
|
-
|
|
3333
|
+
else if ($el.textContent) {
|
|
3334
|
+
$el.textContent = "";
|
|
2338
3335
|
}
|
|
2339
3336
|
}
|
|
2340
3337
|
/**
|
|
2341
3338
|
* 获取元素相对于文档的偏移坐标(加上文档的滚动条)
|
|
2342
|
-
* @param
|
|
3339
|
+
* @param $el 目标元素
|
|
2343
3340
|
* @example
|
|
2344
3341
|
* // 获取元素a.xx的对于文档的偏移坐标
|
|
2345
3342
|
* DOMUtils.offset(document.querySelector("a.xx"))
|
|
2346
3343
|
* DOMUtils.offset("a.xx")
|
|
2347
3344
|
* > 0
|
|
2348
3345
|
*/
|
|
2349
|
-
offset(
|
|
2350
|
-
const
|
|
2351
|
-
if (typeof
|
|
2352
|
-
|
|
3346
|
+
offset($el) {
|
|
3347
|
+
const that = this;
|
|
3348
|
+
if (typeof $el === "string") {
|
|
3349
|
+
$el = that.selector($el);
|
|
2353
3350
|
}
|
|
2354
|
-
if (
|
|
3351
|
+
if ($el == null) {
|
|
2355
3352
|
return;
|
|
2356
3353
|
}
|
|
2357
|
-
const rect =
|
|
3354
|
+
const rect = $el.getBoundingClientRect();
|
|
2358
3355
|
return {
|
|
2359
3356
|
/** y轴偏移 */
|
|
2360
|
-
top: rect.top +
|
|
3357
|
+
top: rect.top + that.windowApi.globalThis.scrollY,
|
|
2361
3358
|
/** x轴偏移 */
|
|
2362
|
-
left: rect.left +
|
|
3359
|
+
left: rect.left + that.windowApi.globalThis.scrollX,
|
|
2363
3360
|
};
|
|
2364
3361
|
}
|
|
2365
|
-
width(
|
|
2366
|
-
const
|
|
2367
|
-
if (typeof
|
|
2368
|
-
|
|
3362
|
+
width($el, isShow = false) {
|
|
3363
|
+
const that = this;
|
|
3364
|
+
if (typeof $el === "string") {
|
|
3365
|
+
$el = that.selector($el);
|
|
2369
3366
|
}
|
|
2370
|
-
if (
|
|
2371
|
-
return
|
|
3367
|
+
if (CommonUtils.isWin($el)) {
|
|
3368
|
+
return that.windowApi.window.document.documentElement.clientWidth;
|
|
2372
3369
|
}
|
|
2373
|
-
if (
|
|
3370
|
+
if ($el.nodeType === 9) {
|
|
2374
3371
|
/* Document文档节点 */
|
|
2375
|
-
|
|
2376
|
-
return Math.max(
|
|
3372
|
+
$el = $el;
|
|
3373
|
+
return Math.max($el.body.scrollWidth, $el.documentElement.scrollWidth, $el.body.offsetWidth, $el.documentElement.offsetWidth, $el.documentElement.clientWidth);
|
|
2377
3374
|
}
|
|
2378
|
-
if (isShow || (!isShow &&
|
|
3375
|
+
if (isShow || (!isShow && CommonUtils.isShow($el))) {
|
|
2379
3376
|
/* 已显示 */
|
|
2380
3377
|
/* 不从style中获取对应的宽度,因为可能使用了class定义了width !important */
|
|
2381
|
-
|
|
3378
|
+
$el = $el;
|
|
2382
3379
|
/* 如果element.style.width为空 则从css里面获取是否定义了width信息如果定义了 则读取css里面定义的宽度width */
|
|
2383
|
-
if (parseFloat(
|
|
2384
|
-
return parseFloat(
|
|
3380
|
+
if (parseFloat(CommonUtils.getStyleValue($el, "width").toString()) > 0) {
|
|
3381
|
+
return parseFloat(CommonUtils.getStyleValue($el, "width").toString());
|
|
2385
3382
|
}
|
|
2386
3383
|
/* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetWidth来进行计算 */
|
|
2387
|
-
if (
|
|
2388
|
-
const borderLeftWidth =
|
|
2389
|
-
const borderRightWidth =
|
|
2390
|
-
const paddingLeft =
|
|
2391
|
-
const paddingRight =
|
|
2392
|
-
const backHeight = parseFloat(
|
|
3384
|
+
if ($el.offsetWidth > 0) {
|
|
3385
|
+
const borderLeftWidth = CommonUtils.getStyleValue($el, "borderLeftWidth");
|
|
3386
|
+
const borderRightWidth = CommonUtils.getStyleValue($el, "borderRightWidth");
|
|
3387
|
+
const paddingLeft = CommonUtils.getStyleValue($el, "paddingLeft");
|
|
3388
|
+
const paddingRight = CommonUtils.getStyleValue($el, "paddingRight");
|
|
3389
|
+
const backHeight = parseFloat($el.offsetWidth.toString()) -
|
|
2393
3390
|
parseFloat(borderLeftWidth.toString()) -
|
|
2394
3391
|
parseFloat(borderRightWidth.toString()) -
|
|
2395
3392
|
parseFloat(paddingLeft.toString()) -
|
|
@@ -2400,41 +3397,41 @@ var DOMUtils = (function () {
|
|
|
2400
3397
|
}
|
|
2401
3398
|
else {
|
|
2402
3399
|
/* 未显示 */
|
|
2403
|
-
|
|
2404
|
-
const { recovery } =
|
|
2405
|
-
const width =
|
|
3400
|
+
$el = $el;
|
|
3401
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
3402
|
+
const width = that.width($el, true);
|
|
2406
3403
|
recovery();
|
|
2407
3404
|
return width;
|
|
2408
3405
|
}
|
|
2409
3406
|
}
|
|
2410
|
-
height(
|
|
2411
|
-
const
|
|
2412
|
-
if (
|
|
2413
|
-
return
|
|
3407
|
+
height($el, isShow = false) {
|
|
3408
|
+
const that = this;
|
|
3409
|
+
if (CommonUtils.isWin($el)) {
|
|
3410
|
+
return that.windowApi.window.document.documentElement.clientHeight;
|
|
2414
3411
|
}
|
|
2415
|
-
if (typeof
|
|
2416
|
-
|
|
3412
|
+
if (typeof $el === "string") {
|
|
3413
|
+
$el = that.selector($el);
|
|
2417
3414
|
}
|
|
2418
|
-
if (
|
|
2419
|
-
|
|
3415
|
+
if ($el.nodeType === 9) {
|
|
3416
|
+
$el = $el;
|
|
2420
3417
|
/* Document文档节点 */
|
|
2421
|
-
return Math.max(
|
|
3418
|
+
return Math.max($el.body.scrollHeight, $el.documentElement.scrollHeight, $el.body.offsetHeight, $el.documentElement.offsetHeight, $el.documentElement.clientHeight);
|
|
2422
3419
|
}
|
|
2423
|
-
if (isShow || (!isShow &&
|
|
2424
|
-
|
|
3420
|
+
if (isShow || (!isShow && CommonUtils.isShow($el))) {
|
|
3421
|
+
$el = $el;
|
|
2425
3422
|
/* 已显示 */
|
|
2426
3423
|
/* 从style中获取对应的高度,因为可能使用了class定义了width !important */
|
|
2427
3424
|
/* 如果element.style.height为空 则从css里面获取是否定义了height信息如果定义了 则读取css里面定义的高度height */
|
|
2428
|
-
if (parseFloat(
|
|
2429
|
-
return parseFloat(
|
|
3425
|
+
if (parseFloat(CommonUtils.getStyleValue($el, "height").toString()) > 0) {
|
|
3426
|
+
return parseFloat(CommonUtils.getStyleValue($el, "height").toString());
|
|
2430
3427
|
}
|
|
2431
3428
|
/* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetHeight来进行计算 */
|
|
2432
|
-
if (
|
|
2433
|
-
const borderTopWidth =
|
|
2434
|
-
const borderBottomWidth =
|
|
2435
|
-
const paddingTop =
|
|
2436
|
-
const paddingBottom =
|
|
2437
|
-
const backHeight = parseFloat(
|
|
3429
|
+
if ($el.offsetHeight > 0) {
|
|
3430
|
+
const borderTopWidth = CommonUtils.getStyleValue($el, "borderTopWidth");
|
|
3431
|
+
const borderBottomWidth = CommonUtils.getStyleValue($el, "borderBottomWidth");
|
|
3432
|
+
const paddingTop = CommonUtils.getStyleValue($el, "paddingTop");
|
|
3433
|
+
const paddingBottom = CommonUtils.getStyleValue($el, "paddingBottom");
|
|
3434
|
+
const backHeight = parseFloat($el.offsetHeight.toString()) -
|
|
2438
3435
|
parseFloat(borderTopWidth.toString()) -
|
|
2439
3436
|
parseFloat(borderBottomWidth.toString()) -
|
|
2440
3437
|
parseFloat(paddingTop.toString()) -
|
|
@@ -2445,200 +3442,160 @@ var DOMUtils = (function () {
|
|
|
2445
3442
|
}
|
|
2446
3443
|
else {
|
|
2447
3444
|
/* 未显示 */
|
|
2448
|
-
|
|
2449
|
-
const { recovery } =
|
|
2450
|
-
const height =
|
|
3445
|
+
$el = $el;
|
|
3446
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
3447
|
+
const height = that.height($el, true);
|
|
2451
3448
|
recovery();
|
|
2452
3449
|
return height;
|
|
2453
3450
|
}
|
|
2454
3451
|
}
|
|
2455
|
-
outerWidth(
|
|
2456
|
-
const
|
|
2457
|
-
if (
|
|
2458
|
-
return
|
|
3452
|
+
outerWidth($el, isShow = false) {
|
|
3453
|
+
const that = this;
|
|
3454
|
+
if (CommonUtils.isWin($el)) {
|
|
3455
|
+
return that.windowApi.window.innerWidth;
|
|
2459
3456
|
}
|
|
2460
|
-
if (typeof
|
|
2461
|
-
|
|
3457
|
+
if (typeof $el === "string") {
|
|
3458
|
+
$el = that.selector($el);
|
|
2462
3459
|
}
|
|
2463
|
-
|
|
2464
|
-
if (isShow || (!isShow &&
|
|
2465
|
-
const style =
|
|
2466
|
-
const marginLeft =
|
|
2467
|
-
const marginRight =
|
|
2468
|
-
return
|
|
3460
|
+
$el = $el;
|
|
3461
|
+
if (isShow || (!isShow && CommonUtils.isShow($el))) {
|
|
3462
|
+
const style = that.windowApi.globalThis.getComputedStyle($el, null);
|
|
3463
|
+
const marginLeft = CommonUtils.getStyleValue(style, "marginLeft");
|
|
3464
|
+
const marginRight = CommonUtils.getStyleValue(style, "marginRight");
|
|
3465
|
+
return $el.offsetWidth + marginLeft + marginRight;
|
|
2469
3466
|
}
|
|
2470
3467
|
else {
|
|
2471
|
-
const { recovery } =
|
|
2472
|
-
const outerWidth =
|
|
3468
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
3469
|
+
const outerWidth = that.outerWidth($el, true);
|
|
2473
3470
|
recovery();
|
|
2474
3471
|
return outerWidth;
|
|
2475
3472
|
}
|
|
2476
3473
|
}
|
|
2477
|
-
outerHeight(
|
|
2478
|
-
const
|
|
2479
|
-
if (
|
|
2480
|
-
return
|
|
3474
|
+
outerHeight($el, isShow = false) {
|
|
3475
|
+
const that = this;
|
|
3476
|
+
if (CommonUtils.isWin($el)) {
|
|
3477
|
+
return that.windowApi.window.innerHeight;
|
|
2481
3478
|
}
|
|
2482
|
-
if (typeof
|
|
2483
|
-
|
|
3479
|
+
if (typeof $el === "string") {
|
|
3480
|
+
$el = that.selector($el);
|
|
2484
3481
|
}
|
|
2485
|
-
|
|
2486
|
-
if (isShow || (!isShow &&
|
|
2487
|
-
const style =
|
|
2488
|
-
const marginTop =
|
|
2489
|
-
const marginBottom =
|
|
2490
|
-
return
|
|
3482
|
+
$el = $el;
|
|
3483
|
+
if (isShow || (!isShow && CommonUtils.isShow($el))) {
|
|
3484
|
+
const style = that.windowApi.globalThis.getComputedStyle($el, null);
|
|
3485
|
+
const marginTop = CommonUtils.getStyleValue(style, "marginTop");
|
|
3486
|
+
const marginBottom = CommonUtils.getStyleValue(style, "marginBottom");
|
|
3487
|
+
return $el.offsetHeight + marginTop + marginBottom;
|
|
2491
3488
|
}
|
|
2492
3489
|
else {
|
|
2493
|
-
const { recovery } =
|
|
2494
|
-
const outerHeight =
|
|
3490
|
+
const { recovery } = CommonUtils.forceShow($el);
|
|
3491
|
+
const outerHeight = that.outerHeight($el, true);
|
|
2495
3492
|
recovery();
|
|
2496
3493
|
return outerHeight;
|
|
2497
3494
|
}
|
|
2498
3495
|
}
|
|
2499
3496
|
/**
|
|
2500
|
-
*
|
|
2501
|
-
* @param
|
|
2502
|
-
* @param
|
|
2503
|
-
* @param duration 动画持续时间,单位为毫秒
|
|
2504
|
-
* @param callback 动画结束后执行的函数
|
|
3497
|
+
* 将一个元素替换为另一个元素
|
|
3498
|
+
* @param $el 目标元素
|
|
3499
|
+
* @param $newEl 新元素
|
|
2505
3500
|
* @example
|
|
2506
|
-
* //
|
|
2507
|
-
* DOMUtils.
|
|
2508
|
-
*
|
|
2509
|
-
* })
|
|
3501
|
+
* // 替换元素a.xx为b.xx
|
|
3502
|
+
* DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
|
|
3503
|
+
* DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
|
|
2510
3504
|
*/
|
|
2511
|
-
|
|
2512
|
-
const
|
|
2513
|
-
if (typeof
|
|
2514
|
-
|
|
3505
|
+
replaceWith($el, $newEl) {
|
|
3506
|
+
const that = this;
|
|
3507
|
+
if (typeof $el === "string") {
|
|
3508
|
+
$el = that.selectorAll($el);
|
|
2515
3509
|
}
|
|
2516
|
-
if (
|
|
3510
|
+
if ($el == null) {
|
|
2517
3511
|
return;
|
|
2518
3512
|
}
|
|
2519
|
-
if (
|
|
3513
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2520
3514
|
// 设置
|
|
2521
|
-
|
|
2522
|
-
|
|
3515
|
+
$el.forEach(($elItem) => {
|
|
3516
|
+
that.replaceWith($elItem, $newEl);
|
|
2523
3517
|
});
|
|
2524
3518
|
return;
|
|
2525
3519
|
}
|
|
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");
|
|
3520
|
+
if (typeof $newEl === "string") {
|
|
3521
|
+
$newEl = that.toElement($newEl, false, false);
|
|
2534
3522
|
}
|
|
2535
|
-
|
|
2536
|
-
|
|
3523
|
+
const $parent = $el.parentElement;
|
|
3524
|
+
if ($parent) {
|
|
3525
|
+
$parent.replaceChild($newEl, $el);
|
|
2537
3526
|
}
|
|
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];
|
|
3527
|
+
else {
|
|
3528
|
+
that.after($el, $newEl);
|
|
3529
|
+
$el.remove();
|
|
2544
3530
|
}
|
|
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
3531
|
}
|
|
2562
3532
|
/**
|
|
2563
3533
|
* 将一个元素包裹在指定的HTML元素中
|
|
2564
|
-
* @param
|
|
3534
|
+
* @param $el 要包裹的元素
|
|
2565
3535
|
* @param wrapperHTML 要包裹的HTML元素的字符串表示形式
|
|
2566
3536
|
* @example
|
|
2567
3537
|
* // 将a.xx元素外面包裹一层div
|
|
2568
3538
|
* DOMUtils.wrap(document.querySelector("a.xx"),"<div></div>")
|
|
2569
3539
|
*/
|
|
2570
|
-
wrap(
|
|
2571
|
-
const
|
|
2572
|
-
if (typeof
|
|
2573
|
-
|
|
3540
|
+
wrap($el, wrapperHTML) {
|
|
3541
|
+
const that = this;
|
|
3542
|
+
if (typeof $el === "string") {
|
|
3543
|
+
$el = that.selectorAll($el);
|
|
2574
3544
|
}
|
|
2575
|
-
if (
|
|
3545
|
+
if ($el == null) {
|
|
2576
3546
|
return;
|
|
2577
3547
|
}
|
|
2578
|
-
if (
|
|
3548
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2579
3549
|
// 设置
|
|
2580
|
-
|
|
2581
|
-
|
|
3550
|
+
$el.forEach(($elItem) => {
|
|
3551
|
+
that.wrap($elItem, wrapperHTML);
|
|
2582
3552
|
});
|
|
2583
3553
|
return;
|
|
2584
3554
|
}
|
|
2585
|
-
|
|
3555
|
+
$el = $el;
|
|
2586
3556
|
// 创建一个新的div元素,并将wrapperHTML作为其innerHTML
|
|
2587
|
-
const wrapper =
|
|
2588
|
-
|
|
2589
|
-
const wrapperFirstChild = wrapper.firstChild;
|
|
3557
|
+
const $wrapper = that.windowApi.document.createElement("div");
|
|
3558
|
+
that.html($wrapper, wrapperHTML);
|
|
3559
|
+
const wrapperFirstChild = $wrapper.firstChild;
|
|
2590
3560
|
// 将要包裹的元素插入目标元素前面
|
|
2591
|
-
const parentElement =
|
|
2592
|
-
parentElement.insertBefore(wrapperFirstChild,
|
|
3561
|
+
const parentElement = $el.parentElement;
|
|
3562
|
+
parentElement.insertBefore(wrapperFirstChild, $el);
|
|
2593
3563
|
// 将要包裹的元素移动到wrapper中
|
|
2594
|
-
wrapperFirstChild.appendChild(
|
|
3564
|
+
wrapperFirstChild.appendChild($el);
|
|
2595
3565
|
}
|
|
2596
|
-
prev(
|
|
2597
|
-
const
|
|
2598
|
-
if (typeof
|
|
2599
|
-
|
|
3566
|
+
prev($el) {
|
|
3567
|
+
const that = this;
|
|
3568
|
+
if (typeof $el === "string") {
|
|
3569
|
+
$el = that.selector($el);
|
|
2600
3570
|
}
|
|
2601
|
-
if (
|
|
3571
|
+
if ($el == null) {
|
|
2602
3572
|
return;
|
|
2603
3573
|
}
|
|
2604
|
-
return
|
|
3574
|
+
return $el.previousElementSibling;
|
|
2605
3575
|
}
|
|
2606
|
-
next(
|
|
2607
|
-
const
|
|
2608
|
-
if (typeof
|
|
2609
|
-
|
|
3576
|
+
next($el) {
|
|
3577
|
+
const that = this;
|
|
3578
|
+
if (typeof $el === "string") {
|
|
3579
|
+
$el = that.selector($el);
|
|
2610
3580
|
}
|
|
2611
|
-
if (
|
|
3581
|
+
if ($el == null) {
|
|
2612
3582
|
return;
|
|
2613
3583
|
}
|
|
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;
|
|
3584
|
+
return $el.nextElementSibling;
|
|
2628
3585
|
}
|
|
2629
|
-
siblings(
|
|
2630
|
-
const
|
|
2631
|
-
if (typeof
|
|
2632
|
-
|
|
3586
|
+
siblings($el) {
|
|
3587
|
+
const that = this;
|
|
3588
|
+
if (typeof $el === "string") {
|
|
3589
|
+
$el = that.selector($el);
|
|
2633
3590
|
}
|
|
2634
|
-
if (
|
|
3591
|
+
if ($el == null) {
|
|
2635
3592
|
return;
|
|
2636
3593
|
}
|
|
2637
|
-
return Array.from(
|
|
3594
|
+
return Array.from($el.parentElement.children).filter(($child) => $child !== $el);
|
|
2638
3595
|
}
|
|
2639
3596
|
/**
|
|
2640
3597
|
* 获取当前元素的父元素
|
|
2641
|
-
* @param
|
|
3598
|
+
* @param $el 当前元素
|
|
2642
3599
|
* @returns 父元素
|
|
2643
3600
|
* @example
|
|
2644
3601
|
* // 获取a.xx元素的父元素
|
|
@@ -2646,27 +3603,27 @@ var DOMUtils = (function () {
|
|
|
2646
3603
|
* DOMUtils.parent("a.xx")
|
|
2647
3604
|
* > <div ...>....</div>
|
|
2648
3605
|
*/
|
|
2649
|
-
parent(
|
|
2650
|
-
const
|
|
2651
|
-
if (typeof
|
|
2652
|
-
|
|
3606
|
+
parent($el) {
|
|
3607
|
+
const that = this;
|
|
3608
|
+
if (typeof $el === "string") {
|
|
3609
|
+
$el = that.selector($el);
|
|
2653
3610
|
}
|
|
2654
|
-
if (
|
|
3611
|
+
if ($el == null) {
|
|
2655
3612
|
return;
|
|
2656
3613
|
}
|
|
2657
|
-
if (
|
|
3614
|
+
if (CommonUtils.isNodeList($el)) {
|
|
2658
3615
|
const resultArray = [];
|
|
2659
|
-
|
|
2660
|
-
resultArray.push(
|
|
3616
|
+
$el.forEach(($elItem) => {
|
|
3617
|
+
resultArray.push(that.parent($elItem));
|
|
2661
3618
|
});
|
|
2662
3619
|
return resultArray;
|
|
2663
3620
|
}
|
|
2664
3621
|
else {
|
|
2665
|
-
return
|
|
3622
|
+
return $el.parentElement;
|
|
2666
3623
|
}
|
|
2667
3624
|
}
|
|
2668
|
-
|
|
2669
|
-
const
|
|
3625
|
+
toElement(html, useParser = false, isComplete = false) {
|
|
3626
|
+
const that = this;
|
|
2670
3627
|
// 去除html前后的空格
|
|
2671
3628
|
html = html.trim();
|
|
2672
3629
|
function parseHTMLByDOMParser() {
|
|
@@ -2679,13 +3636,13 @@ var DOMUtils = (function () {
|
|
|
2679
3636
|
}
|
|
2680
3637
|
}
|
|
2681
3638
|
function parseHTMLByCreateDom() {
|
|
2682
|
-
const
|
|
2683
|
-
|
|
3639
|
+
const $el = that.windowApi.document.createElement("div");
|
|
3640
|
+
that.html($el, html);
|
|
2684
3641
|
if (isComplete) {
|
|
2685
|
-
return
|
|
3642
|
+
return $el;
|
|
2686
3643
|
}
|
|
2687
3644
|
else {
|
|
2688
|
-
return
|
|
3645
|
+
return $el.firstElementChild ?? $el.firstChild;
|
|
2689
3646
|
}
|
|
2690
3647
|
}
|
|
2691
3648
|
if (useParser) {
|
|
@@ -2730,213 +3687,6 @@ var DOMUtils = (function () {
|
|
|
2730
3687
|
.map((item) => `${encodeURIComponent(item.name)}=${encodeURIComponent(item.value)}`)
|
|
2731
3688
|
.join("&");
|
|
2732
3689
|
}
|
|
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
3690
|
/**
|
|
2941
3691
|
* 创建一个新的DOMUtils实例
|
|
2942
3692
|
* @param option
|
|
@@ -2954,7 +3704,7 @@ var DOMUtils = (function () {
|
|
|
2954
3704
|
* DOMUtils.getTextBoundingRect(document.querySelector("input"));
|
|
2955
3705
|
*/
|
|
2956
3706
|
getTextBoundingRect($input, selectionStart, selectionEnd) {
|
|
2957
|
-
const
|
|
3707
|
+
const that = this;
|
|
2958
3708
|
// Basic parameter validation
|
|
2959
3709
|
if (!$input || !("value" in $input))
|
|
2960
3710
|
return $input;
|
|
@@ -3023,7 +3773,7 @@ var DOMUtils = (function () {
|
|
|
3023
3773
|
}
|
|
3024
3774
|
// End of CSS variable checks
|
|
3025
3775
|
// 不能为空,不然获取不到高度
|
|
3026
|
-
const text = $input.value || "G", textLen = text.length, fakeClone =
|
|
3776
|
+
const text = $input.value || "G", textLen = text.length, fakeClone = that.windowApi.document.createElement("div");
|
|
3027
3777
|
if (selectionStart > 0)
|
|
3028
3778
|
appendPart(0, selectionStart);
|
|
3029
3779
|
const fakeRange = appendPart(selectionStart, selectionEnd);
|
|
@@ -3037,7 +3787,7 @@ var DOMUtils = (function () {
|
|
|
3037
3787
|
fakeClone.style.left = leftPos + "px";
|
|
3038
3788
|
fakeClone.style.width = width + "px";
|
|
3039
3789
|
fakeClone.style.height = height + "px";
|
|
3040
|
-
|
|
3790
|
+
that.windowApi.document.body.appendChild(fakeClone);
|
|
3041
3791
|
const returnValue = fakeRange.getBoundingClientRect(); //Get rect
|
|
3042
3792
|
fakeClone?.parentNode?.removeChild(fakeClone); //Remove temp
|
|
3043
3793
|
return returnValue;
|
|
@@ -3049,7 +3799,7 @@ var DOMUtils = (function () {
|
|
|
3049
3799
|
* @returns
|
|
3050
3800
|
*/
|
|
3051
3801
|
function appendPart(start, end) {
|
|
3052
|
-
const span =
|
|
3802
|
+
const span = that.windowApi.document.createElement("span");
|
|
3053
3803
|
span.style.cssText = cssDefaultStyles; //Force styles to prevent unexpected results
|
|
3054
3804
|
span.textContent = text.substring(start, end);
|
|
3055
3805
|
fakeClone.appendChild(span);
|
|
@@ -3057,7 +3807,7 @@ var DOMUtils = (function () {
|
|
|
3057
3807
|
}
|
|
3058
3808
|
// Computing offset position
|
|
3059
3809
|
function getInputOffset() {
|
|
3060
|
-
const body =
|
|
3810
|
+
const body = that.windowApi.document.body, win = that.windowApi.document.defaultView, docElem = that.windowApi.document.documentElement, $box = that.windowApi.document.createElement("div");
|
|
3061
3811
|
$box.style.paddingLeft = $box.style.width = "1px";
|
|
3062
3812
|
body.appendChild($box);
|
|
3063
3813
|
const isBoxModel = $box.offsetWidth == 2;
|
|
@@ -3076,7 +3826,7 @@ var DOMUtils = (function () {
|
|
|
3076
3826
|
* @returns
|
|
3077
3827
|
*/
|
|
3078
3828
|
function getInputCSS(prop, isNumber) {
|
|
3079
|
-
const val =
|
|
3829
|
+
const val = that.windowApi.document.defaultView.getComputedStyle($input, null).getPropertyValue(prop);
|
|
3080
3830
|
if (isNumber) {
|
|
3081
3831
|
return parseFloat(val);
|
|
3082
3832
|
}
|
|
@@ -3085,13 +3835,163 @@ var DOMUtils = (function () {
|
|
|
3085
3835
|
}
|
|
3086
3836
|
}
|
|
3087
3837
|
}
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3838
|
+
addStyle(cssText) {
|
|
3839
|
+
if (typeof cssText !== "string") {
|
|
3840
|
+
throw new Error("DOMUtils.addStyle 参数cssText 必须为String类型");
|
|
3841
|
+
}
|
|
3842
|
+
const $css = this.createElement("style", {
|
|
3843
|
+
type: "text/css",
|
|
3844
|
+
innerHTML: cssText,
|
|
3845
|
+
});
|
|
3846
|
+
if (this.windowApi.document.head) {
|
|
3847
|
+
/* 插入head最后 */
|
|
3848
|
+
this.windowApi.document.head.appendChild($css);
|
|
3849
|
+
}
|
|
3850
|
+
else if (this.windowApi.document.documentElement.childNodes.length === 0) {
|
|
3851
|
+
/* 插入#html后 */
|
|
3852
|
+
this.windowApi.document.documentElement.appendChild($css);
|
|
3853
|
+
}
|
|
3854
|
+
else {
|
|
3855
|
+
/* 插入#html第一个元素前 */
|
|
3856
|
+
this.windowApi.document.documentElement.insertBefore($css, this.windowApi.document.documentElement.childNodes[0]);
|
|
3857
|
+
}
|
|
3858
|
+
return $css;
|
|
3091
3859
|
}
|
|
3092
|
-
/**
|
|
3093
|
-
|
|
3094
|
-
|
|
3860
|
+
/**
|
|
3861
|
+
* 检测点击的地方是否在该元素区域内
|
|
3862
|
+
* @param $el 需要检测的元素
|
|
3863
|
+
* @returns
|
|
3864
|
+
* + true 点击在元素上
|
|
3865
|
+
* + false 未点击在元素上
|
|
3866
|
+
* @example
|
|
3867
|
+
* DOMUtils.checkUserClickInNode(document.querySelector(".xxx"));
|
|
3868
|
+
* > false
|
|
3869
|
+
**/
|
|
3870
|
+
checkUserClickInNode($el) {
|
|
3871
|
+
const that = this;
|
|
3872
|
+
if (!CommonUtils.isDOM($el)) {
|
|
3873
|
+
throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
|
|
3874
|
+
}
|
|
3875
|
+
const clickEvent = that.windowApi.window.event;
|
|
3876
|
+
const touchEvent = that.windowApi.window.event;
|
|
3877
|
+
const $click = clickEvent?.composedPath()?.[0];
|
|
3878
|
+
// 点击的x坐标
|
|
3879
|
+
const clickPosX = clickEvent?.clientX != null ? clickEvent.clientX : touchEvent.touches[0].clientX;
|
|
3880
|
+
// 点击的y坐标
|
|
3881
|
+
const clickPosY = clickEvent?.clientY != null ? clickEvent.clientY : touchEvent.touches[0].clientY;
|
|
3882
|
+
const {
|
|
3883
|
+
/* 要检测的元素的相对屏幕的横坐标最左边 */
|
|
3884
|
+
left: elementPosXLeft,
|
|
3885
|
+
/* 要检测的元素的相对屏幕的横坐标最右边 */
|
|
3886
|
+
right: elementPosXRight,
|
|
3887
|
+
/* 要检测的元素的相对屏幕的纵坐标最上边 */
|
|
3888
|
+
top: elementPosYTop,
|
|
3889
|
+
/* 要检测的元素的相对屏幕的纵坐标最下边 */
|
|
3890
|
+
bottom: elementPosYBottom, } = $el.getBoundingClientRect();
|
|
3891
|
+
if (clickPosX >= elementPosXLeft &&
|
|
3892
|
+
clickPosX <= elementPosXRight &&
|
|
3893
|
+
clickPosY >= elementPosYTop &&
|
|
3894
|
+
clickPosY <= elementPosYBottom) {
|
|
3895
|
+
return true;
|
|
3896
|
+
}
|
|
3897
|
+
else if (($click && $el.contains($click)) || $click == $el) {
|
|
3898
|
+
/* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
|
|
3899
|
+
return true;
|
|
3900
|
+
}
|
|
3901
|
+
else {
|
|
3902
|
+
return false;
|
|
3903
|
+
}
|
|
3904
|
+
}
|
|
3905
|
+
deleteParentNode($el, parentSelector) {
|
|
3906
|
+
if ($el == null) {
|
|
3907
|
+
return;
|
|
3908
|
+
}
|
|
3909
|
+
if (!CommonUtils.isDOM($el)) {
|
|
3910
|
+
throw new Error("DOMUtils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
|
|
3911
|
+
}
|
|
3912
|
+
if (typeof parentSelector !== "string") {
|
|
3913
|
+
throw new Error("DOMUtils.deleteParentNode 参数 targetSelector 必须为 string 类型");
|
|
3914
|
+
}
|
|
3915
|
+
let result = false;
|
|
3916
|
+
const $parent = domUtils.closest($el, parentSelector);
|
|
3917
|
+
if ($parent) {
|
|
3918
|
+
this.remove($parent);
|
|
3919
|
+
result = true;
|
|
3920
|
+
}
|
|
3921
|
+
return result;
|
|
3922
|
+
}
|
|
3923
|
+
*findElementsWithText($el, text, filter) {
|
|
3924
|
+
const that = this;
|
|
3925
|
+
if ($el.outerHTML.includes(text)) {
|
|
3926
|
+
if ($el.children.length === 0) {
|
|
3927
|
+
const filterResult = typeof filter === "function" ? filter($el) : false;
|
|
3928
|
+
if (!filterResult) {
|
|
3929
|
+
yield $el;
|
|
3930
|
+
}
|
|
3931
|
+
}
|
|
3932
|
+
else {
|
|
3933
|
+
const $text = Array.from($el.childNodes).filter(($child) => $child.nodeType === Node.TEXT_NODE);
|
|
3934
|
+
for (const $child of $text) {
|
|
3935
|
+
if ($child.textContent.includes(text)) {
|
|
3936
|
+
const filterResult = typeof filter === "function" ? filter($el) : false;
|
|
3937
|
+
if (!filterResult) {
|
|
3938
|
+
yield $child;
|
|
3939
|
+
}
|
|
3940
|
+
}
|
|
3941
|
+
}
|
|
3942
|
+
}
|
|
3943
|
+
}
|
|
3944
|
+
for (let index = 0; index < $el.children.length; index++) {
|
|
3945
|
+
const $child = $el.children[index];
|
|
3946
|
+
yield* that.findElementsWithText($child, text, filter);
|
|
3947
|
+
}
|
|
3948
|
+
}
|
|
3949
|
+
/**
|
|
3950
|
+
* 寻找可见元素,如果元素不可见,则向上找它的父元素直至找到,如果父元素不存在则返回null
|
|
3951
|
+
* @param $el
|
|
3952
|
+
* @example
|
|
3953
|
+
* let visibleElement = DOMUtils.findVisibleElement(document.querySelector("a.xx"));
|
|
3954
|
+
* > <HTMLElement>
|
|
3955
|
+
*/
|
|
3956
|
+
findVisibleElement($el) {
|
|
3957
|
+
let $current = $el;
|
|
3958
|
+
while ($current) {
|
|
3959
|
+
const rect = $current.getBoundingClientRect();
|
|
3960
|
+
if (rect.length) {
|
|
3961
|
+
return $current;
|
|
3962
|
+
}
|
|
3963
|
+
$current = $current.parentElement;
|
|
3964
|
+
}
|
|
3965
|
+
return null;
|
|
3966
|
+
}
|
|
3967
|
+
/**
|
|
3968
|
+
* 将元素上的文本或元素使用光标进行选中
|
|
3969
|
+
*
|
|
3970
|
+
* 注意,如果设置startIndex和endIndex,且元素上并无可选则的坐标,那么会报错
|
|
3971
|
+
* @param $el 目标元素
|
|
3972
|
+
* @param childTextNode 目标元素下的#text元素
|
|
3973
|
+
* @param startIndex (可选)开始坐标,可为空
|
|
3974
|
+
* @param endIndex (可选)结束坐标,可为空
|
|
3975
|
+
* @example
|
|
3976
|
+
* DOMUtils.setElementSelection(document.querySelector("span"));
|
|
3977
|
+
*/
|
|
3978
|
+
setElementSelection($el, childTextNode, startIndex, endIndex) {
|
|
3979
|
+
const range = this.windowApi.document.createRange();
|
|
3980
|
+
range.selectNodeContents($el);
|
|
3981
|
+
if (childTextNode) {
|
|
3982
|
+
if (childTextNode.nodeType !== Node.TEXT_NODE) {
|
|
3983
|
+
throw new TypeError("childTextNode必须是#text元素");
|
|
3984
|
+
}
|
|
3985
|
+
if (startIndex != null && endIndex != null) {
|
|
3986
|
+
range.setStart(childTextNode, startIndex);
|
|
3987
|
+
range.setEnd(childTextNode, endIndex);
|
|
3988
|
+
}
|
|
3989
|
+
}
|
|
3990
|
+
const selection = this.windowApi.globalThis.getSelection();
|
|
3991
|
+
if (selection) {
|
|
3992
|
+
selection.removeAllRanges();
|
|
3993
|
+
selection.addRange(range);
|
|
3994
|
+
}
|
|
3095
3995
|
}
|
|
3096
3996
|
}
|
|
3097
3997
|
const domUtils = new DOMUtils();
|