]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/data/NodeInterface.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / data / NodeInterface.js
CommitLineData
6527f429
DM
1/** \r
2 * This class is used as a set of methods that are applied to the prototype of a\r
3 * {@link Ext.data.Model Model} to decorate it with a Node API. This means that models\r
4 * used in conjunction with a tree will have all of the tree related methods available\r
5 * on the model. In general, this class will not be used directly by the developer.\r
6 *\r
7 * This class also creates extra {@link Ext.data.Field fields} on the model, if they do\r
8 * not exist, to help maintain the tree state and UI. These fields are documented as\r
9 * config options.\r
10 *\r
11 * The data fields used to render a tree node are: {@link #text}, {@link #leaf},\r
12 * {@link #children}, and {@link #expanded}. Once a node is loaded to the tree store\r
13 * you can use {@link Ext.data.Model#get get()} to fetch the value of a given field\r
14 * name (provided there is not a convenience accessor on the Node for that field).\r
15 *\r
16 * @example\r
17 * Ext.tip.QuickTipManager.init(); // not required when using Ext.application()\r
18 *\r
19 * var root = {\r
20 * expanded: true,\r
21 * children: [{\r
22 * text: "Leaf node (<i>no folder/arrow icon</i>)",\r
23 * leaf: true,\r
24 * qtitle: 'Sample Tip Title',\r
25 * qtip: 'Tip body'\r
26 * }, {\r
27 * text: "Parent node expanded",\r
28 * expanded: true,\r
29 * children: [{\r
30 * text: "Expanded leaf node 1",\r
31 * leaf: true\r
32 * }, {\r
33 * text: "Expanded leaf node 2",\r
34 * leaf: true\r
35 * }]\r
36 * }, {\r
37 * text: "Parent node collapsed",\r
38 * children: [{\r
39 * text: "Collapsed leaf node 1",\r
40 * leaf: true\r
41 * }, {\r
42 * text: "Collapsed leaf node 2",\r
43 * leaf: true\r
44 * }]\r
45 * }]\r
46 * };\r
47 *\r
48 * var tree = Ext.create('Ext.tree.Panel', {\r
49 * title: 'TreePanel',\r
50 * width: 260,\r
51 * height: 200,\r
52 * root: root,\r
53 * rootVisible: false,\r
54 * renderTo: document.body,\r
55 * bbar: ['The first node ', {\r
56 * text: 'is a leaf?',\r
57 * handler: function () {\r
58 * var firstChild = tree.getRootNode().getChildAt(0);\r
59 * Ext.Msg.alert('Is Leaf?', firstChild.isLeaf());\r
60 * }\r
61 * }, {\r
62 * text: 'has text?',\r
63 * handler: function () {\r
64 * var firstChild = tree.getRootNode().getChildAt(0);\r
65 * Ext.Msg.alert('Has Text:', firstChild.get('text'));\r
66 * }\r
67 * }]\r
68 * });\r
69 *\r
70 * The following configs have methods used to set the value / state of the node at\r
71 * runtime:\r
72 *\r
73 * **{@link #children} / {@link #leaf}**\r
74 *\r
75 * - {@link #appendChild}\r
76 * - {@link #hasChildNodes}\r
77 * - {@link #insertBefore}\r
78 * - {@link #insertChild}\r
79 * - {@link #method-remove}\r
80 * - {@link #removeAll}\r
81 * - {@link #removeChild}\r
82 * - {@link #replaceChild}\r
83 *\r
84 * **{@link #expanded}**\r
85 *\r
86 * - {@link #method-expand}\r
87 * - {@link #expandChildren}\r
88 * - {@link #method-collapse}\r
89 * - {@link #collapseChildren}\r
90 *\r
91 * The remaining configs may be set using {@link Ext.data.Model#method-set set()}.\r
92 *\r
93 * node.set('text', 'Changed Text'); // example showing how to change the node label\r
94 *\r
95 * The {@link #qtip}, {@link #qtitle}, and {@link #qshowDelay} use QuickTips and\r
96 * requires initializing {@link Ext.tip.QuickTipManager} unless the application is\r
97 * created using {@link Ext#method-application}.\r
98 *\r
99 * Ext.tip.QuickTipManager.init();\r
100 *\r
101 * For additional information and examples see the description for\r
102 * {@link Ext.tree.Panel}.\r
103 */\r
104Ext.define('Ext.data.NodeInterface', {\r
105 requires: [\r
106 'Ext.data.field.Boolean',\r
107 'Ext.data.field.Integer',\r
108 'Ext.data.field.String',\r
109 'Ext.data.writer.Json',\r
110 'Ext.mixin.Observable'\r
111 ],\r
112\r
113 /**\r
114 * @cfg {Boolean} [expanded=false]\r
115 * True if the node is expanded.\r
116 *\r
117 * When the tree is asynchronously remote loaded, expanding a collapsed node loads\r
118 * the children of that node (if the node has not already been loaded previously).\r
119 *\r
120 * See also: {@link #isExpanded}.\r
121 */\r
122\r
123 /**\r
124 * @cfg {Boolean} [expandable=true]\r
125 * False to prevent expanding/collapsing of this node.\r
126 *\r
127 * See also: {@link #isExpandable}.\r
128 */\r
129\r
130 /**\r
131 * @cfg {Boolean} [checked=null]\r
132 * Set to true or false to show a checkbox alongside this node.\r
133 *\r
134 * To fetch an array of checked nodes use {@link Ext.tree.Panel#method-getChecked\r
135 * getChecked()}.\r
136 */\r
137\r
138 /**\r
139 * @cfg {Boolean} [leaf=false]\r
140 * Set to true to indicate that this child can have no children. The expand icon/arrow will then not be\r
141 * rendered for this node.\r
142 *\r
143 * See also: {@link #isLeaf}.\r
144 */\r
145\r
146 /**\r
147 * @cfg {String} cls\r
148 * CSS class to apply to this node.\r
149 */\r
150\r
151 /**\r
152 * @cfg {String} iconCls\r
153 * @inheritdoc Ext.panel.Header#iconCls\r
154 * @localdoc Use {@link #icon} to set the icon src path directly.\r
155 */\r
156\r
157 /**\r
158 * @cfg {String} icon\r
159 * @inheritdoc Ext.panel.Header#icon\r
160 */\r
161\r
162 /**\r
163 * @cfg {Boolean} [allowDrop=true]\r
164 * Set to false to deny dropping on this node.\r
165 *\r
166 * Applicable when using the {@link Ext.tree.plugin.TreeViewDragDrop\r
167 * TreeViewDragDrop} plugin.\r
168 */\r
169\r
170 /**\r
171 * @cfg {Boolean} [allowDrag=true]\r
172 * Set to false to deny dragging of this node.\r
173 *\r
174 * Applicable when using the {@link Ext.tree.plugin.TreeViewDragDrop\r
175 * TreeViewDragDrop} plugin.\r
176 */\r
177\r
178 /**\r
179 * @cfg {String} href\r
180 * A URL for a link that's created when this config is specified.\r
181 *\r
182 * See also {@link #hrefTarget}.\r
183 */\r
184\r
185 /**\r
186 * @cfg {String} hrefTarget\r
187 * Target for link. Only applicable when {@link #href} is also specified.\r
188 */\r
189\r
190 /**\r
191 * @cfg {String} qtip\r
192 * Tooltip text to show on this node.\r
193 *\r
194 * See also {@link #qtitle}.\r
195 * See also {@link #qshowDelay}.\r
196 */\r
197\r
198 /**\r
199 * @cfg {String} qtitle\r
200 * Tooltip title.\r
201 *\r
202 * See also {@link #qtip}.\r
203 * See also {@link #qshowDelay}.\r
204 */\r
205\r
206 /**\r
207 * @cfg {Number} qshowDelay\r
208 * Tooltip showDelay.\r
209 *\r
210 * See also {@link #qtip}.\r
211 * See also {@link #qtitle}.\r
212 */\r
213\r
214 /**\r
215 * @cfg {String} text\r
216 * The text to show on node label (_html tags are accepted_).\r
217 * The default text for the root node is `ROOT`. All other nodes default to ''.\r
218 *\r
219 * **Note:** By default the node label is `text`, but can be set using the tree's\r
220 * {@link Ext.tree.Panel#cfg-displayField displayField} config.\r
221 */\r
222\r
223 /**\r
224 * @cfg {Ext.data.NodeInterface[]} children\r
225 * Array of child nodes.\r
226 *\r
227 * **Note:** By default the child nodes root is `children`, but can be set using the\r
228 * reader {@link Ext.data.reader.Reader#cfg-rootProperty rootProperty} config on the\r
229 * {@link Ext.data.TreeStore TreeStore's} {@link Ext.data.TreeStore#cfg-proxy proxy}.\r
230 */\r
231\r
232 /**\r
233 * @cfg {Boolean} [loaded=false]\r
234 * @private\r
235 * True if the node has finished loading.\r
236 *\r
237 * See {@link #isLoaded}.\r
238 */\r
239\r
240 /**\r
241 * @cfg {Boolean} [loading=false]\r
242 * @private\r
243 * True if the node is currently loading.\r
244 *\r
245 * See {@link #isLoading}.\r
246 */\r
247\r
248 /**\r
249 * @cfg {Boolean} root\r
250 * @private\r
251 * True if this is the root node.\r
252 *\r
253 * See {@link #isRoot}.\r
254 */\r
255\r
256 /**\r
257 * @cfg {Boolean} isLast\r
258 * @private\r
259 * True if this is the last node.\r
260 *\r
261 * See {@link #method-isLast}.\r
262 */\r
263\r
264 /**\r
265 * @cfg {Boolean} isFirst\r
266 * @private\r
267 * True if this is the first node.\r
268 *\r
269 * See {@link #method-isFirst}.\r
270 */\r
271\r
272 /**\r
273 * @cfg {String} parentId\r
274 * @private\r
275 * ID of parent node.\r
276 *\r
277 * See {@link #parentNode}.\r
278 */\r
279\r
280 /**\r
281 * @cfg {Number} index\r
282 * @private\r
283 * The position of the node inside its parent. When parent has 4 children and the node is third amongst them,\r
284 * index will be 2.\r
285 *\r
286 * See {@link #indexOf} and {@link #indexOfId}.\r
287 */\r
288\r
289 /**\r
290 * @cfg {Number} depth\r
291 * @private\r
292 * The number of parents this node has. A root node has depth 0, a child of it depth 1, and so on...\r
293 *\r
294 * See {@link #getDepth}.\r
295 */\r
296\r
297 /**\r
298 * @property {Ext.data.NodeInterface} nextSibling\r
299 * A reference to this node's next sibling node. `null` if this node does not have a next sibling.\r
300 */\r
301\r
302 /**\r
303 * @property {Ext.data.NodeInterface} previousSibling\r
304 * A reference to this node's previous sibling node. `null` if this node does not have a previous sibling.\r
305 */\r
306\r
307 /**\r
308 * @property {Ext.data.NodeInterface} parentNode\r
309 * A reference to this node's parent node. `null` if this node is the root node.\r
310 */\r
311\r
312 /**\r
313 * @property {Ext.data.NodeInterface} lastChild\r
314 * A reference to this node's last child node. `null` if this node has no children.\r
315 */\r
316\r
317 /**\r
318 * @property {Ext.data.NodeInterface} firstChild\r
319 * A reference to this node's first child node. `null` if this node has no children.\r
320 */\r
321\r
322 /**\r
323 * @property {Ext.data.NodeInterface[]} childNodes\r
324 * An array of this nodes children. Array will be empty if this node has no children.\r
325 */\r
326\r
327 statics: {\r
328 /**\r
329 * This method allows you to decorate a Model's class to implement the NodeInterface.\r
330 * This adds a set of methods, new events, new properties and new fields on every Record.\r
331 * @param {Ext.Class/Ext.data.Model} model The Model class or an instance of the Model class you want to\r
332 * decorate the prototype of.\r
333 * @static\r
334 */\r
335 decorate: function (modelClass) {\r
336 var model = Ext.data.schema.Schema.lookupEntity(modelClass),\r
337 proto = model.prototype,\r
338 idName, idField, idType;\r
339 \r
340 if (!model.prototype.isObservable) {\r
341 model.mixin(Ext.mixin.Observable.prototype.mixinId, Ext.mixin.Observable);\r
342 }\r
343 if (proto.isNode) { // if (already decorated)\r
344 return;\r
345 }\r
346\r
347 idName = proto.idProperty;\r
348 idField = model.getField(idName);\r
349 idType = idField.type;\r
350\r
351 model.override(this.getPrototypeBody());\r
352 model.addFields([\r
353 { name : 'parentId', type : idType, defaultValue : null, allowNull : idField.allowNull },\r
354 { name : 'index', type : 'int', defaultValue : -1, persist : false , convert: null },\r
355 { name : 'depth', type : 'int', defaultValue : 0, persist : false , convert: null },\r
356 { name : 'expanded', type : 'bool', defaultValue : false, persist : false , convert: null },\r
357 { name : 'expandable', type : 'bool', defaultValue : true, persist : false , convert: null },\r
358 { name : 'checked', type : 'auto', defaultValue : null, persist : false , convert: null },\r
359 { name : 'leaf', type : 'bool', defaultValue : false },\r
360 { name : 'cls', type : 'string', defaultValue : '', persist : false , convert: null },\r
361 { name : 'iconCls', type : 'string', defaultValue : '', persist : false , convert: null },\r
362 { name : 'icon', type : 'string', defaultValue : '', persist : false , convert: null },\r
363 { name : 'root', type : 'boolean', defaultValue : false, persist : false , convert: null },\r
364 { name : 'isLast', type : 'boolean', defaultValue : false, persist : false , convert: null },\r
365 { name : 'isFirst', type : 'boolean', defaultValue : false, persist : false , convert: null },\r
366 { name : 'allowDrop', type : 'boolean', defaultValue : true, persist : false , convert: null },\r
367 { name : 'allowDrag', type : 'boolean', defaultValue : true, persist : false , convert: null },\r
368 { name : 'loaded', type : 'boolean', defaultValue : false, persist : false , convert: null },\r
369 { name : 'loading', type : 'boolean', defaultValue : false, persist : false , convert: null },\r
370 { name : 'href', type : 'string', defaultValue : '', persist : false , convert: null },\r
371 { name : 'hrefTarget', type : 'string', defaultValue : '', persist : false , convert: null },\r
372 { name : 'qtip', type : 'string', defaultValue : '', persist : false , convert: null },\r
373 { name : 'qtitle', type : 'string', defaultValue : '', persist : false , convert: null },\r
374 { name : 'qshowDelay', type : 'int', defaultValue : 0, persist : false , convert: null },\r
375 { name : 'children', type : 'auto', defaultValue : null, persist : false , convert: null },\r
376 { name : 'visible', type : 'boolean', defaultValue : true, persist : false },\r
377 { name : 'text', type : 'string', persist : false }\r
378 ]);\r
379 },\r
380\r
381 getPrototypeBody: function() {\r
382 var bubbledEvents = {\r
383 idchanged : true,\r
384 append : true,\r
385 remove : true,\r
386 move : true,\r
387 insert : true,\r
388 beforeappend : true,\r
389 beforeremove : true,\r
390 beforemove : true,\r
391 beforeinsert : true,\r
392 expand : true,\r
393 collapse : true,\r
394 beforeexpand : true,\r
395 beforecollapse: true,\r
396 sort : true\r
397 }, silently = {\r
398 silent: true\r
399 };\r
400\r
401 // bulkUpdate usage:\r
402 // This is used in 3 contexts:\r
403 // a) When registering nodes. When bulk updating, we don't want to descend down the tree\r
404 // recursively making calls to register which is redundant. We do need to call it for each node\r
405 // because they need to be findable via id as soon as append events fire, so we only do the minimum needed.\r
406 // b) When setting a data property on the model. We only need to go through set (and the subsequent event chain)\r
407 // so that the UI can update. If we're doing a bulk update, the UI will update regardless.\r
408 // c) triggerUIUpdate. This is because we know "something has changed", but not exactly what, so we allow the UI to redraw itself.\r
409 // It has no purpose as far as data goes, so skip it when we can\r
410\r
411 return {\r
412 /**\r
413 * @property {Boolean} isNode\r
414 * `true` in this class to identify an object as an instantiated Node, or subclass thereof.\r
415 */\r
416 isNode: true,\r
417\r
418 firstChild: null,\r
419 lastChild: null,\r
420 parentNode: null,\r
421 previousSibling: null,\r
422 nextSibling: null,\r
423\r
424 constructor: function() {\r
425 var me = this;\r
426\r
427 me.mixins.observable.constructor.call(me);\r
428 me.callParent(arguments);\r
429 me.childNodes = [];\r
430\r
431 // These events are fired on this node, and programmatically bubble \r
432 // up the parentNode axis, ending up walking off the top and firing \r
433 // on the owning Ext.data.TreeStore\r
434 /**\r
435 * @event append\r
436 * Fires when a new child node is appended\r
437 * @param {Ext.data.NodeInterface} this This node\r
438 * @param {Ext.data.NodeInterface} node The newly appended node\r
439 * @param {Number} index The index of the newly appended node\r
440 */\r
441 /**\r
442 * @event remove\r
443 * Fires when a child node is removed\r
444 * @param {Ext.data.NodeInterface} this This node\r
445 * @param {Ext.data.NodeInterface} node The removed node\r
446 * @param {Boolean} isMove `true` if the child node is being removed so it can be moved to another position in the tree.\r
447 * @param {Object} context An object providing information about where the removed node came from. It contains the following properties:\r
448 * @param {Ext.data.NodeInterface} context.parentNode The node from which the removed node was removed.\r
449 * @param {Ext.data.NodeInterface} context.previousSibling The removed node's former previous sibling.\r
450 * @param {Ext.data.NodeInterface} context.nextSibling The removed node's former next sibling.\r
451 * (a side effect of calling {@link Ext.data.NodeInterface#appendChild appendChild} or\r
452 * {@link Ext.data.NodeInterface#insertBefore insertBefore} with a node that already has a parentNode)\r
453 */\r
454 /**\r
455 * @event move\r
456 * Fires when this node is moved to a new location in the tree\r
457 * @param {Ext.data.NodeInterface} this This node\r
458 * @param {Ext.data.NodeInterface} oldParent The old parent of this node\r
459 * @param {Ext.data.NodeInterface} newParent The new parent of this node\r
460 * @param {Number} index The index it was moved to\r
461 */\r
462 /**\r
463 * @event insert\r
464 * Fires when a new child node is inserted.\r
465 * @param {Ext.data.NodeInterface} this This node\r
466 * @param {Ext.data.NodeInterface} node The child node inserted\r
467 * @param {Ext.data.NodeInterface} refNode The child node the node was inserted before\r
468 */\r
469 /**\r
470 * @event beforeappend\r
471 * Fires before a new child is appended, return false to cancel the append.\r
472 * @param {Ext.data.NodeInterface} this This node\r
473 * @param {Ext.data.NodeInterface} node The child node to be appended\r
474 */\r
475 /**\r
476 * @event beforeremove\r
477 * Fires before a child is removed, return false to cancel the remove.\r
478 * @param {Ext.data.NodeInterface} this This node\r
479 * @param {Ext.data.NodeInterface} node The child node to be removed\r
480 * @param {Boolean} isMove `true` if the child node is being removed so it can be moved to another position in the tree.\r
481 * (a side effect of calling {@link Ext.data.NodeInterface#appendChild appendChild} or\r
482 * {@link Ext.data.NodeInterface#insertBefore insertBefore} with a node that already has a parentNode)\r
483 */\r
484 /**\r
485 * @event beforemove\r
486 * Fires before this node is moved to a new location in the tree. Return false to cancel the move.\r
487 * @param {Ext.data.NodeInterface} this This node\r
488 * @param {Ext.data.NodeInterface} oldParent The parent of this node\r
489 * @param {Ext.data.NodeInterface} newParent The new parent this node is moving to\r
490 * @param {Number} index The index it is being moved to\r
491 */\r
492 /**\r
493 * @event beforeinsert\r
494 * Fires before a new child is inserted, return false to cancel the insert.\r
495 * @param {Ext.data.NodeInterface} this This node\r
496 * @param {Ext.data.NodeInterface} node The child node to be inserted\r
497 * @param {Ext.data.NodeInterface} refNode The child node the node is being inserted before\r
498 */\r
499 /**\r
500 * @event expand\r
501 * Fires when this node is expanded.\r
502 * @param {Ext.data.NodeInterface} this The expanding node\r
503 */\r
504 /**\r
505 * @event collapse\r
506 * Fires when this node is collapsed.\r
507 * @param {Ext.data.NodeInterface} this The collapsing node\r
508 */\r
509 /**\r
510 * @event beforeexpand\r
511 * Fires before this node is expanded.\r
512 * @param {Ext.data.NodeInterface} this The expanding node\r
513 */\r
514 /**\r
515 * @event beforecollapse\r
516 * Fires before this node is collapsed.\r
517 * @param {Ext.data.NodeInterface} this The collapsing node\r
518 */\r
519 /**\r
520 * @event sort\r
521 * Fires when this node's childNodes are sorted.\r
522 * @param {Ext.data.NodeInterface} this This node.\r
523 * @param {Ext.data.NodeInterface[]} childNodes The childNodes of this node.\r
524 */\r
525 return me;\r
526 },\r
527\r
528 /**\r
529 * Ensures that the passed object is an instance of a Record with the NodeInterface applied\r
530 * @return {Ext.data.NodeInterface}\r
531 */\r
532 createNode: function (node) {\r
533 var me = this,\r
534 childType = me.childType,\r
535 store,\r
536 storeReader,\r
537 nodeProxy,\r
538 nodeReader,\r
539 reader,\r
540 typeProperty,\r
541 T = me.self;\r
542\r
543 // Passed node's internal data object\r
544 if (!node.isModel) {\r
545 // Check this node type's childType configuration\r
546 if (childType) {\r
547 T = me.schema.getEntity(childType);\r
548 }\r
549 // See if the reader has a typeProperty and use it if possible\r
550 else {\r
551 store = me.getTreeStore();\r
552 storeReader = store && store.getProxy().getReader();\r
553 nodeProxy = me.getProxy();\r
554 nodeReader = nodeProxy ? nodeProxy.getReader() : null;\r
555\r
556 // If the node's proxy's reader was configured with a special typeProperty (property name which defines the child type name) use that.\r
557 reader = !storeReader || (nodeReader && nodeReader.initialConfig.typeProperty) ? nodeReader : storeReader;\r
558\r
559 if (reader) {\r
560 typeProperty = reader.getTypeProperty();\r
561 if (typeProperty) {\r
562 T = reader.getChildType(me.schema, node, typeProperty);\r
563 }\r
564 }\r
565 }\r
566\r
567 node = new T(node);\r
568 }\r
569\r
570 // The node may already decorated, but may not have been\r
571 // so when the model constructor was called. If not,\r
572 // setup defaults here\r
573 if (!node.childNodes) {\r
574 node.firstChild = node.lastChild = node.parentNode =\r
575 node.previousSibling = node.nextSibling = null;\r
576 node.childNodes = [];\r
577 }\r
578\r
579 return node;\r
580 },\r
581\r
582 /**\r
583 * Returns true if this node is a leaf\r
584 * @return {Boolean}\r
585 */\r
586 isLeaf: function() {\r
587 return this.get('leaf') === true;\r
588 },\r
589\r
590 /**\r
591 * Sets the first child of this node\r
592 * @private\r
593 * @param {Ext.data.NodeInterface} node\r
594 */\r
595 setFirstChild: function(node) {\r
596 this.firstChild = node;\r
597 },\r
598\r
599 /**\r
600 * Sets the last child of this node\r
601 * @private\r
602 * @param {Ext.data.NodeInterface} node\r
603 */\r
604 setLastChild: function(node) {\r
605 this.lastChild = node;\r
606 },\r
607\r
608 /**\r
609 * Updates general data of this node like isFirst, isLast, depth. This\r
610 * method is internally called after a node is moved. This shouldn't\r
611 * have to be called by the developer unless they are creating custom\r
612 * Tree plugins.\r
613 * @protected\r
614 * @param {Boolean} commit\r
615 * @param {Object} info The info to update. May contain any of the following\r
616 * @param {Object} info.isFirst\r
617 * @param {Object} info.isLast\r
618 * @param {Object} info.index\r
619 * @param {Object} info.depth\r
620 * @param {Object} info.parentId\r
621 */\r
622 updateInfo: function(commit, info) {\r
623 var me = this,\r
624 dataObject = me.data,\r
625 oldDepth = dataObject.depth,\r
626 childInfo = {},\r
627 children = me.childNodes,\r
628 childCount = children.length,\r
629 phantom = me.phantom,\r
630 fields = me.fields,\r
631 modified = me.modified || (me.modified = {}),\r
632 propName, newValue,\r
633 field, currentValue, key,\r
634 newParentId = info.parentId,\r
635 settingIndexInNewParent,\r
636 persistentField, i;\r
637\r
638 //<debug>\r
639 if (!info) {\r
640 Ext.raise('NodeInterface expects update info to be passed');\r
641 }\r
642 //</debug>\r
643\r
644 // Set the passed field values into the data object.\r
645 // We do NOT need the expense of Model.set. We just need to ensure\r
646 // that the dirty flag is set.\r
647 for (propName in info) {\r
648 field = fields[me.fieldOrdinals[propName]];\r
649 newValue = info[propName];\r
650 persistentField = field && field.persist;\r
651\r
652 currentValue = dataObject[propName];\r
653\r
654 // If we are setting the index value, and the developer has changed it to be persistent, and the\r
655 // new parent node is different to the starting one, it must be dirty.\r
656 // The index may be the same value, but it's in a different parent.\r
657 // This is so that a Writer can write the correct persistent fields which must include\r
658 // the index to insert at if the parentId has changed.\r
659 settingIndexInNewParent = persistentField && (propName === 'index') && (currentValue !== -1) && (newParentId && newParentId !== modified.parentId);\r
660\r
661 // If new value is the same (unless we are setting the index in a new parent node), then skip the change.\r
662 if (!settingIndexInNewParent && me.isEqual(currentValue, newValue)) {\r
663 continue;\r
664 }\r
665 dataObject[propName] = newValue;\r
666\r
667 // Only flag dirty when persistent fields are modified\r
668 if (persistentField) {\r
669\r
670 // Already modified, just check if we've reverted it back to start value (unless we are setting the index in a new parent node)\r
671 if (!settingIndexInNewParent && modified.hasOwnProperty(propName)) {\r
672\r
673 // If we have reverted to start value, possibly clear dirty flag\r
674 if (me.isEqual(modified[propName], newValue)) {\r
675 // The original value in me.modified equals the new value, so\r
676 // the field is no longer modified:\r
677 delete modified[propName];\r
678\r
679 // We might have removed the last modified field, so check to\r
680 // see if there are any modified fields remaining and correct\r
681 // me.dirty:\r
682 me.dirty = false;\r
683 for (key in modified) {\r
684 if (modified.hasOwnProperty(key)){\r
685 me.dirty = true;\r
686 break;\r
687 }\r
688 }\r
689 }\r
690 }\r
691\r
692 // Not already modified, set dirty flag\r
693 else {\r
694 me.dirty = true;\r
695 modified[propName] = currentValue;\r
696 }\r
697 }\r
698 }\r
699 if (commit) {\r
700 me.commit();\r
701 me.phantom = phantom;\r
702 }\r
703\r
704 // The only way child data can be influenced is if this node has changed level in this update.\r
705 if (me.data.depth !== oldDepth) {\r
706 childInfo = {\r
707 depth: me.data.depth + 1\r
708 };\r
709 for (i = 0; i < childCount; i++) {\r
710 children[i].updateInfo(commit, childInfo);\r
711 }\r
712 }\r
713 },\r
714\r
715 /**\r
716 * Returns true if this node is the last child of its parent\r
717 * @return {Boolean}\r
718 */\r
719 isLast: function() {\r
720 return this.get('isLast');\r
721 },\r
722\r
723 /**\r
724 * Returns true if this node is the first child of its parent\r
725 * @return {Boolean}\r
726 */\r
727 isFirst: function() {\r
728 return this.get('isFirst');\r
729 },\r
730\r
731 /**\r
732 * Returns true if this node has one or more child nodes, else false.\r
733 * @return {Boolean}\r
734 */\r
735 hasChildNodes: function() {\r
736 return !this.isLeaf() && this.childNodes.length > 0;\r
737 },\r
738\r
739 /**\r
740 * Returns true if this node has one or more child nodes, or if the <tt>expandable</tt>\r
741 * node attribute is explicitly specified as true, otherwise returns false.\r
742 * @return {Boolean}\r
743 */\r
744 isExpandable: function() {\r
745 var me = this;\r
746\r
747 if (me.get('expandable')) {\r
748 return !(me.isLeaf() || (me.isLoaded() && !me.phantom && !me.hasChildNodes()));\r
749 }\r
750 return false;\r
751 },\r
752 \r
753 triggerUIUpdate: function() {\r
754 // This isn't ideal, however none of the underlying fields have changed\r
755 // but we still need to update the UI\r
756 // callJoined calls both the Stores we are joined to, and any TreeStore of which we may be a descendant.\r
757 this.callJoined('afterEdit', []);\r
758 },\r
759\r
760 /**\r
761 * Inserts node(s) as the last child node of this node.\r
762 *\r
763 * If the node was previously a child node of another parent node, it will be removed from that node first.\r
764 *\r
765 * @param {Ext.data.NodeInterface/Ext.data.NodeInterface[]/Object} node The node or Array of nodes to append\r
766 * @param {Boolean} [suppressEvents=false] True to suppress firing of \r
767 * events.\r
768 * @param {Boolean} [commit=false]\r
769 * @return {Ext.data.NodeInterface} The appended node if single append, or null if an array was passed\r
770 */\r
771 appendChild: function(node, suppressEvents, commit) {\r
772 var me = this,\r
773 i, ln,\r
774 index,\r
775 oldParent,\r
776 previousSibling,\r
777 childInfo = {\r
778 isLast: true,\r
779 parentId: me.getId(),\r
780 depth: (me.data.depth||0) + 1\r
781 },\r
782 result,\r
783 treeStore = me.getTreeStore(),\r
784 bulkUpdate = treeStore && treeStore.bulkUpdate;\r
785\r
786 // Coalesce all layouts caused by node append\r
787 Ext.suspendLayouts();\r
788\r
789 // if passed an array do them one by one\r
790 if (Ext.isArray(node)) {\r
791 ln = node.length;\r
792 result = new Array(ln);\r
793 // Suspend view updating and data syncing during update\r
794 me.callTreeStore('beginFill');\r
795 for (i = 0; i < ln; i++) {\r
796 result[i] = me.appendChild(node[i], suppressEvents, commit);\r
797 }\r
798 // Resume view updating and data syncing after appending all new children.\r
799 // This will fire the add event to any views (if its the top level append)\r
800 me.callTreeStore('endFill', [result]);\r
801 } else {\r
802 // Make sure it is a record\r
803 node = me.createNode(node);\r
804\r
805 if (suppressEvents !== true && me.fireEventArgs('beforeappend', [me, node]) === false) {\r
806 Ext.resumeLayouts(true);\r
807 return false;\r
808 }\r
809\r
810 index = me.childNodes.length;\r
811 oldParent = node.parentNode;\r
812\r
813 // it's a move, make sure we move it cleanly\r
814 if (oldParent) {\r
815 if (suppressEvents !== true && node.fireEventArgs('beforemove', [node, oldParent, me, index]) === false) {\r
816 Ext.resumeLayouts(true);\r
817 return false;\r
818 }\r
819 // Return false if a beforeremove listener vetoed the remove\r
820 if (oldParent.removeChild(node, false, suppressEvents, oldParent.getTreeStore() === treeStore) === false) {\r
821 Ext.resumeLayouts(true);\r
822 return false;\r
823 }\r
824 }\r
825\r
826 // Coalesce sync operations across this operation\r
827 // Node field setting (loaded, expanded) and node addition both trigger a sync if autoSync is set.\r
828 treeStore && treeStore.beginUpdate();\r
829\r
830 index = me.childNodes.length;\r
831 if (index === 0) {\r
832 me.setFirstChild(node);\r
833 }\r
834\r
835 me.childNodes[index] = node;\r
836 node.parentNode = me;\r
837 node.nextSibling = null;\r
838\r
839 me.setLastChild(node);\r
840\r
841 previousSibling = me.childNodes[index - 1];\r
842 if (previousSibling) {\r
843 node.previousSibling = previousSibling;\r
844 previousSibling.nextSibling = node;\r
845 previousSibling.updateInfo(commit, {\r
846 isLast: false\r
847 });\r
848 // No need to trigger a ui update if we're doing a bulk update\r
849 if (!bulkUpdate) {\r
850 previousSibling.triggerUIUpdate();\r
851 }\r
852 } else {\r
853 node.previousSibling = null;\r
854 }\r
855\r
856 // Update the new child's info passing in info we already know\r
857 childInfo.isFirst = index === 0;\r
858 childInfo.index = index;\r
859 node.updateInfo(commit, childInfo);\r
860\r
861 // We stop being a leaf as soon as a node is appended\r
862 if (me.isLeaf()) {\r
863 me.set('leaf', false);\r
864 }\r
865\r
866 // As soon as we append a child to this node, we are loaded\r
867 if (!me.isLoaded()) {\r
868 if (bulkUpdate) {\r
869 me.data.loaded = true;\r
870 } else {\r
871 me.set('loaded', true);\r
872 }\r
873 } else if (me.childNodes.length === 1 && !bulkUpdate) {\r
874 me.triggerUIUpdate();\r
875 }\r
876\r
877 // Ensure connectors are correct by updating the UI on all intervening nodes (descendants) between last sibling and new node.\r
878 if (index && me.childNodes[index - 1].isExpanded() && !bulkUpdate) {\r
879 me.childNodes[index - 1].cascadeBy(me.triggerUIUpdate);\r
880 }\r
881\r
882 // We register the subtree before we proceed so relayed events\r
883 // (like nodeappend) from our TreeStore (if we have one) will be\r
884 // able to use getNodeById. The node also needs to be added since \r
885 // we're passing it in the events below. If we're not bulk updating, it\r
886 // means we're just appending a node (with possible children), so do it\r
887 // deeply here to ensure everything is captured.\r
888 if (treeStore) {\r
889 treeStore.registerNode(me, !bulkUpdate);\r
890 if (bulkUpdate) {\r
891 treeStore.registerNode(node);\r
892 }\r
893 }\r
894\r
895 // This node MUST fire its events first, so that if the TreeStore's\r
896 // onNodeAppend loads and appends local children, the events are still in order;\r
897 // This node appended this child first, before the descendant cascade.\r
898 if (suppressEvents !== true) {\r
899 me.fireEventArgs('append', [me, node, index]);\r
900\r
901 if (oldParent) {\r
902 node.fireEventArgs('move', [node, oldParent, me, index]);\r
903 }\r
904 }\r
905\r
906 // Inform the TreeStore so that the node can be inserted\r
907 // and registered.\r
908 me.callTreeStore('onNodeAppend', [node, index]);\r
909\r
910 result = node;\r
911\r
912 // Coalesce sync operations across this operation\r
913 // Node field setting (loaded, expanded) and node addition both trigger a sync if autoSync is set.\r
914 if (treeStore) {\r
915 treeStore.endUpdate();\r
916 }\r
917 }\r
918\r
919 // Flush layouts caused by updating of the UI\r
920 Ext.resumeLayouts(true);\r
921\r
922 return result;\r
923 },\r
924\r
925 /**\r
926 * Returns the tree this node is in.\r
927 * @return {Ext.tree.Panel} The tree panel which owns this node.\r
928 */\r
929 getOwnerTree: function() {\r
930 var store = this.getTreeStore();\r
931 if (store) {\r
932 return store.ownerTree;\r
933 }\r
934 },\r
935\r
936 /**\r
937 * Returns the {@link Ext.data.TreeStore} which owns this node.\r
938 * @return {Ext.data.TreeStore} The TreeStore which owns this node.\r
939 */\r
940 getTreeStore: function() {\r
941 var root = this;\r
942\r
943 while (root && !root.treeStore) {\r
944 root = root.parentNode;\r
945 }\r
946 return root && root.treeStore;\r
947 },\r
948\r
949 /**\r
950 * Removes a child node from this node.\r
951 * @param {Ext.data.NodeInterface} node The node to remove\r
952 * @param {Boolean} [erase=false] True to erase the record using the\r
953 * configured proxy.\r
954 * @return {Ext.data.NodeInterface} The removed node\r
955 */\r
956 removeChild: function(node, erase, suppressEvents, isMove) {\r
957 var me = this,\r
958 index = me.indexOf(node),\r
959 i, childCount,\r
960 previousSibling,\r
961 treeStore = me.getTreeStore(),\r
962 bulkUpdate = treeStore && treeStore.bulkUpdate,\r
963 removeContext;\r
964\r
965 if (index === -1 || (suppressEvents !== true && me.fireEventArgs('beforeremove', [me, node, !!isMove]) === false)) {\r
966 return false;\r
967 }\r
968\r
969 // Coalesce all layouts caused by node removal\r
970 Ext.suspendLayouts();\r
971\r
972 // Coalesce sync operations across this operation\r
973 treeStore && treeStore.beginUpdate();\r
974\r
975 // remove it from childNodes collection\r
976 Ext.Array.erase(me.childNodes, index, 1);\r
977\r
978 // update child refs\r
979 if (me.firstChild === node) {\r
980 me.setFirstChild(node.nextSibling);\r
981 }\r
982 if (me.lastChild === node) {\r
983 me.setLastChild(node.previousSibling);\r
984 }\r
985\r
986 // Update previous sibling to point to its new next.\r
987 previousSibling = node.previousSibling;\r
988 if (previousSibling) {\r
989 node.previousSibling.nextSibling = node.nextSibling;\r
990 }\r
991 \r
992 // Update the next sibling to point to its new previous\r
993 if (node.nextSibling) {\r
994 node.nextSibling.previousSibling = node.previousSibling;\r
995\r
996 // And if it's the new first child, let it know\r
997 if (index === 0) {\r
998 node.nextSibling.updateInfo(false, {\r
999 isFirst: true\r
1000 });\r
1001 }\r
1002\r
1003 // Update subsequent siblings' index values\r
1004 for (i = index, childCount = me.childNodes.length; i < childCount; i++) {\r
1005 me.childNodes[i].updateInfo(false, {\r
1006 index: i\r
1007 });\r
1008 }\r
1009 }\r
1010\r
1011 // If the removed node had no next sibling, but had a previous,\r
1012 // update the previous sibling so it knows it's the last\r
1013 else if (previousSibling) {\r
1014 previousSibling.updateInfo(false, {\r
1015 isLast: true\r
1016 });\r
1017\r
1018 // We're removing the last child.\r
1019 // Ensure connectors are correct by updating the UI on all intervening nodes (descendants) between previous sibling and new node.\r
1020 if (!bulkUpdate) {\r
1021 if (previousSibling.isExpanded()) {\r
1022 previousSibling.cascadeBy(me.triggerUIUpdate);\r
1023 }\r
1024 // No intervening descendant nodes, just update the previous sibling\r
1025 else {\r
1026 previousSibling.triggerUIUpdate();\r
1027 }\r
1028 }\r
1029 }\r
1030\r
1031 // If this node suddenly doesn't have child nodes anymore, update \r
1032 // myself\r
1033 if (!me.childNodes.length && !bulkUpdate) {\r
1034 me.triggerUIUpdate();\r
1035 }\r
1036\r
1037 // Flush layouts caused by updating the UI\r
1038 Ext.resumeLayouts(true);\r
1039\r
1040 if (suppressEvents !== true) {\r
1041 // Context argument to events.\r
1042 removeContext = {\r
1043 parentNode: node.parentNode,\r
1044 previousSibling: node.previousSibling,\r
1045 nextSibling: node.nextSibling\r
1046 };\r
1047 // Inform the TreeStore so that descendant nodes can be removed.\r
1048 me.callTreeStore('beforeNodeRemove', [[node], !!isMove]);\r
1049\r
1050 node.previousSibling = node.nextSibling = node.parentNode = null;\r
1051\r
1052 me.fireEventArgs('remove', [me, node, !!isMove, removeContext]);\r
1053\r
1054 // Inform the TreeStore so that the node unregistered and unjoined.\r
1055 me.callTreeStore('onNodeRemove', [[node], !!isMove]);\r
1056 }\r
1057\r
1058 // Update removed node's pointers *after* firing event so that listeners\r
1059 // can tell where the removal took place\r
1060 if (erase) {\r
1061 node.erase(true);\r
1062 } else {\r
1063 node.clear();\r
1064 }\r
1065\r
1066 // Must clear the parentNode silently upon remove from the TreeStore.\r
1067 // Any subsequent append to any node will trigger dirtiness\r
1068 // (It may be added to a different node of the same ID, e.g. "root").\r
1069 // lastParentId still needed for TreeStore's clearRemovedOnLoad functionality to be able to link\r
1070 // nodes in the removed array to nodes under the reloading node's tree.\r
1071 // to be able to \r
1072 if (!isMove) {\r
1073 node.set({\r
1074 parentId: null,\r
1075 lastParentId: me.getId()\r
1076 }, silently);\r
1077 }\r
1078\r
1079 // Coalesce sync operations across this operation\r
1080 if (treeStore) {\r
1081 treeStore.endUpdate();\r
1082 }\r
1083\r
1084 return node;\r
1085 },\r
1086\r
1087 /**\r
1088 * Creates a copy (clone) of this Node.\r
1089 * @param {String} [id] A new id, defaults to this Node's id.\r
1090 * @param {Boolean} [deep=false] True to recursively copy all child Nodes into the new Node.\r
1091 * False to copy without child Nodes.\r
1092 * @return {Ext.data.NodeInterface} A copy of this Node.\r
1093 */\r
1094 copy: function(newId, deep) {\r
1095 var me = this,\r
1096 result = me.callParent([newId]),\r
1097 len = me.childNodes ? me.childNodes.length : 0,\r
1098 i;\r
1099\r
1100 // Move child nodes across to the copy if required\r
1101 if (deep) {\r
1102 for (i = 0; i < len; i++) {\r
1103 result.appendChild(me.childNodes[i].copy(undefined, true));\r
1104 }\r
1105 }\r
1106 return result;\r
1107 },\r
1108\r
1109 /**\r
1110 * Clears the node.\r
1111 * @private\r
1112 * @param {Boolean} [erase=false] True to erase the node using the configured\r
1113 * proxy.\r
1114 */\r
1115 clear: function(erase) {\r
1116 var me = this;\r
1117\r
1118 // clear any references from the node\r
1119 me.parentNode = me.previousSibling = me.nextSibling = null;\r
1120 if (erase) {\r
1121 me.firstChild = me.lastChild = me.childNodes = null;\r
1122 }\r
1123 },\r
1124\r
1125 drop: function() {\r
1126 var me = this,\r
1127 childNodes = me.childNodes,\r
1128 parentNode = me.parentNode,\r
1129 len = childNodes ? childNodes.length : 0,\r
1130 i,\r
1131 node,\r
1132 treeStore;\r
1133\r
1134 // Ensure Model operations are performed.\r
1135 // Store removal is NOT handled.\r
1136 // TreeStore's afterDrop does nothing.\r
1137 me.callParent();\r
1138\r
1139 // If called in recursion from here, there'll be no parentNode\r
1140 if (parentNode) {\r
1141 treeStore = me.getTreeStore();\r
1142 // TreeStore.onNodeRemove also adds invisible descendant nodes to the remove tracking array.\r
1143 parentNode.removeChild(me);\r
1144 }\r
1145 // If we are the root, there'll be no parent node. It's a special case. We must update the TreeStore's root with a null node.\r
1146 else if (me.get('root')) {\r
1147 treeStore = me.getTreeStore();\r
1148 treeStore.setRoot(null);\r
1149 }\r
1150 // Removing a node removes the node and all *VISIBLE* descendant nodes from the Store and\r
1151 // adds them to the remove tracking array.\r
1152 //\r
1153 // After this point, no descendant nodes have a connection to the TreeStore.\r
1154\r
1155 // Coalesce sync operations across this operation\r
1156 treeStore && treeStore.beginUpdate();\r
1157\r
1158\r
1159 // Recurse down dropping all descendants.\r
1160 // This will NOT remove them from the store's data collection\r
1161 for (i = 0; i < len; i++) {\r
1162 node = childNodes[i];\r
1163\r
1164 // Detach descendant nodes so that they do not all attempt to perform removal from the parent.\r
1165 node.clear();\r
1166\r
1167 // Drop descendant nodes.\r
1168 node.drop();\r
1169 }\r
1170\r
1171 // Coalesce sync operations across this operation\r
1172 treeStore && treeStore.endUpdate();\r
1173 },\r
1174\r
1175 /**\r
1176 * Destroys the node.\r
1177 */\r
1178 erase: function(options) {\r
1179 var me = this,\r
1180 childNodes = me.childNodes,\r
1181 len = childNodes && childNodes.length,\r
1182 i,\r
1183 node;\r
1184\r
1185 // This unhooks this node from the tree structure\r
1186 // The UI is updated.\r
1187 // Now to recursively erase.\r
1188 me.remove();\r
1189\r
1190 // Clear removes linkage, so the erase's call into drop cannot recurse.\r
1191 // this method has to recurse to do all its stuff.\r
1192 me.clear(true);\r
1193 me.callParent([options]);\r
1194 for (i = 0; i < len; i++) {\r
1195 node = childNodes[i];\r
1196\r
1197 // The top level in the cascade is already removed.\r
1198 // Prevent the recursive erase calls doing further node removal.\r
1199 node.parentNode = null;\r
1200 node.erase(options);\r
1201 }\r
1202 },\r
1203\r
1204 /**\r
1205 * Inserts the first node before the second node in this nodes childNodes collection.\r
1206 * @param {Ext.data.NodeInterface/Ext.data.NodeInterface[]/Object} node The node to insert\r
1207 * @param {Ext.data.NodeInterface} refNode The node to insert before (if null the node is appended)\r
1208 * @return {Ext.data.NodeInterface} The inserted node\r
1209 */\r
1210 insertBefore: function(node, refNode, suppressEvents) {\r
1211 var me = this,\r
1212 index = me.indexOf(refNode),\r
1213 oldParent = node.parentNode,\r
1214 refIndex = index,\r
1215 childCount, previousSibling, i,\r
1216 treeStore = me.getTreeStore(),\r
1217 bulkUpdate = treeStore && treeStore.bulkUpdate;\r
1218\r
1219 if (!refNode) { // like standard Dom, refNode can be null for append\r
1220 return me.appendChild(node);\r
1221 }\r
1222\r
1223 // nothing to do\r
1224 if (node === refNode) {\r
1225 return false;\r
1226 }\r
1227\r
1228 // Make sure it is a record with the NodeInterface\r
1229 node = me.createNode(node);\r
1230\r
1231 if (suppressEvents !== true && me.fireEventArgs('beforeinsert', [me, node, refNode]) === false) {\r
1232 return false;\r
1233 }\r
1234\r
1235 // when moving internally, indexes will change after remove\r
1236 if (oldParent === me && me.indexOf(node) < index) {\r
1237 refIndex--;\r
1238 }\r
1239\r
1240 // it's a move, make sure we move it cleanly\r
1241 if (oldParent) {\r
1242 if (suppressEvents !== true && node.fireEventArgs('beforemove', [node, oldParent, me, index, refNode]) === false) {\r
1243 return false;\r
1244 }\r
1245 // Return false if a beforeremove listener vetoed the remove\r
1246 if (oldParent.removeChild(node, false, suppressEvents, oldParent.getTreeStore() === treeStore) === false) {\r
1247 return false;\r
1248 }\r
1249 }\r
1250\r
1251 // Coalesce sync operations across this operation\r
1252 // Node field setting (loaded, expanded) and node addition both trigger a sync if autoSync is set.\r
1253 // Nodes acquire a treeStore early now by virtue of getting a parentNode, so set operations on them will\r
1254 // arrive to this Store's onCollectionUpdate\r
1255 treeStore && treeStore.beginUpdate();\r
1256\r
1257 if (refIndex === 0) {\r
1258 me.setFirstChild(node);\r
1259 }\r
1260\r
1261 Ext.Array.splice(me.childNodes, refIndex, 0, node);\r
1262 node.parentNode = me;\r
1263\r
1264 node.nextSibling = refNode;\r
1265 refNode.previousSibling = node;\r
1266\r
1267 previousSibling = me.childNodes[refIndex - 1];\r
1268 if (previousSibling) {\r
1269 node.previousSibling = previousSibling;\r
1270 previousSibling.nextSibling = node;\r
1271 } else {\r
1272 node.previousSibling = null;\r
1273 }\r
1274\r
1275 // Integrate the new node into its new position.\r
1276 node.updateInfo(false, {\r
1277 parentId: me.getId(),\r
1278 index: refIndex,\r
1279 isFirst: refIndex === 0,\r
1280 isLast: false,\r
1281 depth: (me.data.depth||0) + 1\r
1282 });\r
1283\r
1284 // Update the index for all following siblings.\r
1285 for (i = refIndex + 1, childCount = me.childNodes.length; i < childCount; i++) {\r
1286 me.childNodes[i].updateInfo(false, {\r
1287 index: i\r
1288 });\r
1289 }\r
1290\r
1291 if (!me.isLoaded()) {\r
1292 if (bulkUpdate) {\r
1293 me.data.loaded = true;\r
1294 } else {\r
1295 me.set('loaded', true);\r
1296 }\r
1297 }\r
1298 // If this node didn't have any child nodes before, update myself\r
1299 else if (me.childNodes.length === 1 && !bulkUpdate) {\r
1300 me.triggerUIUpdate();\r
1301 }\r
1302\r
1303 // We register the subtree before we proceed so relayed events\r
1304 // (like nodeappend) from our TreeStore (if we have one) will be\r
1305 // able to use getNodeById.\r
1306 if (treeStore) {\r
1307 treeStore.registerNode(me, !bulkUpdate);\r
1308 }\r
1309\r
1310 // This node MUST fire its events first, so that if the TreeStore's\r
1311 // onNodeInsert loads and appends local children, the events are still in order;\r
1312 // This node appended this child first, before the descendant cascade.\r
1313 if (suppressEvents !== true) {\r
1314 me.fireEventArgs('insert', [me, node, refNode]);\r
1315\r
1316 if (oldParent) {\r
1317 node.fireEventArgs('move', [node, oldParent, me, refIndex, refNode]);\r
1318 }\r
1319 }\r
1320\r
1321 // Inform the TreeStore so that the node can be registered and added\r
1322 me.callTreeStore('onNodeInsert', [node, refIndex]);\r
1323\r
1324 // Coalesce sync operations across this operation\r
1325 // Node field setting (loaded, expanded) and node addition both trigger a sync if autoSync is set.\r
1326 if (treeStore) {\r
1327 treeStore.endUpdate();\r
1328 }\r
1329\r
1330 return node;\r
1331 },\r
1332\r
1333 /**\r
1334 * Inserts a node into this node.\r
1335 * @param {Number} index The zero-based index to insert the node at\r
1336 * @param {Ext.data.NodeInterface/Object} node The node to insert\r
1337 * @return {Ext.data.NodeInterface} The node you just inserted\r
1338 */\r
1339 insertChild: function(index, node) {\r
1340 var sibling = this.childNodes[index];\r
1341 if (sibling) {\r
1342 return this.insertBefore(node, sibling);\r
1343 }\r
1344 else {\r
1345 return this.appendChild(node);\r
1346 }\r
1347 },\r
1348\r
1349 /**\r
1350 * @private\r
1351 * Used by {@link Ext.tree.Column#initTemplateRendererData} to determine whether a node is the last *visible*\r
1352 * sibling.\r
1353 * \r
1354 */\r
1355 isLastVisible: function() {\r
1356 var me = this,\r
1357 result = me.data.isLast,\r
1358 next = me.nextSibling;\r
1359\r
1360 // If it is not the true last and the store is filtered\r
1361 // we need to see if any following siblings are visible.\r
1362 // If any are, return false.\r
1363 if (!result && me.getTreeStore().isFiltered()) {\r
1364 while (next) {\r
1365 if (next.data.visible) {\r
1366 return false;\r
1367 }\r
1368 next = next.nextSibling;\r
1369 }\r
1370 return true;\r
1371 }\r
1372 return result;\r
1373 },\r
1374\r
1375 /**\r
1376 * Removes this node from its parent.\r
1377 *\r
1378 * **If** the node is not phantom (only added in the client side), then it may be marked for removal.\r
1379 *\r
1380 * If the owning {@link Ext.data.TreeStore tree store} is set to {@link Ext.data.ProxyStore#trackRemoved track removed}\r
1381 * then the node will be added to the stack of nodes due to be removed the next time the store is synced with the server.\r
1382 *\r
1383 * If the owning {@link Ext.data.TreeStore tree store} is set to {@link Ext.data.ProxyStore#autoSync auto synchronize}\r
1384 * then the synchronize request will be initiated immediately.\r
1385 *\r
1386 * @param {Boolean} [erase=false] True to erase the node using the configured proxy. This is only needed when the\r
1387 * owning {@link Ext.data.TreeStore tree store} is not taking care of synchronization operations.\r
1388 *\r
1389 * @return {Ext.data.NodeInterface} this\r
1390 */\r
1391 remove: function(erase, suppressEvents) {\r
1392 var me = this,\r
1393 parentNode = me.parentNode;\r
1394\r
1395 if (parentNode) {\r
1396 parentNode.removeChild(me, erase, suppressEvents);\r
1397 } else if (erase) {\r
1398 // If we don't have a parent, just erase it\r
1399 me.erase(true);\r
1400 }\r
1401 return me;\r
1402 },\r
1403\r
1404 /**\r
1405 * Removes all child nodes from this node.\r
1406 * @param {Boolean} [erase=false] True to erase the node using the configured\r
1407 * proxy.\r
1408 * @return {Ext.data.NodeInterface} this\r
1409 * @return {Ext.data.NodeInterface} this\r
1410 */\r
1411 removeAll: function(erase, suppressEvents, fromParent) {\r
1412 // This method duplicates logic from removeChild for the sake of\r
1413 // speed since we can make a number of assumptions because we're\r
1414 // getting rid of everything\r
1415 var me = this,\r
1416 childNodes = me.childNodes,\r
1417 len = childNodes.length,\r
1418 node, treeStore, i;\r
1419\r
1420 // Avoid all this if nothing to remove\r
1421 if (!len) {\r
1422 return;\r
1423 }\r
1424\r
1425 // Inform the TreeStore so that descendant nodes can be removed.\r
1426 if (!fromParent) {\r
1427 treeStore = me.getTreeStore();\r
1428\r
1429 // Coalesce sync operations across this operation\r
1430 if (treeStore) {\r
1431 treeStore.beginUpdate();\r
1432\r
1433 // The remove of visible descendants is handled by the top level\r
1434 // call to onNodeRemove, so suspend firing the remove event so\r
1435 // that every descendant remove does not update the UI.\r
1436 treeStore.suspendEvent('remove');\r
1437\r
1438 me.callTreeStore('beforeNodeRemove', [childNodes, false]);\r
1439 }\r
1440 }\r
1441\r
1442 for (i = 0; i < len; ++i) {\r
1443 node = childNodes[i];\r
1444\r
1445 node.previousSibling = node.nextSibling = node.parentNode = null;\r
1446\r
1447 me.fireEventArgs('remove', [me, node, false]);\r
1448\r
1449 if (erase) {\r
1450 node.erase(true);\r
1451 }\r
1452 // Otherwise.... apparently, removeAll is always recursive.\r
1453 else {\r
1454 node.removeAll(false, suppressEvents, true);\r
1455 }\r
1456 }\r
1457\r
1458 // Inform the TreeStore so that all descendants are unregistered and unjoined.\r
1459 if (!fromParent && treeStore) {\r
1460 treeStore.resumeEvent('remove');\r
1461 me.callTreeStore('onNodeRemove', [childNodes, false]);\r
1462\r
1463 // Coalesce sync operations across this operation\r
1464 treeStore.endUpdate();\r
1465 }\r
1466\r
1467 me.firstChild = me.lastChild = null;\r
1468\r
1469 childNodes.length = 0;\r
1470 if (!fromParent) {\r
1471 me.triggerUIUpdate();\r
1472 }\r
1473 \r
1474 return me;\r
1475 },\r
1476\r
1477 /**\r
1478 * Returns the child node at the specified index.\r
1479 * @param {Number} index\r
1480 * @return {Ext.data.NodeInterface}\r
1481 */\r
1482 getChildAt: function(index) {\r
1483 return this.childNodes[index];\r
1484 },\r
1485\r
1486 /**\r
1487 * Replaces one child node in this node with another.\r
1488 * @param {Ext.data.NodeInterface} newChild The replacement node\r
1489 * @param {Ext.data.NodeInterface} oldChild The node to replace\r
1490 * @return {Ext.data.NodeInterface} The replaced node\r
1491 */\r
1492 replaceChild: function(newChild, oldChild, suppressEvents) {\r
1493 var s = oldChild ? oldChild.nextSibling : null;\r
1494\r
1495 this.removeChild(oldChild, false, suppressEvents);\r
1496 this.insertBefore(newChild, s, suppressEvents);\r
1497 return oldChild;\r
1498 },\r
1499\r
1500 /**\r
1501 * Returns the index of a child node\r
1502 * @param {Ext.data.NodeInterface} node\r
1503 * @return {Number} The index of the node or -1 if it was not found\r
1504 */\r
1505 indexOf: function(child) {\r
1506 return Ext.Array.indexOf(this.childNodes, child);\r
1507 },\r
1508 \r
1509 /**\r
1510 * Returns the index of a child node that matches the id\r
1511 * @param {String} id The id of the node to find\r
1512 * @return {Number} The index of the node or -1 if it was not found\r
1513 */\r
1514 indexOfId: function(id) {\r
1515 var childNodes = this.childNodes,\r
1516 len = childNodes.length,\r
1517 i = 0;\r
1518 \r
1519 for (; i < len; ++i) {\r
1520 if (childNodes[i].getId() === id) {\r
1521 return i;\r
1522 } \r
1523 }\r
1524 return -1;\r
1525 },\r
1526\r
1527 /**\r
1528 * Gets the hierarchical path from the root of the current node.\r
1529 * @param {String} [field] The field to construct the path from. Defaults to the model idProperty.\r
1530 * @param {String} [separator='/'] A separator to use.\r
1531 * @return {String} The node path\r
1532 */\r
1533 getPath: function(field, separator) {\r
1534 field = field || this.idProperty;\r
1535 separator = separator || '/';\r
1536\r
1537 var path = [this.get(field)],\r
1538 parent = this.parentNode;\r
1539\r
1540 while (parent) {\r
1541 path.unshift(parent.get(field));\r
1542 parent = parent.parentNode;\r
1543 }\r
1544 return separator + path.join(separator);\r
1545 },\r
1546\r
1547 /**\r
1548 * Returns depth of this node (the root node has a depth of 0)\r
1549 * @return {Number}\r
1550 */\r
1551 getDepth: function() {\r
1552 return this.get('depth');\r
1553 },\r
1554\r
1555 /**\r
1556 * Bubbles up the tree from this node, calling the specified function with each node. The arguments to the function\r
1557 * will be the args provided or the current node. If the function returns false at any point,\r
1558 * the bubble is stopped.\r
1559 * @param {Function} fn The function to call\r
1560 * @param {Object} [scope] The scope (this reference) in which the function is executed. Defaults to the current Node.\r
1561 * @param {Array} [args] The args to call the function with. Defaults to passing the current Node.\r
1562 */\r
1563 bubble: function(fn, scope, args) {\r
1564 var p = this;\r
1565 while (p) {\r
1566 if (fn.apply(scope || p, args || [p]) === false) {\r
1567 break;\r
1568 }\r
1569 p = p.parentNode;\r
1570 }\r
1571 },\r
1572\r
1573 //<deprecated since=0.99>\r
1574 cascade: function() {\r
1575 if (Ext.isDefined(Ext.global.console)) {\r
1576 Ext.global.console.warn('Ext.data.Node: cascade has been deprecated. Please use cascadeBy instead.');\r
1577 }\r
1578 return this.cascadeBy.apply(this, arguments);\r
1579 },\r
1580 //</deprecated>\r
1581\r
1582 /**\r
1583 * Cascades down the tree from this node, calling the specified functions with each node. The arguments to the function\r
1584 * will be the args provided or the current node. If the `before` function returns false at any point,\r
1585 * the cascade is stopped on that branch.\r
1586 *\r
1587 * Note that the 3 argument form passing `fn, scope, args` is still supported. The `fn` function is as before, called\r
1588 * *before* cascading down into child nodes. If it returns `false`, the child nodes are not traversed.\r
1589 *\r
1590 * @param {Object} spec An object containing before and after functions, scope and an argument list.\r
1591 * @param {Function} [spec.before] A function to call on a node *before* cascading down into child nodes.\r
1592 * If it returns `false`, the child nodes are not traversed.\r
1593 * @param {Function} [spec.after] A function to call on a node *after* cascading down into child nodes.\r
1594 * @param {Object} [spec.scope] The scope (this reference) in which the functions are executed. Defaults to the current Node.\r
1595 * @param {Array} [spec.args] The args to call the function with. Defaults to passing the current Node.\r
1596 */\r
1597 cascadeBy: function(before, scope, args, after) {\r
1598 var me = this;\r
1599\r
1600 if (arguments.length === 1 && !Ext.isFunction(before)) {\r
1601 after = before.after;\r
1602 scope = before.scope;\r
1603 args = before.args;\r
1604 before = before.before;\r
1605 }\r
1606 if (!before || before.apply(scope || me, args || [me]) !== false) {\r
1607 var childNodes = me.childNodes,\r
1608 length = childNodes.length,\r
1609 i;\r
1610\r
1611 for (i = 0; i < length; i++) {\r
1612 childNodes[i].cascadeBy.call(childNodes[i], before, scope, args, after);\r
1613 }\r
1614\r
1615 if (after) {\r
1616 after.apply(scope || me, args || [me]);\r
1617 }\r
1618 }\r
1619 },\r
1620\r
1621 /**\r
1622 * Iterates the child nodes of this node, calling the specified function \r
1623 * with each node. The arguments to the function will be the args \r
1624 * provided or the current node. If the function returns false at any \r
1625 * point, the iteration stops.\r
1626 * @param {Function} fn The function to call\r
1627 * @param {Object} [scope] The scope (_this_ reference) in which the \r
1628 * function is executed. Defaults to the Node on which eachChild is \r
1629 * called.\r
1630 * @param {Array} [args] The args to call the function with. Defaults to \r
1631 * passing the current Node.\r
1632 */\r
1633 eachChild: function(fn, scope, args) {\r
1634 var childNodes = this.childNodes,\r
1635 length = childNodes.length,\r
1636 i;\r
1637\r
1638 for (i = 0; i < length; i++) {\r
1639 if (fn.apply(scope || this, args || [childNodes[i]]) === false) {\r
1640 break;\r
1641 }\r
1642 }\r
1643 },\r
1644\r
1645 /**\r
1646 * Finds the first child that has the attribute with the specified value.\r
1647 * @param {String} attribute The attribute name\r
1648 * @param {Object} value The value to search for\r
1649 * @param {Boolean} [deep=false] True to search through nodes deeper than the immediate children\r
1650 * @return {Ext.data.NodeInterface} The found child or null if none was found\r
1651 */\r
1652 findChild: function(attribute, value, deep) {\r
1653 return this.findChildBy(function() {\r
1654 return this.get(attribute) == value;\r
1655 }, null, deep);\r
1656 },\r
1657\r
1658 /**\r
1659 * Finds the first child by a custom function. The child matches if the function passed returns true.\r
1660 * @param {Function} fn A function which must return true if the passed Node is the required Node.\r
1661 * @param {Object} [scope] The scope (this reference) in which the function is executed. Defaults to the Node being tested.\r
1662 * @param {Boolean} [deep=false] True to search through nodes deeper than the immediate children\r
1663 * @return {Ext.data.NodeInterface} The found child or null if none was found\r
1664 */\r
1665 findChildBy: function(fn, scope, deep) {\r
1666 var cs = this.childNodes,\r
1667 len = cs.length,\r
1668 i = 0, n, res;\r
1669\r
1670 for (; i < len; i++) {\r
1671 n = cs[i];\r
1672 if (fn.call(scope || n, n) === true) {\r
1673 return n;\r
1674 }\r
1675 else if (deep) {\r
1676 res = n.findChildBy(fn, scope, deep);\r
1677 if (res !== null) {\r
1678 return res;\r
1679 }\r
1680 }\r
1681 }\r
1682\r
1683 return null;\r
1684 },\r
1685\r
1686 /**\r
1687 * Returns true if this node is an ancestor (at any point) of the passed node.\r
1688 * @param {Ext.data.NodeInterface} node\r
1689 * @return {Boolean}\r
1690 */\r
1691 contains: function(node) {\r
1692 return node.isAncestor(this);\r
1693 },\r
1694\r
1695 /**\r
1696 * Returns true if the passed node is an ancestor (at any point) of this node.\r
1697 * @param {Ext.data.NodeInterface} node\r
1698 * @return {Boolean}\r
1699 */\r
1700 isAncestor: function(node) {\r
1701 var p = this.parentNode;\r
1702 while (p) {\r
1703 if (p === node) {\r
1704 return true;\r
1705 }\r
1706 p = p.parentNode;\r
1707 }\r
1708 return false;\r
1709 },\r
1710\r
1711 /**\r
1712 * Sorts this nodes children using the supplied sort function.\r
1713 * @param {Function} [sortFn] A function which, when passed two Nodes, returns -1, 0 or 1 depending upon required sort order.\r
1714 *\r
1715 * It omitted, the node is sorted according to the existing sorters in the owning {@link Ext.data.TreeStore TreeStore}.\r
1716 * @param {Boolean} [recursive=false] True to apply this sort recursively\r
1717 * @param {Boolean} [suppressEvent=false] True to not fire a sort event.\r
1718 */\r
1719 sort: function(sortFn, recursive, suppressEvent) {\r
1720 var me = this,\r
1721 childNodes = me.childNodes,\r
1722 ln = childNodes.length,\r
1723 i, n, info = {\r
1724 isFirst: true\r
1725 };\r
1726\r
1727 if (ln > 0) {\r
1728 if (!sortFn) {\r
1729 sortFn = me.getTreeStore().getSortFn();\r
1730 }\r
1731 Ext.Array.sort(childNodes, sortFn);\r
1732 me.setFirstChild(childNodes[0]);\r
1733 me.setLastChild(childNodes[ln - 1]);\r
1734\r
1735 for (i = 0; i < ln; i++) {\r
1736 n = childNodes[i];\r
1737 n.previousSibling = childNodes[i-1];\r
1738 n.nextSibling = childNodes[i+1];\r
1739 \r
1740 // Update the index and first/last status of children\r
1741 info.isLast = (i === ln - 1);\r
1742 info.index = i;\r
1743 n.updateInfo(false, info);\r
1744 info.isFirst = false;\r
1745\r
1746 if (recursive && !n.isLeaf()) {\r
1747 n.sort(sortFn, true, true);\r
1748 }\r
1749 }\r
1750\r
1751 // The suppressEvent flag is basically used to indicate a recursive sort\r
1752 if (suppressEvent !== true) {\r
1753 me.fireEventArgs('sort', [me, childNodes]);\r
1754\r
1755 // Inform the TreeStore that this node is sorted\r
1756 me.callTreeStore('onNodeSort', [childNodes]);\r
1757 }\r
1758 }\r
1759 },\r
1760\r
1761 /**\r
1762 * Returns `true` if this node is expanded.\r
1763 * @return {Boolean}\r
1764 */\r
1765 isExpanded: function() {\r
1766 return this.get('expanded');\r
1767 },\r
1768\r
1769 /**\r
1770 * Returns true if this node is loaded\r
1771 * @return {Boolean}\r
1772 */\r
1773 isLoaded: function() {\r
1774 return this.get('loaded');\r
1775 },\r
1776 \r
1777 /**\r
1778 * Returns true if this node is a branch node, and the entire branch is fully loaded.\r
1779 *\r
1780 * Using this method, it is possible to ascertain whether an\r
1781 * `expandAll()` call (_classic toolkit TreePanel method_) will have \r
1782 * access to all descendant nodes without incurring a store load.\r
1783 * @return {Boolean}\r
1784 */\r
1785 isBranchLoaded: function() {\r
1786 var isBranchLoaded = !this.isLeaf() && this.isLoaded();\r
1787\r
1788 if (isBranchLoaded) {\r
1789 this.cascadeBy(function(node) {\r
1790 if (!node.isLeaf()) {\r
1791 isBranchLoaded = isBranchLoaded || node.isBranchLoaded();\r
1792 }\r
1793 return isBranchLoaded;\r
1794 });\r
1795 }\r
1796 return isBranchLoaded;\r
1797 },\r
1798\r
1799 /**\r
1800 * Returns true if this node is loading\r
1801 * @return {Boolean}\r
1802 */\r
1803 isLoading: function() {\r
1804 return this.get('loading');\r
1805 },\r
1806\r
1807 /**\r
1808 * Returns true if this node is the root node\r
1809 * @return {Boolean}\r
1810 */\r
1811 isRoot: function() {\r
1812 return !this.parentNode;\r
1813 },\r
1814\r
1815 /**\r
1816 * Returns true if this node is visible. Note that visibility refers to\r
1817 * the structure of the tree, the {@link Ext.tree.Panel#rootVisible}\r
1818 * configuration is not taken into account here. If this method is called\r
1819 * on the root node, it will always be visible.\r
1820 * @return {Boolean}\r
1821 */\r
1822 isVisible: function() {\r
1823 var parent = this.parentNode;\r
1824 while (parent) {\r
1825 if (!parent.isExpanded()) {\r
1826 return false;\r
1827 }\r
1828 parent = parent.parentNode;\r
1829 }\r
1830 return true;\r
1831 },\r
1832\r
1833 /**\r
1834 * Expand this node.\r
1835 * @param {Boolean} [recursive=false] True to recursively expand all the children\r
1836 * @param {Function} [callback] The function to execute once the expand completes\r
1837 * @param {Object} [scope] The scope to run the callback in\r
1838 */\r
1839 expand: function(recursive, callback, scope) {\r
1840 var me = this,\r
1841 treeStore,\r
1842 resumeAddEvent;\r
1843\r
1844 // all paths must call the callback (eventually) or things like\r
1845 // selectPath fail\r
1846\r
1847 // First we start by checking if this node is a parent\r
1848 if (!me.isLeaf()) {\r
1849 // If it's loading, wait until it loads before proceeding\r
1850 if (me.isLoading()) {\r
1851 me.on('expand', function() {\r
1852 me.expand(recursive, callback, scope);\r
1853 }, me, {single: true});\r
1854 } else {\r
1855 // Now we check if this record is already expanding or expanded\r
1856 if (!me.isExpanded()) {\r
1857\r
1858 if (me.fireEventArgs('beforeexpand', [me]) !== false) {\r
1859\r
1860 // Here we are testing if all the descendant nodes required by a recursive expansion\r
1861 // are available without an asynchronous store load.\r
1862 //\r
1863 // That is either all branch nodes are loaded, or the store loads synchronously.\r
1864 //\r
1865 // If that is the case, then we do not want the TreeStore to fire add events\r
1866 // and update the UI (and layout) for every batch of child nodes inserted.\r
1867 // Instead, we suspend the add event, and at the end, fire a data refresh\r
1868 // so that the UI gets only one update. It will be a view refresh, but will\r
1869 // still be more efficient.\r
1870 if (recursive) {\r
1871 // Only the topmost node in a recursive expand should suspend the add event\r
1872 // and fire the refresh event, so if our parent is synchronously, recursively expanding,\r
1873 // we just flag that we are doing likewise.\r
1874 if (me.parentNode && me.parentNode.isSynchronousRecursiveExpand) {\r
1875 me.isSynchronousRecursiveExpand = true;\r
1876 }\r
1877 else {\r
1878 treeStore = me.getTreeStore();\r
1879 if (treeStore.getProxy().isSynchronous || me.isBranchLoaded()) {\r
1880 me.isSynchronousRecursiveExpand = true;\r
1881 treeStore.suspendEvent('add');\r
1882 resumeAddEvent = true;\r
1883 }\r
1884 }\r
1885 }\r
1886\r
1887 // Inform the TreeStore that we intend to expand, and that it should call onChildNodesAvailable\r
1888 // when the child nodes are available\r
1889 me.callTreeStore('onBeforeNodeExpand', [me.onChildNodesAvailable, me, [recursive, callback, scope]]);\r
1890\r
1891 // If we suspended the add event so that all additions of descendant nodes\r
1892 // did not update the UI, then resume the event here, and refresh the data\r
1893 if (resumeAddEvent) {\r
1894 treeStore.resumeEvent('add');\r
1895 treeStore.fireEvent('refresh', treeStore);\r
1896 }\r
1897 me.isSynchronousRecursiveExpand = false;\r
1898 }\r
1899\r
1900 } else if (recursive) {\r
1901 // If it is is already expanded but we want to recursively expand then call expandChildren\r
1902 me.expandChildren(true, callback, scope);\r
1903 } else {\r
1904 Ext.callback(callback, scope || me, [me.childNodes]);\r
1905 }\r
1906 }\r
1907 } else {\r
1908 // If it's not then we fire the callback right away\r
1909 Ext.callback(callback, scope || me); // leaf = no childNodes\r
1910 }\r
1911 },\r
1912\r
1913 /**\r
1914 * @private\r
1915 * Called as a callback from the beforeexpand listener fired by {@link #method-expand} when the child nodes have been loaded and appended.\r
1916 */\r
1917 onChildNodesAvailable: function(records, recursive, callback, scope) {\r
1918 var me = this,\r
1919 treeStore = me.getTreeStore(),\r
1920 bulkUpdate = treeStore && treeStore.bulkUpdate,\r
1921 ancestor,\r
1922 i,\r
1923 collapsedAncestors;\r
1924\r
1925 // Bracket expansion with layout suspension.\r
1926 // In optimum case, when recursive, child node data are loaded and expansion is synchronous within the suspension.\r
1927 Ext.suspendLayouts();\r
1928\r
1929 // Collect collapsed ancestors.\r
1930 // We are going to expand the topmost one while ensuring that\r
1931 // any intervening collapsed nodes have their expanded state as true.\r
1932 for (ancestor = me.parentNode; ancestor; ancestor = ancestor.parentNode) {\r
1933 if (!ancestor.isExpanded()) {\r
1934 (collapsedAncestors || (collapsedAncestors = [])).unshift(ancestor);\r
1935 }\r
1936 }\r
1937\r
1938 // Not structural. The TreeView's onUpdate listener just updates the [+] icon to [-] in response.\r
1939 \r
1940 if (bulkUpdate) {\r
1941 me.data.expanded = true;\r
1942 } else {\r
1943 me.set('expanded', true);\r
1944 }\r
1945\r
1946 // Set the intervening collapsed nodes to expanded state, then expand the topmost.\r
1947 // The whole descendant tree will be inserted into the collection below the topmost ancestor.\r
1948 if (collapsedAncestors) {\r
1949 // Ensure intervening collapsed nodes have their status set to expanded\r
1950 // Not structural. The TreeView's onUpdate listener just updates the [+] icon to [-] in response.\r
1951 for (i = 1; i < collapsedAncestors.length; i++) {\r
1952 ancestor = collapsedAncestors[i];\r
1953 if (bulkUpdate) {\r
1954 ancestor.data.expanded = true;\r
1955 } else {\r
1956 ancestor.set('expanded', true);\r
1957 }\r
1958 }\r
1959\r
1960 // Expand the topmost collapsed one.\r
1961 // The correctly set expanded states all the way down will ensure that\r
1962 // All nodes needed are inserted into the Store.\r
1963 collapsedAncestors[0].expand();\r
1964\r
1965 // Fire the expand event on all those intervening collapsed nodes\r
1966 for (i = 1; i < collapsedAncestors.length; i++) {\r
1967 ancestor = collapsedAncestors[i];\r
1968 ancestor.fireEventArgs('expand', [ancestor, ancestor.childNodes]);\r
1969 }\r
1970 } else {\r
1971 // TreeStore's onNodeExpand inserts the child nodes below the parent\r
1972 me.callTreeStore('onNodeExpand', [records, false]);\r
1973 }\r
1974\r
1975 me.fireEventArgs('expand', [me, records]);\r
1976\r
1977 // Call the expandChildren method if recursive was set to true\r
1978 if (recursive) {\r
1979 me.expandChildren(true, callback, scope);\r
1980 } else {\r
1981 Ext.callback(callback, scope || me, [me.childNodes]);\r
1982 }\r
1983\r
1984 Ext.resumeLayouts(true);\r
1985 },\r
1986\r
1987 /**\r
1988 * Expand all the children of this node.\r
1989 * @param {Boolean} [recursive=false] True to recursively expand all the children\r
1990 * @param {Function} [callback] The function to execute once all the children are expanded\r
1991 * @param {Object} [scope] The scope to run the callback in\r
1992 */\r
1993 expandChildren: function(recursive, callback, scope, /* private */ singleExpand) {\r
1994 var me = this,\r
1995 origCallback, i, allNodes, expandNodes, ln, node, treeStore;\r
1996\r
1997 // Ext 4.2.0 broke the API for this method by adding a singleExpand argument\r
1998 // at index 1. As of 4.2.3 The method signature has been reverted back\r
1999 // to its original pre-4.2.0 state, however, we must check to see if\r
2000 // the 4.2.0 version is being used for compatibility reasons.\r
2001 if (Ext.isBoolean(callback)) {\r
2002 origCallback = callback;\r
2003 callback = scope;\r
2004 scope = singleExpand;\r
2005 singleExpand = origCallback;\r
2006 }\r
2007\r
2008 if (singleExpand === undefined) {\r
2009 treeStore = me.getTreeStore();\r
2010 singleExpand = treeStore && treeStore.singleExpand;\r
2011 }\r
2012 allNodes = me.childNodes;\r
2013 expandNodes = [];\r
2014 ln = singleExpand ? Math.min(allNodes.length, 1) : allNodes.length;\r
2015\r
2016 for (i = 0; i < ln; ++i) {\r
2017 node = allNodes[i];\r
2018 if (!node.isLeaf()) {\r
2019 expandNodes[expandNodes.length] = node;\r
2020 }\r
2021 }\r
2022 ln = expandNodes.length;\r
2023\r
2024 for (i = 0; i < ln; ++i) {\r
2025 expandNodes[i].expand(recursive);\r
2026 }\r
2027\r
2028 if (callback) {\r
2029 Ext.callback(callback, scope || me, [me.childNodes]);\r
2030 }\r
2031 },\r
2032\r
2033 /**\r
2034 * Collapse this node.\r
2035 * @param {Boolean} [recursive=false] True to recursively collapse all the children\r
2036 * @param {Function} [callback] The function to execute once the collapse completes\r
2037 * @param {Object} [scope] The scope to run the callback in\r
2038 */\r
2039 collapse: function(recursive, callback, scope) {\r
2040 var me = this,\r
2041 expanded = me.isExpanded(),\r
2042 treeStore = me.getTreeStore(),\r
2043 bulkUpdate = treeStore && treeStore.bulkUpdate,\r
2044 len = me.childNodes.length,\r
2045 i, collapseChildren;\r
2046\r
2047 // If this is a parent and\r
2048 // already collapsed but the recursive flag is passed to target child nodes\r
2049 // or\r
2050 // the collapse is not vetoed by a listener\r
2051 if (!me.isLeaf() && ((!expanded && recursive) || me.fireEventArgs('beforecollapse', [me]) !== false)) {\r
2052 // Bracket collapsing with layout suspension.\r
2053 // Collapsing is synchronous within the suspension.\r
2054 Ext.suspendLayouts();\r
2055\r
2056 // Inform listeners of a collapse event if we are still expanded.\r
2057 if (me.isExpanded()) {\r
2058 \r
2059 // Set up the callback to set non-leaf descendants to collapsed if necessary.\r
2060 // If recursive, we just need to set all non-leaf descendants to collapsed state.\r
2061 // We *DO NOT* call collapse on them. That would attempt to remove their descendants\r
2062 // from the UI, and that is done: THIS node is collapsed - ALL descendants are removed from the UI.\r
2063 // Descendant non-leaves just silently change state.\r
2064 if (recursive) {\r
2065 collapseChildren = function() {\r
2066 for (i = 0; i < len; i++) {\r
2067 me.childNodes[i].setCollapsed(true);\r
2068 }\r
2069 };\r
2070 if (callback) {\r
2071 callback = Ext.Function.createSequence(collapseChildren, Ext.Function.bind(callback, scope, [me.childNodes]));\r
2072 } else {\r
2073 callback = collapseChildren;\r
2074 }\r
2075 } else if (callback) {\r
2076 callback = Ext.Function.bind(callback, scope, [me.childNodes]);\r
2077 }\r
2078\r
2079 // Not structural. The TreeView's onUpdate listener just updates the [+] icon to [-] in response.\r
2080 if (bulkUpdate) {\r
2081 me.data.expanded = false;\r
2082 } else {\r
2083 me.set('expanded', false);\r
2084 }\r
2085\r
2086 // Call the TreeStore's onNodeCollapse which removes all descendant nodes to achieve UI collapse\r
2087 // and passes callback on in its beforecollapse event which is poked into the animWrap for\r
2088 // final calling in the animation callback.\r
2089 me.callTreeStore('onNodeCollapse', [me.childNodes, callback, scope]);\r
2090\r
2091 me.fireEventArgs('collapse', [me, me.childNodes]);\r
2092\r
2093 // So that it's not called at the end\r
2094 callback = null;\r
2095 }\r
2096\r
2097 // If recursive, we just need to set all non-leaf descendants to collapsed state.\r
2098 // We *DO NOT* call collapse on them. That would attempt to remove their descendants\r
2099 // from the UI, and that is done: THIS node is collapsed - ALL descendants are removed from the UI.\r
2100 // Descendant non-leaves just silently change state.\r
2101 else if (recursive) {\r
2102 for (i = 0; i < len; i++) {\r
2103 me.childNodes[i].setCollapsed(true);\r
2104 }\r
2105 }\r
2106\r
2107 Ext.resumeLayouts(true);\r
2108 }\r
2109\r
2110 // Call the passed callback\r
2111 Ext.callback(callback, scope || me, [me.childNodes]);\r
2112 },\r
2113\r
2114 /**\r
2115 * @private\r
2116 *\r
2117 * Sets the node into the collapsed state without affecting the UI.\r
2118 *\r
2119 * This is called when a node is collapsed with the recursive flag. All the descendant\r
2120 * nodes will have been removed from the store, but descendant non-leaf nodes still\r
2121 * need to be set to the collapsed state without affecting the UI.\r
2122 */\r
2123 setCollapsed: function(recursive) {\r
2124 var me = this,\r
2125 len = me.childNodes.length,\r
2126 i;\r
2127\r
2128 // Only if we are not a leaf node and the collapse was not vetoed by a listener.\r
2129 if (!me.isLeaf() && me.fireEventArgs('beforecollapse', [me]) !== false) {\r
2130\r
2131 // Update the state directly.\r
2132 me.data.expanded = false;\r
2133\r
2134 // Listened for by NodeStore.onNodeCollapse, but will do nothing except pass on the\r
2135 // documented events because the records have already been removed from the store when\r
2136 // the ancestor node was collapsed.\r
2137 me.fireEventArgs('collapse', [me, me.childNodes]);\r
2138\r
2139 if (recursive) {\r
2140 for (i = 0; i < len; i++) {\r
2141 me.childNodes[i].setCollapsed(true);\r
2142 }\r
2143 }\r
2144 }\r
2145 },\r
2146\r
2147 /**\r
2148 * Collapse all the children of this node.\r
2149 * @param {Function} [recursive=false] True to recursively collapse all the children\r
2150 * @param {Function} [callback] The function to execute once all the children are collapsed\r
2151 * @param {Object} [scope] The scope to run the callback in\r
2152 */\r
2153 collapseChildren: function(recursive, callback, scope) {\r
2154 var me = this,\r
2155 i,\r
2156 allNodes = me.childNodes,\r
2157 ln = allNodes.length,\r
2158 collapseNodes = [],\r
2159 node;\r
2160\r
2161 // Only bother with loaded, expanded, non-leaf nodes\r
2162 for (i = 0; i < ln; ++i) {\r
2163 node = allNodes[i];\r
2164 if (!node.isLeaf() && node.isLoaded() && node.isExpanded()) {\r
2165 collapseNodes.push(node);\r
2166 }\r
2167 }\r
2168 ln = collapseNodes.length;\r
2169\r
2170 if (ln) {\r
2171 // Collapse the collapsible children.\r
2172 // Pass our callback to the last one.\r
2173 for (i = 0; i < ln; ++i) {\r
2174 node = collapseNodes[i];\r
2175 if (i === ln - 1) {\r
2176 node.collapse(recursive, callback, scope);\r
2177 } else {\r
2178 node.collapse(recursive);\r
2179 }\r
2180 }\r
2181 } else {\r
2182 // Nothing to collapse, so fire the callback\r
2183 Ext.callback(callback, scope);\r
2184 }\r
2185 },\r
2186\r
2187 /**\r
2188 * Fires the specified event with the passed parameters (minus the event name, plus the `options` object passed\r
2189 * to {@link Ext.mixin.Observable#addListener addListener}).\r
2190 *\r
2191 * An event may be set to bubble up an Observable parent hierarchy (See {@link Ext.Component#getBubbleTarget}) by\r
2192 * calling {@link Ext.mixin.Observable#enableBubble enableBubble}.\r
2193 *\r
2194 * @param {String} eventName The name of the event to fire.\r
2195 * @param {Object...} args Variable number of parameters are passed to handlers.\r
2196 * @return {Boolean} returns false if any of the handlers return false otherwise it returns true.\r
2197 */\r
2198 fireEvent: function(eventName) {\r
2199 return this.fireEventArgs(eventName, Ext.Array.slice(arguments, 1));\r
2200 },\r
2201\r
2202 // Node events always bubble, but events which bubble are always created, so bubble in a loop and\r
2203 // only fire when there are listeners at each level.\r
2204 // bubbled events always fire because they cannot tell if there is a listener at each level.\r
2205 fireEventArgs: function(eventName, args) {\r
2206 // Use the model prototype directly. If we have a BaseModel and then a SubModel,\r
2207 // if we access the superclass fireEventArgs it will just refer to the same method\r
2208 // and we end up in an infinite loop.\r
2209 var fireEventArgs = Ext.mixin.Observable.prototype.fireEventArgs,\r
2210 result, eventSource, topNode;\r
2211\r
2212 // The event bubbles (all native NodeInterface events do)...\r
2213 if (bubbledEvents[eventName]) {\r
2214 for (eventSource = this; result !== false && eventSource; eventSource = (topNode = eventSource).parentNode) {\r
2215 if (eventSource.hasListeners && eventSource.hasListeners[eventName]) {\r
2216 result = fireEventArgs.call(eventSource, eventName, args);\r
2217 }\r
2218 }\r
2219\r
2220 // We hit the topmost node in the loop above.\r
2221 // Fire the event on its TreeStore if any (might be a disembodied tree fragment with no TreeStore)\r
2222 if (result !== false) {\r
2223 eventSource = topNode.getTreeStore();\r
2224 if (eventSource && eventSource.hasListeners && eventSource.hasListeners[eventName = 'node' + eventName]) {\r
2225 result = eventSource.fireEventArgs(eventName, args);\r
2226 }\r
2227 }\r
2228 return result;\r
2229 }\r
2230 // Event does not bubble - call superclass fireEventArgs method\r
2231 else {\r
2232 return fireEventArgs.apply(this, arguments);\r
2233 }\r
2234 },\r
2235\r
2236 /**\r
2237 * Creates an object representation of this node including its children.\r
2238 */\r
2239 serialize: function(writerParam) {\r
2240 var writer = writerParam || new Ext.data.writer.Json({\r
2241 writeAllFields: true\r
2242 }),\r
2243 result = writer.getRecordData(this),\r
2244 childNodes = this.childNodes,\r
2245 len = childNodes.length,\r
2246 children, i;\r
2247\r
2248 if (len > 0) {\r
2249 result.children = children = [];\r
2250 for (i = 0; i < len; i++) {\r
2251 children.push(childNodes[i].serialize(writer));\r
2252 }\r
2253 }\r
2254 return result;\r
2255 },\r
2256\r
2257 // Used to inform the TreeStore that we belong to about some event which requires its participation.\r
2258 callTreeStore: function(funcName, args) {\r
2259 var me = this,\r
2260 target = me.getTreeStore(),\r
2261 fn = target && target[funcName];\r
2262\r
2263 if (target && fn) {\r
2264 args = args || [];\r
2265 if (args[0] !== me) {\r
2266 args.unshift(me);\r
2267 }\r
2268 fn.apply(target, args);\r
2269 }\r
2270 },\r
2271\r
2272 // Override private methods from Model superclass\r
2273 privates: {\r
2274 \r
2275 join: function(store) {\r
2276\r
2277 // Only the root node is linked to the TreeStore\r
2278 if (store.isTreeStore) {\r
2279 if (this.isRoot()) {\r
2280 this.treeStore = this.store = store;\r
2281 }\r
2282 }\r
2283\r
2284 // Other stores are always joined.\r
2285 // So a tree node could also be used by a flat store linked to a DataView\r
2286 else {\r
2287 this.callParent([store]);\r
2288 }\r
2289 },\r
2290\r
2291 // Used by Model base class methods to inform all interested Stores that the record has been mutated.\r
2292 callJoined: function(funcName, args) {\r
2293 this.callParent([funcName, args]);\r
2294 this.callTreeStore(funcName, args);\r
2295 }\r
2296 }\r
2297 };\r
2298 }\r
2299 }\r
2300});\r