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