3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.data.Tree
18 * This class is used as a container for a series of nodes. The nodes themselves maintain
19 * the relationship between parent/child. The tree itself acts as a manager. It gives functionality
20 * to retrieve a node by its identifier: {@link #getNodeById}.
22 * The tree also relays events from any of it's child nodes, allowing them to be handled in a
23 * centralized fashion. In general this class is not used directly, rather used internally
24 * by other parts of the framework.
27 Ext.define('Ext.data.Tree', {
31 observable: "Ext.util.Observable"
35 * The root node for this tree
41 * Creates new Tree object.
42 * @param {Node} root (optional) The root node
44 constructor: function(root) {
49 me.mixins.observable.constructor.call(me);
57 * Returns the root node for this tree.
58 * @return {Ext.data.NodeInterface}
60 getRootNode : function() {
65 * Sets the root node for this tree.
66 * @param {Ext.data.NodeInterface} node
67 * @return {Ext.data.NodeInterface} The root node
69 setRootNode : function(node) {
73 Ext.data.NodeInterface.decorate(node);
75 if (me.fireEvent('beforeappend', null, node) !== false) {
76 node.set('root', true);
79 me.relayEvents(node, [
82 * Fires when a new child node is appended to a node in this tree.
83 * @param {Tree} tree The owner tree
84 * @param {Node} parent The parent node
85 * @param {Node} node The newly appended node
86 * @param {Number} index The index of the newly appended node
92 * Fires when a child node is removed from a node in this tree.
93 * @param {Tree} tree The owner tree
94 * @param {Node} parent The parent node
95 * @param {Node} node The child node removed
101 * Fires when a node is moved to a new location in the tree
102 * @param {Tree} tree The owner tree
103 * @param {Node} node The node moved
104 * @param {Node} oldParent The old parent of this node
105 * @param {Node} newParent The new parent of this node
106 * @param {Number} index The index it was moved to
112 * Fires when a new child node is inserted in a node in this tree.
113 * @param {Tree} tree The owner tree
114 * @param {Node} parent The parent node
115 * @param {Node} node The child node inserted
116 * @param {Node} refNode The child node the node was inserted before
121 * @event beforeappend
122 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
123 * @param {Tree} tree The owner tree
124 * @param {Node} parent The parent node
125 * @param {Node} node The child node to be appended
130 * @event beforeremove
131 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
132 * @param {Tree} tree The owner tree
133 * @param {Node} parent The parent node
134 * @param {Node} node The child node to be removed
140 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
141 * @param {Tree} tree The owner tree
142 * @param {Node} node The node being moved
143 * @param {Node} oldParent The parent of the node
144 * @param {Node} newParent The new parent the node is moving to
145 * @param {Number} index The index it is being moved to
150 * @event beforeinsert
151 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
152 * @param {Tree} tree The owner tree
153 * @param {Node} parent The parent node
154 * @param {Node} node The child node to be inserted
155 * @param {Node} refNode The child node the node is being inserted before
161 * Fires when this node is expanded.
162 * @param {Node} this The expanding node
168 * Fires when this node is collapsed.
169 * @param {Node} this The collapsing node
174 * @event beforeexpand
175 * Fires before this node is expanded.
176 * @param {Node} this The expanding node
181 * @event beforecollapse
182 * Fires before this node is collapsed.
183 * @param {Node} this The collapsing node
189 * Fires whenever the root node is changed in the tree.
190 * @param {Ext.data.Model} root The new root
197 insert: me.onNodeInsert,
198 append: me.onNodeAppend,
199 remove: me.onNodeRemove
202 me.registerNode(node);
203 me.fireEvent('append', null, node);
204 me.fireEvent('rootchange', node);
211 * Flattens all the nodes in the tree into an array.
213 * @return {Array} The flattened nodes.
217 hash = this.nodeHash,
221 if (hash.hasOwnProperty(key)) {
222 nodes.push(hash[key]);
229 * Fired when a node is inserted into the root or one of it's children
231 * @param {Ext.data.NodeInterface} parent The parent node
232 * @param {Ext.data.NodeInterface} node The inserted node
234 onNodeInsert: function(parent, node) {
235 this.registerNode(node);
239 * Fired when a node is appended into the root or one of it's children
241 * @param {Ext.data.NodeInterface} parent The parent node
242 * @param {Ext.data.NodeInterface} node The appended node
244 onNodeAppend: function(parent, node) {
245 this.registerNode(node);
249 * Fired when a node is removed from the root or one of it's children
251 * @param {Ext.data.NodeInterface} parent The parent node
252 * @param {Ext.data.NodeInterface} node The removed node
254 onNodeRemove: function(parent, node) {
255 this.unregisterNode(node);
259 * Gets a node in this tree by its id.
261 * @return {Ext.data.NodeInterface} The match node.
263 getNodeById : function(id) {
264 return this.nodeHash[id];
268 * Registers a node with the tree
270 * @param {Ext.data.NodeInterface} The node to register
272 registerNode : function(node) {
273 this.nodeHash[node.getId() || node.internalId] = node;
277 * Unregisters a node with the tree
279 * @param {Ext.data.NodeInterface} The node to unregister
281 unregisterNode : function(node) {
282 delete this.nodeHash[node.getId() || node.internalId];
288 * @param {Function} sorterFn The function to use for sorting
289 * @param {Boolean} recursive True to perform recursive sorting
291 sort: function(sorterFn, recursive) {
292 this.getRootNode().sort(sorterFn, recursive);
298 * @param {Function} sorterFn The function to use for filtering
299 * @param {Boolean} recursive True to perform recursive filtering
301 filter: function(filters, recursive) {
302 this.getRootNode().filter(filters, recursive);