-/**\r
- * @class Ext.data.Tree\r
- * @extends Ext.util.Observable\r
- * Represents a tree data structure and bubbles all the events for its nodes. The nodes\r
- * in the tree have most standard DOM functionality.\r
- * @constructor\r
- * @param {Node} root (optional) The root node\r
- */\r
-Ext.data.Tree = function(root){\r
- this.nodeHash = {};\r
- /**\r
- * The root node for this tree\r
- * @type Node\r
- */\r
- this.root = null;\r
- if(root){\r
- this.setRootNode(root);\r
- }\r
- this.addEvents(\r
- /**\r
- * @event append\r
- * Fires when a new child node is appended to a node in this tree.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} parent The parent node\r
- * @param {Node} node The newly appended node\r
- * @param {Number} index The index of the newly appended node\r
- */\r
- "append",\r
- /**\r
- * @event remove\r
- * Fires when a child node is removed from a node in this tree.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} parent The parent node\r
- * @param {Node} node The child node removed\r
- */\r
- "remove",\r
- /**\r
- * @event move\r
- * Fires when a node is moved to a new location in the tree\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} node The node moved\r
- * @param {Node} oldParent The old parent of this node\r
- * @param {Node} newParent The new parent of this node\r
- * @param {Number} index The index it was moved to\r
- */\r
- "move",\r
- /**\r
- * @event insert\r
- * Fires when a new child node is inserted in a node in this tree.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} parent The parent node\r
- * @param {Node} node The child node inserted\r
- * @param {Node} refNode The child node the node was inserted before\r
- */\r
- "insert",\r
- /**\r
- * @event beforeappend\r
- * Fires before a new child is appended to a node in this tree, return false to cancel the append.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} parent The parent node\r
- * @param {Node} node The child node to be appended\r
- */\r
- "beforeappend",\r
- /**\r
- * @event beforeremove\r
- * Fires before a child is removed from a node in this tree, return false to cancel the remove.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} parent The parent node\r
- * @param {Node} node The child node to be removed\r
- */\r
- "beforeremove",\r
- /**\r
- * @event beforemove\r
- * Fires before a node is moved to a new location in the tree. Return false to cancel the move.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} node The node being moved\r
- * @param {Node} oldParent The parent of the node\r
- * @param {Node} newParent The new parent the node is moving to\r
- * @param {Number} index The index it is being moved to\r
- */\r
- "beforemove",\r
- /**\r
- * @event beforeinsert\r
- * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} parent The parent node\r
- * @param {Node} node The child node to be inserted\r
- * @param {Node} refNode The child node the node is being inserted before\r
- */\r
- "beforeinsert"\r
- );\r
-\r
- Ext.data.Tree.superclass.constructor.call(this);\r
-};\r
-\r
-Ext.extend(Ext.data.Tree, Ext.util.Observable, {\r
- /**\r
- * @cfg {String} pathSeparator\r
- * The token used to separate paths in node ids (defaults to '/').\r
- */\r
- pathSeparator: "/",\r
-\r
- // private\r
- proxyNodeEvent : function(){\r
- return this.fireEvent.apply(this, arguments);\r
- },\r
-\r
- /**\r
- * Returns the root node for this tree.\r
- * @return {Node}\r
- */\r
- getRootNode : function(){\r
- return this.root;\r
- },\r
-\r
- /**\r
- * Sets the root node for this tree.\r
- * @param {Node} node\r
- * @return {Node}\r
- */\r
- setRootNode : function(node){\r
- this.root = node;\r
- node.ownerTree = this;\r
- node.isRoot = true;\r
- this.registerNode(node);\r
- return node;\r
- },\r
-\r
- /**\r
- * Gets a node in this tree by its id.\r
- * @param {String} id\r
- * @return {Node}\r
- */\r
- getNodeById : function(id){\r
- return this.nodeHash[id];\r
- },\r
-\r
- // private\r
- registerNode : function(node){\r
- this.nodeHash[node.id] = node;\r
- },\r
-\r
- // private\r
- unregisterNode : function(node){\r
- delete this.nodeHash[node.id];\r
- },\r
-\r
- toString : function(){\r
- return "[Tree"+(this.id?" "+this.id:"")+"]";\r
- }\r
-});\r
-\r
-/**\r
- * @class Ext.data.Node\r
- * @extends Ext.util.Observable\r
- * @cfg {Boolean} leaf true if this node is a leaf and does not have children\r
- * @cfg {String} id The id for this node. If one is not specified, one is generated.\r
- * @constructor\r
- * @param {Object} attributes The attributes/config for the node\r
- */\r
-Ext.data.Node = function(attributes){\r
- /**\r
- * The attributes supplied for the node. You can use this property to access any custom attributes you supplied.\r
- * @type {Object}\r
- */\r
- this.attributes = attributes || {};\r
- this.leaf = this.attributes.leaf;\r
- /**\r
- * The node id. @type String\r
- */\r
- this.id = this.attributes.id;\r
- if(!this.id){\r
- this.id = Ext.id(null, "xnode-");\r
- this.attributes.id = this.id;\r
- }\r
- /**\r
- * All child nodes of this node. @type Array\r
- */\r
- this.childNodes = [];\r
- if(!this.childNodes.indexOf){ // indexOf is a must\r
- this.childNodes.indexOf = function(o){\r
- for(var i = 0, len = this.length; i < len; i++){\r
- if(this[i] == o){\r
- return i;\r
- }\r
- }\r
- return -1;\r
- };\r
- }\r
- /**\r
- * The parent node for this node. @type Node\r
- */\r
- this.parentNode = null;\r
- /**\r
- * The first direct child node of this node, or null if this node has no child nodes. @type Node\r
- */\r
- this.firstChild = null;\r
- /**\r
- * The last direct child node of this node, or null if this node has no child nodes. @type Node\r
- */\r
- this.lastChild = null;\r
- /**\r
- * The node immediately preceding this node in the tree, or null if there is no sibling node. @type Node\r
- */\r
- this.previousSibling = null;\r
- /**\r
- * The node immediately following this node in the tree, or null if there is no sibling node. @type Node\r
- */\r
- this.nextSibling = null;\r
-\r
- this.addEvents({\r
- /**\r
- * @event append\r
- * Fires when a new child node is appended\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} node The newly appended node\r
- * @param {Number} index The index of the newly appended node\r
- */\r
- "append" : true,\r
- /**\r
- * @event remove\r
- * Fires when a child node is removed\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} node The removed node\r
- */\r
- "remove" : true,\r
- /**\r
- * @event move\r
- * Fires when this node is moved to a new location in the tree\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} oldParent The old parent of this node\r
- * @param {Node} newParent The new parent of this node\r
- * @param {Number} index The index it was moved to\r
- */\r
- "move" : true,\r
- /**\r
- * @event insert\r
- * Fires when a new child node is inserted.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} node The child node inserted\r
- * @param {Node} refNode The child node the node was inserted before\r
- */\r
- "insert" : true,\r
- /**\r
- * @event beforeappend\r
- * Fires before a new child is appended, return false to cancel the append.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} node The child node to be appended\r
- */\r
- "beforeappend" : true,\r
- /**\r
- * @event beforeremove\r
- * Fires before a child is removed, return false to cancel the remove.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} node The child node to be removed\r
- */\r
- "beforeremove" : true,\r
- /**\r
- * @event beforemove\r
- * Fires before this node is moved to a new location in the tree. Return false to cancel the move.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} oldParent The parent of this node\r
- * @param {Node} newParent The new parent this node is moving to\r
- * @param {Number} index The index it is being moved to\r
- */\r
- "beforemove" : true,\r
- /**\r
- * @event beforeinsert\r
- * Fires before a new child is inserted, return false to cancel the insert.\r
- * @param {Tree} tree The owner tree\r
- * @param {Node} this This node\r
- * @param {Node} node The child node to be inserted\r
- * @param {Node} refNode The child node the node is being inserted before\r
- */\r
- "beforeinsert" : true\r
- });\r
- this.listeners = this.attributes.listeners;\r
- Ext.data.Node.superclass.constructor.call(this);\r
-};\r
-\r
-Ext.extend(Ext.data.Node, Ext.util.Observable, {\r
- // private\r
- fireEvent : function(evtName){\r
- // first do standard event for this node\r
- if(Ext.data.Node.superclass.fireEvent.apply(this, arguments) === false){\r
- return false;\r
- }\r
- // then bubble it up to the tree if the event wasn't cancelled\r
- var ot = this.getOwnerTree();\r
- if(ot){\r
- if(ot.proxyNodeEvent.apply(ot, arguments) === false){\r
- return false;\r
- }\r
- }\r
- return true;\r
- },\r
-\r
- /**\r
- * Returns true if this node is a leaf\r
- * @return {Boolean}\r
- */\r
- isLeaf : function(){\r
- return this.leaf === true;\r
- },\r
-\r
- // private\r
- setFirstChild : function(node){\r
- this.firstChild = node;\r
- },\r
-\r
- //private\r
- setLastChild : function(node){\r
- this.lastChild = node;\r
- },\r
-\r
-\r
- /**\r
- * Returns true if this node is the last child of its parent\r
- * @return {Boolean}\r
- */\r
- isLast : function(){\r
- return (!this.parentNode ? true : this.parentNode.lastChild == this);\r
- },\r
-\r
- /**\r
- * Returns true if this node is the first child of its parent\r
- * @return {Boolean}\r
- */\r
- isFirst : function(){\r
- return (!this.parentNode ? true : this.parentNode.firstChild == this);\r
- },\r
-\r
- /**\r
- * Returns true if this node has one or more child nodes, else false.\r
- * @return {Boolean}\r
- */\r
- hasChildNodes : function(){\r
- return !this.isLeaf() && this.childNodes.length > 0;\r
- },\r
- \r
- /**\r
- * Returns true if this node has one or more child nodes, or if the <tt>expandable</tt>\r
- * node attribute is explicitly specified as true (see {@link #attributes}), otherwise returns false.\r
- * @return {Boolean}\r
- */\r
- isExpandable : function(){\r
- return this.attributes.expandable || this.hasChildNodes();\r
- },\r
-\r
- /**\r
- * Insert node(s) as the last child node of this node.\r
- * @param {Node/Array} node The node or Array of nodes to append\r
- * @return {Node} The appended node if single append, or null if an array was passed\r
- */\r
- appendChild : function(node){\r
- var multi = false;\r
- if(Ext.isArray(node)){\r
- multi = node;\r
- }else if(arguments.length > 1){\r
- multi = arguments;\r
- }\r
- // if passed an array or multiple args do them one by one\r
- if(multi){\r
- for(var i = 0, len = multi.length; i < len; i++) {\r
- this.appendChild(multi[i]);\r
- }\r
- }else{\r
- if(this.fireEvent("beforeappend", this.ownerTree, this, node) === false){\r
- return false;\r
- }\r
- var index = this.childNodes.length;\r
- var oldParent = node.parentNode;\r
- // it's a move, make sure we move it cleanly\r
- if(oldParent){\r
- if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index) === false){\r
- return false;\r
- }\r
- oldParent.removeChild(node);\r
- }\r
- index = this.childNodes.length;\r
- if(index === 0){\r
- this.setFirstChild(node);\r
- }\r
- this.childNodes.push(node);\r
- node.parentNode = this;\r
- var ps = this.childNodes[index-1];\r
- if(ps){\r
- node.previousSibling = ps;\r
- ps.nextSibling = node;\r
- }else{\r
- node.previousSibling = null;\r
- }\r
- node.nextSibling = null;\r
- this.setLastChild(node);\r
- node.setOwnerTree(this.getOwnerTree());\r
- this.fireEvent("append", this.ownerTree, this, node, index);\r
- if(oldParent){\r
- node.fireEvent("move", this.ownerTree, node, oldParent, this, index);\r
- }\r
- return node;\r
- }\r
- },\r
-\r
- /**\r
- * Removes a child node from this node.\r
- * @param {Node} node The node to remove\r
- * @param {Boolean} destroy <tt>true</tt> to destroy the node upon removal. Defaults to <tt>false</tt>.\r
- * @return {Node} The removed node\r
- */\r
- removeChild : function(node, destroy){\r
- var index = this.childNodes.indexOf(node);\r
- if(index == -1){\r
- return false;\r
- }\r
- if(this.fireEvent("beforeremove", this.ownerTree, this, node) === false){\r
- return false;\r
- }\r
-\r
- // remove it from childNodes collection\r
- this.childNodes.splice(index, 1);\r
-\r
- // update siblings\r
- if(node.previousSibling){\r
- node.previousSibling.nextSibling = node.nextSibling;\r
- }\r
- if(node.nextSibling){\r
- node.nextSibling.previousSibling = node.previousSibling;\r
- }\r
-\r
- // update child refs\r
- if(this.firstChild == node){\r
- this.setFirstChild(node.nextSibling);\r
- }\r
- if(this.lastChild == node){\r
- this.setLastChild(node.previousSibling);\r
- }\r
-\r
- node.clear();\r
- this.fireEvent("remove", this.ownerTree, this, node);\r
- if(destroy){\r
- node.destroy();\r
- }\r
- return node;\r
- },\r
- \r
- // private\r
- clear : function(destroy){\r
- // clear any references from the node\r
- this.setOwnerTree(null, destroy);\r
- this.parentNode = this.previousSibling = this.nextSibling = null\r
- if(destroy){\r
- this.firstChild = this.lastChild = null; \r
- }\r
- },\r
- \r
- /**\r
- * Destroys the node.\r
- */\r
- destroy : function(){\r
- this.purgeListeners();\r
- this.clear(true); \r
- Ext.each(this.childNodes, function(n){\r
- n.destroy();\r
- });\r
- this.childNodes = null;\r
- },\r
-\r
- /**\r
- * Inserts the first node before the second node in this nodes childNodes collection.\r
- * @param {Node} node The node to insert\r
- * @param {Node} refNode The node to insert before (if null the node is appended)\r
- * @return {Node} The inserted node\r
- */\r
- insertBefore : function(node, refNode){\r
- if(!refNode){ // like standard Dom, refNode can be null for append\r
- return this.appendChild(node);\r
- }\r
- // nothing to do\r
- if(node == refNode){\r
- return false;\r
- }\r
-\r
- if(this.fireEvent("beforeinsert", this.ownerTree, this, node, refNode) === false){\r
- return false;\r
- }\r
- var index = this.childNodes.indexOf(refNode);\r
- var oldParent = node.parentNode;\r
- var refIndex = index;\r
-\r
- // when moving internally, indexes will change after remove\r
- if(oldParent == this && this.childNodes.indexOf(node) < index){\r
- refIndex--;\r
- }\r
-\r
- // it's a move, make sure we move it cleanly\r
- if(oldParent){\r
- if(node.fireEvent("beforemove", node.getOwnerTree(), node, oldParent, this, index, refNode) === false){\r
- return false;\r
- }\r
- oldParent.removeChild(node);\r
- }\r
- if(refIndex === 0){\r
- this.setFirstChild(node);\r
- }\r
- this.childNodes.splice(refIndex, 0, node);\r
- node.parentNode = this;\r
- var ps = this.childNodes[refIndex-1];\r
- if(ps){\r
- node.previousSibling = ps;\r
- ps.nextSibling = node;\r
- }else{\r
- node.previousSibling = null;\r
- }\r
- node.nextSibling = refNode;\r
- refNode.previousSibling = node;\r
- node.setOwnerTree(this.getOwnerTree());\r
- this.fireEvent("insert", this.ownerTree, this, node, refNode);\r
- if(oldParent){\r
- node.fireEvent("move", this.ownerTree, node, oldParent, this, refIndex, refNode);\r
- }\r
- return node;\r
- },\r
-\r
- /**\r
- * Removes this node from its parent\r
- * @param {Boolean} destroy <tt>true</tt> to destroy the node upon removal. Defaults to <tt>false</tt>.\r
- * @return {Node} this\r
- */\r
- remove : function(destroy){\r
- this.parentNode.removeChild(this, destroy);\r
- return this;\r
- },\r
-\r
- /**\r
- * Returns the child node at the specified index.\r
- * @param {Number} index\r
- * @return {Node}\r
- */\r
- item : function(index){\r
- return this.childNodes[index];\r
- },\r
-\r
- /**\r
- * Replaces one child node in this node with another.\r
- * @param {Node} newChild The replacement node\r
- * @param {Node} oldChild The node to replace\r
- * @return {Node} The replaced node\r
- */\r
- replaceChild : function(newChild, oldChild){\r
- var s = oldChild ? oldChild.nextSibling : null;\r
- this.removeChild(oldChild);\r
- this.insertBefore(newChild, s);\r
- return oldChild;\r
- },\r
-\r
- /**\r
- * Returns the index of a child node\r
- * @param {Node} node\r
- * @return {Number} The index of the node or -1 if it was not found\r
- */\r
- indexOf : function(child){\r
- return this.childNodes.indexOf(child);\r
- },\r
-\r
- /**\r
- * Returns the tree this node is in.\r
- * @return {Tree}\r
- */\r
- getOwnerTree : function(){\r
- // if it doesn't have one, look for one\r
- if(!this.ownerTree){\r
- var p = this;\r
- while(p){\r
- if(p.ownerTree){\r
- this.ownerTree = p.ownerTree;\r
- break;\r
- }\r
- p = p.parentNode;\r
- }\r
- }\r
- return this.ownerTree;\r
- },\r
-\r
- /**\r
- * Returns depth of this node (the root node has a depth of 0)\r
- * @return {Number}\r
- */\r
- getDepth : function(){\r
- var depth = 0;\r
- var p = this;\r
- while(p.parentNode){\r
- ++depth;\r
- p = p.parentNode;\r
- }\r
- return depth;\r
- },\r
-\r
- // private\r
- setOwnerTree : function(tree, destroy){\r
- // if it is a move, we need to update everyone\r
- if(tree != this.ownerTree){\r
- if(this.ownerTree){\r
- this.ownerTree.unregisterNode(this);\r
- }\r
- this.ownerTree = tree;\r
- // If we're destroying, we don't need to recurse since it will be called on each child node\r
- if(destroy !== true){\r
- Ext.each(this.childNodes, function(n){\r
- n.setOwnerTree(tree);\r
- });\r
- }\r
- if(tree){\r
- tree.registerNode(this);\r
- }\r
- }\r
- },\r
- \r
- /**\r
- * Changes the id of this node.\r
- * @param {String} id The new id for the node.\r
- */\r
- setId: function(id){\r
- if(id !== this.id){\r
- var t = this.ownerTree;\r
- if(t){\r
- t.unregisterNode(this);\r
- }\r
- this.id = this.attributes.id = id;\r
- if(t){\r
- t.registerNode(this);\r
- }\r
- this.onIdChange(id);\r
- }\r
- },\r
- \r
- // private\r
- onIdChange: Ext.emptyFn,\r
-\r
- /**\r
- * Returns the path for this node. The path can be used to expand or select this node programmatically.\r
- * @param {String} attr (optional) The attr to use for the path (defaults to the node's id)\r
- * @return {String} The path\r
- */\r
- getPath : function(attr){\r
- attr = attr || "id";\r
- var p = this.parentNode;\r
- var b = [this.attributes[attr]];\r
- while(p){\r
- b.unshift(p.attributes[attr]);\r
- p = p.parentNode;\r
- }\r
- var sep = this.getOwnerTree().pathSeparator;\r
- return sep + b.join(sep);\r
- },\r
-\r
- /**\r
- * Bubbles up the tree from this node, calling the specified function with each node. The arguments to the function\r
- * will be the args provided or the current node. If the function returns false at any point,\r
- * the bubble is stopped.\r
- * @param {Function} fn The function to call\r
- * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.\r
- * @param {Array} args (optional) The args to call the function with (default to passing the current Node)\r
- */\r
- bubble : function(fn, scope, args){\r
- var p = this;\r
- while(p){\r
- if(fn.apply(scope || p, args || [p]) === false){\r
- break;\r
- }\r
- p = p.parentNode;\r
- }\r
- },\r
-\r
- /**\r
- * Cascades down the tree from this node, calling the specified function with each node. The arguments to the function\r
- * will be the args provided or the current node. If the function returns false at any point,\r
- * the cascade is stopped on that branch.\r
- * @param {Function} fn The function to call\r
- * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.\r
- * @param {Array} args (optional) The args to call the function with (default to passing the current Node)\r
- */\r
- cascade : function(fn, scope, args){\r
- if(fn.apply(scope || this, args || [this]) !== false){\r
- var cs = this.childNodes;\r
- for(var i = 0, len = cs.length; i < len; i++) {\r
- cs[i].cascade(fn, scope, args);\r
- }\r
- }\r
- },\r
-\r
- /**\r
- * Interates the child nodes of this node, calling the specified function with each node. The arguments to the function\r
- * will be the args provided or the current node. If the function returns false at any point,\r
- * the iteration stops.\r
- * @param {Function} fn The function to call\r
- * @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
- * @param {Array} args (optional) The args to call the function with (default to passing the current Node)\r
- */\r
- eachChild : function(fn, scope, args){\r
- var cs = this.childNodes;\r
- for(var i = 0, len = cs.length; i < len; i++) {\r
- if(fn.apply(scope || this, args || [cs[i]]) === false){\r
- break;\r
- }\r
- }\r
- },\r
-\r
- /**\r
- * Finds the first child that has the attribute with the specified value.\r
- * @param {String} attribute The attribute name\r
- * @param {Mixed} value The value to search for\r
- * @return {Node} The found child or null if none was found\r
- */\r
- findChild : function(attribute, value){\r
- var cs = this.childNodes;\r
- for(var i = 0, len = cs.length; i < len; i++) {\r
- if(cs[i].attributes[attribute] == value){\r
- return cs[i];\r
- }\r
- }\r
- return null;\r
- },\r
-\r
- /**\r
- * Finds the first child by a custom function. The child matches if the function passed returns <code>true</code>.\r
- * @param {Function} fn A function which must return <code>true</code> if the passed Node is the required Node.\r
- * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the Node being tested.\r
- * @return {Node} The found child or null if none was found\r
- */\r
- findChildBy : function(fn, scope){\r
- var cs = this.childNodes;\r
- for(var i = 0, len = cs.length; i < len; i++) {\r
- if(fn.call(scope||cs[i], cs[i]) === true){\r
- return cs[i];\r
- }\r
- }\r
- return null;\r
- },\r
-\r
- /**\r
- * Sorts this nodes children using the supplied sort function.\r
- * @param {Function} fn A function which, when passed two Nodes, returns -1, 0 or 1 depending upon required sort order.\r
- * @param {Object} scope (optional)The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.\r
- */\r
- sort : function(fn, scope){\r
- var cs = this.childNodes;\r
- var len = cs.length;\r
- if(len > 0){\r
- var sortFn = scope ? function(){fn.apply(scope, arguments);} : fn;\r
- cs.sort(sortFn);\r
- for(var i = 0; i < len; i++){\r
- var n = cs[i];\r
- n.previousSibling = cs[i-1];\r
- n.nextSibling = cs[i+1];\r
- if(i === 0){\r
- this.setFirstChild(n);\r
- }\r
- if(i == len-1){\r
- this.setLastChild(n);\r
- }\r
- }\r
- }\r
- },\r
-\r
- /**\r
- * Returns true if this node is an ancestor (at any point) of the passed node.\r
- * @param {Node} node\r
- * @return {Boolean}\r
- */\r
- contains : function(node){\r
- return node.isAncestor(this);\r
- },\r
-\r
- /**\r
- * Returns true if the passed node is an ancestor (at any point) of this node.\r
- * @param {Node} node\r
- * @return {Boolean}\r
- */\r
- isAncestor : function(node){\r
- var p = this.parentNode;\r
- while(p){\r
- if(p == node){\r
- return true;\r
- }\r
- p = p.parentNode;\r
- }\r
- return false;\r
- },\r
-\r
- toString : function(){\r
- return "[Node"+(this.id?" "+this.id:"")+"]";\r
- }\r
+Ext.define('Ext.data.Tree', {
+ alias: 'data.tree',
+
+ mixins: {
+ observable: "Ext.util.Observable"
+ },
+
+ /**
+ * The root node for this tree
+ * @type Node
+ */
+ root: null,
+
+ constructor: function(root) {
+ var me = this;
+
+ me.nodeHash = {};
+
+ me.mixins.observable.constructor.call(me);
+
+ if (root) {
+ me.setRootNode(root);
+ }
+ },
+
+ /**
+ * Returns the root node for this tree.
+ * @return {Ext.data.NodeInterface}
+ */
+ getRootNode : function() {
+ return this.root;
+ },
+
+ /**
+ * Sets the root node for this tree.
+ * @param {Ext.data.NodeInterface} node
+ * @return {Ext.data.NodeInterface} The root node
+ */
+ setRootNode : function(node) {
+ var me = this;
+
+ me.root = node;
+ Ext.data.NodeInterface.decorate(node);
+
+ if (me.fireEvent('beforeappend', null, node) !== false) {
+ node.set('root', true);
+ node.updateInfo();
+
+ me.relayEvents(node, [
+ /**
+ * @event append
+ * Fires when a new child node is appended to a node in this tree.
+ * @param {Tree} tree The owner tree
+ * @param {Node} parent The parent node
+ * @param {Node} node The newly appended node
+ * @param {Number} index The index of the newly appended node
+ */
+ "append",
+
+ /**
+ * @event remove
+ * Fires when a child node is removed from a node in this tree.
+ * @param {Tree} tree The owner tree
+ * @param {Node} parent The parent node
+ * @param {Node} node The child node removed
+ */
+ "remove",
+
+ /**
+ * @event move
+ * Fires when a node is moved to a new location in the tree
+ * @param {Tree} tree The owner tree
+ * @param {Node} node The node moved
+ * @param {Node} oldParent The old parent of this node
+ * @param {Node} newParent The new parent of this node
+ * @param {Number} index The index it was moved to
+ */
+ "move",
+
+ /**
+ * @event insert
+ * Fires when a new child node is inserted in a node in this tree.
+ * @param {Tree} tree The owner tree
+ * @param {Node} parent The parent node
+ * @param {Node} node The child node inserted
+ * @param {Node} refNode The child node the node was inserted before
+ */
+ "insert",
+
+ /**
+ * @event beforeappend
+ * Fires before a new child is appended to a node in this tree, return false to cancel the append.
+ * @param {Tree} tree The owner tree
+ * @param {Node} parent The parent node
+ * @param {Node} node The child node to be appended
+ */
+ "beforeappend",
+
+ /**
+ * @event beforeremove
+ * Fires before a child is removed from a node in this tree, return false to cancel the remove.
+ * @param {Tree} tree The owner tree
+ * @param {Node} parent The parent node
+ * @param {Node} node The child node to be removed
+ */
+ "beforeremove",
+
+ /**
+ * @event beforemove
+ * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
+ * @param {Tree} tree The owner tree
+ * @param {Node} node The node being moved
+ * @param {Node} oldParent The parent of the node
+ * @param {Node} newParent The new parent the node is moving to
+ * @param {Number} index The index it is being moved to
+ */
+ "beforemove",
+
+ /**
+ * @event beforeinsert
+ * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
+ * @param {Tree} tree The owner tree
+ * @param {Node} parent The parent node
+ * @param {Node} node The child node to be inserted
+ * @param {Node} refNode The child node the node is being inserted before
+ */
+ "beforeinsert",
+
+ /**
+ * @event expand
+ * Fires when this node is expanded.
+ * @param {Node} this The expanding node
+ */
+ "expand",
+
+ /**
+ * @event collapse
+ * Fires when this node is collapsed.
+ * @param {Node} this The collapsing node
+ */
+ "collapse",
+
+ /**
+ * @event beforeexpand
+ * Fires before this node is expanded.
+ * @param {Node} this The expanding node
+ */
+ "beforeexpand",
+
+ /**
+ * @event beforecollapse
+ * Fires before this node is collapsed.
+ * @param {Node} this The collapsing node
+ */
+ "beforecollapse" ,
+
+ /**
+ * @event rootchange
+ * Fires whenever the root node is changed in the tree.
+ * @param {Ext.data.Model} root The new root
+ */
+ "rootchange"
+ ]);
+
+ node.on({
+ scope: me,
+ insert: me.onNodeInsert,
+ append: me.onNodeAppend,
+ remove: me.onNodeRemove
+ });
+
+ me.registerNode(node);
+ me.fireEvent('append', null, node);
+ me.fireEvent('rootchange', node);
+ }
+
+ return node;
+ },
+
+ /**
+ * Flattens all the nodes in the tree into an array.
+ * @private
+ * @return {Array} The flattened nodes.
+ */
+ flatten: function(){
+ var nodes = [],
+ hash = this.nodeHash,
+ key;
+
+ for (key in hash) {
+ if (hash.hasOwnProperty(key)) {
+ nodes.push(hash[key]);
+ }
+ }
+ return nodes;
+ },
+
+ /**
+ * Fired when a node is inserted into the root or one of it's children
+ * @private
+ * @param {Ext.data.NodeInterface} parent The parent node
+ * @param {Ext.data.NodeInterface} node The inserted node
+ */
+ onNodeInsert: function(parent, node) {
+ this.registerNode(node);
+ },
+
+ /**
+ * Fired when a node is appended into the root or one of it's children
+ * @private
+ * @param {Ext.data.NodeInterface} parent The parent node
+ * @param {Ext.data.NodeInterface} node The appended node
+ */
+ onNodeAppend: function(parent, node) {
+ this.registerNode(node);
+ },
+
+ /**
+ * Fired when a node is removed from the root or one of it's children
+ * @private
+ * @param {Ext.data.NodeInterface} parent The parent node
+ * @param {Ext.data.NodeInterface} node The removed node
+ */
+ onNodeRemove: function(parent, node) {
+ this.unregisterNode(node);
+ },
+
+ /**
+ * Gets a node in this tree by its id.
+ * @param {String} id
+ * @return {Ext.data.NodeInterface} The match node.
+ */
+ getNodeById : function(id) {
+ return this.nodeHash[id];
+ },
+
+ /**
+ * Registers a node with the tree
+ * @private
+ * @param {Ext.data.NodeInterface} The node to register
+ */
+ registerNode : function(node) {
+ this.nodeHash[node.getId() || node.internalId] = node;
+ },
+
+ /**
+ * Unregisters a node with the tree
+ * @private
+ * @param {Ext.data.NodeInterface} The node to unregister
+ */
+ unregisterNode : function(node) {
+ delete this.nodeHash[node.getId() || node.internalId];
+ },
+
+ /**
+ * Sorts this tree
+ * @private
+ * @param {Function} sorterFn The function to use for sorting
+ * @param {Boolean} recursive True to perform recursive sorting
+ */
+ sort: function(sorterFn, recursive) {
+ this.getRootNode().sort(sorterFn, recursive);
+ },
+
+ /**
+ * Filters this tree
+ * @private
+ * @param {Function} sorterFn The function to use for filtering
+ * @param {Boolean} recursive True to perform recursive filtering
+ */
+ filter: function(filters, recursive) {
+ this.getRootNode().filter(filters, recursive);
+ }