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