3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.data.Tree
\r
9 * @extends Ext.util.Observable
\r
10 * Represents a tree data structure and bubbles all the events for its nodes. The nodes
\r
11 * in the tree have most standard DOM functionality.
\r
13 * @param {Node} root (optional) The root node
\r
15 Ext.data.Tree = function(root){
\r
18 * The root node for this tree
\r
23 this.setRootNode(root);
\r
28 * Fires when a new child node is appended to a node in this tree.
\r
29 * @param {Tree} tree The owner tree
\r
30 * @param {Node} parent The parent node
\r
31 * @param {Node} node The newly appended node
\r
32 * @param {Number} index The index of the newly appended node
\r
37 * Fires when a child node is removed from a node in this tree.
\r
38 * @param {Tree} tree The owner tree
\r
39 * @param {Node} parent The parent node
\r
40 * @param {Node} node The child node removed
\r
45 * Fires when a node is moved to a new location in the tree
\r
46 * @param {Tree} tree The owner tree
\r
47 * @param {Node} node The node moved
\r
48 * @param {Node} oldParent The old parent of this node
\r
49 * @param {Node} newParent The new parent of this node
\r
50 * @param {Number} index The index it was moved to
\r
55 * Fires when a new child node is inserted in a node in this tree.
\r
56 * @param {Tree} tree The owner tree
\r
57 * @param {Node} parent The parent node
\r
58 * @param {Node} node The child node inserted
\r
59 * @param {Node} refNode The child node the node was inserted before
\r
63 * @event beforeappend
\r
64 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
\r
65 * @param {Tree} tree The owner tree
\r
66 * @param {Node} parent The parent node
\r
67 * @param {Node} node The child node to be appended
\r
71 * @event beforeremove
\r
72 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
\r
73 * @param {Tree} tree The owner tree
\r
74 * @param {Node} parent The parent node
\r
75 * @param {Node} node The child node to be removed
\r
80 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
\r
81 * @param {Tree} tree The owner tree
\r
82 * @param {Node} node The node being moved
\r
83 * @param {Node} oldParent The parent of the node
\r
84 * @param {Node} newParent The new parent the node is moving to
\r
85 * @param {Number} index The index it is being moved to
\r
89 * @event beforeinsert
\r
90 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
\r
91 * @param {Tree} tree The owner tree
\r
92 * @param {Node} parent The parent node
\r
93 * @param {Node} node The child node to be inserted
\r
94 * @param {Node} refNode The child node the node is being inserted before
\r
99 Ext.data.Tree.superclass.constructor.call(this);
\r
102 Ext.extend(Ext.data.Tree, Ext.util.Observable, {
\r
104 * @cfg {String} pathSeparator
\r
105 * The token used to separate paths in node ids (defaults to '/').
\r
107 pathSeparator: "/",
\r
110 proxyNodeEvent : function(){
\r
111 return this.fireEvent.apply(this, arguments);
\r
115 * Returns the root node for this tree.
\r
118 getRootNode : function(){
\r
123 * Sets the root node for this tree.
\r
124 * @param {Node} node
\r
127 setRootNode : function(node){
\r
129 node.ownerTree = this;
\r
130 node.isRoot = true;
\r
131 this.registerNode(node);
\r
136 * Gets a node in this tree by its id.
\r
137 * @param {String} id
\r
140 getNodeById : function(id){
\r
141 return this.nodeHash[id];
\r
145 registerNode : function(node){
\r
146 this.nodeHash[node.id] = node;
\r
150 unregisterNode : function(node){
\r
151 delete this.nodeHash[node.id];
\r
154 toString : function(){
\r
155 return "[Tree"+(this.id?" "+this.id:"")+"]";
\r
160 * @class Ext.data.Node
\r
161 * @extends Ext.util.Observable
\r
162 * @cfg {Boolean} leaf true if this node is a leaf and does not have children
\r
163 * @cfg {String} id The id for this node. If one is not specified, one is generated.
\r
165 * @param {Object} attributes The attributes/config for the node
\r
167 Ext.data.Node = function(attributes){
\r
169 * The attributes supplied for the node. You can use this property to access any custom attributes you supplied.
\r
172 this.attributes = attributes || {};
\r
173 this.leaf = this.attributes.leaf;
\r
175 * The node id. @type String
\r
177 this.id = this.attributes.id;
\r
179 this.id = Ext.id(null, "xnode-");
\r
180 this.attributes.id = this.id;
\r
183 * All child nodes of this node. @type Array
\r
185 this.childNodes = [];
\r
186 if(!this.childNodes.indexOf){ // indexOf is a must
\r
187 this.childNodes.indexOf = function(o){
\r
188 for(var i = 0, len = this.length; i < len; i++){
\r
197 * The parent node for this node. @type Node
\r
199 this.parentNode = null;
\r
201 * The first direct child node of this node, or null if this node has no child nodes. @type Node
\r
203 this.firstChild = null;
\r
205 * The last direct child node of this node, or null if this node has no child nodes. @type Node
\r
207 this.lastChild = null;
\r
209 * The node immediately preceding this node in the tree, or null if there is no sibling node. @type Node
\r
211 this.previousSibling = null;
\r
213 * The node immediately following this node in the tree, or null if there is no sibling node. @type Node
\r
215 this.nextSibling = null;
\r
220 * Fires when a new child node is appended
\r
221 * @param {Tree} tree The owner tree
\r
222 * @param {Node} this This node
\r
223 * @param {Node} node The newly appended node
\r
224 * @param {Number} index The index of the newly appended node
\r
229 * Fires when a child node is removed
\r
230 * @param {Tree} tree The owner tree
\r
231 * @param {Node} this This node
\r
232 * @param {Node} node The removed node
\r
237 * Fires when this node is moved to a new location in the tree
\r
238 * @param {Tree} tree The owner tree
\r
239 * @param {Node} this This node
\r
240 * @param {Node} oldParent The old parent of this node
\r
241 * @param {Node} newParent The new parent of this node
\r
242 * @param {Number} index The index it was moved to
\r
247 * Fires when a new child node is inserted.
\r
248 * @param {Tree} tree The owner tree
\r
249 * @param {Node} this This node
\r
250 * @param {Node} node The child node inserted
\r
251 * @param {Node} refNode The child node the node was inserted before
\r
255 * @event beforeappend
\r
256 * Fires before a new child is appended, return false to cancel the append.
\r
257 * @param {Tree} tree The owner tree
\r
258 * @param {Node} this This node
\r
259 * @param {Node} node The child node to be appended
\r
261 "beforeappend" : true,
\r
263 * @event beforeremove
\r
264 * Fires before a child is removed, return false to cancel the remove.
\r
265 * @param {Tree} tree The owner tree
\r
266 * @param {Node} this This node
\r
267 * @param {Node} node The child node to be removed
\r
269 "beforeremove" : true,
\r
271 * @event beforemove
\r
272 * Fires before this node is moved to a new location in the tree. Return false to cancel the move.
\r
273 * @param {Tree} tree The owner tree
\r
274 * @param {Node} this This node
\r
275 * @param {Node} oldParent The parent of this node
\r
276 * @param {Node} newParent The new parent this node is moving to
\r
277 * @param {Number} index The index it is being moved to
\r
279 "beforemove" : true,
\r
281 * @event beforeinsert
\r
282 * Fires before a new child is inserted, return false to cancel the insert.
\r
283 * @param {Tree} tree The owner tree
\r
284 * @param {Node} this This node
\r
285 * @param {Node} node The child node to be inserted
\r
286 * @param {Node} refNode The child node the node is being inserted before
\r
288 "beforeinsert" : true
\r
290 this.listeners = this.attributes.listeners;
\r
291 Ext.data.Node.superclass.constructor.call(this);
\r
294 Ext.extend(Ext.data.Node, Ext.util.Observable, {
\r
296 fireEvent : function(evtName){
\r
297 // first do standard event for this node
\r
298 if(Ext.data.Node.superclass.fireEvent.apply(this, arguments) === false){
\r
301 // then bubble it up to the tree if the event wasn't cancelled
\r
302 var ot = this.getOwnerTree();
\r
304 if(ot.proxyNodeEvent.apply(ot, arguments) === false){
\r
312 * Returns true if this node is a leaf
\r
313 * @return {Boolean}
\r
315 isLeaf : function(){
\r
316 return this.leaf === true;
\r
320 setFirstChild : function(node){
\r
321 this.firstChild = node;
\r
325 setLastChild : function(node){
\r
326 this.lastChild = node;
\r
331 * Returns true if this node is the last child of its parent
\r
332 * @return {Boolean}
\r
334 isLast : function(){
\r
335 return (!this.parentNode ? true : this.parentNode.lastChild == this);
\r
339 * Returns true if this node is the first child of its parent
\r
340 * @return {Boolean}
\r
342 isFirst : function(){
\r
343 return (!this.parentNode ? true : this.parentNode.firstChild == this);
\r
347 * Returns true if this node has one or more child nodes, else false.
\r
348 * @return {Boolean}
\r
350 hasChildNodes : function(){
\r
351 return !this.isLeaf() && this.childNodes.length > 0;
\r
355 * Returns true if this node has one or more child nodes, or if the <tt>expandable</tt>
\r
356 * node attribute is explicitly specified as true (see {@link #attributes}), otherwise returns false.
\r
357 * @return {Boolean}
\r
359 isExpandable : function(){
\r
360 return this.attributes.expandable || this.hasChildNodes();
\r
364 * Insert node(s) as the last child node of this node.
\r
365 * @param {Node/Array} node The node or Array of nodes to append
\r
366 * @return {Node} The appended node if single append, or null if an array was passed
\r
368 appendChild : function(node){
\r
370 if(Ext.isArray(node)){
\r
372 }else if(arguments.length > 1){
\r
375 // if passed an array or multiple args do them one by one
\r
377 for(var i = 0, len = multi.length; i < len; i++) {
\r
378 this.appendChild(multi[i]);
\r
381 if(this.fireEvent("beforeappend", this.ownerTree, this, node) === false){
\r
384 var index = this.childNodes.length;
\r
385 var oldParent = node.parentNode;
\r
386 // it's a move, make sure we move it cleanly
\r
388 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index) === false){
\r
391 oldParent.removeChild(node);
\r
393 index = this.childNodes.length;
\r
395 this.setFirstChild(node);
\r
397 this.childNodes.push(node);
\r
398 node.parentNode = this;
\r
399 var ps = this.childNodes[index-1];
\r
401 node.previousSibling = ps;
\r
402 ps.nextSibling = node;
\r
404 node.previousSibling = null;
\r
406 node.nextSibling = null;
\r
407 this.setLastChild(node);
\r
408 node.setOwnerTree(this.getOwnerTree());
\r
409 this.fireEvent("append", this.ownerTree, this, node, index);
\r
411 node.fireEvent("move", this.ownerTree, node, oldParent, this, index);
\r
418 * Removes a child node from this node.
\r
419 * @param {Node} node The node to remove
\r
420 * @param {Boolean} destroy <tt>true</tt> to destroy the node upon removal. Defaults to <tt>false</tt>.
\r
421 * @return {Node} The removed node
\r
423 removeChild : function(node, destroy){
\r
424 var index = this.childNodes.indexOf(node);
\r
428 if(this.fireEvent("beforeremove", this.ownerTree, this, node) === false){
\r
432 // remove it from childNodes collection
\r
433 this.childNodes.splice(index, 1);
\r
436 if(node.previousSibling){
\r
437 node.previousSibling.nextSibling = node.nextSibling;
\r
439 if(node.nextSibling){
\r
440 node.nextSibling.previousSibling = node.previousSibling;
\r
443 // update child refs
\r
444 if(this.firstChild == node){
\r
445 this.setFirstChild(node.nextSibling);
\r
447 if(this.lastChild == node){
\r
448 this.setLastChild(node.previousSibling);
\r
452 this.fireEvent("remove", this.ownerTree, this, node);
\r
460 clear : function(destroy){
\r
461 // clear any references from the node
\r
462 this.setOwnerTree(null, destroy);
\r
463 this.parentNode = this.previousSibling = this.nextSibling = null
\r
465 this.firstChild = this.lastChild = null;
\r
470 * Destroys the node.
\r
472 destroy : function(){
\r
473 this.purgeListeners();
\r
475 Ext.each(this.childNodes, function(n){
\r
478 this.childNodes = null;
\r
482 * Inserts the first node before the second node in this nodes childNodes collection.
\r
483 * @param {Node} node The node to insert
\r
484 * @param {Node} refNode The node to insert before (if null the node is appended)
\r
485 * @return {Node} The inserted node
\r
487 insertBefore : function(node, refNode){
\r
488 if(!refNode){ // like standard Dom, refNode can be null for append
\r
489 return this.appendChild(node);
\r
492 if(node == refNode){
\r
496 if(this.fireEvent("beforeinsert", this.ownerTree, this, node, refNode) === false){
\r
499 var index = this.childNodes.indexOf(refNode);
\r
500 var oldParent = node.parentNode;
\r
501 var refIndex = index;
\r
503 // when moving internally, indexes will change after remove
\r
504 if(oldParent == this && this.childNodes.indexOf(node) < index){
\r
508 // it's a move, make sure we move it cleanly
\r
510 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index, refNode) === false){
\r
513 oldParent.removeChild(node);
\r
515 if(refIndex === 0){
\r
516 this.setFirstChild(node);
\r
518 this.childNodes.splice(refIndex, 0, node);
\r
519 node.parentNode = this;
\r
520 var ps = this.childNodes[refIndex-1];
\r
522 node.previousSibling = ps;
\r
523 ps.nextSibling = node;
\r
525 node.previousSibling = null;
\r
527 node.nextSibling = refNode;
\r
528 refNode.previousSibling = node;
\r
529 node.setOwnerTree(this.getOwnerTree());
\r
530 this.fireEvent("insert", this.ownerTree, this, node, refNode);
\r
532 node.fireEvent("move", this.ownerTree, node, oldParent, this, refIndex, refNode);
\r
538 * Removes this node from its parent
\r
539 * @param {Boolean} destroy <tt>true</tt> to destroy the node upon removal. Defaults to <tt>false</tt>.
\r
540 * @return {Node} this
\r
542 remove : function(destroy){
\r
543 this.parentNode.removeChild(this, destroy);
\r
548 * Returns the child node at the specified index.
\r
549 * @param {Number} index
\r
552 item : function(index){
\r
553 return this.childNodes[index];
\r
557 * Replaces one child node in this node with another.
\r
558 * @param {Node} newChild The replacement node
\r
559 * @param {Node} oldChild The node to replace
\r
560 * @return {Node} The replaced node
\r
562 replaceChild : function(newChild, oldChild){
\r
563 var s = oldChild ? oldChild.nextSibling : null;
\r
564 this.removeChild(oldChild);
\r
565 this.insertBefore(newChild, s);
\r
570 * Returns the index of a child node
\r
571 * @param {Node} node
\r
572 * @return {Number} The index of the node or -1 if it was not found
\r
574 indexOf : function(child){
\r
575 return this.childNodes.indexOf(child);
\r
579 * Returns the tree this node is in.
\r
582 getOwnerTree : function(){
\r
583 // if it doesn't have one, look for one
\r
584 if(!this.ownerTree){
\r
588 this.ownerTree = p.ownerTree;
\r
594 return this.ownerTree;
\r
598 * Returns depth of this node (the root node has a depth of 0)
\r
601 getDepth : function(){
\r
604 while(p.parentNode){
\r
612 setOwnerTree : function(tree, destroy){
\r
613 // if it is a move, we need to update everyone
\r
614 if(tree != this.ownerTree){
\r
615 if(this.ownerTree){
\r
616 this.ownerTree.unregisterNode(this);
\r
618 this.ownerTree = tree;
\r
619 // If we're destroying, we don't need to recurse since it will be called on each child node
\r
620 if(destroy !== true){
\r
621 Ext.each(this.childNodes, function(n){
\r
622 n.setOwnerTree(tree);
\r
626 tree.registerNode(this);
\r
632 * Changes the id of this node.
\r
633 * @param {String} id The new id for the node.
\r
635 setId: function(id){
\r
636 if(id !== this.id){
\r
637 var t = this.ownerTree;
\r
639 t.unregisterNode(this);
\r
641 this.id = this.attributes.id = id;
\r
643 t.registerNode(this);
\r
645 this.onIdChange(id);
\r
650 onIdChange: Ext.emptyFn,
\r
653 * Returns the path for this node. The path can be used to expand or select this node programmatically.
\r
654 * @param {String} attr (optional) The attr to use for the path (defaults to the node's id)
\r
655 * @return {String} The path
\r
657 getPath : function(attr){
\r
658 attr = attr || "id";
\r
659 var p = this.parentNode;
\r
660 var b = [this.attributes[attr]];
\r
662 b.unshift(p.attributes[attr]);
\r
665 var sep = this.getOwnerTree().pathSeparator;
\r
666 return sep + b.join(sep);
\r
670 * Bubbles up the tree from this node, calling the specified function with each node. The arguments to the function
\r
671 * will be the args provided or the current node. If the function returns false at any point,
\r
672 * the bubble is stopped.
\r
673 * @param {Function} fn The function to call
\r
674 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
\r
675 * @param {Array} args (optional) The args to call the function with (default to passing the current Node)
\r
677 bubble : function(fn, scope, args){
\r
680 if(fn.apply(scope || p, args || [p]) === false){
\r
688 * Cascades down the tree from this node, calling the specified function with each node. The arguments to the function
\r
689 * will be the args provided or the current node. If the function returns false at any point,
\r
690 * the cascade is stopped on that branch.
\r
691 * @param {Function} fn The function to call
\r
692 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
\r
693 * @param {Array} args (optional) The args to call the function with (default to passing the current Node)
\r
695 cascade : function(fn, scope, args){
\r
696 if(fn.apply(scope || this, args || [this]) !== false){
\r
697 var cs = this.childNodes;
\r
698 for(var i = 0, len = cs.length; i < len; i++) {
\r
699 cs[i].cascade(fn, scope, args);
\r
705 * Interates the child nodes of this node, calling the specified function with each node. The arguments to the function
\r
706 * will be the args provided or the current node. If the function returns false at any point,
\r
707 * the iteration stops.
\r
708 * @param {Function} fn The function to call
\r
709 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node in the iteration.
\r
710 * @param {Array} args (optional) The args to call the function with (default to passing the current Node)
\r
712 eachChild : function(fn, scope, args){
\r
713 var cs = this.childNodes;
\r
714 for(var i = 0, len = cs.length; i < len; i++) {
\r
715 if(fn.apply(scope || this, args || [cs[i]]) === false){
\r
722 * Finds the first child that has the attribute with the specified value.
\r
723 * @param {String} attribute The attribute name
\r
724 * @param {Mixed} value The value to search for
\r
725 * @return {Node} The found child or null if none was found
\r
727 findChild : function(attribute, value){
\r
728 var cs = this.childNodes;
\r
729 for(var i = 0, len = cs.length; i < len; i++) {
\r
730 if(cs[i].attributes[attribute] == value){
\r
738 * Finds the first child by a custom function. The child matches if the function passed returns <code>true</code>.
\r
739 * @param {Function} fn A function which must return <code>true</code> if the passed Node is the required Node.
\r
740 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the Node being tested.
\r
741 * @return {Node} The found child or null if none was found
\r
743 findChildBy : function(fn, scope){
\r
744 var cs = this.childNodes;
\r
745 for(var i = 0, len = cs.length; i < len; i++) {
\r
746 if(fn.call(scope||cs[i], cs[i]) === true){
\r
754 * Sorts this nodes children using the supplied sort function.
\r
755 * @param {Function} fn A function which, when passed two Nodes, returns -1, 0 or 1 depending upon required sort order.
\r
756 * @param {Object} scope (optional)The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
\r
758 sort : function(fn, scope){
\r
759 var cs = this.childNodes;
\r
760 var len = cs.length;
\r
762 var sortFn = scope ? function(){fn.apply(scope, arguments);} : fn;
\r
764 for(var i = 0; i < len; i++){
\r
766 n.previousSibling = cs[i-1];
\r
767 n.nextSibling = cs[i+1];
\r
769 this.setFirstChild(n);
\r
772 this.setLastChild(n);
\r
779 * Returns true if this node is an ancestor (at any point) of the passed node.
\r
780 * @param {Node} node
\r
781 * @return {Boolean}
\r
783 contains : function(node){
\r
784 return node.isAncestor(this);
\r
788 * Returns true if the passed node is an ancestor (at any point) of this node.
\r
789 * @param {Node} node
\r
790 * @return {Boolean}
\r
792 isAncestor : function(node){
\r
793 var p = this.parentNode;
\r
803 toString : function(){
\r
804 return "[Node"+(this.id?" "+this.id:"")+"]";
\r