4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.data.Tree
15 * @extends Roo.util.Observable
16 * Represents a tree data structure and bubbles all the events for its nodes. The nodes
17 * in the tree have most standard DOM functionality.
19 * @param {Node} root (optional) The root node
21 Roo.data.Tree = function(root){
24 * The root node for this tree
29 this.setRootNode(root);
34 * Fires when a new child node is appended to a node in this tree.
35 * @param {Tree} tree The owner tree
36 * @param {Node} parent The parent node
37 * @param {Node} node The newly appended node
38 * @param {Number} index The index of the newly appended node
43 * Fires when a child node is removed from a node in this tree.
44 * @param {Tree} tree The owner tree
45 * @param {Node} parent The parent node
46 * @param {Node} node The child node removed
51 * Fires when a node is moved to a new location in the tree
52 * @param {Tree} tree The owner tree
53 * @param {Node} node The node moved
54 * @param {Node} oldParent The old parent of this node
55 * @param {Node} newParent The new parent of this node
56 * @param {Number} index The index it was moved to
61 * Fires when a new child node is inserted in a node in this tree.
62 * @param {Tree} tree The owner tree
63 * @param {Node} parent The parent node
64 * @param {Node} node The child node inserted
65 * @param {Node} refNode The child node the node was inserted before
70 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
71 * @param {Tree} tree The owner tree
72 * @param {Node} parent The parent node
73 * @param {Node} node The child node to be appended
75 "beforeappend" : true,
78 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
79 * @param {Tree} tree The owner tree
80 * @param {Node} parent The parent node
81 * @param {Node} node The child node to be removed
83 "beforeremove" : true,
86 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
87 * @param {Tree} tree The owner tree
88 * @param {Node} node The node being moved
89 * @param {Node} oldParent The parent of the node
90 * @param {Node} newParent The new parent the node is moving to
91 * @param {Number} index The index it is being moved to
96 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
97 * @param {Tree} tree The owner tree
98 * @param {Node} parent The parent node
99 * @param {Node} node The child node to be inserted
100 * @param {Node} refNode The child node the node is being inserted before
102 "beforeinsert" : true
105 Roo.data.Tree.superclass.constructor.call(this);
108 Roo.extend(Roo.data.Tree, Roo.util.Observable, {
111 proxyNodeEvent : function(){
112 return this.fireEvent.apply(this, arguments);
116 * Returns the root node for this tree.
119 getRootNode : function(){
124 * Sets the root node for this tree.
128 setRootNode : function(node){
130 node.ownerTree = this;
132 this.registerNode(node);
137 * Gets a node in this tree by its id.
141 getNodeById : function(id){
142 return this.nodeHash[id];
145 registerNode : function(node){
146 this.nodeHash[node.id] = node;
149 unregisterNode : function(node){
150 delete this.nodeHash[node.id];
153 toString : function(){
154 return "[Tree"+(this.id?" "+this.id:"")+"]";
159 * @class Roo.data.Node
160 * @extends Roo.util.Observable
161 * @cfg {Boolean} leaf true if this node is a leaf and does not have children
162 * @cfg {String} id The id for this node. If one is not specified, one is generated.
164 * @param {Object} attributes The attributes/config for the node
166 Roo.data.Node = function(attributes){
168 * The attributes supplied for the node. You can use this property to access any custom attributes you supplied.
171 this.attributes = attributes || {};
172 this.leaf = this.attributes.leaf;
174 * The node id. @type String
176 this.id = this.attributes.id;
178 this.id = Roo.id(null, "ynode-");
179 this.attributes.id = this.id;
182 * All child nodes of this node. @type Array
184 this.childNodes = [];
185 if(!this.childNodes.indexOf){ // indexOf is a must
186 this.childNodes.indexOf = function(o){
187 for(var i = 0, len = this.length; i < len; i++){
196 * The parent node for this node. @type Node
198 this.parentNode = null;
200 * The first direct child node of this node, or null if this node has no child nodes. @type Node
202 this.firstChild = null;
204 * The last direct child node of this node, or null if this node has no child nodes. @type Node
206 this.lastChild = null;
208 * The node immediately preceding this node in the tree, or null if there is no sibling node. @type Node
210 this.previousSibling = null;
212 * The node immediately following this node in the tree, or null if there is no sibling node. @type Node
214 this.nextSibling = null;
219 * Fires when a new child node is appended
220 * @param {Tree} tree The owner tree
221 * @param {Node} this This node
222 * @param {Node} node The newly appended node
223 * @param {Number} index The index of the newly appended node
228 * Fires when a child node is removed
229 * @param {Tree} tree The owner tree
230 * @param {Node} this This node
231 * @param {Node} node The removed node
236 * Fires when this node is moved to a new location in the tree
237 * @param {Tree} tree The owner tree
238 * @param {Node} this This node
239 * @param {Node} oldParent The old parent of this node
240 * @param {Node} newParent The new parent of this node
241 * @param {Number} index The index it was moved to
246 * Fires when a new child node is inserted.
247 * @param {Tree} tree The owner tree
248 * @param {Node} this This node
249 * @param {Node} node The child node inserted
250 * @param {Node} refNode The child node the node was inserted before
254 * @event beforeappend
255 * Fires before a new child is appended, return false to cancel the append.
256 * @param {Tree} tree The owner tree
257 * @param {Node} this This node
258 * @param {Node} node The child node to be appended
260 "beforeappend" : true,
262 * @event beforeremove
263 * Fires before a child is removed, return false to cancel the remove.
264 * @param {Tree} tree The owner tree
265 * @param {Node} this This node
266 * @param {Node} node The child node to be removed
268 "beforeremove" : true,
271 * Fires before this node is moved to a new location in the tree. Return false to cancel the move.
272 * @param {Tree} tree The owner tree
273 * @param {Node} this This node
274 * @param {Node} oldParent The parent of this node
275 * @param {Node} newParent The new parent this node is moving to
276 * @param {Number} index The index it is being moved to
280 * @event beforeinsert
281 * Fires before a new child is inserted, return false to cancel the insert.
282 * @param {Tree} tree The owner tree
283 * @param {Node} this This node
284 * @param {Node} node The child node to be inserted
285 * @param {Node} refNode The child node the node is being inserted before
287 "beforeinsert" : true
289 this.listeners = this.attributes.listeners;
290 Roo.data.Node.superclass.constructor.call(this);
293 Roo.extend(Roo.data.Node, Roo.util.Observable, {
294 fireEvent : function(evtName){
295 // first do standard event for this node
296 if(Roo.data.Node.superclass.fireEvent.apply(this, arguments) === false){
299 // then bubble it up to the tree if the event wasn't cancelled
300 var ot = this.getOwnerTree();
302 if(ot.proxyNodeEvent.apply(ot, arguments) === false){
310 * Returns true if this node is a leaf
314 return this.leaf === true;
318 setFirstChild : function(node){
319 this.firstChild = node;
323 setLastChild : function(node){
324 this.lastChild = node;
329 * Returns true if this node is the last child of its parent
333 return (!this.parentNode ? true : this.parentNode.lastChild == this);
337 * Returns true if this node is the first child of its parent
340 isFirst : function(){
341 return (!this.parentNode ? true : this.parentNode.firstChild == this);
344 hasChildNodes : function(){
345 return !this.isLeaf() && this.childNodes.length > 0;
349 * Insert node(s) as the last child node of this node.
350 * @param {Node/Array} node The node or Array of nodes to append
351 * @return {Node} The appended node if single append, or null if an array was passed
353 appendChild : function(node){
355 if(node instanceof Array){
357 }else if(arguments.length > 1){
360 // if passed an array or multiple args do them one by one
362 for(var i = 0, len = multi.length; i < len; i++) {
363 this.appendChild(multi[i]);
366 if(this.fireEvent("beforeappend", this.ownerTree, this, node) === false){
369 var index = this.childNodes.length;
370 var oldParent = node.parentNode;
371 // it's a move, make sure we move it cleanly
373 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index) === false){
376 oldParent.removeChild(node);
378 index = this.childNodes.length;
380 this.setFirstChild(node);
382 this.childNodes.push(node);
383 node.parentNode = this;
384 var ps = this.childNodes[index-1];
386 node.previousSibling = ps;
387 ps.nextSibling = node;
389 node.previousSibling = null;
391 node.nextSibling = null;
392 this.setLastChild(node);
393 node.setOwnerTree(this.getOwnerTree());
394 this.fireEvent("append", this.ownerTree, this, node, index);
396 node.fireEvent("move", this.ownerTree, node, oldParent, this, index);
403 * Removes a child node from this node.
404 * @param {Node} node The node to remove
405 * @return {Node} The removed node
407 removeChild : function(node){
408 var index = this.childNodes.indexOf(node);
412 if(this.fireEvent("beforeremove", this.ownerTree, this, node) === false){
416 // remove it from childNodes collection
417 this.childNodes.splice(index, 1);
420 if(node.previousSibling){
421 node.previousSibling.nextSibling = node.nextSibling;
423 if(node.nextSibling){
424 node.nextSibling.previousSibling = node.previousSibling;
428 if(this.firstChild == node){
429 this.setFirstChild(node.nextSibling);
431 if(this.lastChild == node){
432 this.setLastChild(node.previousSibling);
435 node.setOwnerTree(null);
436 // clear any references from the node
437 node.parentNode = null;
438 node.previousSibling = null;
439 node.nextSibling = null;
440 this.fireEvent("remove", this.ownerTree, this, node);
445 * Inserts the first node before the second node in this nodes childNodes collection.
446 * @param {Node} node The node to insert
447 * @param {Node} refNode The node to insert before (if null the node is appended)
448 * @return {Node} The inserted node
450 insertBefore : function(node, refNode){
451 if(!refNode){ // like standard Dom, refNode can be null for append
452 return this.appendChild(node);
459 if(this.fireEvent("beforeinsert", this.ownerTree, this, node, refNode) === false){
462 var index = this.childNodes.indexOf(refNode);
463 var oldParent = node.parentNode;
464 var refIndex = index;
466 // when moving internally, indexes will change after remove
467 if(oldParent == this && this.childNodes.indexOf(node) < index){
471 // it's a move, make sure we move it cleanly
473 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index, refNode) === false){
476 oldParent.removeChild(node);
479 this.setFirstChild(node);
481 this.childNodes.splice(refIndex, 0, node);
482 node.parentNode = this;
483 var ps = this.childNodes[refIndex-1];
485 node.previousSibling = ps;
486 ps.nextSibling = node;
488 node.previousSibling = null;
490 node.nextSibling = refNode;
491 refNode.previousSibling = node;
492 node.setOwnerTree(this.getOwnerTree());
493 this.fireEvent("insert", this.ownerTree, this, node, refNode);
495 node.fireEvent("move", this.ownerTree, node, oldParent, this, refIndex, refNode);
501 * Returns the child node at the specified index.
502 * @param {Number} index
505 item : function(index){
506 return this.childNodes[index];
510 * Replaces one child node in this node with another.
511 * @param {Node} newChild The replacement node
512 * @param {Node} oldChild The node to replace
513 * @return {Node} The replaced node
515 replaceChild : function(newChild, oldChild){
516 this.insertBefore(newChild, oldChild);
517 this.removeChild(oldChild);
522 * Returns the index of a child node
524 * @return {Number} The index of the node or -1 if it was not found
526 indexOf : function(child){
527 return this.childNodes.indexOf(child);
531 * Returns the tree this node is in.
534 getOwnerTree : function(){
535 // if it doesn't have one, look for one
540 this.ownerTree = p.ownerTree;
546 return this.ownerTree;
550 * Returns depth of this node (the root node has a depth of 0)
553 getDepth : function(){
564 setOwnerTree : function(tree){
565 // if it's move, we need to update everyone
566 if(tree != this.ownerTree){
568 this.ownerTree.unregisterNode(this);
570 this.ownerTree = tree;
571 var cs = this.childNodes;
572 for(var i = 0, len = cs.length; i < len; i++) {
573 cs[i].setOwnerTree(tree);
576 tree.registerNode(this);
582 * Returns the path for this node. The path can be used to expand or select this node programmatically.
583 * @param {String} attr (optional) The attr to use for the path (defaults to the node's id)
584 * @return {String} The path
586 getPath : function(attr){
588 var p = this.parentNode;
589 var b = [this.attributes[attr]];
591 b.unshift(p.attributes[attr]);
594 var sep = this.getOwnerTree().pathSeparator;
595 return sep + b.join(sep);
599 * Bubbles up the tree from this node, calling the specified function with each node. The scope (<i>this</i>) of
600 * function call will be the scope provided or the current node. The arguments to the function
601 * will be the args provided or the current node. If the function returns false at any point,
602 * the bubble is stopped.
603 * @param {Function} fn The function to call
604 * @param {Object} scope (optional) The scope of the function (defaults to current node)
605 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
607 bubble : function(fn, scope, args){
610 if(fn.call(scope || p, args || p) === false){
618 * Cascades down the tree from this node, calling the specified function with each node. The scope (<i>this</i>) of
619 * function call will be the scope provided or the current node. The arguments to the function
620 * will be the args provided or the current node. If the function returns false at any point,
621 * the cascade is stopped on that branch.
622 * @param {Function} fn The function to call
623 * @param {Object} scope (optional) The scope of the function (defaults to current node)
624 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
626 cascade : function(fn, scope, args){
627 if(fn.call(scope || this, args || this) !== false){
628 var cs = this.childNodes;
629 for(var i = 0, len = cs.length; i < len; i++) {
630 cs[i].cascade(fn, scope, args);
636 * Interates the child nodes of this node, calling the specified function with each node. The scope (<i>this</i>) of
637 * function call will be the scope provided or the current node. The arguments to the function
638 * will be the args provided or the current node. If the function returns false at any point,
639 * the iteration stops.
640 * @param {Function} fn The function to call
641 * @param {Object} scope (optional) The scope of the function (defaults to current node)
642 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
644 eachChild : function(fn, scope, args){
645 var cs = this.childNodes;
646 for(var i = 0, len = cs.length; i < len; i++) {
647 if(fn.call(scope || this, args || cs[i]) === false){
654 * Finds the first child that has the attribute with the specified value.
655 * @param {String} attribute The attribute name
656 * @param {Mixed} value The value to search for
657 * @return {Node} The found child or null if none was found
659 findChild : function(attribute, value){
660 var cs = this.childNodes;
661 for(var i = 0, len = cs.length; i < len; i++) {
662 if(cs[i].attributes[attribute] == value){
670 * Finds the first child by a custom function. The child matches if the function passed
672 * @param {Function} fn
673 * @param {Object} scope (optional)
674 * @return {Node} The found child or null if none was found
676 findChildBy : function(fn, scope){
677 var cs = this.childNodes;
678 for(var i = 0, len = cs.length; i < len; i++) {
679 if(fn.call(scope||cs[i], cs[i]) === true){
687 * Sorts this nodes children using the supplied sort function
688 * @param {Function} fn
689 * @param {Object} scope (optional)
691 sort : function(fn, scope){
692 var cs = this.childNodes;
695 var sortFn = scope ? function(){fn.apply(scope, arguments);} : fn;
697 for(var i = 0; i < len; i++){
699 n.previousSibling = cs[i-1];
700 n.nextSibling = cs[i+1];
702 this.setFirstChild(n);
705 this.setLastChild(n);
712 * Returns true if this node is an ancestor (at any point) of the passed node.
716 contains : function(node){
717 return node.isAncestor(this);
721 * Returns true if the passed node is an ancestor (at any point) of this node.
725 isAncestor : function(node){
726 var p = this.parentNode;
736 toString : function(){
737 return "[Node"+(this.id?" "+this.id:"")+"]";