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