summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/data/Tree.js
blob: afa5b208a84ba80dd57ac4e40d73e51d72e25a77 (plain)
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
YAHOO.namespace('ext.data');

/**
 * @class YAHOO.ext.data.Tree
 * @extends YAHOO.ext.util.Observable
 * The class represents a tree data structure and bubbles all the events for it's nodes. The nodes
 * in the tree have most standard DOM functionality.
 * @constructor
 * @param {Node} root (optional) The root node 
 */
YAHOO.ext.data.Tree = function(root){
   this.nodeHash = {};
   this.root = null;
   if(root){
       this.setRootNode(root);
   }
   this.events = {
       'append' : true,
       'remove' : true,
       'move' : true,
       'insert' : true,
       'beforeappend' : true,
       'beforeremove' : true,
       'beforemove' : true,
       'beforeinsert' : true
   };
};

YAHOO.extendX(YAHOO.ext.data.Tree, YAHOO.ext.util.Observable, {
    pathSeparator: '/',
    
    getRootNode : function(){
        return this.root;
    },
    
    setRootNode : function(node){
        this.root = node;
        node.ownerTree = this;
        node.isRoot = true;
        return node;
    },
    
    getNodeById : function(id){
        return this.nodeHash[id];  
    },
    
    registerNode : function(node){
        this.nodeHash[node.id] = node;
    },
    
    unregisterNode : function(node){
        delete this.nodeHash[node.id];
    },
    
    toString : function(){
        return '[Tree'+(this.id?' '+this.id:'')+']';
    }  
});

/**
 * @class YAHOO.ext.tree.Node
 * @extends YAHOO.ext.util.Observable
 * @cfg {String} text The text for this node
 * @cfg {String} id The id for this node
 * @constructor
 * @param {Object} attributes The attributes/config for the node 
 */
YAHOO.ext.data.Node = function(attributes){
    this.attributes = attributes || {};
    this.leaf = this.attributes.leaf;
    this.id = this.attributes.id;
    if(!this.id){
        this.id = YAHOO.util.Dom.generateId(null, 'ynode-');
        this.attributes.id = this.id;
    }
    
    this.childNodes = [];
    if(!this.childNodes.indexOf){ // indexOf is a must
        this.childNodes.indexOf = function(o){
            for(var i = 0, len = this.length; i < len; i++){
                if(this[i] == o) return i;
            }
            return -1;
        };
    }
    this.parentNode = null;
    this.firstChild = null;
    this.lastChild = null;
    this.previousSibling = null;
    this.nextSibling = null;
    
    this.events = {
       'append' : true,
       'remove' : true,
       'move' : true,
       'insert' : true,
       'beforeappend' : true,
       'beforeremove' : true,
       'beforemove' : true,
       'beforeinsert' : true
   };
};

