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