-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksa.js
3253 lines (3020 loc) · 104 KB
/
ksa.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
/**
* KSA前端底层驱动 V2.0
*
* 目前版本还处于开发中,请勿保存并用于生产环境!
*
* ---------------------------------------
* 待正式发布版本后,源代码将会公开开源
*
* Author : ksaos.com && cr180.com(Mr Wu - ChenJun)
* Update : 2020年7月29日
*/
function debug(data) {
console.log.apply(this, arguments);//debug
}
var consoleGroupN = {};
function debugTime(key) {
consoleGroupN[key] = consoleGroupN[key] >= 0 ? consoleGroupN[key] + 1 : 0;
key = key + '-' + consoleGroupN[key];
console.time(key);
return function () {
console.timeEnd(key);
};
}
(function (document, undefined, K) {
"use strict";
//on绑定事件时的索引
var bindEventData = {};
/**
* 给事件对象统一加上阻止冒泡事件
* @param events
* @returns {*}
*/
function eventAddstopPropagation(events) {
if (events) {
events.isStop = false;
events.stop = function () {
this.stopPropagation();
this.preventDefault();
this.isStop = true;
}
}
return events;
}
//浏览器不会单独解析table某个标签,它们必须有规则的在一起
//所以需要针对table做单独的处理
var wrapMap = {
thead : [1, "<table>", "</table>"],
col : [2, "<table><colgroup>", "</colgroup></table>"],
tr : [2, "<table><tbody>", "</tbody></table>"],
td : [3, "<table><tbody><tr>", "</tr></tbody></table>"],
_default : [0, "", ""]
};
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
wrapMap.th = wrapMap.td;
var rtagName = (/<([a-z][^\/>\x20\t\r\n\f]*)/i);
/**
* 字符串转虚拟dom
* @param code
* @returns {ActiveX.IXMLDOMNodeList | NodeListOf<ChildNode>}
*/
function createDom(code, isEle) {
var fragment = document.createDocumentFragment();
var tag = (rtagName.exec(code) || ["", ""])[1].toLowerCase();
var wrap = wrapMap[tag] || wrapMap._default;
//创建虚拟dom并写入字符串
//var dom = document.createRange().createContextualFragment(code);
var dom = fragment.appendChild(document.createElement("div"));
dom.innerHTML = wrap[1] + code + wrap[2];
var j = wrap[0];
while (j--) {
dom = dom.lastChild;
}
//script的处理
var globalScriptEvent = {}, //等待上一次script标签完成加载后需要执行的code
scriptLoadCallback = {}, //存在src属性的script标签 队列,load成功后自动删除
scriptSrcLast, scriptSrcLastID; //上一次存在src属性的script标签标记
$(dom).find('script').map(function (ele) {
var script = document.createElement('script');
$.loop($.attrs(ele), function (v, k) {
script[k] = v;
});
var scriptID = $.objectID(script);
if (ele.src) {
scriptLoadCallback[scriptID] = ele;
script.onload = function () {
delete scriptLoadCallback[scriptID];
//如果队列为空
if ($.isEmpty(scriptLoadCallback)) {
$.loop(globalScriptEvent[scriptID], function (elArr) {
elArr[0].text = elArr[1];
});
}
};
scriptSrcLast = script;
scriptSrcLastID = scriptID;
}
if (scriptSrcLastID) {
if (ele.text) {
globalScriptEvent[scriptSrcLastID] = globalScriptEvent[scriptSrcLastID] ? globalScriptEvent[scriptSrcLastID] : [];
globalScriptEvent[scriptSrcLastID].push([script, ele.text]);
}
} else {
script.text = ele.text;
}
$(ele).after(script).remove();
});
var eles = [];
$.loop(dom.childNodes, function (e) {
if (!isEle || (e.nodeType !== 3 && e.nodeType !== 8)) {
eles.push(e);
}
});
return eles;
}
$.getScript = function(url, callFun){
var script = document.createElement('script');
script.src = url;
script.onload = function () {
callFun && callFun();
};
$('head').append(script);
}
$.getCSS = function(url, callFun){
var script = document.createElement('link');
script.rel = 'stylesheet';
script.type = 'text/css';
script.href = url;
script.onload = function () {
callFun && callFun();
};
$('head').append(script);
}
/**
* 创建临时dom并回调
* @param {html|Node} code html或节点
* @param {function} callback 回调函数
* @param {boolean} reOrder 是否倒序回调
* @returns {tempDom}
*/
function tempDom(code, callback, reOrder) {
var dom;
var isFunc = $.isFunction(code);
if (code instanceof $) {
dom = code;
} else if ($.isDomAll(code)) {
dom = code instanceof NodeList ? code : [code];
} else if ($.isArray(code)) {
dom = code;
//创建一个虚拟dom
} else if ($.isString(code)) {
dom = createDom(code);
}
//将传入的html或dom对象添加到虚拟dom中
var length = this.length;
$.loop(this, function (ele, index) {
var isClone = index !== length - 1;
if (isFunc) {
dom = code.call(ele, index, ele.innerHTML);
if ($.isString(dom)) {
dom = createDom(dom);
isClone = 0;
}
if (dom instanceof $) {
dom = [dom[0]];
isClone = 0;
} else if (dom instanceof HTMLElement || dom instanceof Node) {
isClone = 0;
}
dom = $.toArray(dom);
}
var n = !reOrder ? dom.length : 0;
var i = reOrder ? dom.length - 1 : 0;
while (reOrder ? i >= 0 : i < n) {
var eles = dom[i];
eles = !isClone ? eles : $(eles).clone(true, true)[0];
callback.call(ele, ele, eles);
reOrder ? i-- : i++;
}
});
return this;
}
/**
* 检查指定元素是否被选择器选择
* @param ele 需要检查的元素
* @param selector 选择器
* @returns {boolean|number|*}
*/
function isSelectDom(ele, selector) {
if (!selector || !ele || ele.nodeType !== 1) {
return false
}
var matchesSelector = ele.matches || ele.matchesSelector || ele.webkitMatchesSelector || ele.mozMatchesSelector || ele.msMatchesSelector || ele.oMatchesSelector;
try {
return matchesSelector.call(ele, selectorStr(selector));
} catch (e) {
console.error(new Error('选择器语法可能有错误:' + selector));
}
}
/**
* 递归遍历DOM节点
* @param element 需遍历的dom
* @param key 需遍历的节点
* @param selector 选择器
* @returns {[]}
*/
function dir(element, key, selector) {
var rdom = [];
$.loop(element, function (el) {
while ((el = el[key])) {
if (!selector || isSelectDom(el, selector)) {
rdom.push(el);
}
}
});
return rdom;
}
function selectorStr(selector) {
if (selector) {
selector = selector.trim();
selector = selector.replace(/(:selected)/g, ':checked');//option需要使用checked选中
}
return selector;
}
function selectorAll(dom, selector) {
selector = selectorStr(selector);
if(selector.substr(0,1) =='>'){
selector = ':scope '+selector;
}
var ele = dom.querySelectorAll(selector);
//如果选择器没有找到 尝试查找ks-selector属性
if (!ele.length && selector.indexOf('[') === -1) {
ele = dom.querySelectorAll('[ks-selector="' + selector + '"]');
}
return ele;
}
function $(selector) {
var dt = new K_S_A(selector);
dt.__proto__ = K;
return dt;
}
function K_S_A(selector) {
if (selector) {
if (selector instanceof $) {
return selector;
} else if ($.isWindow(selector) || $.isDomAll(selector)) {
selector = [selector];
} else if ($.isString(selector)) {
selector = selectorStr(selector);
//选择器存在左尖括号并且大于3个字符 则认为是html源码 = 创建虚拟dom
if (selector.indexOf('<') === 0 && selector.length >= 3) {
selector = createDom(selector, true);
} else {
selector = [].slice.call(selectorAll(document, selector));
}
} else if ($.isFunction(selector)) {
$.ready(selector);
return;
}
var length = selector.length;
var obj = {};
$.loop(selector, function (ele, key) {
obj[key] = ele;
});
selector = obj;
selector.length = length;
} else {
selector = {length : 0};
}
if (selector.length) {
$.arrayMerge(this, selector);
this.length = selector.length;
}
return this;
}
K_S_A.prototype = $.prototype = K = {};
/**
* 文档加载完成后执行代码
* 与jQuery用法相同
* @param callback
* @returns {*}
*/
var DocumentReadyFunction = [];
K.ready = function (callback) {
this.map(function (ele) {
//如果是document与window的事件 则送到队列中 防止多次触发
if (ele === document || ele === window) {
DocumentReadyFunction.push(function () {
callback.call(ele)
});
} else {
ele.addEventListener('DOMContentLoaded', function () {
callback.call(ele);
}, false);
}
});
return this;
}
/**
* 监听dom变化(仅在KSA语法中有效)
*/
K.DOMchange = function (action, Callback) {
this.map(function (ele) {
var isBindEvent = ele.KSADOMchangeEvent ? true : false;
if (!ele.KSADOMchangeEvent) {
ele.KSADOMchangeEvent = {};
}
$.loop($.explode(' ', action, ''), function (ac) {
ac = ac.toLowerCase();
if (!ele.KSADOMchangeEvent[ac]) {
ele.KSADOMchangeEvent[ac] = [];
}
ele.KSADOMchangeEvent[ac].push(Callback);
});
//如果已经绑定过事件,则不再绑定
if (isBindEvent) {
return;
}
$(ele).on('KSADOMchange', function (e) {
var ths = this, Arg = e.KSAcallbackArgs;
Arg[0] = Arg[0].toLowerCase();
//Arg参数 1=动作 2=新值 3=旧值
if (!this.KSADOMchangeEvent || !this.KSADOMchangeEvent[Arg[0]]) {
return;
}
$.loop(this.KSADOMchangeEvent[Arg[0]], function (fun) {
fun.apply(ths, [Arg[1], Arg[2]]);
});
});
});
return this;
}
/**
* 克隆一个元素
* 与jQuery用法相同
* @param {boolean} withs 是否需要克隆子元素
* @param {boolean} deepWith 是否需要克隆事件
* @returns {*|jQuery|HTMLElement}
*/
K.clone = function (withs, deepWith) {
var newObj = $();
this.each(function (index, ele) {
var eleID = $.objectID(ele);
var events = bindEventData[eleID];
var newEle = ele.cloneNode(withs);
$.objectID(newEle, '');
//复制事件
if (deepWith && events) {
var $newEle = $(newEle);
$.loop(events, function (evn, evnName) {
$.loop(evn, function (ev) {
$newEle.on(evnName, ev.selector, ev.callback);
})
});
}
newObj.push(newEle);
});
return newObj;
}
// ====================== 创建虚拟DOM ====================== //
$.dom = function (code) {
var dom = createDom(code);
return dom.length === 1 ? dom[0] : dom;
}
// ====================== 文档操作 ====================== //
/**
* 更新元素的class值
* @param ele 元素对象
* @param inClass 需要处理的class 多个以空格分割
* @param isDel 是否为删除
* @param ms 是否需要延迟 毫秒
*/
function eleUpdateClass(ele, inClass, isDel, ms) {
if (!inClass) {
return;
}
function _clsrun() {
var className = ele.getAttribute('class') || '';
var newClass = {};
$.loop($.explode(' ', (className + ' ' + inClass), ''), function (val) {
val = val.trim();
if (val) {
newClass[val] = 1;
}
});
if (isDel) {
inClass = $.explode(' ', inClass, '');
$.loop(newClass, function (val, key) {
if ($.inArray(key, inClass)) {
delete newClass[key];
}
});
}
newClass = $.implode(' ', Object.keys(newClass));
if (newClass && newClass != className) {
ele.className = newClass;
} else if (!newClass) {
ele.removeAttribute('class');
}
}
if (ms > 0) {
window.setTimeout(_clsrun, ms);
} else {
_clsrun();
}
}
/**
* 添加class
* 最后更新 : 2020年6月19日 20:01:50
* 与jQuery用法相同
* @param name 需要添加的class值,多个以空格分割
* @param ms 延迟多少毫秒后添加
* @returns {$}
*/
K.addClass = function (name, ms) {
if (name) {
var msArr = ms && $.isArray(ms) ? ms : null;
this.each(function (index, ele) {
var inms = msArr ? msArr[index] : ms;
eleUpdateClass(ele, name, false, inms);
});
}
return this;
}
/**
* 删除class
* 最后更新 : 2020年6月19日 20:01:50
* 与jQuery用法相同
* @param name 需要删除的class值,多个以空格分割
* @param ms 多少毫秒后删除
* @returns {$}
*/
K.removeClass = function (name, ms) {
if (name) {
var msArr = ms && $.isArray(ms) ? ms : null;
this.each(function (index, ele) {
var inms = msArr ? msArr[index] : ms;
eleUpdateClass(ele, name, true, inms);
});
}
return this;
}
/**
* 检查被选元素集合中是否存在指定class
* 与jQuery用法相同
* @param cls
* @returns {boolean}
*/
K.hasClass = function (cls) {
var ele, i = 0;
while ((ele = this[i++])) {
if (ele.nodeType === 1) {
var cl = ele.getAttribute('class');
if (cl && cl.length && $.inArray(cls, $.explode(' ', cl, ''))) {
return true;
}
}
}
return false;
}
K.prop = function (key, value) {
key = $.explode(' ', key, '');
//删除或更新
if ($.isset(value)) {
value = value === '' ? null : value;
this.map(function (ele) {
$.loop(key, function (k) {
//移除模式
if (value === null) {
delete ele[k];
//更改模式
} else if ($.isset(value)) {
var old = ele[k];
if (old !== value) {
ele[k] = value;
$(ele).trigger('KSADOMchange', ['attr.' + k, value, old]);
}
}
});
});
return this;
//读取模式
} else {
var ele = this[0];
if (!ele) {
return;
}
var dt = {};
$.loop(key, function (k) {
dt[key] = ele[key];
});
if (key.length === 1) {
return dt[key[0]];
} else {
return dt;
}
}
};
var ElementAttrBooleanArr = ['active', 'checked', 'selected', 'async', 'autofocus', 'autoplay', 'controls', 'defer', 'disabled', 'hidden', 'ismap', 'loop', 'multiple', 'open', 'readonly', 'required', 'scoped'];
/**
* 读取指定元素所有attr属性值
* @param {*} ele
*/
$.attrs = function (ele) {
if (!ele || !ele.attributes) {
return;
}
var attrs = {};
$.loop(ele.attributes, function (val) {
var v = val.value;
if ($.inArray(val.name, ElementAttrBooleanArr)) {
v = $.inArray(val.name, ['checked', 'selected']) ? ele[val.name] : v;
v = v === '' ? true : !!v;
}
attrs[val.name] = v;
});
return attrs;
}
/**
* 设置元素原生属性与标签属性
* @param key 属性名
* @param value 属性值
* @param isCustom 是否为自定义属性
* @returns {EleAttrPropUped|boolean}
* @constructor
*/
function EleAttrPropUped(key, value, isCustom) {
var isValue = $.isset(value);
value = !!value;
if (!isValue) {
var ele = this[0];
if (!ele) {
return;
}
if (ele.tagName === 'INPUT' && $.inArray(key, ['checked'])) {
return ele[key];
}
var val = ele.getAttribute(key);
if ($.isNull(val)) {
val = ele[key];
}
val = val === '' ? true : val;
return $.inArray(val, ['false', 'undefined', 'null']) ? false : !!val;
} else if ($.isset(value)) {
this.map(function (ele) {
var old = ele[key];
if (old === value) {
return;
}
if (value) {
ele[key] = value;
ele.setAttribute(key, key);
} else {
if (isCustom) {
delete ele[key];
} else {
ele[key] = false;
}
ele.removeAttribute(key);
}
$(ele).trigger('KSADOMchange', ['attr.' + key, value, old]);
});
return this;
}
}
K.checked = function (value) {
return EleAttrPropUped.call(this, 'checked', value);
}
K.selected = function (value) {
return EleAttrPropUped.call(this, 'selected', value);
}
K.disabled = function (value) {
return EleAttrPropUped.call(this, 'disabled', value);
}
K.active = function (value) {
return EleAttrPropUped.call(this, 'active', value, 1);
}
/**
* attr操作
* 与jQuery用法相同
* key与value都不传值时表示读取所有属性 以object方式返回
* 如选择器有多个节点,则根据节点序号以数组方式返回
* @param {string|object} key 属性名支持单个值、多个值(空格分开)、对象(写入模式,键名=属性名 键值=属性值)
* @param value 属性值 不传入=读取模式 null|空值=删除模式
* @returns {K|[]}
*/
K.attr = function (key, value) {
value = value === '' ? null : value;
var valueIsNull = value === null;
var isKey = $.isset(key);
var keyIsobj = $.isObject(key);
var isvalue = $.isset(value);
var md = '';
if (keyIsobj || (isKey && isvalue && !valueIsNull)) {
md = 'set';
} else if (isKey && valueIsNull) {
md = 'del';
} else if ((!isKey || !keyIsobj) && !isvalue) {
md = 'get';
}
if (!keyIsobj) {
key = $.explode(' ', key, '');
}
var dataAttr = {};//需要变更的-data属性
if (md == 'get') {
var ele = this[0];
if (!ele) {
return;
}
var attrs = {};
//读取所有标签属性
if (!isvalue && !isKey) {
attrs = $.attrs(ele);
} else {
$.loop(key, function (k) {
var attrV = ele.getAttribute(k);
if (!$.isNull(attrV)) {
attrs[k] = attrV;
}
});
}
if (key.length === 1) {
return attrs[key[0]];
} else {
return attrs;
}
//写入模式
} else if (md == 'set') {
var sets;
if (keyIsobj) {
sets = key;
} else {
sets = {};
$.loop(key, function (k) {
sets[k] = value;
});
}
if (sets) {
this.map(function (ele) {
var oldAttrs = $.attrs(ele);
var isUpdate;
$.loop(sets, function (val, k) {
var odV = oldAttrs[k] === '' ? true : oldAttrs[k];//如果旧值是空值 则以true表示
//新旧值不同才更新
if (!odV || val !== odV) {
val === '' || $.isNull(val) ? ele.removeAttribute(k) : ele.setAttribute(k, (val === true ? '' : val));
$(ele).trigger('KSADOMchange', ['attr.' + k, val, odV]);
if (k.indexOf('data-') === 0) {
dataAttr[k.substr(5)] = val;
}
isUpdate = 1;
}
});
//触发ele的属性变更事件
isUpdate && $(ele).trigger('KSADOMchange', ['attr']);
});
}
if (!$.isEmpty(dataAttr)) {
this.data(dataAttr);
}
return this;
} else if (md == 'del') {
this.map(function (ele) {
var attrs = $.attrs(ele);
$.loop(key, function (k) {
if (k.indexOf('data-') === 0) {
dataAttr[k.substr(5)] = null;
}
ele.removeAttribute(k);
$.isset(attrs[k]) && $(ele).trigger('KSADOMchange', ['attr.' + k]);
});
});
if (!$.isEmpty(dataAttr)) {
this.data(dataAttr);
}
return this;
}
}
/**
* 移除元素属性
* 与jQuery用法相同
* @param key
* @returns {*}
*/
K.removeAttr = function (key) {
if (key) {
this.attr(key, null);
}
return this;
}
/**
* 设置或返回元素data-属性值
* 与jQuery用法相同
* @param key
* @param value
* @returns {{}|*}
*/
K.data = function (key, value) {
value = value === '' ? null : value;
var valueIsNull = value === null;
var isKey = $.isset(key);
var keyIsobj = $.isObject(key);
var isvalue = $.isset(value);
var md = '';
if (keyIsobj || (isKey && isvalue && !valueIsNull)) {
md = 'set';
} else if (isKey && valueIsNull) {
md = 'del';
} else if ((!isKey || !keyIsobj) && !isvalue) {
md = 'get';
}
key = key && !keyIsobj ? $.explode(' ', key, '') : key;
if (md == 'set') {
this.map(function (ele) {
if (!ele._KSAOS_COM_ELE_DATA) {
ele._KSAOS_COM_ELE_DATA = {};
}
var _Attrs = ele._KSAOS_COM_ELE_DATA;
var setData = {};
if (keyIsobj) {
setData = key;
} else {
$.loop(key, function (k) {
setData[k] = value;
});
}
$.loop(setData, function (v, k) {
v = v === '' ? null : v;
var sk = 'data-' + k;
if (!$.isObject(v)) {
//值为null则删除
if ($.isNull(v)) {
ele.removeAttribute(sk);
$.isset(_Attrs[k]) && delete _Attrs[k];
$(ele).trigger('KSADOMchange', ['data.' + k]);
//值不同才更新
} else if (_Attrs[k] !== v) {
ele.setAttribute(sk, v);
$(ele).trigger('KSADOMchange', ['data.' + k, v, _Attrs[k]]);
_Attrs[k] = v;
}
} else {
_Attrs[k] = v;
}
});
});
return this;
} else if (md == 'get') {
var ele = this[0];
if (!ele) {
return;
}
if (!ele._KSAOS_COM_ELE_DATA) {
ele._KSAOS_COM_ELE_DATA = {};
}
if (key) {
var getdt = {};
$.loop(key, function (k) {
var v = ele._KSAOS_COM_ELE_DATA[k];
if (!$.isset(v)) {
v = ele.getAttribute('data-' + k);
}
if (!$.isNull(v)) {
getdt[k] = v;
}
});
if (key.length === 1) {
return getdt[key[0]];
} else {
return getdt;
}
} else {
var getdt = ele._KSAOS_COM_ELE_DATA || {};
$.loop(this.attr(), function (val, k) {
if (k.indexOf('data-') === 0) {
getdt[k.substr(5)] = val;
}
});
if (!$.isEmpty(getdt)) {
return getdt;
}
}
} else if (md === 'del') {
this.map(function (ele) {
var _Attrs = ele._KSAOS_COM_ELE_DATA;
if (_Attrs) {
$.loop(key, function (k) {
if ($.isset(_Attrs[k])) {
$(ele).trigger('KSADOMchange', ['data.' + k, undefined, _Attrs[k]]);
ele.removeAttribute('data-' + k);
delete _Attrs[k];
}
});
}
});
return this;
}
}
/**
* 移除data-属性
* 与jQuery用法相同
* @param key
* @returns {*}
*/
K.removeData = function (key) {
if (key) {
this.data(key, null);
}
return this;
}
/**
* 清空节点
* 与jQuery用法相同
*/
K.empty = function () {
this.map(function (ele) {
var h = ele.innerHTML;
ele.innerHTML = '';
$(ele).trigger('KSADOMchange', ['html', '', h]);
});
return this;
}
/**
* 表单值的读写
* 与jQuery用法相同
* @param value
* @returns {K|[]}
*/
K.val = function (value) {
//写入值
if ($.isset(value) && value !== null) {
this.map(function (ele, index) {
if ($.inArray(ele.tagName, ['INPUT', 'SELECT', 'TEXTAREA'])) {
if ($.isFunction(value)) {
var oldvalue = ele.value;
if (ele.tagName == 'SELECT' && ele.multiple) {
oldvalue = [];
$.loop(ele.options, function (e) {
e.selected && oldvalue.push(e.value);
});
}
value = value.call(ele, index, oldvalue);
}
//所有选中状态必须经过attr函数 否则无法完成变更事件触发
switch (ele.tagName) {
case 'INPUT':
var tp = ele.getAttribute('type');
tp = tp.indexOf('ks-') === 0 ? tp.substr(3) : tp;
if (tp === 'checkbox') {
var val = $(ele).attr('value');
if ($.isset(val)) {
$(ele).checked($.isArray(value) ? $.inArray(val, value) : val == value);
} else {
$(ele).checked($.isObject(value) ? $.isEmpty(value) : !!value);
}
} else if (tp === 'radio') {
$(ele).checked($(ele).attr('value') == value);
} else {
ele.value = value;
}
break;
case 'SELECT':
value = $.isArray(value) ? value : [value];
if (ele.options) {
if(value.length == 1){
ele.value = value[0];
}else{
$.loop(ele.options, function (e) {
var r = $.inArray(e.value, value);
if (r != e.selected) {
e.selected = r;
$(e).attr('selected', 'selected');
}
});
}
}
$(ele).trigger('ksachange');//触发内部事件
break;
case 'TEXTAREA':
ele.value = value;
break;
default:
}
$(ele).trigger('KSADOMchange', ['val', value]);
}
});
return this;
//获取值,如果有多个对象,则按数组顺序返回对应值
} else {
var t = [];
var ele = this[0];
if (!ele || ele.disabled) {
return;
}
var tg = ele.tagName;
switch (tg) {
case 'INPUT':
var tp = ele.getAttribute('type');
tp = tp.indexOf('ks-') === 0 ? tp.substr(3) : tp;
if ($.inArray(tp, ['checkbox', 'radio'])) {
ele.checked && t.push(ele.value);
} else {
t.push(ele.value);
}
break;
case 'SELECT':
$(ele).find('option:selected').map(function (e) {
t.push(e.value);
});
break;
case 'TEXTAREA':
t.push(ele.value);
break;
default:
}
if (t.length === 1) {
return t[0];
} else {
return t;
}
}
}
/**
* 写入或读取文本
* 与jQuery用法相同
* @param {html|Node} value 传值表示写入
* @returns {string|$}
*/
K.text = function (value) {