YAHOO.extendX(YAHOO.ext.data.Node, YAHOO.ext.util.Observable, {
    fireEvent : function(evtName){
        // first do standard event for this node
        if(YAHOO.ext.data.Node.superclass.fireEvent.apply(this, arguments) === false){
            return false;
        }
        // then bubble it up to the tree if the event wasn't cancelled
        if(this.ownerTree){
            if(this.ownerTree.fireEvent.apply(this.ownerTree, arguments) === false){
                return false;
            }
        }
        return true;
    },
    
    isLeaf : function(){
        return this.leaf === true;  
    },
    
    setFirstChild : function(node){
        this.firstChild = node;  
    },
    
    setLastChild : function(node){
        this.lastChild = node;
    },
    
    isLast : function(){
       return (!this.parentNode ? true : this.parentNode.lastChild == this);   
    },
    
    isFirst : function(){
       return (!this.parentNode ? true : this.parentNode.firstChild == this);   
    },
    
    hasChildNodes : function(){
        return !this.isLeaf() && this.childNodes.length > 0;
    },
    
    appendChild : function(node){
        var multi = false;
        if(node instanceof Array){
            multi = node;
        }else if(arguments.length > 1){
            multi = arguments;
        }
        // if passed an array or multiple args do them one by one
        if(multi){
            for(var i = 0, len = multi.length; i < len; i++) {
            	this.appendChild(multi[i]);
            }
        }else{
            if(this.fireEvent('beforeappend', this.ownerTree, this, node) === false){
                return false;
            }
            var index = this.childNodes.length;
            var oldParent = node.parentNode;
            // it's a move, make sure we move it cleanly
            if(oldParent){
                if(node.fireEvent('beforemove', node.getOwnerTree(), node, oldParent, this, index) === false){
                    return false;
                }
                oldParent.removeChild(node);
            }
            var index = this.childNodes.length;
            if(index == 0){
                this.setFirstChild(node);
            }
            this.childNodes.push(node);
            node.parentNode = this;
            var ps = this.childNodes[index-1];
            if(ps){
                node.previousSibling = ps;
                ps.nextSibling = node;
            }
            this.setLastChild(node);
            node.setOwnerTree(this.getOwnerTree());
            this.fireEvent('append', this.ownerTree, this, node, index);
            if(oldParent){
                node.fireEvent('move', this.ownerTree, node, oldParent, this, index);
            }
            return node;
        }
    },
    
    removeChild : function(node){
        var index = this.childNodes.indexOf(node);
        if(index == -1){
            return false;
        }
        if(this.fireEvent('beforeremove', this.ownerTree, this, node) === false){
            return false;
        }
            
        // remove it from childNodes collection
        this.childNodes.splice(index, 1);
        
        // update siblings
        if(node.previousSibling){
            node.previousSibling.nextSibling = node.nextSibling;
        }
        if(node.nextSibling){
            node.nextSibling.previousSibling = node.previousSibling;
        }
        
        // update child refs
        if(this.firstChild == node){
            this.setFirstChild(node.nextSibling);
        }
        if(this.lastChild == node){
            this.setLastChild(node.previousSibling);
        }
        
        node.setOwnerTree(null);
        // clear any references from the node
        node.parentNode = null;
        node.previousSibling = null;
        node.nextSibling = null;
        this.fireEvent('remove', this.ownerTree, this, node);
        return node;
    },
    
    insertBefore : function(node, refNode){
        if(!refNode){ // like standard Dom, refNode can be null for append
            return this.appendChild(node);
        }
        // nothing to do
        if(node == refNode){
            return false;
        }
        
        if(this.fireEvent('beforeinsert', this.ownerTree, this, node, refNode) === false){
            return false;
        }
        var index = this.childNodes.indexOf(refNode);
        var oldParent = node.parentNode;
        var refIndex = index;
        
        // when moving internally, indexes will change after remove
        if(oldParent == this && this.childNodes.indexOf(node) < index){
            refIndex--;
        }
        
        // it's a move, make sure we move it cleanly
        if(oldParent){
            if(node.fireEvent('beforemove', node.getOwnerTree(), node, oldParent, this, index, refNode) === false){
                return false;
            }
            oldParent.removeChild(node);
        }
        if(refIndex == 0){
            this.setFirstChild(node);
        }
        this.childNodes.splice(refIndex, 0, node);
        node.parentNode = this;
        var ps = this.childNodes[refIndex-1];
        if(ps){
            node.previousSibling = ps;
            ps.nextSibling = node;
        }
        node.nextSibling = refNode;
        node.setOwnerTree(this.getOwnerTree());
        this.fireEvent('insert', this.ownerTree, this, node, refNode);
        if(oldParent){
            node.fireEvent('move', this.ownerTree, node, oldParent, this, refIndex, refNode);
        }
        return node;
    },
    
    item : function(index){
        return this.childNodes[index];  
    },
    
    replaceChild : function(newChild, oldChild){
        this.insertBefore(newChild, oldChild);
        this.removeChild(oldChild);
        return oldChild;
    },
    
    indexOf : function(child){
        return this.childNodes.indexOf(child);  
    },
    
    getOwnerTree : function(){
        // if it doesn't have one, look for one
        if(!this.ownerTree){
            var p = this;
            while(p){
                if(p.ownerTree){
                    this.ownerTree = p.ownerTree;
                    break;
                }
                p = p.parentNode;
            }
        }
        return this.ownerTree;
    },
    
    setOwnerTree : function(tree){
        // if it's move, we need to update everyone
        if(tree != this.ownerTree){
            if(this.ownerTree){
                this.ownerTree.unregisterNode(this);
            }
            this.ownerTree = tree;
            var cs = this.childNodes;
            for(var i = 0, len = cs.length; i < len; i++) {
            	cs[i].setOwnerTree(tree);
            }
            if(tree){
                tree.registerNode(this);
            }
        }
    },
    
    getPath : function(attr){
        attr = attr || 'id';
        var p = this.parentNode;
        var b = [this.attributes[attr]];
        while(p){
            b.unshift(p.attributes[attr]);
            p = p.parentNode;
        }
        var sep = this.getOwnerTree().pathSeparator;
        return sep + b.join(sep);
    },
    
    bubble : function(fn, scope, args){
        var p = this;
        while(p){
            if(fn.call(scope || p, args || p) === false){
                break;
            }
            p = p.parentNode;
        }
    },
    
    cascade : function(fn, scope, args){
        if(fn.call(scope || this, args || this) !== false){
            var cs = this.childNodes;
            for(var i = 0, len = cs.length; i < len; i++) {
            	cs[i].cascade(fn, scope, args);
            }
        }
    },
    
    eachChild : function(fn, scope, args){
        var cs = this.childNodes;
        for(var i = 0, len = cs.length; i < len; i++) {
        	if(fn.call(scope || this, args || cs[i]) === false){
        	    break;
        	}
        }
    },
    
    findChild : function(attribute, value){
        var cs = this.childNodes;
        for(var i = 0, len = cs.length; i < len; i++) {
        	if(cs[i].attributes[attribute] == value){
        	    return cs[i];
        	}
        }
        return null;
    },
    
    /**
     * Sorts this nodes children using the supplied sort function
     * @param {Function} fn
     * @param {Object} scope
     */
    sort : function(fn, scope){
        var cs = this.childNodes;
        var len = cs.length;
        if(len > 0){
            var sortFn = scope ? function(){fn.apply(scope, arguments);} : fn;
            cs.sort(sortFn);
            for(var i = 0; i < len; i++){
                var n = cs[i];
                n.previousSibling = cs[i-1];
                n.nextSibling = cs[i+1];
                if(i == 0){
                    this.setFirstChild(n);
                }
                if(i == len-1){
                    this.setLastChild(n);
                }
            }
        }
    },
    
    contains : function(node){
        return node.isAncestor(this);
    },
    
    isAncestor : function(node){
        var p = this.parentNode;
        while(p){
            if(p == node){
                return true;
            }
            p = p.parentNode;
        }
        return false;
    },
    
    toString : function(){
        return '[Node'+(this.id?' '+this.id:'')+']';
    }
});