3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.data.Tree"></div>/**
\r
9 * @class Ext.data.Tree
\r
10 * @extends Ext.util.Observable
\r
11 * Represents a tree data structure and bubbles all the events for its nodes. The nodes
\r
12 * in the tree have most standard DOM functionality.
\r
14 * @param {Node} root (optional) The root node
\r
16 Ext.data.Tree = function(root){
\r
18 <div id="prop-Ext.data.Tree-root"></div>/**
\r
19 * The root node for this tree
\r
24 this.setRootNode(root);
\r
27 <div id="event-Ext.data.Tree-append"></div>/**
\r
29 * Fires when a new child node is appended to a node in this tree.
\r
30 * @param {Tree} tree The owner tree
\r
31 * @param {Node} parent The parent node
\r
32 * @param {Node} node The newly appended node
\r
33 * @param {Number} index The index of the newly appended node
\r
36 <div id="event-Ext.data.Tree-remove"></div>/**
\r
38 * Fires when a child node is removed from a node in this tree.
\r
39 * @param {Tree} tree The owner tree
\r
40 * @param {Node} parent The parent node
\r
41 * @param {Node} node The child node removed
\r
44 <div id="event-Ext.data.Tree-move"></div>/**
\r
46 * Fires when a node is moved to a new location in the tree
\r
47 * @param {Tree} tree The owner tree
\r
48 * @param {Node} node The node moved
\r
49 * @param {Node} oldParent The old parent of this node
\r
50 * @param {Node} newParent The new parent of this node
\r
51 * @param {Number} index The index it was moved to
\r
54 <div id="event-Ext.data.Tree-insert"></div>/**
\r
56 * Fires when a new child node is inserted in a node in this tree.
\r
57 * @param {Tree} tree The owner tree
\r
58 * @param {Node} parent The parent node
\r
59 * @param {Node} node The child node inserted
\r
60 * @param {Node} refNode The child node the node was inserted before
\r
63 <div id="event-Ext.data.Tree-beforeappend"></div>/**
\r
64 * @event beforeappend
\r
65 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
\r
66 * @param {Tree} tree The owner tree
\r
67 * @param {Node} parent The parent node
\r
68 * @param {Node} node The child node to be appended
\r
71 <div id="event-Ext.data.Tree-beforeremove"></div>/**
\r
72 * @event beforeremove
\r
73 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
\r
74 * @param {Tree} tree The owner tree
\r
75 * @param {Node} parent The parent node
\r
76 * @param {Node} node The child node to be removed
\r
79 <div id="event-Ext.data.Tree-beforemove"></div>/**
\r
81 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
\r
82 * @param {Tree} tree The owner tree
\r
83 * @param {Node} node The node being moved
\r
84 * @param {Node} oldParent The parent of the node
\r
85 * @param {Node} newParent The new parent the node is moving to
\r
86 * @param {Number} index The index it is being moved to
\r
89 <div id="event-Ext.data.Tree-beforeinsert"></div>/**
\r
90 * @event beforeinsert
\r
91 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
\r
92 * @param {Tree} tree The owner tree
\r
93 * @param {Node} parent The parent node
\r
94 * @param {Node} node The child node to be inserted
\r
95 * @param {Node} refNode The child node the node is being inserted before
\r
100 Ext.data.Tree.superclass.constructor.call(this);
\r
103 Ext.extend(Ext.data.Tree, Ext.util.Observable, {
\r
104 <div id="cfg-Ext.data.Tree-pathSeparator"></div>/**
\r
105 * @cfg {String} pathSeparator
\r
106 * The token used to separate paths in node ids (defaults to '/').
\r
108 pathSeparator: "/",
\r
111 proxyNodeEvent : function(){
\r
112 return this.fireEvent.apply(this, arguments);
\r
115 <div id="method-Ext.data.Tree-getRootNode"></div>/**
\r
116 * Returns the root node for this tree.
\r
119 getRootNode : function(){
\r
123 <div id="method-Ext.data.Tree-setRootNode"></div>/**
\r
124 * Sets the root node for this tree.
\r
125 * @param {Node} node
\r
128 setRootNode : function(node){
\r
130 node.ownerTree = this;
\r
131 node.isRoot = true;
\r
132 this.registerNode(node);
\r
136 <div id="method-Ext.data.Tree-getNodeById"></div>/**
\r
137 * Gets a node in this tree by its id.
\r
138 * @param {String} id
\r
141 getNodeById : function(id){
\r
142 return this.nodeHash[id];
\r
146 registerNode : function(node){
\r
147 this.nodeHash[node.id] = node;
\r
151 unregisterNode : function(node){
\r
152 delete this.nodeHash[node.id];
\r
155 toString : function(){
\r
156 return "[Tree"+(this.id?" "+this.id:"")+"]";
\r
160 <div id="cls-Ext.data.Node"></div>/**
\r
161 * @class Ext.data.Node
\r
162 * @extends Ext.util.Observable
\r
163 * @cfg {Boolean} leaf true if this node is a leaf and does not have children
\r
164 * @cfg {String} id The id for this node. If one is not specified, one is generated.
\r
166 * @param {Object} attributes The attributes/config for the node
\r
168 Ext.data.Node = function(attributes){
\r
170 * The attributes supplied for the node. You can use this property to access any custom attributes you supplied.
\r
173 this.attributes = attributes || {};
\r
174 this.leaf = this.attributes.leaf;
\r
176 * The node id. @type String
\r
178 this.id = this.attributes.id;
\r
180 this.id = Ext.id(null, "xnode-");
\r
181 this.attributes.id = this.id;
\r
184 * All child nodes of this node. @type Array
\r
186 this.childNodes = [];
\r
187 if(!this.childNodes.indexOf){ // indexOf is a must
\r
188 this.childNodes.indexOf = function(o){
\r
189 for(var i = 0, len = this.length; i < len; i++){
\r
198 * The parent node for this node. @type Node
\r
200 this.parentNode = null;
\r
202 * The first direct child node of this node, or null if this node has no child nodes. @type Node
\r
204 this.firstChild = null;
\r
206 * The last direct child node of this node, or null if this node has no child nodes. @type Node
\r
208 this.lastChild = null;
\r
210 * The node immediately preceding this node in the tree, or null if there is no sibling node. @type Node
\r
212 this.previousSibling = null;
\r
214 * The node immediately following this node in the tree, or null if there is no sibling node. @type Node
\r
216 this.nextSibling = null;
\r
221 * Fires when a new child node is appended
\r
222 * @param {Tree} tree The owner tree
\r
223 * @param {Node} this This node
\r
224 * @param {Node} node The newly appended node
\r
225 * @param {Number} index The index of the newly appended node
\r
230 * Fires when a child node is removed
\r
231 * @param {Tree} tree The owner tree
\r
232 * @param {Node} this This node
\r
233 * @param {Node} node The removed node
\r
238 * Fires when this node is moved to a new location in the tree
\r
239 * @param {Tree} tree The owner tree
\r
240 * @param {Node} this This node
\r
241 * @param {Node} oldParent The old parent of this node
\r
242 * @param {Node} newParent The new parent of this node
\r
243 * @param {Number} index The index it was moved to
\r
248 * Fires when a new child node is inserted.
\r
249 * @param {Tree} tree The owner tree
\r
250 * @param {Node} this This node
\r
251 * @param {Node} node The child node inserted
\r
252 * @param {Node} refNode The child node the node was inserted before
\r
256 * @event beforeappend
\r
257 * Fires before a new child is appended, return false to cancel the append.
\r
258 * @param {Tree} tree The owner tree
\r
259 * @param {Node} this This node
\r
260 * @param {Node} node The child node to be appended
\r
262 "beforeappend" : true,
\r
264 * @event beforeremove
\r
265 * Fires before a child is removed, return false to cancel the remove.
\r
266 * @param {Tree} tree The owner tree
\r
267 * @param {Node} this This node
\r
268 * @param {Node} node The child node to be removed
\r
270 "beforeremove" : true,
\r
272 * @event beforemove
\r
273 * Fires before this node is moved to a new location in the tree. Return false to cancel the move.
\r
274 * @param {Tree} tree The owner tree
\r
275 * @param {Node} this This node
\r
276 * @param {Node} oldParent The parent of this node
\r
277 * @param {Node} newParent The new parent this node is moving to
\r
278 * @param {Number} index The index it is being moved to
\r
280 "beforemove" : true,
\r
282 * @event beforeinsert
\r
283 * Fires before a new child is inserted, return false to cancel the insert.
\r
284 * @param {Tree} tree The owner tree
\r
285 * @param {Node} this This node
\r
286 * @param {Node} node The child node to be inserted
\r
287 * @param {Node} refNode The child node the node is being inserted before
\r
289 "beforeinsert" : true
\r
291 this.listeners = this.attributes.listeners;
\r
292 Ext.data.Node.superclass.constructor.call(this);
\r
295 Ext.extend(Ext.data.Node, Ext.util.Observable, {
\r
297 fireEvent : function(evtName){
\r
298 // first do standard event for this node
\r
299 if(Ext.data.Node.superclass.fireEvent.apply(this, arguments) === false){
\r
302 // then bubble it up to the tree if the event wasn't cancelled
\r
303 var ot = this.getOwnerTree();
\r
305 if(ot.proxyNodeEvent.apply(ot, arguments) === false){
\r
313 * Returns true if this node is a leaf
\r
314 * @return {Boolean}
\r
316 isLeaf : function(){
\r
317 return this.leaf === true;
\r
321 setFirstChild : function(node){
\r
322 this.firstChild = node;
\r
326 setLastChild : function(node){
\r
327 this.lastChild = node;
\r
332 * Returns true if this node is the last child of its parent
\r
333 * @return {Boolean}
\r
335 isLast : function(){
\r
336 return (!this.parentNode ? true : this.parentNode.lastChild == this);
\r
340 * Returns true if this node is the first child of its parent
\r
341 * @return {Boolean}
\r
343 isFirst : function(){
\r
344 return (!this.parentNode ? true : this.parentNode.firstChild == this);
\r
348 * Returns true if this node has one or more child nodes, else false.
\r
349 * @return {Boolean}
\r
351 hasChildNodes : function(){
\r
352 return !this.isLeaf() && this.childNodes.length > 0;
\r
356 * Returns true if this node has one or more child nodes, or if the <tt>expandable</tt>
\r
357 * node attribute is explicitly specified as true (see {@link #attributes}), otherwise returns false.
\r
358 * @return {Boolean}
\r
360 isExpandable : function(){
\r
361 return this.attributes.expandable || this.hasChildNodes();
\r
365 * Insert node(s) as the last child node of this node.
\r
366 * @param {Node/Array} node The node or Array of nodes to append
\r
367 * @return {Node} The appended node if single append, or null if an array was passed
\r
369 appendChild : function(node){
\r
371 if(Ext.isArray(node)){
\r
373 }else if(arguments.length > 1){
\r
376 // if passed an array or multiple args do them one by one
\r
378 for(var i = 0, len = multi.length; i < len; i++) {
\r
379 this.appendChild(multi[i]);
\r
382 if(this.fireEvent("beforeappend", this.ownerTree, this, node) === false){
\r
385 var index = this.childNodes.length;
\r
386 var oldParent = node.parentNode;
\r
387 // it's a move, make sure we move it cleanly
\r
389 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index) === false){
\r
392 oldParent.removeChild(node);
\r
394 index = this.childNodes.length;
\r
396 this.setFirstChild(node);
\r
398 this.childNodes.push(node);
\r
399 node.parentNode = this;
\r
400 var ps = this.childNodes[index-1];
\r
402 node.previousSibling = ps;
\r
403 ps.nextSibling = node;
\r
405 node.previousSibling = null;
\r
407 node.nextSibling = null;
\r
408 this.setLastChild(node);
\r
409 node.setOwnerTree(this.getOwnerTree());
\r
410 this.fireEvent("append", this.ownerTree, this, node, index);
\r
412 node.fireEvent("move", this.ownerTree, node, oldParent, this, index);
\r
419 * Removes a child node from this node.
\r
420 * @param {Node} node The node to remove
\r
421 * @return {Node} The removed node
\r
423 removeChild : function(node){
\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
451 node.setOwnerTree(null);
\r
452 // clear any references from the node
\r
453 node.parentNode = null;
\r
454 node.previousSibling = null;
\r
455 node.nextSibling = null;
\r
456 this.fireEvent("remove", this.ownerTree, this, node);
\r
461 * Inserts the first node before the second node in this nodes childNodes collection.
\r
462 * @param {Node} node The node to insert
\r
463 * @param {Node} refNode The node to insert before (if null the node is appended)
\r
464 * @return {Node} The inserted node
\r
466 insertBefore : function(node, refNode){
\r
467 if(!refNode){ // like standard Dom, refNode can be null for append
\r
468 return this.appendChild(node);
\r
471 if(node == refNode){
\r
475 if(this.fireEvent("beforeinsert", this.ownerTree, this, node, refNode) === false){
\r
478 var index = this.childNodes.indexOf(refNode);
\r
479 var oldParent = node.parentNode;
\r
480 var refIndex = index;
\r
482 // when moving internally, indexes will change after remove
\r
483 if(oldParent == this && this.childNodes.indexOf(node) < index){
\r
487 // it's a move, make sure we move it cleanly
\r
489 if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index, refNode) === false){
\r
492 oldParent.removeChild(node);
\r
494 if(refIndex === 0){
\r
495 this.setFirstChild(node);
\r
497 this.childNodes.splice(refIndex, 0, node);
\r
498 node.parentNode = this;
\r
499 var ps = this.childNodes[refIndex-1];
\r
501 node.previousSibling = ps;
\r
502 ps.nextSibling = node;
\r
504 node.previousSibling = null;
\r
506 node.nextSibling = refNode;
\r
507 refNode.previousSibling = node;
\r
508 node.setOwnerTree(this.getOwnerTree());
\r
509 this.fireEvent("insert", this.ownerTree, this, node, refNode);
\r
511 node.fireEvent("move", this.ownerTree, node, oldParent, this, refIndex, refNode);
\r
517 * Removes this node from its parent
\r
518 * @return {Node} this
\r
520 remove : function(){
\r
521 this.parentNode.removeChild(this);
\r
526 * Returns the child node at the specified index.
\r
527 * @param {Number} index
\r
530 item : function(index){
\r
531 return this.childNodes[index];
\r
535 * Replaces one child node in this node with another.
\r
536 * @param {Node} newChild The replacement node
\r
537 * @param {Node} oldChild The node to replace
\r
538 * @return {Node} The replaced node
\r
540 replaceChild : function(newChild, oldChild){
\r
541 var s = oldChild ? oldChild.nextSibling : null;
\r
542 this.removeChild(oldChild);
\r
543 this.insertBefore(newChild, s);
\r
548 * Returns the index of a child node
\r
549 * @param {Node} node
\r
550 * @return {Number} The index of the node or -1 if it was not found
\r
552 indexOf : function(child){
\r
553 return this.childNodes.indexOf(child);
\r
557 * Returns the tree this node is in.
\r
560 getOwnerTree : function(){
\r
561 // if it doesn't have one, look for one
\r
562 if(!this.ownerTree){
\r
566 this.ownerTree = p.ownerTree;
\r
572 return this.ownerTree;
\r
576 * Returns depth of this node (the root node has a depth of 0)
\r
579 getDepth : function(){
\r
582 while(p.parentNode){
\r
590 setOwnerTree : function(tree){
\r
591 // if it is a move, we need to update everyone
\r
592 if(tree != this.ownerTree){
\r
593 if(this.ownerTree){
\r
594 this.ownerTree.unregisterNode(this);
\r
596 this.ownerTree = tree;
\r
597 var cs = this.childNodes;
\r
598 for(var i = 0, len = cs.length; i < len; i++) {
\r
599 cs[i].setOwnerTree(tree);
\r
602 tree.registerNode(this);
\r
608 * Changes the id of this node.
\r
609 * @param {String} id The new id for the node.
\r
611 setId: function(id){
\r
612 if(id !== this.id){
\r
613 var t = this.ownerTree;
\r
615 t.unregisterNode(this);
\r
619 t.registerNode(this);
\r
621 this.onIdChange(id);
\r
626 onIdChange: Ext.emptyFn,
\r
629 * Returns the path for this node. The path can be used to expand or select this node programmatically.
\r
630 * @param {String} attr (optional) The attr to use for the path (defaults to the node's id)
\r
631 * @return {String} The path
\r
633 getPath : function(attr){
\r
634 attr = attr || "id";
\r
635 var p = this.parentNode;
\r
636 var b = [this.attributes[attr]];
\r
638 b.unshift(p.attributes[attr]);
\r
641 var sep = this.getOwnerTree().pathSeparator;
\r
642 return sep + b.join(sep);
\r
646 * Bubbles up the tree from this node, calling the specified function with each node. The scope (<i>this</i>) of
\r
647 * function call will be the scope provided or the current node. The arguments to the function
\r
648 * will be the args provided or the current node. If the function returns false at any point,
\r
649 * the bubble is stopped.
\r
650 * @param {Function} fn The function to call
\r
651 * @param {Object} scope (optional) The scope of the function (defaults to current node)
\r
652 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
\r
654 bubble : function(fn, scope, args){
\r
657 if(fn.apply(scope || p, args || [p]) === false){
\r
665 * Cascades down the tree from this node, calling the specified function with each node. The scope (<i>this</i>) of
\r
666 * function call will be the scope provided or the current node. The arguments to the function
\r
667 * will be the args provided or the current node. If the function returns false at any point,
\r
668 * the cascade is stopped on that branch.
\r
669 * @param {Function} fn The function to call
\r
670 * @param {Object} scope (optional) The scope of the function (defaults to current node)
\r
671 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
\r
673 cascade : function(fn, scope, args){
\r
674 if(fn.apply(scope || this, args || [this]) !== false){
\r
675 var cs = this.childNodes;
\r
676 for(var i = 0, len = cs.length; i < len; i++) {
\r
677 cs[i].cascade(fn, scope, args);
\r
683 * Interates the child nodes of this node, calling the specified function with each node. The scope (<i>this</i>) of
\r
684 * function call will be the scope provided or the current node. The arguments to the function
\r
685 * will be the args provided or the current node. If the function returns false at any point,
\r
686 * the iteration stops.
\r
687 * @param {Function} fn The function to call
\r
688 * @param {Object} scope (optional) The scope of the function (defaults to current node)
\r
689 * @param {Array} args (optional) The args to call the function with (default to passing the current node)
\r
691 eachChild : function(fn, scope, args){
\r
692 var cs = this.childNodes;
\r
693 for(var i = 0, len = cs.length; i < len; i++) {
\r
694 if(fn.apply(scope || this, args || [cs[i]]) === false){
\r
701 * Finds the first child that has the attribute with the specified value.
\r
702 * @param {String} attribute The attribute name
\r
703 * @param {Mixed} value The value to search for
\r
704 * @return {Node} The found child or null if none was found
\r
706 findChild : function(attribute, value){
\r
707 var cs = this.childNodes;
\r
708 for(var i = 0, len = cs.length; i < len; i++) {
\r
709 if(cs[i].attributes[attribute] == value){
\r
717 * Finds the first child by a custom function. The child matches if the function passed
\r
719 * @param {Function} fn
\r
720 * @param {Object} scope (optional)
\r
721 * @return {Node} The found child or null if none was found
\r
723 findChildBy : function(fn, scope){
\r
724 var cs = this.childNodes;
\r
725 for(var i = 0, len = cs.length; i < len; i++) {
\r
726 if(fn.call(scope||cs[i], cs[i]) === true){
\r
734 * Sorts this nodes children using the supplied sort function
\r
735 * @param {Function} fn
\r
736 * @param {Object} scope (optional)
\r
738 sort : function(fn, scope){
\r
739 var cs = this.childNodes;
\r
740 var len = cs.length;
\r
742 var sortFn = scope ? function(){fn.apply(scope, arguments);} : fn;
\r
744 for(var i = 0; i < len; i++){
\r
746 n.previousSibling = cs[i-1];
\r
747 n.nextSibling = cs[i+1];
\r
749 this.setFirstChild(n);
\r
752 this.setLastChild(n);
\r
759 * Returns true if this node is an ancestor (at any point) of the passed node.
\r
760 * @param {Node} node
\r
761 * @return {Boolean}
\r
763 contains : function(node){
\r
764 return node.isAncestor(this);
\r
768 * Returns true if the passed node is an ancestor (at any point) of this node.
\r
769 * @param {Node} node
\r
770 * @return {Boolean}
\r
772 isAncestor : function(node){
\r
773 var p = this.parentNode;
\r
783 toString : function(){
\r
784 return "[Node"+(this.id?" "+this.id:"")+"]";
\r