@@ -26,17 +26,27 @@ layui.define('jquery', function(exports) {
26
26
27
27
var enterSkin = 'layui-atree-enter' ,
28
28
Atree = function ( options ) {
29
- this . options = options ;
30
- this . nodes = options . nodes || [ ] ;
31
- this . props = this . options . props || props ;
29
+
30
+ //缓冲重要变量
31
+ this . $vm = this ;
32
+ this . $options = options || { } ;
33
+ this . $el = this . utils . isElementNode ( this . $options . elem ) ? this . $options . elem : $ ( this . $options . elem ) ;
34
+
35
+ //模块的属性
36
+ this . nodes = this . $options . nodes || [ ] ;
37
+ this . props = this . $options . props || props ;
38
+
39
+ //模块的别名
32
40
this . nameKey = this . props . name || props . name ;
33
41
this . idKey = this . props . id || props . id ;
34
42
this . childrenKey = this . props . children || props . children ;
35
43
this . checkboxKey = this . props . checkbox || props . checkbox ;
36
44
this . spreadKey = this . props . spread || props . spread ;
37
45
this . addBtnLabelKey = this . props . addBtnLabel || props . addBtnLabel ;
38
46
this . deleteBtnLabelKey = this . props . deleteBtnLabel || props . deleteBtnLabel ;
47
+
39
48
} ;
49
+
40
50
//图标
41
51
var icon = {
42
52
arrow : [ '' , '' ] //箭头
@@ -46,26 +56,36 @@ layui.define('jquery', function(exports) {
46
56
leaf : '' //叶节点
47
57
} ;
48
58
59
+ //工具包
60
+ Atree . prototype . utils = ( function ( ) {
61
+ return {
62
+ isElementNode : function ( node ) {
63
+ return node . nodeType === 1 ;
64
+ }
65
+ }
66
+ } ) ( )
67
+
49
68
//初始化
50
- Atree . prototype . init = function ( elem ) {
51
- var that = this ;
52
- elem . addClass ( 'layui-box layui-atree' ) ; //添加tree样式
53
- if ( that . options . skin ) {
54
- elem . addClass ( 'layui-atree-skin-' + that . options . skin ) ;
69
+ Atree . prototype . init = function ( ) {
70
+ var that = this . $vm ,
71
+ options = this . $options
72
+ that . $el . addClass ( 'layui-box layui-atree' ) ; //添加tree样式
73
+ if ( options . skin ) {
74
+ that . $el . addClass ( 'layui-atree-skin-' + options . skin ) ;
55
75
}
56
- that . tree ( elem ) ;
57
- that . on ( elem ) ;
76
+ that . tree ( that . $el , that . nodes ) ;
77
+ that . on ( that . $el ) ;
58
78
} ;
59
79
60
80
//树节点解析
61
81
Atree . prototype . tree = function ( elem , children , parent ) {
62
- var that = this ,
63
- options = that . options
64
- var nodes = children || options . nodes ;
82
+ var that = this . $vm ,
83
+ options = this . $ options
84
+ var nodes = children ;
65
85
layui . each ( nodes , function ( index , item ) {
66
86
var hasChild = item [ that . childrenKey ] && item [ that . childrenKey ] . length > 0 ;
67
87
var dom = that . getDom ( item ) ;
68
- if ( parent ) item . parent = parent ;
88
+ if ( parent ) item . $ parent = parent ;
69
89
var ul = $ ( dom . ul ( item ) ) ;
70
90
var li = $ ( that . getNode ( item ) ) ;
71
91
@@ -89,8 +109,8 @@ layui.define('jquery', function(exports) {
89
109
90
110
//节点dom拼接
91
111
Atree . prototype . getDom = function ( item ) {
92
- var that = this ,
93
- options = that . options ,
112
+ var that = this . $vm ,
113
+ options = this . $ options,
94
114
item = item ,
95
115
hasChild = item [ that . childrenKey ] && item [ that . childrenKey ] . length > 0 ;
96
116
return {
@@ -126,8 +146,8 @@ layui.define('jquery', function(exports) {
126
146
}
127
147
//获取树节点
128
148
Atree . prototype . getNode = function ( item ) {
129
- var that = this ,
130
- options = that . options
149
+ var that = this . $vm ,
150
+ options = this . $ options
131
151
var dom = that . getDom ( item ) ;
132
152
var li = [ '<li ' +
133
153
( item [ that . spreadKey ] || options . spreadAll ? 'data-spread="' + ( item [ that . spreadKey ] || true ) + '"' : '' ) +
@@ -155,8 +175,8 @@ layui.define('jquery', function(exports) {
155
175
156
176
//父绑定事件
157
177
Atree . prototype . bindUlEvent = function ( li , item ) {
158
- var that = this ,
159
- options = that . options
178
+ var that = this . $vm ,
179
+ options = this . $ options
160
180
//触发点击节点回调
161
181
typeof options . click === 'function' && that . click ( li , item ) ;
162
182
@@ -175,15 +195,15 @@ layui.define('jquery', function(exports) {
175
195
176
196
//选中回调函数
177
197
Atree . prototype . change = function ( ) {
178
- var that = this ,
179
- options = that . options ;
198
+ var that = this . $vm ,
199
+ options = this . $ options;
180
200
options . change ( changeList ) ;
181
201
} ,
182
202
183
203
//新增方法回调
184
204
Atree . prototype . add = function ( elem , item ) {
185
- var that = this ,
186
- options = that . options ;
205
+ var that = this . $vm ,
206
+ options = this . $ options;
187
207
var node = elem . children ( '.layui-atree-node' ) ;
188
208
var addBtn = node . children ( '.layui-atree-menu' ) . children ( '.layui-atree-add' )
189
209
var arrow = node . children ( '.layui-atree-spread' )
@@ -222,8 +242,8 @@ layui.define('jquery', function(exports) {
222
242
223
243
//删除方法回调
224
244
Atree . prototype . delete = function ( elem , item ) {
225
- var that = this ,
226
- options = that . options ;
245
+ var that = this . $vm ,
246
+ options = this . $ options;
227
247
var node = elem . children ( '.layui-atree-node' ) ;
228
248
var deleteBtn = node . children ( '.layui-atree-menu' ) . children ( '.layui-atree-delete' )
229
249
var ul = elem . children ( 'ul' ) ,
@@ -247,8 +267,8 @@ layui.define('jquery', function(exports) {
247
267
248
268
//点击节点回调
249
269
Atree . prototype . click = function ( elem , item ) {
250
- var that = this ,
251
- options = that . options ;
270
+ var that = this . $vm ,
271
+ options = this . $ options;
252
272
var node = elem . children ( '.layui-atree-node' ) ;
253
273
node . children ( 'a' ) . on ( 'click' , function ( e ) {
254
274
layui . stope ( e ) ;
@@ -258,34 +278,36 @@ layui.define('jquery', function(exports) {
258
278
259
279
//节点选择
260
280
Atree . prototype . checkbox = function ( elem , item ) {
261
- var that = this ,
262
- options = that . options ;
281
+ var that = this . $vm ,
282
+ options = this . $ options;
263
283
var node = elem . children ( '.layui-atree-node' ) ;
264
284
var checkbox = node . children ( '.layui-atree-check' )
265
285
var ul = elem . children ( 'ul' ) ,
266
286
a = node . children ( 'a' ) ;
287
+
267
288
//递归设置子节点
268
- var whileAllCheck = function ( dom , item , type ) {
289
+ var setAllChildCheck = function ( dom , item , type ) {
269
290
var list = dom . children ( '.layui-show' ) . find ( 'li' ) ;
270
291
var children = item ? item . children || [ ] : [ ] ;
271
292
for ( var i = 0 ; i < list . length ; i ++ ) {
272
293
var li = $ ( list [ i ] ) ;
273
294
setCheck ( li , children [ i ] , type ) ;
274
- whileAllCheck ( li , children [ i ] , type ) ;
295
+ setAllChildCheck ( li , children [ i ] , type ) ;
275
296
}
276
297
}
298
+
277
299
//递归设置父节点
278
- var whileAllPatentCheck = function ( dom , item , type ) {
300
+ var setAllPatentCheck = function ( dom , item , type ) {
279
301
var parent = dom . parent ( ) . parent ( ) ;
280
302
var list = parent . children ( '.layui-show' ) . find ( 'li' ) ;
281
303
var isChildrenCheck = true ;
282
304
for ( var i = 0 ; i < list . length ; i ++ ) {
283
305
var li = $ ( list [ i ] ) ;
284
306
if ( ! li . data ( 'check' ) ) isChildrenCheck = false ;
285
307
}
286
- if ( item . parent && ( isChildrenCheck || ! type ) ) {
287
- setCheck ( parent , item . parent , type ) ;
288
- whileAllPatentCheck ( parent , item . parent , type ) ;
308
+ if ( item . $ parent && ( isChildrenCheck || ! type ) ) {
309
+ setCheck ( parent , item . $ parent, type ) ;
310
+ setAllPatentCheck ( parent , item . $ parent, type ) ;
289
311
}
290
312
}
291
313
@@ -294,12 +316,12 @@ layui.define('jquery', function(exports) {
294
316
var checkbox = elem . children ( '.layui-atree-node' ) . find ( '.layui-atree-check' ) ;
295
317
if ( type ) {
296
318
elem . data ( 'check' , true )
297
- elem . attr ( "data-check" , true ) ;
319
+ elem . attr ( "data-check" , true ) ;
298
320
checkbox . html ( icon . checkbox [ 1 ] )
299
321
checkbox . addClass ( ' is-checked' ) ;
300
322
} else {
301
323
elem . data ( 'check' , null ) ;
302
- elem . attr ( "data-check" , null ) ;
324
+ elem . attr ( "data-check" , null ) ;
303
325
checkbox . removeClass ( ' is-checked' ) ;
304
326
checkbox . html ( icon . checkbox [ 0 ] )
305
327
}
@@ -319,11 +341,11 @@ layui.define('jquery', function(exports) {
319
341
} else {
320
342
checkFlag = true ;
321
343
}
322
-
344
+
323
345
setCheck ( elem , item , checkFlag )
324
- whileAllCheck ( elem , item , checkFlag ) ;
325
- whileAllPatentCheck ( elem , item , checkFlag ) ;
326
-
346
+ setAllChildCheck ( elem , item , checkFlag ) ;
347
+ setAllPatentCheck ( elem , item , checkFlag ) ;
348
+
327
349
that . change ( ) ;
328
350
}
329
351
checkbox . on ( 'click' , check ) ;
@@ -332,8 +354,8 @@ layui.define('jquery', function(exports) {
332
354
333
355
//伸展节点
334
356
Atree . prototype . spread = function ( elem , item ) {
335
- var that = this ,
336
- options = that . options ;
357
+ var that = this . $vm ,
358
+ options = this . $ options;
337
359
var node = elem . children ( '.layui-atree-node' ) ;
338
360
var arrow = node . children ( '.layui-atree-spread' )
339
361
var ul = elem . children ( 'ul' ) ,
@@ -349,20 +371,20 @@ layui.define('jquery', function(exports) {
349
371
Atree . prototype . open = function ( elem , ul , arrow ) {
350
372
if ( elem . data ( 'spread' ) ) {
351
373
elem . data ( 'spread' , null )
352
- elem . attr ( "data-spread" , null ) ;
374
+ elem . attr ( "data-spread" , null ) ;
353
375
ul . removeClass ( 'layui-show' ) ;
354
376
arrow . html ( icon . arrow [ 0 ] ) ;
355
377
} else {
356
378
elem . data ( 'spread' , true ) ;
357
- elem . attr ( "data-spread" , true ) ;
379
+ elem . attr ( "data-spread" , true ) ;
358
380
ul . addClass ( 'layui-show' ) ;
359
381
arrow . html ( icon . arrow [ 1 ] ) ;
360
382
}
361
383
} ;
362
384
//通用事件
363
385
Atree . prototype . on = function ( elem ) {
364
- var that = this ,
365
- options = that . options ;
386
+ var that = this . $vm ,
387
+ options = this . $ options;
366
388
var dragStr = 'layui-atree-drag' ;
367
389
368
390
//屏蔽选中文字
@@ -401,8 +423,8 @@ layui.define('jquery', function(exports) {
401
423
//拖拽节点
402
424
Atree . prototype . move = { } ;
403
425
Atree . prototype . drag = function ( elem , item ) {
404
- var that = this ,
405
- options = that . options ;
426
+ var that = this . $vm ,
427
+ options = this . $ options;
406
428
var a = elem . children ( 'a' ) ,
407
429
mouseenter = function ( ) {
408
430
var othis = $ ( this ) ,
0 commit comments