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