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 * The TreeStore is a store implementation that is backed by by an {@link Ext.data.Tree}.
17 * It provides convenience methods for loading nodes, as well as the ability to use
18 * the hierarchical tree structure combined with a store. This class is generally used
19 * in conjunction with {@link Ext.tree.Panel}. This class also relays many events from
20 * the Tree for convenience.
24 * If no Model is specified, an implicit model will be created that implements {@link Ext.data.NodeInterface}.
25 * The standard Tree fields will also be copied onto the Model for maintaining their state.
27 * # Reading Nested Data
29 * For the tree to read nested data, the {@link Ext.data.reader.Reader} must be configured with a root property,
30 * so the reader can find nested data for each node. If a root is not specified, it will default to
33 Ext.define('Ext.data.TreeStore', {
34 extend: 'Ext.data.AbstractStore',
36 requires: ['Ext.data.Tree', 'Ext.data.NodeInterface', 'Ext.data.NodeStore'],
39 * @cfg {Ext.data.Model/Ext.data.NodeInterface/Object} root
40 * The root node for this store. For example:
46 * { text: "Child 1", leaf: true },
47 * { text: "Child 2", expanded: true, children: [
48 * { text: "GrandChild", leaf: true }
53 * Setting the `root` config option is the same as calling {@link #setRootNode}.
57 * @cfg {Boolean} clearOnLoad
58 * Remove previously existing child nodes before loading. Default to true.
63 * @cfg {String} nodeParam
64 * The name of the parameter sent to the server which contains the identifier of the node.
70 * @cfg {String} defaultRootId
71 * The default root id. Defaults to 'root'
73 defaultRootId: 'root',
76 * @cfg {String} defaultRootProperty
77 * The root property to specify on the reader if one is not explicitly defined.
79 defaultRootProperty: 'children',
82 * @cfg {Boolean} folderSort
83 * Set to true to automatically prepend a leaf sorter. Defaults to `undefined`.
87 constructor: function(config) {
92 config = Ext.apply({}, config);
95 * If we have no fields declare for the store, add some defaults.
96 * These will be ignored if a model is explicitly specified.
98 fields = config.fields || me.fields;
100 config.fields = [{name: 'text', type: 'string'}];
103 me.callParent([config]);
105 // We create our data tree.
106 me.tree = Ext.create('Ext.data.Tree');
108 me.relayEvents(me.tree, [
111 * Fires when a new child node is appended to a node in this store's tree.
112 * @param {Tree} tree The owner tree
113 * @param {Node} parent The parent node
114 * @param {Node} node The newly appended node
115 * @param {Number} index The index of the newly appended node
121 * Fires when a child node is removed from a node in this store's tree.
122 * @param {Tree} tree The owner tree
123 * @param {Node} parent The parent node
124 * @param {Node} node The child node removed
130 * Fires when a node is moved to a new location in the store's tree
131 * @param {Tree} tree The owner tree
132 * @param {Node} node The node moved
133 * @param {Node} oldParent The old parent of this node
134 * @param {Node} newParent The new parent of this node
135 * @param {Number} index The index it was moved to
141 * Fires when a new child node is inserted in a node in this store's tree.
142 * @param {Tree} tree The owner tree
143 * @param {Node} parent The parent node
144 * @param {Node} node The child node inserted
145 * @param {Node} refNode The child node the node was inserted before
150 * @event beforeappend
151 * Fires before a new child is appended to a node in this store's tree, return false to cancel the append.
152 * @param {Tree} tree The owner tree
153 * @param {Node} parent The parent node
154 * @param {Node} node The child node to be appended
159 * @event beforeremove
160 * Fires before a child is removed from a node in this store's tree, return false to cancel the remove.
161 * @param {Tree} tree The owner tree
162 * @param {Node} parent The parent node
163 * @param {Node} node The child node to be removed
169 * Fires before a node is moved to a new location in the store's tree. Return false to cancel the move.
170 * @param {Tree} tree The owner tree
171 * @param {Node} node The node being moved
172 * @param {Node} oldParent The parent of the node
173 * @param {Node} newParent The new parent the node is moving to
174 * @param {Number} index The index it is being moved to
179 * @event beforeinsert
180 * Fires before a new child is inserted in a node in this store's tree, return false to cancel the insert.
181 * @param {Tree} tree The owner tree
182 * @param {Node} parent The parent node
183 * @param {Node} node The child node to be inserted
184 * @param {Node} refNode The child node the node is being inserted before
190 * Fires when this node is expanded.
191 * @param {Node} this The expanding node
197 * Fires when this node is collapsed.
198 * @param {Node} this The collapsing node
203 * @event beforeexpand
204 * Fires before this node is expanded.
205 * @param {Node} this The expanding node
210 * @event beforecollapse
211 * Fires before this node is collapsed.
212 * @param {Node} this The collapsing node
218 * Fires when this TreeStore is sorted.
219 * @param {Node} node The node that is sorted.
225 * Fires whenever the root node is changed in the tree.
226 * @param {Ext.data.Model} root The new root
233 remove: me.onNodeRemove,
234 // this event must follow the relay to beforeitemexpand to allow users to
235 // cancel the expand:
236 beforeexpand: me.onBeforeNodeExpand,
237 beforecollapse: me.onBeforeNodeCollapse,
238 append: me.onNodeAdded,
239 insert: me.onNodeAdded
247 me.setRootNode(root);
253 * Fires when the root node on this TreeStore is changed.
254 * @param {Ext.data.TreeStore} store This TreeStore
255 * @param {Node} The new root node.
260 //<deprecated since=0.99>
261 if (Ext.isDefined(me.nodeParameter)) {
262 if (Ext.isDefined(Ext.global.console)) {
263 Ext.global.console.warn('Ext.data.TreeStore: nodeParameter has been deprecated. Please use nodeParam instead.');
265 me.nodeParam = me.nodeParameter;
266 delete me.nodeParameter;
272 setProxy: function(proxy) {
276 if (proxy instanceof Ext.data.proxy.Proxy) {
277 // proxy instance, check if a root was set
278 needsRoot = Ext.isEmpty(proxy.getReader().root);
279 } else if (Ext.isString(proxy)) {
280 // string type, means a reader can't be set
283 // object, check if a reader and a root were specified.
284 reader = proxy.reader;
285 needsRoot = !(reader && !Ext.isEmpty(reader.root));
287 proxy = this.callParent(arguments);
289 reader = proxy.getReader();
290 reader.root = this.defaultRootProperty;
292 reader.buildExtractors(true);
297 onBeforeSort: function() {
298 if (this.folderSort) {
302 }, 'prepend', false);
307 * Called before a node is expanded.
309 * @param {Ext.data.NodeInterface} node The node being expanded.
310 * @param {Function} callback The function to run after the expand finishes
311 * @param {Object} scope The scope in which to run the callback function
313 onBeforeNodeExpand: function(node, callback, scope) {
314 if (node.isLoaded()) {
315 Ext.callback(callback, scope || node, [node.childNodes]);
317 else if (node.isLoading()) {
318 this.on('load', function() {
319 Ext.callback(callback, scope || node, [node.childNodes]);
320 }, this, {single: true});
325 callback: function() {
326 Ext.callback(callback, scope || node, [node.childNodes]);
333 getNewRecords: function() {
334 return Ext.Array.filter(this.tree.flatten(), this.filterNew);
338 getUpdatedRecords: function() {
339 return Ext.Array.filter(this.tree.flatten(), this.filterUpdated);
343 * Called before a node is collapsed.
345 * @param {Ext.data.NodeInterface} node The node being collapsed.
346 * @param {Function} callback The function to run after the collapse finishes
347 * @param {Object} scope The scope in which to run the callback function
349 onBeforeNodeCollapse: function(node, callback, scope) {
350 callback.call(scope || node, node.childNodes);
353 onNodeRemove: function(parent, node) {
354 var removed = this.removed;
356 if (!node.isReplace && Ext.Array.indexOf(removed, node) == -1) {
361 onNodeAdded: function(parent, node) {
362 var proxy = this.getProxy(),
363 reader = proxy.getReader(),
364 data = node.raw || node.data,
367 Ext.Array.remove(this.removed, node);
369 if (!node.isLeaf() && !node.isLoaded()) {
370 dataRoot = reader.getRoot(data);
372 this.fillNode(node, reader.extractData(dataRoot));
373 delete data[reader.root];
379 * Sets the root node for this store. See also the {@link #root} config option.
380 * @param {Ext.data.Model/Ext.data.NodeInterface/Object} root
381 * @return {Ext.data.NodeInterface} The new root
383 setRootNode: function(root) {
388 // create a default rootNode and create internal data struct.
390 id: me.defaultRootId,
394 root = Ext.ModelManager.create(root, me.model);
396 Ext.data.NodeInterface.decorate(root);
398 // Because we have decorated the model with new fields,
399 // we need to build new extactor functions on the reader.
400 me.getProxy().getReader().buildExtractors(true);
402 // When we add the root to the tree, it will automaticaly get the NodeInterface
403 me.tree.setRootNode(root);
405 // If the user has set expanded: true on the root, we want to call the expand function
406 if (!root.isLoaded() && root.isExpanded()) {
416 * Returns the root node for this tree.
417 * @return {Ext.data.NodeInterface}
419 getRootNode: function() {
420 return this.tree.getRootNode();
424 * Returns the record node by id
425 * @return {Ext.data.NodeInterface}
427 getNodeById: function(id) {
428 return this.tree.getNodeById(id);
432 * Loads the Store using its configured {@link #proxy}.
433 * @param {Object} options Optional config object. This is passed into the {@link Ext.data.Operation Operation}
434 * object that is created and then sent to the proxy's {@link Ext.data.proxy.Proxy#read} function.
435 * The options can also contain a node, which indicates which node is to be loaded. If not specified, it will
436 * default to the root node.
438 load: function(options) {
439 options = options || {};
440 options.params = options.params || {};
443 node = options.node || me.tree.getRootNode(),
446 // If there is not a node it means the user hasnt defined a rootnode yet. In this case lets just
447 // create one for them.
449 node = me.setRootNode({
454 if (me.clearOnLoad) {
458 Ext.applyIf(options, {
461 options.params[me.nodeParam] = node ? node.getId() : 'root';
464 node.set('loading', true);
467 return me.callParent([options]);
472 * Fills a node with a series of child records.
474 * @param {Ext.data.NodeInterface} node The node to fill
475 * @param {Array} records The records to add
477 fillNode: function(node, records) {
479 ln = records ? records.length : 0,
480 i = 0, sortCollection;
482 if (ln && me.sortOnLoad && !me.remoteSort && me.sorters && me.sorters.items) {
483 sortCollection = Ext.create('Ext.util.MixedCollection');
484 sortCollection.addAll(records);
485 sortCollection.sort(me.sorters.items);
486 records = sortCollection.items;
489 node.set('loaded', true);
490 for (; i < ln; i++) {
491 node.appendChild(records[i], undefined, true);
498 onProxyLoad: function(operation) {
500 successful = operation.wasSuccessful(),
501 records = operation.getRecords(),
502 node = operation.node;
504 node.set('loading', false);
506 records = me.fillNode(node, records);
509 me.fireEvent('read', me, operation.node, records, successful);
510 me.fireEvent('load', me, operation.node, records, successful);
511 //this is a callback that would have been passed to the 'read' function and is optional
512 Ext.callback(operation.callback, operation.scope || me, [records, operation, successful]);
516 * Creates any new records when a write is returned from the server.
518 * @param {Array} records The array of new records
519 * @param {Ext.data.Operation} operation The operation that just completed
520 * @param {Boolean} success True if the operation was successful
522 onCreateRecords: function(records, operation, success) {
525 length = records.length,
526 originalRecords = operation.records,
533 * Loop over each record returned from the server. Assume they are
534 * returned in order of how they were sent. If we find a matching
535 * record, replace it with the newly created one.
537 for (; i < length; ++i) {
539 original = originalRecords[i];
541 parentNode = original.parentNode;
543 // prevent being added to the removed cache
544 original.isReplace = true;
545 parentNode.replaceChild(record, original);
546 delete original.isReplace;
548 record.phantom = false;
555 * Updates any records when a write is returned from the server.
557 * @param {Array} records The array of updated records
558 * @param {Ext.data.Operation} operation The operation that just completed
559 * @param {Boolean} success True if the operation was successful
561 onUpdateRecords: function(records, operation, success){
565 length = records.length,
571 for (; i < length; ++i) {
573 original = me.tree.getNodeById(record.getId());
574 parentNode = original.parentNode;
576 // prevent being added to the removed cache
577 original.isReplace = true;
578 parentNode.replaceChild(record, original);
579 original.isReplace = false;
586 * Removes any records when a write is returned from the server.
588 * @param {Array} records The array of removed records
589 * @param {Ext.data.Operation} operation The operation that just completed
590 * @param {Boolean} success True if the operation was successful
592 onDestroyRecords: function(records, operation, success){
599 removeAll: function() {
600 this.getRootNode().destroy(true);
601 this.fireEvent('clear', this);
605 doSort: function(sorterFn) {
608 //the load function will pick up the new sorters and request the sorted data from the proxy
611 me.tree.sort(sorterFn, true);
612 me.fireEvent('datachanged', me);
614 me.fireEvent('sort', me);