zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
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
/*
    树形
    source: 除delete之外的所有
    view:   当前数据视图,用于配合table,tree显示
    
    数据存储是数组,但是可以体现树形逻辑
*/
/**
    @name Edo.data.DataTree
    @class 
    @typeName datatree
    @description 树形数据源
    @extend Edo.data.DataTable
    @example 
*/ 
Edo.data.DataTree = function(data){
    Edo.data.DataTree.superclass.constructor.call(this, data);
}
Edo.data.DataTree.extend(Edo.data.DataTable, {    
    /**
        @name Edo.data.DataTree#load
        @function
        @description 加载数据
        @param {Array} tree 树形数据
    */    
    load: function(tree, view){
        if(!tree) tree = [];
        if(!(tree instanceof Array)) tree = [tree];     
        
        this.children = tree;
        
        this.source = [];
        var data = [];
        this._createTable(tree, data);
        this.source = data;
        
        if(!view){
            view = [];
            this._createTree(this.children, -1, 0, view, this.filterFn);
        }
        
        Edo.data.DataTree.superclass.load.call(this, data, view);
    },
    //是不是在本方法内,重新排定table?
    syncTreeView: function(action, event){
        var view = [];
        this._createTree(this.children, -1, 0, view, this.filterFn);
        this.refresh(view, action, event);
    },
    //deep深度, stopNode截止的节点, 
    /**
        @name Edo.data.DataTree#collapse
        @function
        @description 收缩节点
        @param {Object} node 节点对象
        @param {Boolean} deep 是否影响子级
    */    
    collapse: function(node, deep){
        node.expanded = false;
        
        if(deep){
            this.iterateChildren(node, function(o){
                o.expanded = false;
            });
        }
        this.canFire = true;
        this.syncTreeView('collapse',{record: node});            
    },
    /**
        @name Edo.data.DataTree#expand
        @function
        @description 展开节点
        @param {Object} node 节点对象        
        @param {Boolean} deep 是否展开所有子节点
    */
    expand: function(node, deep){
        node.expanded = true;
        
        var p = this.findParent(node);
        while(p){
            p.expanded = true;
            p = this.findParent(p);
        }
        
        if(deep){
            this.iterateChildren(node, function(o){
                o.expanded = true;
            });
        }
        
        this.canFire = true;
        this.syncTreeView('expand',{record: node});            
    },
    /**
        @name Edo.data.DataTree#toggle
        @function
        @description 伸展节点
        @param {Object} node 节点对象
        @param {Boolean} deep 是否影响子级
    */    
    toggle: function(node, deep){
    
        if(node.expanded) this.collapse(node,deep);
        else this.expand(node,deep);
    },    
    /**
        @name Edo.data.DataTree#add
        @function
        @description 增加节点
        @param {Object} node 节点对象
        @param {Object} parentNode 父节点
    */    
    add: function(node, p){
        if(!p) p = this;
        if(!p.children) p.children = [];
        return this.insert(p.children.length, node, p);
    },
    /**
        @name Edo.data.DataTree#addRange
        @function
        @description 增加节点
        @param {Array} nodes 节点对象
        @param {Object} parentNode 父节点
    */
    addRange: function(nodes, p){
        if(!p) p = this;
        if(!p.children) p.children = [];
        return this.insertRange(p.children.length, nodes, p);
    },
    /**
        @name Edo.data.DataTree#insert
        @function
        @description 增加节点到索引处
        @param {Number} index 索引
        @param {Object} node 节点对象
        @param {Object} parentNode 父节点
    */
    insert: function(index, node, p){
        this.insertRange(index, [node], p);
    },
    /**
        @name Edo.data.DataTree#insertRange
        @function
        @description 增加节点到索引处
        @param {Number} index 索引
        @param {Array} nodes 节点对象
        @param {Object} parentNode 父节点
    */
    insertRange: function(index, records, p){    
        if(!records || !(records instanceof Array)) return;
        
        if(!p) p = this;
        if(!p.children) p.children = [];                        
        
        var cs = p.children;
        for(var i = 0, len = records.length; i < len; i++){
            var r = records[i];            
            cs.insert(index+i, r);            
            this._doAdd(r);
            
            this.iterateChildren(r, function(n){            
                this._doAdd(n);
            }, this);
        }                
        
        //重新生成表格, 这时候, 这个data应该是source, 没有任何过滤
        var data = [];
        this._createTable(this.children, data);
        this.source = data;
        
        this.syncTreeView('add',{
            records: records,
            index: index,
            parentNode: p
        });       
        this.changed = true;
    },
    /**
        @name Edo.data.DataTree#remove
        @function
        @description 删除节点        
        @param {Object} node 节点对象        
    */    
    remove: function(node){
        var p = this.findParent(node);
        if(p){
            p.children.remove(node);
            
            this._doRemove(node);
            
            var data = [];
            this._createTable(this.children, data);                     
            this.source = data;
            
            this.syncTreeView('remove',{
                
                records: [node],
                parent: p
            });
            this.changed = true;
        }                        
    },
    /**
        @name Edo.data.DataTree#removeAt
        @function
        @description 删除节点    
        @param {Number} index 删除索引号    
        @param {Object} parentNode 父节点对象        
    */        
    removeAt: function(index, p){
        if(!p) throw new Error("父节点为空");
        var node = p && p.children ? p.children[index] : null;
        if(node){
            this.remove(node, p);
        }
    },
    /**
        @name Edo.data.DataTree#removeRange
        @function
        @description 删除数据
        @param {Array} records 数据对象数组
        @params {Object} node 节点
    */
    removeRange: function(records, node){
    
        if(!records || !(records instanceof Array)) return;  
        records = records.clone();
        for(var i=0,l=records.length; i<l; i++){
            var record = records[i];
            
            node = this.findParent(record);
            if(node){
                node.children.remove(record);
                this._doRemove(record);
            }
        }
        
        this.changed = true;
        this.syncTreeView('remove',{
            records: records,
            parent: node
        });
        
    },
    /**
        @name Edo.data.DataTree#move
        @function
        @description 移动节点
        @param {Object} node 节点对象
        @param {Object} targetNode 目标节点对象
        @param {String} action 如何移动插入:数字,preend, append, add
    */
    move: function(nodes, target, action){    
        if(!nodes) return;
        if(!Edo.isArray(nodes)) nodes = [nodes];
        var p = this.findParent(target);
        if(!p) return;
        
        this.beginChange();
        for(var i=0,l=nodes.length; i<l; i++){
            var node = nodes[i];
            
            var index = p.children.indexOf(target);
        
            if(Edo.isNumber(action)){
                p = target;
                index = action;
            }else if(action == 'append') {
                index+=1;
            }else if(action == 'add'){
                p = target;
                if(!p.children) p.children = []
                index = p.children.length;
            }else if (action == "preend"){
                
            }
            
            //这里好像有点性能问题
            if(this.isAncestor(node, p) || node == p){
                return false;
            }            
            
            var p1 = this.findParent(node);
            if(p1 == p){
                var index2 = p1.children.indexOf(node);
                if(index2 <= index){
                    index -= 1;
                }
            }
            
            var status = node.__status;
            var status2 = p.__status;
            
            this.remove(node);
            this.insert(index, node, p);        
            
            
            node.__status = status;
            p.__status = status2;
        }
        
        this.endChange('move', {records: nodes, index: index, parentNode: p});        
        
        this.changed = true;
    },
    /**
        @name Edo.data.DataTree#filter
        @function
        @description 筛选过滤节点(树状方式遍历过滤)
        @param {Function} fn 过滤方法
        @param {Object} scope        
    */   
    filter: function(fn, scope){
        //遍历每一个节点,如果true,则生成
        var view = [];
        this.filterFn = fn;
        this._createTree(this.children, -1, 0, view, fn, scope);
        this.refresh(view, 'filter');
    },
    /**
        @name Edo.data.DataTree#findParent
        @function
        @description 查找父节点
        @param {Object} node 节点对象               
    */       
    findParent: function(node){
        //return findParent(this, node);
        var p = this.getById(node.__pid);
        if(!p && this.getById(node.__id)) p = this;
        return p;
    },
    findTop: function(node){        
        while(1){
            if(node.__pid == -1) return node;
            node = this.getById(node.__pid);            
        }        
    },
    /**
        @name Edo.data.DataTree#getChildAt
        @function
        @description 获得一个节点下children中index位置的节点
        @param {Object} parentNode 父节点对象               
        @param {Number} index               
        @return {Object}
    */       
    getChildAt: function(parentNode, index){
        if(parentNode.children){
            return parentNode.children[index];
        }
    },
    /**
        @name Edo.data.DataTree#indexOfChild
        @function
        @description 获得一个节点在父节点下的索引号
        @param {Object} parentNode 父节点对象               
        @param {Object} node               
        @return {Number}
    */           
    indexOfChild: function(parentNode, node){
        if(parentNode.children){
            return parentNode.indexOf(node);
        }
        return -1;
    },
    /**
        @name Edo.data.DataTree#isFirst
        @function
        @description 是否是第一个节点                
        @param {Object} node               
        @return {Boolean}
    */        
    isFirst: function(node){
        return !node.__preid;
    },
    /**
        @name Edo.data.DataTree#isLast
        @function
        @description 是否是最后一个节点                
        @param {Object} node               
        @return {Boolean}
    */
    isLast: function(node){
        return !node.__nextid;
    },
    /**
        @name Edo.data.DataTree#isLeaf
        @function
        @description 是否是叶子节点(没有子节点)               
        @param {Object} node               
        @return {Boolean}
    */
    isLeaf: function(node){
        return !node.children ||  node.children.length == 0;
    },
    getPrev: function(node){
        return this.getById(node.__preid);
    },
    getNext: function(node){
        return this.getById(node.__nextid);
    },
    //Returns the path for this node. The path can be used to expand or select this node programmatically.
    //attr: (可选) 默认的组成路径的属性,默认是__id.
    getPath: function(node, attr){
        
    },
    /**
        @name Edo.data.DataTree#getDepth
        @function
        @description 获得节点层次
        @param {Object} node               
        @return {Number}
    */
    getDepth: function(node){
        return node.__depth;
    },
    /**
        @name Edo.data.DataTree#eachChildren
        @function
        @description 遍历节点下一个子级
        @param {Object} node      
        @param {Function} fn                 
        @param {Object} scope
    */
    eachChildren: function(node, fn, scope){
        var cs = node.children;
        if(cs){
            cs.each(fn, scope);
        }
    },    
    /**
        @name Edo.data.DataTree#iterateChildren
        @function
        @description 遍历所有层次的子节点
        @param {Object} node      
        @param {Function} fn                 
        @param {Object} scope
    */    
    iterateChildren: function(p, fn, scope){
        if(!fn) return;
        p = p || this;
        var cs = p.children;
        if(cs){
            for(var i=0,l=cs.length; i<l; i++){
                var c = cs[i];
                if(fn.call( scope|| this, c, i) === false) return;
                this.iterateChildren(c, fn, scope);
            }
        }
    },
    /**
        @name Edo.data.DataTree#contains
        @function
        @description 是否包含子节点
        @param {Object} parentNode      
        @param {Object} childNode
    */     
    contains: function(parentNode, childNode){
        while(childNode.__pid != -1){
            if(childNode.__pid == parentNode.__id) return true;
            childNode = this.getById(childNode.__pid);
        }
        return false;        
    },
    /**
        @name Edo.data.DataTree#hasChildren
        @function
        @description 是否有子节点
        @param {Object} node
    */  
    hasChildren: function(node){
        return node.children && node.children.length > 0;
    },
    
    //将树形结构数据,转换为表格结构(其实是得到source源数据表格)
    _createTable: function(tree, data){       //是一个树结构的数组
        
        for(var i=0,l=tree.length; i<l; i++){
            var t = tree[i];
            
            if(!t.__id) t.__id =  Edo.data.DataTable.id++;
                        
            var cs = t.children;            
            
            data[data.length] = t;
                        
            if(cs && cs.length > 0){
                this._createTable(cs, data);
            }
        }
    },
    //得到树形的view数据视图表格
    _createTree: function(tree, pid, depth, data, fn, scope){
        if(!fn) fn = this.filterFn;
    
        var hasInView = false;  //是否有包含在视图内的节点
        for(var i=0,l=tree.length; i<l; i++){
            var t = tree[i];
            
            t.__pid = pid;
            t.__depth = depth;
            t.__hasChildren = t.children && t.children.length > 0;
                        
            if(typeof(t.expanded) === 'undefined') t.expanded = true;
            else t.expanded = !!t.expanded;            
            
            //__preid, __nextid
            t.__preid = t.__nextid = null;
            if(i != 0) t.__preid = tree[i-1].__id;
            if(i != l-1) t.__nextid = tree[i+1].__id;
            
            var next = fn ? fn.call(scope, t, i) : true;   //是否继续
            
            var index = data ? data.length : 0;    //保留索引
            
            if(next !== false){     //如果next不为false,则先加上了
                if(data) data[index] = t;
                
                hasInView = true;
            }
            var childInView = false;
                        
            if(t.__hasChildren){
                //如果确定是不是收缩的,则传递data, 否则传递null
                childInView = this._createTree(t.children, t.__id, depth+1, t.expanded !== false ? data : null, fn);
            }
            
            if(childInView && next === false){  //如果next为false,但是子元素内有符合条件的, 则插入
                if(data) data.insert(index, t);
                
                hasInView = true;
            }
        }
        return hasInView;
    },
    isAncestor: function(p, n){
        if(!p || !n) return false;
        while(n){
            n = this.findParent(n);
            if(n == p) return true;            
        }
        return false;        
    },
    isDisplay: function(record){
        var p = this.findParent(record);
        if(p == this) return true;
        if(!p.expanded) return false;
        return this.isDisplay(p);
    },
    /**
        @name Edo.data.DataTree#getChildren
        @function
        @description 获得节点的子节点(或者是深度子节点集合)
        @param {Object} node 节点对象
        @param {Boolean} deep 是否获取深度子节点集合
    */
    getChildren: function(node, deep){
        if(deep){
            var children = [];
            this.iterateChildren(record, function(node){
                children.add(node);
            });
            return children;
        }else{
            return node.children;
        }
    }
});
 
Edo.data.DataTree.regType('datatree');
 
//判断n是否是p的子节点
Edo.data.DataTree.isAncestor = function(p, n){                    
    if(!p || !n) return false;
    var cs = p.children;
    if(cs){
        for(var i=0,l=cs.length; i<l; i++){
            var c = cs[i];
            if(c == n) return true;
            var r = Edo.data.DataTree.isAncestor(c, n);
            if(r) return true;
        }
    }
    return false;
}