-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvelfun4.js
3447 lines (2952 loc) · 133 KB
/
velfun4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************
腳本名:VelFun
版本號:4-4.60
通 道:Release
作 者:龍翔翎(Velade)
更新日期:2024-06-11
********************/
; (function (window, undefined) {
const isOffline = !location.origin.match(/^(http:|https:)\/\//);
const version = "4-4.60";
const channel = "Release";
const author = "Velade";
const releaseDate = "2024-06-11";
/**
* @typedef {object} velfunEle VelFun元素
*/
/**
* VelFun 入口&选择器
* @param {string|HTMLElement} selector 主选择器
* @param {string|HTMLElement|velfunEle} context 父级(上下文)选择器
* @returns {velfunEle}
*/
const velfun = function (selector, context) {
if (_.info.isIE()) {
return false;
}
return new velfun.fn.init(selector, context);
}
velfun.private = new Object();
velfun.msgtype = Object.freeze({
MSG_OK: "MSG_OK",
MSG_OK_Cancel: "MSG_OK_Cancel",
MSG_YES_NO: "MSG_YES_NO"
})
/**
* 将文本类型的html代码转换为HTMLElement对象
* @param {string} html 文本类型的html代码
* @param {function} callback 回调,可选利用回调异步执行或同步执行,二者同时存在,也可以同时使用。
* @returns {HTMLElement} 返回html对象
*/
velfun.htmltodom = function (html, callback) {
const template = document.createElement('template');
let range = document.createRange();
range.selectNodeContents(template);
if (callback !== undefined) {
callback.call(range.createContextualFragment(html), range.createContextualFragment(html));
}
return range.createContextualFragment(html);
}
/**
* 将事件绑定到元素,默认绑定到document的版本,是_(document).bind()的简化写法
* @param {string} ev 事件名称,不包括on(和addEventListener一样)
* @param {string} selector 子元素选择器,用以指定具体元素,此处只接受字符串类型,在使用委托/代理模式捕捉动态元素时有效
* @param {function} func 事件触发的函数
* @param {boolean} pop 允许间接触发?默认值 False,子元素的事件不会传递给父元素。
* @returns {velfunEle} 返回该元素
*/
velfun.bind = function (ev, selector, func, bubbling) {
const _this = _(document);
if (typeof ev == "object") {
const _args = ev;
ev = _args.event;
selector = _args.selector;
func = _args.callback;
bubbling = _args.bubbling;
}
if (typeof func == "boolean" && bubbling == undefined) {
bubbling = func;
func = undefined;
}
if (typeof selector == "function" && func == undefined) {
func = selector;
selector = undefined;
}
if(bubbling == undefined) bubbling = false;
if (typeof func === "function") {
let eventKey = "";
do {
eventKey = "ev_" + velfun.random(100000000000,999999999999);
} while(Object.keys(velfun.global.Events).includes(eventKey));
if(selector) {
velfun.global.Events[eventKey] = function (e) {
if (e.target.matches(selector) || bubbling) func.call(_(e.target), e, _this);
};
}else {
velfun.global.Events[eventKey] = function (e) {
func.call(_(e.target), e, _this);
};
}
_this.each((i,th)=>{
th.self = th;
th.addEventListener(ev, velfun.global.Events[eventKey])
})
return eventKey;
} else {
try {
console.error("%cVelFun%cError%c\n %cbind%c No valid callback function found.","background-color: black; color:white;padding: 0 5px; border-radius: 1000px 0 0 1000px;","background-color: red; color:white;padding: 0 5px; border-radius: 0 1000px 1000px 0;","","background-color: red; color:white;padding: 0 5px; border-radius: 1000px;","");
} catch (e) {
document.writeln("VelFun Error:\n bind:No valid callback function found.");
}
}
return _this;
}
/**
* 解除绑定的事件,解除绑定到document的版本,是_(document).unbind()的简化写法
* @param {String} ev 要解绑的事件
* @param {String} funcID 要解绑的事件对应ID(之前绑定时返回的值)
*/
velfun.unbind = function (ev,funcID) {
document.removeEventListener(ev,velfun.global.Events[funcID]);
}
/**
* 提示框
* @param {string} Message 消息内容,支持html
* @param {string} Title 弹窗标题,可以省略
* @param {string} Type 按钮类型:MSG_OK 默认 仅确定;MSG_YES_NO 是、否两个按钮;MSG_OK_Cancel 确定、取消两个按钮
* @param {array} Position 弹窗的座标,[x, y]的格式
* @param {function} callback 按下按钮后的回调,参数1为按钮是否是「肯定」的,即OK YES按钮为肯定,其他为否定。
*/
velfun.Msgbox = async function (Message, Title, Type, Position, callback) {
if (typeof Message == "object") {
const _args = Message;
Message = _args.Message || _args.message;
Title = _args.Title || _args.title;
Type = _args.Type || _args.type;
Position = _args.Position || _args.position;
callback = _args.callback;
}
msgboxList.push(function () { return Msgbox_do(Message, Title, Type, Position, callback); });
if (_("#_MessageBox_").length == 0) {
const fun = msgboxList.shift();
fun();
}
}
/**
* 提示框-精简版 为低配电脑而设计,去除了一些效果和所有动画
* @param {string} Message 消息内容,支持html
* @param {string} Title 弹窗标题,可以省略
* @param {string} Type 按钮类型:MSG_OK 默认 仅确定;MSG_YES_NO 是、否两个按钮;MSG_OK_Cancel 确定、取消两个按钮
* @param {array} Position 弹窗的座标,[x, y]的格式
* @param {function} callback 按下按钮后的回调,参数1为按钮是否是「肯定」的,即OK YES按钮为肯定,其他为否定。
*/
velfun.Msgbox_lite = async function (Message, Title, Type, Position, callback) {
if (typeof Message == "object") {
const _args = Message;
Message = _args.Message || _args.message;
Title = _args.Title || _args.title;
Type = _args.Type || _args.type;
Position = _args.Position || _args.position;
callback = _args.callback;
}
msgboxList.push(function () { return Msgbox_lite_do(Message, Title, Type, Position, callback); });
if (_("#_MessageBox_").length == 0) {
const fun = msgboxList.shift();
fun();
}
}
/**
* 全屏显示的选择项
* @param {object} opt_arr JSON格式的选项,具体格式请参考官网手册
* @param {string} title 标题,可以省略,省略后不显示标题
*/
velfun.Options = function (opt_arr, title) {
optionsList.push(function () { Options_do(opt_arr, title); });
if (_("#_OPTIONS_").length == 0) {
const fun = optionsList.shift();
fun();
}
}
/**
* 整数随机数
* @param {number} min 两个参数时为最小值,但只指定一个参数时为最大值
* @param {number} max 最大值,省略时min视为最大值
* @returns {number} 随机的结果
*/
velfun.random = function (min, max) {
let vel_minval, vel_maxval, vel_randomval;
if (typeof min == 'number' & typeof max == 'number') {
vel_minval = min;
vel_maxval = max - min;
vel_randomval = Math.round(Math.random() * vel_maxval + vel_minval);
} else if (typeof min == 'number' & max === undefined) {
vel_minval = 0;
vel_maxval = min;
vel_randomval = Math.round(Math.random() * vel_maxval);
} else {
try {
console.error("%cVelFun%cError%c\n %crandom%c The parameter is missing or is not a number.","background-color: black; color:white;padding: 0 5px; border-radius: 1000px 0 0 1000px;","background-color: red; color:white;padding: 0 5px; border-radius: 0 1000px 1000px 0;","","background-color: red; color:white;padding: 0 5px; border-radius: 1000px;","");
} catch (e) {
document.writeln("VelFun Error:\n random:The parameter is missing or is not a number.");
}
}
return vel_randomval;
}
const hexColors = [
"#EBD9CB", "#F9F6F1", "#DBE2EC", "#8D91AA", "#E2E2E2", "#DECECE",
"#F7F0EA", "#E7ADAC", "#78677A", "#D8B0B0", "#F0F0F2", "#E7E7E5",
"#ACD4D6", "#797979", "#DFDFDF", "#EBC1A8", "#EBDAC8", "#F6F2F1",
"#CAC0B7", "#D2D3D5", "#A2886D", "#C3BAB1", "#F1E2DB", "#DACCC1",
"#BFCAC2", "#013E41", "#C6DEE0", "#F7EDEB", "#A6BAAF", "#4A475C",
"#CEAEB9", "#E9CEC3", "#EED9D4", "#8D7477", "#6E4740", "#5E5D65",
"#CB9B8F", "#F6E1DC", "#B5BAC0", "#5B7493", "#E3BCB5", "#A7BEC6",
"#F4F4F4", "#B98A82", "#F5F4F0", "#BFBFC1", "#B4A29E", "#E0D3C3",
"#EBE8E3", "#E4DBD2", "#BEA8AA", "#9AA2AD", "#9B8D8C", "#DECFCC",
"#C0B9B2", "#ACA39A", "#94938E", "#817F7C", "#6F6C69", "#5D5A57",
"#4B4844", "#393631", "#27241E", "#D1C4B6", "#B8A999", "#9F8E7C",
"#867460", "#6D5943", "#544E37", "#3B422A", "#E0D5C1", "#C7BBA3",
"#AE9F85", "#958367", "#7C6749", "#63502C", "#4A3910", "#F0E6CD",
"#D7CCAF", "#BEB191", "#A59673", "#8C7B55", "#736038", "#5A451B",
"#EDE3D9", "#D4C9C0", "#BBB0A7", "#A2968E", "#897D75", "#70635C",
"#574A43", "#3E312A", "#E7E5E1", "#CECDC9", "#B5B4B1", "#9C9B99",
"#838281", "#6A6969", "#514F51", "#383638"
];
const rgbColors = HexToRgb(hexColors);
function HexToRgb (colors){
let rgbs = [];
colors.forEach(c => {
let cs = c.replace("#","");
cs = cs.match(/.{2}/g);
let _tmp = [];
cs.forEach(e=>{
_tmp.push(parseInt(e,16));
})
rgbs.push(_tmp);
});
return rgbs;
}
function RgbToHsl(colors) {
let hsls = [];
colors.forEach(c => {
if(!Array.isArray(c)) c = c.split(",");
let r = c[0] / 255;
let g = c[1] / 255;
let b = c[2] / 255;
let max = Math.max(r, g, b);
let min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // 无色彩
} else {
let d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
h = Math.round(h * 360);
s = Math.round(s * 100);
l = Math.round(l * 100);
hsls.push([h,s,l]);
})
return hsls;
}
function RgbToHex (colors){
let hexs = [];
colors.forEach(c => {
if(!Array.isArray(c)) c = c.split(",");
hexs.push(`#${parseInt(c[0]).toString(16).toUpperCase()}${parseInt(c[1]).toString(16).toUpperCase()}${parseInt(c[2]).toString(16).toUpperCase()}`);
});
return hexs;
}
function closeColor(source,targets) {
let topRScore = 99999999;
let topGScore = 99999999;
let topBScore = 99999999;
let topColor = 0;
for (const key in targets) {
let rgbColor = targets[key];
if(typeof rgbColor == "string"){
rgbColor = rgbColor.match(/\d+/g);
}
let scoreR = Math.abs(source[0] - rgbColor[0]);
let scoreG = Math.abs(source[1] - rgbColor[1]);
let scoreB = Math.abs(source[2] - rgbColor[2]);
if(scoreR <= topRScore && scoreG <= topGScore && scoreB <= topBScore){
topRScore = scoreR;
topGScore = scoreG;
topBScore = scoreB;
topColor = targets[key];
}
}
return topColor;
}
function getLofColor(targetLight, colors) {
let minABS = 99999999;
let topColor = null;
for (const key in Object.values(colors)) {
let _hsl = RgbToHsl([colors[key]])[0];
if(Math.abs(_hsl[2] - targetLight) < minABS) {
minABS = Math.abs(_hsl[2] - targetLight);
topColor = colors[key];
}
}
return topColor;
}
/**
* 获取主题色
* @param {String} url 图片的路径,支持dataUri
* @param {String} format 输出的格式,默认RGB,支持RGB HEX HSL
*/
velfun.getMainColors = velfun.getmaincolors = velfun.gmc = function (url,format = "rgb"){
return new Promise((resolve,reject)=>{
let colorsCounter = [];
let ti = document.createElement("canvas");
let ctxt = ti.getContext("2d");
let img = new Image;
img.crossOrigin = 'anonymous';
img.onload = ()=>{
ctxt.canvas.width = img.width;
ctxt.canvas.height = img.height;
ctxt.drawImage(img, 0, 0);
let data = ctxt.getImageData(img.width * 0.25,img.height * 0.25,img.width * 0.75, img.height * 0.75).data;
data = Array.from(data);
let imgPixels = [];
for(let i = 0;i < data.length;i += 4){
imgPixels.push(data.slice(i,i+4));
}
let pointsNum = 0;
for (let i = 0; i < imgPixels.length; i += parseInt(imgPixels.length / 5000)) {
let topColor = closeColor(imgPixels[i],rgbColors);
if(!colorsCounter[topColor]) colorsCounter[topColor] = 0;
colorsCounter[topColor] ++;
pointsNum ++;
}
const entries = Object.entries(colorsCounter);
entries.sort((a, b) => b[1] - a[1]);
let mainColors = Object.keys(Object.fromEntries(entries));
mainColors = mainColors.slice(0,mainColors.length * 0.25);
let mainColor = mainColors[0];
let darkColor = getLofColor(10,mainColors);
let midColor = getLofColor(60,mainColors);
let lightColor = getLofColor(90,mainColors);
if(format.toUpperCase() == "RGB") {
resolve({
"mainColor":mainColor,
"darkColor":darkColor,
"midColor":midColor,
"lightColor":lightColor,
"0%":getLofColor(0,mainColors),
"25%":getLofColor(25,mainColors),
"50%":getLofColor(50,mainColors),
"75%":getLofColor(75,mainColors),
"100%":getLofColor(100,mainColors)
});
}else if(format.toUpperCase() == "HSL") {
let _hslColors = RgbToHsl([
mainColor,
darkColor,
midColor,
lightColor,
getLofColor(0,mainColors),
getLofColor(25,mainColors),
getLofColor(50,mainColors),
getLofColor(75,mainColors),
getLofColor(100,mainColors)
]);
resolve({
"mainColor":_hslColors[0],
"darkColor":_hslColors[1],
"midColor":_hslColors[2],
"lightColor":_hslColors[3],
"0%":_hslColors[4],
"25%":_hslColors[5],
"50%":_hslColors[6],
"75%":_hslColors[7],
"100%":_hslColors[8]
});
}else if(format.toUpperCase() == "HEX") {
let _hslColors = RgbToHex([
mainColor,
darkColor,
midColor,
lightColor,
getLofColor(0,mainColors),
getLofColor(25,mainColors),
getLofColor(50,mainColors),
getLofColor(75,mainColors),
getLofColor(100,mainColors)
]);
resolve({
"mainColor":_hslColors[0],
"darkColor":_hslColors[1],
"midColor":_hslColors[2],
"lightColor":_hslColors[3],
"0%":_hslColors[4],
"25%":_hslColors[5],
"50%":_hslColors[6],
"75%":_hslColors[7],
"100%":_hslColors[8]
});
}else {
reject("Wrong format");
}
}
img.src = url;
})
}
/**
* [已废弃] 初始化上传控件,上传控件现在移动到libvelui库,此处的已不再维护,并很快将彻底移除
* @deprecated since version 4.50
* @returns
*/
velfun.initUpload = function () {
var veluploads = _("v-upload");
if (veluploads.length > 0) {
try {
console.error("%cVelFun%cDeprecated Error%c\n %cinitUpload%c This function is deprecated, You can use it under 4.40 version only.","background-color: black; color:white;padding: 0 5px; border-radius: 1000px 0 0 1000px;","background-color: red; color:white;padding: 0 5px; border-radius: 0 1000px 1000px 0;","","background-color: gray; color:white;padding: 0 5px; border-radius: 1000px;","");
} catch (e) { }
}
return false;
}
/**
* 弹出提示
* @param {string} content 弹出提示的内容
* @param {string} title 提示的标题
* @param {number} duration 多久自动关闭,单位毫秒,默认三秒
*/
velfun.Tip = function (content, title, duration) {
if (typeof content == "object") {
const _args = content;
content = _args.content;
title = _args.title;
duration = _args.duration;
}
tipList.push(function () { Tip_do(content, title, duration); });
if (tipReady) {
tipReady = false;
const fun = tipList.shift();
fun();
}
}
/**
* 手动初始化所有v-coloricon元素
*/
velfun.setColoricon = function () {
const coloricons = _("v-coloricon");
for (let i = 0; i < coloricons.length; i++) {
const thisicon = _(coloricons[i]);
if (_("img", thisicon).length > 0) continue;
const width = thisicon.attr("width") || thisicon.css("width");
const height = thisicon.attr("height") || thisicon.css("height");
const src = thisicon.attr("src");
thisicon.css("width:" + width + "px;height:" + height + "px;overflow:hidden;display:inline-block;text-indent:0px;padding:0px;");
thisicon.append("<img src='" + src + "' style='width:" + width + "px;height:" + height + "px;position:relative;left:-" + width + "px;border-right:1px solid transparent;filter:drop-shadow(" + width + "px 0 0 black)'>");
}
}
/**
* 设置网页语言(需要网页以VelFun多语言方式开发)
* @param {string} langfile 指定语言文件路径
* @returns
*/
velfun.setLang = function (langfile = "") {
if (isOffline) return;
if (langfile == "") return;
if (_.observer != undefined) _.observer.disconnect();
_.io.get(langfile, function (langfiledata) {
_.langdata = JSON.parse(langfiledata);
/**调用时强制更新**/
const nl = _.getTextNodes(_("html")[0]);
for (const tn of nl) {
if (tn.nodeValue.trim() == "") continue;
if (tn.tempStr == undefined) {
tn.tempStr = tn.nodeValue.trim();
} else {
tn.nodeValue = tn.tempStr;
}
for (const key in _.langdata) {
const nt = _.langdata[key];
tn.nodeValue = tn.nodeValue.replaceAll(`@t-${key};`, nt);
}
}
const al = _("[title],[placeholder],[value]");
al.each(function () {
velfun.private.setAttrsLang(this, _.langdata);
});
_.observer.observe(_.obbody, _.obconfig);
});
}
/**
* 获取元素下所有纯文本节点
* @param {HTMLElement} ele 父元素对象
* @returns {array} 所有#TEXT节点
*/
velfun.getTextNodes = function (ele) {
if (ele.nodeType == 3) return [ele];
const nodes = ele.childNodes;
const textnodes = [];
for (const i in nodes) {
if (nodes[i].nodeType == 3) {
textnodes.push(nodes[i]);
} else {
const r = _.getTextNodes(nodes[i]);
for (const tn of r) {
textnodes.push(tn);
}
}
}
return textnodes;
}
/**
* 数组/对象深度拷贝
* @param {array} from 从那个数组/对象拷贝
* @returns {array} 得到的独立的新数组/对象
*/
velfun.deepCopy = function (from) {
let new_obj = new Object();
if (Array.isArray(from)) {
new_obj = new Array();
}
for (const key in from) {
let val = from[key];
if (typeof val == "object") {
new_obj[key] = velfun.deepCopy(val);
} else {
new_obj[key] = val;
}
}
return new_obj;
}
velfun.global = {};
velfun.version = {
velfun: version,
channel: channel,
author: author,
releaseDate: releaseDate
};
//Init
velfun.fn = velfun.prototype = {
length: 0,
velfun: "4",
version: version,
channel: channel,
author: author,
releaseDate: releaseDate,
selector: "",
init: function (selector, context) {
const _this = this;
_this.velfun = "4";
_this.version = version;
_this.channel = channel;
_this.author = author;
_this.releaseDate = releaseDate;
try {
if (selector === undefined) {
console.error("%cVelFun%cError%c\n %cSelector%c The selector cannot be empty and an empty object was returned!","background-color: black; color:white;padding: 0 5px; border-radius: 1000px 0 0 1000px;","background-color: red; color:white;padding: 0 5px; border-radius: 0 1000px 1000px 0;","","background-color: red; color:white;padding: 0 5px; border-radius: 1000px;","");
_this.length = 0;
_this.selector = "";
return _this;
}
if (selector.velfun !== undefined) {
return selector;
}
if (typeof selector === "function") {
window.addEventListener("load", function () {
//Callback
selector.call(_(document));
});
return _this;
}
let doms;
let parent = document;
if (context !== undefined) {
parent = _(context)[0];
}
if (typeof selector === "string") {
if (parent === undefined) {
return;
}
doms = parent.querySelectorAll(selector);
} else if (typeof selector === "object") {
_this.length = 1;
_this.selector = "DOM";
_this[0] = selector;
return _this;
} else {
try {
console.error("%cVelFun%cError%c\n %cSelector%c Not support selector","background-color: black; color:white;padding: 0 5px; border-radius: 1000px 0 0 1000px;","background-color: red; color:white;padding: 0 5px; border-radius: 0 1000px 1000px 0;","","background-color: red; color:white;padding: 0 5px; border-radius: 1000px;","");
} catch (e) {
document.writeln("VelFun Error:\n Selector:Not support selector");
}
_this.length = 0;
_this.selector = "";
return _this;
}
_this.length = doms.length;
_this.selector = selector;
for (let i = 0; i < doms.length; i++) {
_this[i] = doms[i];
}
return _this;
} catch (e) {
try {
console.error("%cVelFun%cError%c\n %cSelector%c An error occurred and an empty object was returned!","background-color: black; color:white;padding: 0 5px; border-radius: 1000px 0 0 1000px;","background-color: red; color:white;padding: 0 5px; border-radius: 0 1000px 1000px 0;","","background-color: red; color:white;padding: 0 5px; border-radius: 1000px;","", e);
} catch (e) {
document.writeln("VelFun Error:\n Selector:An error occurred and an empty object was returned!");
}
_this.length = 0;
_this.selector = "";
return _this;
}
}
}
//Object Function
/**
* 获取或设定元素属性
* @param {string} attrname 属性名
* @param {string} attrvalue 属性值,如果省略此参数,则为读取
* @returns {velfunEle|string} 如果为设定则返回元素对象。如果为读取则返回属性值,返回的属性值一律为字符串类型
*/
velfun.fn.attr = function (attrname, attrvalue) {
const _this = this;
if (_this.length == 0) return this;
let values = [];
_this.each((i, item) => {
if (item.nodeType == 3) return;
if (attrvalue === undefined) {
values.push((item.getAttribute) ? item.getAttribute(attrname) : null);
} else {
if (attrvalue === "") {
item.removeAttribute(attrname);
} else if (attrvalue === attrname) {
item.setAttribute(attrname, "");
} else {
item.setAttribute(attrname, attrvalue);
}
}
})
if (attrvalue === undefined) {
if (values.length == 1) return values[0];
else if (values.length == 0) return null;
else return values;
} else {
return _this;
}
}
/**
* 判断元素是否具有某个属性
* @param {string} attrname 属性名
* @returns {boolean} 属性是否存在
*/
velfun.fn.hasAttr = function (attrname) {
if (this.length == 0) return this;
return (this.attr(attrname) !== null) ? true : false;
}
/**
* 获取或设定元素的值
* @param {string} value 值,省略时为获取元素的值
* @returns {string|velfunEle} 获取时获取元素的值,设定时返回元素对象
*/
velfun.fn.val = function (value) {
if (this.length == 0) return this;
const _this = this;
let values = [];
_this.each((i, item) => {
if (_this[0].tagName.toLowerCase() == "v-select") {
if (value === undefined) {
values.push(_("input[type='hidden']", item).attr("value"));
} else {
if (!_this[0].hasAttr("readonly") && !_this[0].hasAttr("disable")) {
_("v-option[value='" + value + "']", item).trigger("click");
}
}
} else {
if (value === undefined) {
values.push(item.value);
} else {
item.value = value;
}
}
})
if (value === undefined) {
if (values.length == 1) return values[0];
else if (values.length == 0) return null;
else return values;
} else {
return _this;
}
}
/**
* 获取或设定元素样式
* @param {string} css 样式,设定时同style写法,获取时填写样式属性名,获取只能单个获取
* @param {number} index 第几个元素,以0开始,仅用于获取,未指定时默认读取第一个元素的属性
* @returns {string|velfunEle} 设定时返回元素对象,获取时返回获取的值
*/
velfun.fn.css = function (css, index) {
if (this.length == 0) return this;
const _this = this;
let isRead = false;
// 轉譯dataurl中的特殊符號
css = css.replaceAll(/data:(.+?);base64,/g, "data[Colon]$1[Semicolon]base64,");
css = css.replace(/\s*?:\s*?/, ":");
css = css.replace(/\s*?;\s*?/, ";");
let newstyles = css.split(";");
newstyles = newstyles.filter(Boolean);
if (newstyles.length <= 1) {
if (newstyles[0]) {
const tmp_NaA = newstyles[0].split(":");
if (!tmp_NaA[1]) {
isRead = true;
}
} else if (css) {
const tmp_NaA = css[0].split(":");
if (!css[1]) {
isRead = true;
}
}
}
if (isRead) {
//Read css
if (index === undefined) {
index = 0;
}
let re = null;
switch (css) {
case "transform":
re = _this[0].style.transform;
break;
default:
re = window.getComputedStyle(_this[index]).getPropertyValue(css);
}
return re;
} else {
//Write css
for (let i = 0; i < _this.length; i++) {
if (_this.hasOwnProperty(i)) {
let tostyle = _(_this[i]).attr("style");
let oldstyles = {};
if (tostyle != null) {
tostyle = tostyle.replaceAll(/data:(.+?);base64,/g, "data[Colon]$1[Semicolon]base64,");
tostyle = tostyle.replace(/\s*?:\s*?/, ":");
tostyle = tostyle.replace(/\s*?;\s*?/, ";");
oldstyles = tostyle.split(";");
oldstyles = oldstyles.filter(Boolean);
}
let newstyles_a = {}, oldstyles_a = {};
for (const i2 in newstyles) {
if (newstyles.hasOwnProperty(i2)) {
let NaA = newstyles[i2].split(":");
NaA = NaA.filter(Boolean);
let cssvals = [];
for (let n = 1; n <= NaA.length; n++) {
if (NaA[n]) {
cssvals.push(NaA[n].replaceAll(/data\[Colon\](.+?)\[Semicolon\]base64,/g, "data:$1;base64,"));
}
}
newstyles_a[NaA[0].trim()] = cssvals.join(":");
}
}
for (const i2 in oldstyles) {
if (oldstyles.hasOwnProperty(i2)) {
let NaA = oldstyles[i2].split(":");
NaA = NaA.filter(Boolean);
let cssvals = [];
for (let n = 1; n <= NaA.length; n++) {
if (NaA[n]) {
cssvals.push(NaA[n].replaceAll(/data\[Colon\](.+?)\[Semicolon\]base64,/g, "data:$1;base64,"));
}
}
oldstyles_a[NaA[0].trim()] = cssvals.join(":");
}
}
for (const key in newstyles_a) {
if (newstyles_a.hasOwnProperty(key)) {
oldstyles_a[key] = newstyles_a[key];
}
}
let newstyles_s = "";
for (const key in oldstyles_a) {
if (oldstyles_a.hasOwnProperty(key)) {
newstyles_s += key + ":" + oldstyles_a[key] + ";";
}
}
_(_this[i]).attr("style", newstyles_s);
}
}
return this;
}
}
/**
* 获取或设定对象html内容/代码
* @param {string} html 文本类型的html代码,用于设定
* @param {number} index 第几个元素,以0开始,仅用于获取,未指定时默认读取第一个元素的属性
* @returns {string|velfunEle} 获取时返回文本类型的html代码,设定时返回该元素对象(非设定/添加的html对象)
*/
velfun.fn.html = function (html, index) {
if (this.length == 0) return this;
const _this = this;
if (html === undefined) {
//ReadHTML
if (index === undefined) {
return this[0].innerHTML;
} else {
return this[index].innerHTML;
}
} else {
//WriteHTML
for (let i = 0; i < _this.length; i++) {
if (_this.hasOwnProperty(i)) {
const tdom = _this[i];
tdom.innerHTML = html;
}
}
return _this;
}
}
/**
* 获取或设定对象纯文本内容
* @param {string} text 要设定的纯文本, 即使包含html内容,也会被转换为文本直接显示出来
* @param {number} index 第几个元素,以0开始,仅用于获取,未指定时默认读取第一个元素的属性
* @returns {string|velfunEle} 获取时返回文本,设定时返回该元素对象
*/
velfun.fn.text = function (text, index) {
if (this.length == 0) return this;
const _this = this;
if (text === undefined) {
//ReadTEXT
if (index === undefined) {
return this[0].innerText;
} else {
return this[index].innerText;
}
} else {
//WriteTEXT
for (let i = 0; i < _this.length; i++) {
if (_this.hasOwnProperty(i)) {
const tdom = _this[i];
tdom.innerText = text;
}
}
return _this;
}
}
/**
* 获取元素类名
* @param {number} index 第几个元素,以0开始,仅用于获取,未指定时默认读取第一个元素的属性
* @returns {DOMTokenList} 返回元素类名的数组
*/
velfun.fn.getClass = velfun.fn.getclass = function (index) {
if (this.length == 0) return this;
index = index || 0;
return this[index].classList;
}
/**
* 判断元素是否存在/包含类名
* @param {string} classname 要查询的类名
* @returns {boolean} 返回元素是否包含查询的类名
*/
velfun.fn.hasClass = velfun.fn.hasclass = function (classname) {
if (this.length == 0) return this;
const classes = this.getClass();
return classes.contains(classname);
}
/**
* 为元素添加类名
* @param {string} classname 要添加的类名
* @returns {velfunEle} 返回该元素
*/
velfun.fn.addClass = velfun.fn.addclass = function (classname) {
if (this.length == 0) return this;
const _this = this;
_this.each((i, item) => {
const nowClasses = _(item).getclass();
if (!nowClasses.contains(classname)) {
nowClasses.add(classname);
_(item)[0].classList = nowClasses;
}
})
return _this;
}
/**
* 从元素移除类名
* @param {string} classname 要移除的类名
* @returns {velfunEle} 返回该元素
*/
velfun.fn.removeClass = velfun.fn.removeclass = function (classname) {
if (this.length == 0) return this;
const _this = this;
_this.each((i, item) => {
const nowClasses = _(item).getclass();
nowClasses.remove(classname);
_(item)[0].classList = nowClasses;
})
return _this;
}
/**
* 替换元素类名
* @param {string} oldclass 要被替换的旧类名
* @param {string} newclass 要替换的新类名
* @returns {velfunEle} 返回该元素
*/
velfun.fn.replaceClass = velfun.fn.replaceclass = function (oldclass, newclass) {
if (this.length == 0) return this;
const _this = this;
_this.each((i, item) => {
const nowClasses = _(item).getclass();
nowClasses.replace(oldclass, newclass);
_(item)[0].classList = nowClasses;
})
return this;
}
velfun.global.Events = new Object();
/**
* 将事件绑定到元素
* @param {string} ev 事件名称,不包括on(和addEventListener一样)
* @param {string} selector 子元素选择器,用以指定具体元素,此处只接受字符串类型,在使用委托/代理模式捕捉动态元素时有效
* @param {function} func 事件触发的函数
* @param {boolean} pop 允许间接触发?默认值 False,子元素的事件不会传递给父元素。
* @returns {velfunEle} 返回该元素
*/
velfun.fn.bind = function (ev, selector, func, bubbling) {
if (typeof ev == "object") {
const _args = ev;
ev = _args.event;
selector = _args.selector;
func = _args.callback;
bubbling = _args.bubbling;
}
if (this.length == 0) return this;