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++){
188 if(this[i] == o) return i;
194 * The parent node for this node. @type Node
196 this.parentNode = null;
198 * The first direct child node of this node, or null if this node has no child nodes. @type Node
200 this.firstChild = null;
202 * The last direct child node of this node, or null if this node has no child nodes. @type Node
204 this.lastChild = null;
206 * The node immediately preceding this node in the tree, or null if there is no sibling node. @type Node
208 this.previousSibling = null;
210 * The node immediately following this node in the tree, or null if there is no sibling node. @type Node
212 this.nextSibling = null;
217 * Fires when a new child node is appended
218 * @param {Tree} tree The owner tree
219 * @param {Node} this This node
220 * @param {Node} node The newly appended node
221 * @param {Number} index The index of the newly appended node
226 * Fires when a child node is removed
227 * @param {Tree} tree The owner tree
228 * @param {Node} this This node
229 * @param {Node} node The removed node
234 * Fires when this node is moved to a new location in the tree
235 * @param {Tree} tree The owner tree
236 * @param {Node} this This node
237 * @param {Node} oldParent The old parent of this node
238 * @param {Node} newParent The new parent of this node
239 * @param {Number} index The index it was moved to
244 * Fires when a new child node is inserted.
245 * @param {Tree} tree The owner tree
246 * @param {Node} this This node
247 * @param {Node} node The child node inserted
248 * @param {Node} refNode The child node the node was inserted before
252 * @event beforeappend
253 * Fires before a new child is appended, return false to cancel the append.
254 * @param {Tree} tree The owner tree
255 * @param {Node} this This node
256 * @param {Node} node The child node to be appended
258 "beforeappend" : true,
260 * @event beforeremove
261 * Fires before a child is removed, return false to cancel the remove.
262 * @param {Tree} tree The owner tree
263 * @param {Node} this This node
264 * @param {Node} node The child node to be removed
266 "beforeremove" : true,
269 * Fires before this node is moved to a new location in the tree. Return false to cancel the move.
270 * @param {Tree} tree The owner tree
271 * @param {Node} this This node
272 * @param {Node} oldParent The parent of this node
273 * @param {Node} newParent The new parent this node is moving to
274 * @param {Number} index The index it is being moved to
278 * @event beforeinsert
279 * Fires before a new child is inserted, return false to cancel the insert.
280 * @param {Tree} tree The owner tree
281 * @param {Node} this This node
282 * @param {Node} node The child node to be inserted
283 * @param {Node} refNode The child node the node is being inserted before
285 "beforeinsert" : true
287 this.listeners = this.attributes.listeners;
288 Roo.data.Node.superclass.constructor.call(this);
291 Roo.extend(Roo.data.Node, Roo.util.Observable, {
292 fireEvent : function(evtName){
293 // first do standard event for this node
294 if(Roo.data.Node.superclass.fireEvent.apply(this, arguments) === false){
297 // then bubble it up to the tree if the event wasn't cancelled
298 var ot = this.getOwnerTree();
300 if(ot.proxyNodeEvent.apply(ot, arguments) === false){
308 * Returns true if this node is a leaf
312 return this.leaf === true;
316 setFirstChild : function(node){
317 this.firstChild = node;
321 setLastChild : function(node){
322 this.lastChild = node;
327 * Returns true if this node is the last child of its parent
331 return (!this.parentNode ? true : this.parentNode.lastChild == this);
335 * Returns true if this node is the first child of its parent
338 isFirst : function(){
339 return (!this.parentNode ? true : this.parentNode.firstChild == this);
342 hasChildNodes : function(){
343 return !this.isLeaf() && this.childNodes.length > 0;
347 * Insert node(s) as the last child node of this node.
348 * @param {Node/Array} node The node or Array of nodes to append
349 * @return {Node} The appended node if single append, or null if an array was passed
351 appendChild : function(node){
353 if(node instanceof Array){
355 }else if(arguments.length > 1){
358 // if passed an array or multiple args do them one by one
360 for(var i = 0, len = multi.length; i < len; i++) {
361 this.appendChild(multi[i]);
364 if(this.fireEvent("beforeappend", this.ownerTree, this, node) === false){
367 var index = this.childNodes.length;
368 var oldParent = node.parentNode;
369 // it's a move, make sure we move it cleanly
371 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index) === false){
374 oldParent.removeChild(node);
376 index = this.childNodes.length;
378 this.setFirstChild(node);
380 this.childNodes.push(node);
381 node.parentNode = this;
382 var ps = this.childNodes[index-1];
384 node.previousSibling = ps;
385 ps.nextSibling = node;
387 node.previousSibling = null;
389 node.nextSibling = null;
390 this.setLastChild(node);
391 node.setOwnerTree(this.getOwnerTree());
392 this.fireEvent("append", this.ownerTree, this, node, index);
394 node.fireEvent("move", this.ownerTree, node, oldParent, this, index);
401 * Removes a child node from this node.
402 * @param {Node} node The node to remove
403 * @return {Node} The removed node
405 removeChild : function(node){
406 var index = this.childNodes.indexOf(node);
410 if(this.fireEvent("beforeremove", this.ownerTree, this, node) === false){
414 // remove it from childNodes collection
415 this.childNodes.splice(index, 1);
418 if(node.previousSibling){
419 node.previousSibling.nextSibling = node.nextSibling;
421 if(node.nextSibling){
422 node.nextSibling.previousSibling = node.previousSibling;
426 if(this.firstChild == node){
427 this.setFirstChild(node.nextSibling);
429 if(this.lastChild == node){
430 this.setLastChild(node.previousSibling);
433 node.setOwnerTree(null);
434 // clear any references from the node
435 node.parentNode = null;
436 node.previousSibling = null;
437 node.nextSibling = null;
438 this.fireEvent("remove", this.ownerTree, this, node);
443 * Inserts the first node before the second node in this nodes childNodes collection.
444 * @param {Node} node The node to insert
445 * @param {Node} refNode The node to insert before (if null the node is appended)
446 * @return {Node} The inserted node
448 insertBefore : function(node, refNode){
449 if(!refNode){ // like standard Dom, refNode can be null for append
450 return this.appendChild(node);
457 if(this.fireEvent("beforeinsert", this.ownerTree, this, node, refNode) === false){
460 var index = this.childNodes.indexOf(refNode);
461 var oldParent = node.parentNode;
462 var refIndex = index;
464 // when moving internally, indexes will change after remove
465 if(oldParent == this && this.childNodes.indexOf(node) < index){
469 // it's a move, make sure we move it cleanly
471 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index, refNode) === false){
474 oldParent.removeChild(node);
477 this.setFirstChild(node);
479 this.childNodes.splice(refIndex, 0, node);
480 node.parentNode = this;
481 var ps = this.childNodes[refIndex-1];
483 node.previousSibling = ps;
484 ps.nextSibling = node;
486 node.previousSibling = null;
488 node.nextSibling = refNode;
489 refNode.previousSibling = node;
490 node.setOwnerTree(this.getOwnerTree());
491 this.fireEvent("insert", this.ownerTree, this, node, refNode);
493 node.fireEvent("move", this.ownerTree, node, oldParent, this, refIndex, refNode);
499 * Returns the child node at the specified index.
500 * @param {Number} index
503 item : function(index){
504 return this.childNodes[index];
508 * Replaces one child node in this node with another.
509 * @param {Node} newChild The replacement node
510 * @param {Node} oldChild The node to replace
511 * @return {Node} The replaced node
513 replaceChild : function(newChild, oldChild){
514 this.insertBefore(newChild, oldChild);
515 this.removeChild(oldChild);
520 * Returns the index of a child node
522 * @return {Number} The index of the node or -1 if it was not found
524 indexOf : function(child){
525 return this.childNodes.indexOf(child);
529 * Returns the tree this node is in.
532 getOwnerTree : function(){
533 // if it doesn't have one, look for one
538 this.ownerTree = p.ownerTree;
544 return this.ownerTree;
548 * Returns depth of this node (the root node has a depth of 0)
551 getDepth : function(){
562 setOwnerTree : function(tree){
563 // if it's move, we need to update everyone
564 if(tree != this.ownerTree){
566 this.ownerTree.unregisterNode(this);
568 this.ownerTree = tree;
569 var cs = this.childNodes;
570 for(var i = 0, len = cs.length; i < len; i++) {
571 cs[i].setOwnerTree(tree);
574 tree.registerNode(this);
580 * Returns the path for this node. The path can be used to expand or select this node programmatically.
581 * @param {String} attr (optional) The attr to use for the path (defaults to the node's id)
582 * @return {String} The path
584 getPath : function(attr){
586 var p = this.parentNode;
587 var b = [this.attributes[attr]];
589 b.unshift(p.attributes[attr]);
592 var sep = this.getOwnerTree().pathSeparator;
593 return sep + b.join(sep);
597 * Bubbles up the tree from this node, calling the specified function with each node. The scope (<i>this</i>) of
598 * function call will be the scope provided or the current node. The arguments to the function
599 * will be the args provided or the current node. If the function returns false at any point,
600 * the bubble is stopped.
601 * @param {Function} fn The function to call
602 * @param {Object} scope (optional) The scope of the function (defaults to current node)
603 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
605 bubble : function(fn, scope, args){
608 if(fn.call(scope || p, args || p) === false){
616 * Cascades down the tree from this node, calling the specified function with each node. The scope (<i>this</i>) of
617 * function call will be the scope provided or the current node. The arguments to the function
618 * will be the args provided or the current node. If the function returns false at any point,
619 * the cascade is stopped on that branch.
620 * @param {Function} fn The function to call
621 * @param {Object} scope (optional) The scope of the function (defaults to current node)
622 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
624 cascade : function(fn, scope, args){
625 if(fn.call(scope || this, args || this) !== false){
626 var cs = this.childNodes;
627 for(var i = 0, len = cs.length; i < len; i++) {
628 cs[i].cascade(fn, scope, args);
634 * Interates the child nodes of this node, calling the specified function with each node. The scope (<i>this</i>) of
635 * function call will be the scope provided or the current node. The arguments to the function
636 * will be the args provided or the current node. If the function returns false at any point,
637 * the iteration stops.
638 * @param {Function} fn The function to call
639 * @param {Object} scope (optional) The scope of the function (defaults to current node)
640 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
642 eachChild : function(fn, scope, args){
643 var cs = this.childNodes;
644 for(var i = 0, len = cs.length; i < len; i++) {
645 if(fn.call(scope || this, args || cs[i]) === false){
652 * Finds the first child that has the attribute with the specified value.
653 * @param {String} attribute The attribute name
654 * @param {Mixed} value The value to search for
655 * @return {Node} The found child or null if none was found
657 findChild : function(attribute, value){
658 var cs = this.childNodes;
659 for(var i = 0, len = cs.length; i < len; i++) {
660 if(cs[i].attributes[attribute] == value){
668 * Finds the first child by a custom function. The child matches if the function passed
670 * @param {Function} fn
671 * @param {Object} scope (optional)
672 * @return {Node} The found child or null if none was found
674 findChildBy : function(fn, scope){
675 var cs = this.childNodes;
676 for(var i = 0, len = cs.length; i < len; i++) {
677 if(fn.call(scope||cs[i], cs[i]) === true){
685 * Sorts this nodes children using the supplied sort function
686 * @param {Function} fn
687 * @param {Object} scope (optional)
689 sort : function(fn, scope){
690 var cs = this.childNodes;
693 var sortFn = scope ? function(){fn.apply(scope, arguments);} : fn;
695 for(var i = 0; i < len; i++){
697 n.previousSibling = cs[i-1];
698 n.nextSibling = cs[i+1];
700 this.setFirstChild(n);
703 this.setLastChild(n);
710 * Returns true if this node is an ancestor (at any point) of the passed node.
714 contains : function(node){
715 return node.isAncestor(this);
719 * Returns true if the passed node is an ancestor (at any point) of this node.
723 isAncestor : function(node){
724 var p = this.parentNode;
734 toString : function(){
735 return "[Node"+(this.id?" "+this.id:"")+"]";