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