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