4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-Tree'>/**
19 </span> * @class Ext.data.Tree
21 * This class is used as a container for a series of nodes. The nodes themselves maintain
22 * the relationship between parent/child. The tree itself acts as a manager. It gives functionality
23 * to retrieve a node by its identifier: {@link #getNodeById}.
25 * The tree also relays events from any of it's child nodes, allowing them to be handled in a
26 * centralized fashion. In general this class is not used directly, rather used internally
27 * by other parts of the framework.
30 Ext.define('Ext.data.Tree', {
34 observable: "Ext.util.Observable"
37 <span id='Ext-data-Tree-property-root'> /**
38 </span> * The root node for this tree
43 <span id='Ext-data-Tree-method-constructor'> /**
44 </span> * Creates new Tree object.
45 * @param {Node} root (optional) The root node
47 constructor: function(root) {
52 me.mixins.observable.constructor.call(me);
59 <span id='Ext-data-Tree-method-getRootNode'> /**
60 </span> * Returns the root node for this tree.
61 * @return {Ext.data.NodeInterface}
63 getRootNode : function() {
67 <span id='Ext-data-Tree-method-setRootNode'> /**
68 </span> * Sets the root node for this tree.
69 * @param {Ext.data.NodeInterface} node
70 * @return {Ext.data.NodeInterface} The root node
72 setRootNode : function(node) {
76 Ext.data.NodeInterface.decorate(node);
78 if (me.fireEvent('beforeappend', null, node) !== false) {
79 node.set('root', true);
82 me.relayEvents(node, [
83 <span id='Ext-data-Tree-event-append'> /**
84 </span> * @event append
85 * Fires when a new child node is appended to a node in this tree.
86 * @param {Tree} tree The owner tree
87 * @param {Node} parent The parent node
88 * @param {Node} node The newly appended node
89 * @param {Number} index The index of the newly appended node
93 <span id='Ext-data-Tree-event-remove'> /**
94 </span> * @event remove
95 * Fires when a child node is removed from a node in this tree.
96 * @param {Tree} tree The owner tree
97 * @param {Node} parent The parent node
98 * @param {Node} node The child node removed
102 <span id='Ext-data-Tree-event-move'> /**
103 </span> * @event move
104 * Fires when a node is moved to a new location in the tree
105 * @param {Tree} tree The owner tree
106 * @param {Node} node The node moved
107 * @param {Node} oldParent The old parent of this node
108 * @param {Node} newParent The new parent of this node
109 * @param {Number} index The index it was moved to
113 <span id='Ext-data-Tree-event-insert'> /**
114 </span> * @event insert
115 * Fires when a new child node is inserted in a node in this tree.
116 * @param {Tree} tree The owner tree
117 * @param {Node} parent The parent node
118 * @param {Node} node The child node inserted
119 * @param {Node} refNode The child node the node was inserted before
123 <span id='Ext-data-Tree-event-beforeappend'> /**
124 </span> * @event beforeappend
125 * Fires before a new child is appended to a node in this tree, return false to cancel the append.
126 * @param {Tree} tree The owner tree
127 * @param {Node} parent The parent node
128 * @param {Node} node The child node to be appended
130 "beforeappend",
132 <span id='Ext-data-Tree-event-beforeremove'> /**
133 </span> * @event beforeremove
134 * Fires before a child is removed from a node in this tree, return false to cancel the remove.
135 * @param {Tree} tree The owner tree
136 * @param {Node} parent The parent node
137 * @param {Node} node The child node to be removed
139 "beforeremove",
141 <span id='Ext-data-Tree-event-beforemove'> /**
142 </span> * @event beforemove
143 * Fires before a node is moved to a new location in the tree. Return false to cancel the move.
144 * @param {Tree} tree The owner tree
145 * @param {Node} node The node being moved
146 * @param {Node} oldParent The parent of the node
147 * @param {Node} newParent The new parent the node is moving to
148 * @param {Number} index The index it is being moved to
150 "beforemove",
152 <span id='Ext-data-Tree-event-beforeinsert'> /**
153 </span> * @event beforeinsert
154 * Fires before a new child is inserted in a node in this tree, return false to cancel the insert.
155 * @param {Tree} tree The owner tree
156 * @param {Node} parent The parent node
157 * @param {Node} node The child node to be inserted
158 * @param {Node} refNode The child node the node is being inserted before
160 "beforeinsert",
162 <span id='Ext-data-Tree-event-expand'> /**
163 </span> * @event expand
164 * Fires when this node is expanded.
165 * @param {Node} this The expanding node
169 <span id='Ext-data-Tree-event-collapse'> /**
170 </span> * @event collapse
171 * Fires when this node is collapsed.
172 * @param {Node} this The collapsing node
174 "collapse",
176 <span id='Ext-data-Tree-event-beforeexpand'> /**
177 </span> * @event beforeexpand
178 * Fires before this node is expanded.
179 * @param {Node} this The expanding node
181 "beforeexpand",
183 <span id='Ext-data-Tree-event-beforecollapse'> /**
184 </span> * @event beforecollapse
185 * Fires before this node is collapsed.
186 * @param {Node} this The collapsing node
188 "beforecollapse" ,
190 <span id='Ext-data-Tree-event-rootchange'> /**
191 </span> * @event rootchange
192 * Fires whenever the root node is changed in the tree.
193 * @param {Ext.data.Model} root The new root
195 "rootchange"
200 insert: me.onNodeInsert,
201 append: me.onNodeAppend,
202 remove: me.onNodeRemove
205 me.registerNode(node);
206 me.fireEvent('append', null, node);
207 me.fireEvent('rootchange', node);
213 <span id='Ext-data-Tree-method-flatten'> /**
214 </span> * Flattens all the nodes in the tree into an array.
216 * @return {Array} The flattened nodes.
220 hash = this.nodeHash,
224 if (hash.hasOwnProperty(key)) {
225 nodes.push(hash[key]);
231 <span id='Ext-data-Tree-method-onNodeInsert'> /**
232 </span> * Fired when a node is inserted into the root or one of it's children
234 * @param {Ext.data.NodeInterface} parent The parent node
235 * @param {Ext.data.NodeInterface} node The inserted node
237 onNodeInsert: function(parent, node) {
238 this.registerNode(node);
241 <span id='Ext-data-Tree-method-onNodeAppend'> /**
242 </span> * Fired when a node is appended into the root or one of it's children
244 * @param {Ext.data.NodeInterface} parent The parent node
245 * @param {Ext.data.NodeInterface} node The appended node
247 onNodeAppend: function(parent, node) {
248 this.registerNode(node);
251 <span id='Ext-data-Tree-method-onNodeRemove'> /**
252 </span> * Fired when a node is removed from the root or one of it's children
254 * @param {Ext.data.NodeInterface} parent The parent node
255 * @param {Ext.data.NodeInterface} node The removed node
257 onNodeRemove: function(parent, node) {
258 this.unregisterNode(node);
261 <span id='Ext-data-Tree-method-getNodeById'> /**
262 </span> * Gets a node in this tree by its id.
264 * @return {Ext.data.NodeInterface} The match node.
266 getNodeById : function(id) {
267 return this.nodeHash[id];
270 <span id='Ext-data-Tree-method-registerNode'> /**
271 </span> * Registers a node with the tree
273 * @param {Ext.data.NodeInterface} The node to register
275 registerNode : function(node) {
276 this.nodeHash[node.getId() || node.internalId] = node;
279 <span id='Ext-data-Tree-method-unregisterNode'> /**
280 </span> * Unregisters a node with the tree
282 * @param {Ext.data.NodeInterface} The node to unregister
284 unregisterNode : function(node) {
285 delete this.nodeHash[node.getId() || node.internalId];
288 <span id='Ext-data-Tree-method-sort'> /**
289 </span> * Sorts this tree
291 * @param {Function} sorterFn The function to use for sorting
292 * @param {Boolean} recursive True to perform recursive sorting
294 sort: function(sorterFn, recursive) {
295 this.getRootNode().sort(sorterFn, recursive);
298 <span id='Ext-data-Tree-method-filter'> /**
299 </span> * Filters this tree
301 * @param {Function} sorterFn The function to use for filtering
302 * @param {Boolean} recursive True to perform recursive filtering
304 filter: function(filters, recursive) {
305 this.getRootNode().filter(filters, recursive);