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