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