Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / widgets / tree / TreeNode.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.tree.TreeNode\r
9  * @extends Ext.data.Node\r
10  * @cfg {String} text The text for this node\r
11  * @cfg {Boolean} expanded true to start the node expanded\r
12  * @cfg {Boolean} allowDrag False to make this node undraggable if {@link #draggable} = true (defaults to true)\r
13  * @cfg {Boolean} allowDrop False if this node cannot have child nodes dropped on it (defaults to true)\r
14  * @cfg {Boolean} disabled true to start the node disabled\r
15  * @cfg {String} icon The path to an icon for the node. The preferred way to do this\r
16  * is to use the cls or iconCls attributes and add the icon via a CSS background image.\r
17  * @cfg {String} cls A css class to be added to the node\r
18  * @cfg {String} iconCls A css class to be added to the nodes icon element for applying css background images\r
19  * @cfg {String} href URL of the link used for the node (defaults to #)\r
20  * @cfg {String} hrefTarget target frame for the link\r
21  * @cfg {Boolean} hidden True to render hidden. (Defaults to false).\r
22  * @cfg {String} qtip An Ext QuickTip for the node\r
23  * @cfg {Boolean} expandable If set to true, the node will always show a plus/minus icon, even when empty\r
24  * @cfg {String} qtipCfg An Ext QuickTip config for the node (used instead of qtip)\r
25  * @cfg {Boolean} singleClickExpand True for single click expand on this node\r
26  * @cfg {Function} uiProvider A UI <b>class</b> to use for this node (defaults to Ext.tree.TreeNodeUI)\r
27  * @cfg {Boolean} checked True to render a checked checkbox for this node, false to render an unchecked checkbox\r
28  * (defaults to undefined with no checkbox rendered)\r
29  * @cfg {Boolean} draggable True to make this node draggable (defaults to false)\r
30  * @cfg {Boolean} isTarget False to not allow this node to act as a drop target (defaults to true)\r
31  * @cfg {Boolean} allowChildren False to not allow this node to have child nodes (defaults to true)\r
32  * @cfg {Boolean} editable False to not allow this node to be edited by an {@link Ext.tree.TreeEditor} (defaults to true)\r
33  * @constructor\r
34  * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node\r
35  */\r
36 Ext.tree.TreeNode = function(attributes){\r
37     attributes = attributes || {};\r
38     if(Ext.isString(attributes)){\r
39         attributes = {text: attributes};\r
40     }\r
41     this.childrenRendered = false;\r
42     this.rendered = false;\r
43     Ext.tree.TreeNode.superclass.constructor.call(this, attributes);\r
44     this.expanded = attributes.expanded === true;\r
45     this.isTarget = attributes.isTarget !== false;\r
46     this.draggable = attributes.draggable !== false && attributes.allowDrag !== false;\r
47     this.allowChildren = attributes.allowChildren !== false && attributes.allowDrop !== false;\r
48 \r
49     /**\r
50      * Read-only. The text for this node. To change it use <code>{@link #setText}</code>.\r
51      * @type String\r
52      */\r
53     this.text = attributes.text;\r
54     /**\r
55      * True if this node is disabled.\r
56      * @type Boolean\r
57      */\r
58     this.disabled = attributes.disabled === true;\r
59     /**\r
60      * True if this node is hidden.\r
61      * @type Boolean\r
62      */\r
63     this.hidden = attributes.hidden === true;\r
64 \r
65     this.addEvents(\r
66         /**\r
67         * @event textchange\r
68         * Fires when the text for this node is changed\r
69         * @param {Node} this This node\r
70         * @param {String} text The new text\r
71         * @param {String} oldText The old text\r
72         */\r
73         'textchange',\r
74         /**\r
75         * @event beforeexpand\r
76         * Fires before this node is expanded, return false to cancel.\r
77         * @param {Node} this This node\r
78         * @param {Boolean} deep\r
79         * @param {Boolean} anim\r
80         */\r
81         'beforeexpand',\r
82         /**\r
83         * @event beforecollapse\r
84         * Fires before this node is collapsed, return false to cancel.\r
85         * @param {Node} this This node\r
86         * @param {Boolean} deep\r
87         * @param {Boolean} anim\r
88         */\r
89         'beforecollapse',\r
90         /**\r
91         * @event expand\r
92         * Fires when this node is expanded\r
93         * @param {Node} this This node\r
94         */\r
95         'expand',\r
96         /**\r
97         * @event disabledchange\r
98         * Fires when the disabled status of this node changes\r
99         * @param {Node} this This node\r
100         * @param {Boolean} disabled\r
101         */\r
102         'disabledchange',\r
103         /**\r
104         * @event collapse\r
105         * Fires when this node is collapsed\r
106         * @param {Node} this This node\r
107         */\r
108         'collapse',\r
109         /**\r
110         * @event beforeclick\r
111         * Fires before click processing. Return false to cancel the default action.\r
112         * @param {Node} this This node\r
113         * @param {Ext.EventObject} e The event object\r
114         */\r
115         'beforeclick',\r
116         /**\r
117         * @event click\r
118         * Fires when this node is clicked\r
119         * @param {Node} this This node\r
120         * @param {Ext.EventObject} e The event object\r
121         */\r
122         'click',\r
123         /**\r
124         * @event checkchange\r
125         * Fires when a node with a checkbox's checked property changes\r
126         * @param {Node} this This node\r
127         * @param {Boolean} checked\r
128         */\r
129         'checkchange',\r
130         /**\r
131         * @event beforedblclick\r
132         * Fires before double click processing. Return false to cancel the default action.\r
133         * @param {Node} this This node\r
134         * @param {Ext.EventObject} e The event object\r
135         */\r
136         'beforedblclick',\r
137         /**\r
138         * @event dblclick\r
139         * Fires when this node is double clicked\r
140         * @param {Node} this This node\r
141         * @param {Ext.EventObject} e The event object\r
142         */\r
143         'dblclick',\r
144         /**\r
145         * @event contextmenu\r
146         * Fires when this node is right clicked\r
147         * @param {Node} this This node\r
148         * @param {Ext.EventObject} e The event object\r
149         */\r
150         'contextmenu',\r
151         /**\r
152         * @event beforechildrenrendered\r
153         * Fires right before the child nodes for this node are rendered\r
154         * @param {Node} this This node\r
155         */\r
156         'beforechildrenrendered'\r
157     );\r
158 \r
159     var uiClass = this.attributes.uiProvider || this.defaultUI || Ext.tree.TreeNodeUI;\r
160 \r
161     /**\r
162      * Read-only. The UI for this node\r
163      * @type TreeNodeUI\r
164      */\r
165     this.ui = new uiClass(this);\r
166 };\r
167 Ext.extend(Ext.tree.TreeNode, Ext.data.Node, {\r
168     preventHScroll : true,\r
169     /**\r
170      * Returns true if this node is expanded\r
171      * @return {Boolean}\r
172      */\r
173     isExpanded : function(){\r
174         return this.expanded;\r
175     },\r
176 \r
177 /**\r
178  * Returns the UI object for this node.\r
179  * @return {TreeNodeUI} The object which is providing the user interface for this tree\r
180  * node. Unless otherwise specified in the {@link #uiProvider}, this will be an instance\r
181  * of {@link Ext.tree.TreeNodeUI}\r
182  */\r
183     getUI : function(){\r
184         return this.ui;\r
185     },\r
186 \r
187     getLoader : function(){\r
188         var owner;\r
189         return this.loader || ((owner = this.getOwnerTree()) && owner.loader ? owner.loader : (this.loader = new Ext.tree.TreeLoader()));\r
190     },\r
191 \r
192     // private override\r
193     setFirstChild : function(node){\r
194         var of = this.firstChild;\r
195         Ext.tree.TreeNode.superclass.setFirstChild.call(this, node);\r
196         if(this.childrenRendered && of && node != of){\r
197             of.renderIndent(true, true);\r
198         }\r
199         if(this.rendered){\r
200             this.renderIndent(true, true);\r
201         }\r
202     },\r
203 \r
204     // private override\r
205     setLastChild : function(node){\r
206         var ol = this.lastChild;\r
207         Ext.tree.TreeNode.superclass.setLastChild.call(this, node);\r
208         if(this.childrenRendered && ol && node != ol){\r
209             ol.renderIndent(true, true);\r
210         }\r
211         if(this.rendered){\r
212             this.renderIndent(true, true);\r
213         }\r
214     },\r
215 \r
216     // these methods are overridden to provide lazy rendering support\r
217     // private override\r
218     appendChild : function(n){\r
219         if(!n.render && !Ext.isArray(n)){\r
220             n = this.getLoader().createNode(n);\r
221         }\r
222         var node = Ext.tree.TreeNode.superclass.appendChild.call(this, n);\r
223         if(node && this.childrenRendered){\r
224             node.render();\r
225         }\r
226         this.ui.updateExpandIcon();\r
227         return node;\r
228     },\r
229 \r
230     // private override\r
231     removeChild : function(node, destroy){\r
232         this.ownerTree.getSelectionModel().unselect(node);\r
233         Ext.tree.TreeNode.superclass.removeChild.apply(this, arguments);\r
234         // if it's been rendered remove dom node\r
235         if(node.ui.rendered){\r
236             node.ui.remove();\r
237         }\r
238         if(this.childNodes.length < 1){\r
239             this.collapse(false, false);\r
240         }else{\r
241             this.ui.updateExpandIcon();\r
242         }\r
243         if(!this.firstChild && !this.isHiddenRoot()) {\r
244             this.childrenRendered = false;\r
245         }\r
246         return node;\r
247     },\r
248 \r
249     // private override\r
250     insertBefore : function(node, refNode){\r
251         if(!node.render){\r
252             node = this.getLoader().createNode(node);\r
253         }\r
254         var newNode = Ext.tree.TreeNode.superclass.insertBefore.call(this, node, refNode);\r
255         if(newNode && refNode && this.childrenRendered){\r
256             node.render();\r
257         }\r
258         this.ui.updateExpandIcon();\r
259         return newNode;\r
260     },\r
261 \r
262     /**\r
263      * Sets the text for this node\r
264      * @param {String} text\r
265      */\r
266     setText : function(text){\r
267         var oldText = this.text;\r
268         this.text = this.attributes.text = text;\r
269         if(this.rendered){ // event without subscribing\r
270             this.ui.onTextChange(this, text, oldText);\r
271         }\r
272         this.fireEvent('textchange', this, text, oldText);\r
273     },\r
274 \r
275     /**\r
276      * Triggers selection of this node\r
277      */\r
278     select : function(){\r
279         var t = this.getOwnerTree();\r
280         if(t){\r
281             t.getSelectionModel().select(this);\r
282         }\r
283     },\r
284 \r
285     /**\r
286      * Triggers deselection of this node\r
287      * @param {Boolean} silent (optional) True to stop selection change events from firing.\r
288      */\r
289     unselect : function(silent){\r
290         var t = this.getOwnerTree();\r
291         if(t){\r
292             t.getSelectionModel().unselect(this, silent);\r
293         }\r
294     },\r
295 \r
296     /**\r
297      * Returns true if this node is selected\r
298      * @return {Boolean}\r
299      */\r
300     isSelected : function(){\r
301         var t = this.getOwnerTree();\r
302         return t ? t.getSelectionModel().isSelected(this) : false;\r
303     },\r
304 \r
305     /**\r
306      * Expand this node.\r
307      * @param {Boolean} deep (optional) True to expand all children as well\r
308      * @param {Boolean} anim (optional) false to cancel the default animation\r
309      * @param {Function} callback (optional) A callback to be called when\r
310      * expanding this node completes (does not wait for deep expand to complete).\r
311      * Called with 1 parameter, this node.\r
312      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.\r
313      */\r
314     expand : function(deep, anim, callback, scope){\r
315         if(!this.expanded){\r
316             if(this.fireEvent('beforeexpand', this, deep, anim) === false){\r
317                 return;\r
318             }\r
319             if(!this.childrenRendered){\r
320                 this.renderChildren();\r
321             }\r
322             this.expanded = true;\r
323             if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){\r
324                 this.ui.animExpand(function(){\r
325                     this.fireEvent('expand', this);\r
326                     this.runCallback(callback, scope || this, [this]);\r
327                     if(deep === true){\r
328                         this.expandChildNodes(true);\r
329                     }\r
330                 }.createDelegate(this));\r
331                 return;\r
332             }else{\r
333                 this.ui.expand();\r
334                 this.fireEvent('expand', this);\r
335                 this.runCallback(callback, scope || this, [this]);\r
336             }\r
337         }else{\r
338            this.runCallback(callback, scope || this, [this]);\r
339         }\r
340         if(deep === true){\r
341             this.expandChildNodes(true);\r
342         }\r
343     },\r
344 \r
345     runCallback : function(cb, scope, args){\r
346         if(Ext.isFunction(cb)){\r
347             cb.apply(scope, args);\r
348         }\r
349     },\r
350 \r
351     isHiddenRoot : function(){\r
352         return this.isRoot && !this.getOwnerTree().rootVisible;\r
353     },\r
354 \r
355     /**\r
356      * Collapse this node.\r
357      * @param {Boolean} deep (optional) True to collapse all children as well\r
358      * @param {Boolean} anim (optional) false to cancel the default animation\r
359      * @param {Function} callback (optional) A callback to be called when\r
360      * expanding this node completes (does not wait for deep expand to complete).\r
361      * Called with 1 parameter, this node.\r
362      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.\r
363      */\r
364     collapse : function(deep, anim, callback, scope){\r
365         if(this.expanded && !this.isHiddenRoot()){\r
366             if(this.fireEvent('beforecollapse', this, deep, anim) === false){\r
367                 return;\r
368             }\r
369             this.expanded = false;\r
370             if((this.getOwnerTree().animate && anim !== false) || anim){\r
371                 this.ui.animCollapse(function(){\r
372                     this.fireEvent('collapse', this);\r
373                     this.runCallback(callback, scope || this, [this]);\r
374                     if(deep === true){\r
375                         this.collapseChildNodes(true);\r
376                     }\r
377                 }.createDelegate(this));\r
378                 return;\r
379             }else{\r
380                 this.ui.collapse();\r
381                 this.fireEvent('collapse', this);\r
382                 this.runCallback(callback, scope || this, [this]);\r
383             }\r
384         }else if(!this.expanded){\r
385             this.runCallback(callback, scope || this, [this]);\r
386         }\r
387         if(deep === true){\r
388             var cs = this.childNodes;\r
389             for(var i = 0, len = cs.length; i < len; i++) {\r
390                 cs[i].collapse(true, false);\r
391             }\r
392         }\r
393     },\r
394 \r
395     // private\r
396     delayedExpand : function(delay){\r
397         if(!this.expandProcId){\r
398             this.expandProcId = this.expand.defer(delay, this);\r
399         }\r
400     },\r
401 \r
402     // private\r
403     cancelExpand : function(){\r
404         if(this.expandProcId){\r
405             clearTimeout(this.expandProcId);\r
406         }\r
407         this.expandProcId = false;\r
408     },\r
409 \r
410     /**\r
411      * Toggles expanded/collapsed state of the node\r
412      */\r
413     toggle : function(){\r
414         if(this.expanded){\r
415             this.collapse();\r
416         }else{\r
417             this.expand();\r
418         }\r
419     },\r
420 \r
421     /**\r
422      * Ensures all parent nodes are expanded, and if necessary, scrolls\r
423      * the node into view.\r
424      * @param {Function} callback (optional) A function to call when the node has been made visible.\r
425      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.\r
426      */\r
427     ensureVisible : function(callback, scope){\r
428         var tree = this.getOwnerTree();\r
429         tree.expandPath(this.parentNode ? this.parentNode.getPath() : this.getPath(), false, function(){\r
430             var node = tree.getNodeById(this.id);  // Somehow if we don't do this, we lose changes that happened to node in the meantime\r
431             tree.getTreeEl().scrollChildIntoView(node.ui.anchor);\r
432             this.runCallback(callback, scope || this, [this]);\r
433         }.createDelegate(this));\r
434     },\r
435 \r
436     /**\r
437      * Expand all child nodes\r
438      * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes\r
439      */\r
440     expandChildNodes : function(deep){\r
441         var cs = this.childNodes;\r
442         for(var i = 0, len = cs.length; i < len; i++) {\r
443                 cs[i].expand(deep);\r
444         }\r
445     },\r
446 \r
447     /**\r
448      * Collapse all child nodes\r
449      * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes\r
450      */\r
451     collapseChildNodes : function(deep){\r
452         var cs = this.childNodes;\r
453         for(var i = 0, len = cs.length; i < len; i++) {\r
454                 cs[i].collapse(deep);\r
455         }\r
456     },\r
457 \r
458     /**\r
459      * Disables this node\r
460      */\r
461     disable : function(){\r
462         this.disabled = true;\r
463         this.unselect();\r
464         if(this.rendered && this.ui.onDisableChange){ // event without subscribing\r
465             this.ui.onDisableChange(this, true);\r
466         }\r
467         this.fireEvent('disabledchange', this, true);\r
468     },\r
469 \r
470     /**\r
471      * Enables this node\r
472      */\r
473     enable : function(){\r
474         this.disabled = false;\r
475         if(this.rendered && this.ui.onDisableChange){ // event without subscribing\r
476             this.ui.onDisableChange(this, false);\r
477         }\r
478         this.fireEvent('disabledchange', this, false);\r
479     },\r
480 \r
481     // private\r
482     renderChildren : function(suppressEvent){\r
483         if(suppressEvent !== false){\r
484             this.fireEvent('beforechildrenrendered', this);\r
485         }\r
486         var cs = this.childNodes;\r
487         for(var i = 0, len = cs.length; i < len; i++){\r
488             cs[i].render(true);\r
489         }\r
490         this.childrenRendered = true;\r
491     },\r
492 \r
493     // private\r
494     sort : function(fn, scope){\r
495         Ext.tree.TreeNode.superclass.sort.apply(this, arguments);\r
496         if(this.childrenRendered){\r
497             var cs = this.childNodes;\r
498             for(var i = 0, len = cs.length; i < len; i++){\r
499                 cs[i].render(true);\r
500             }\r
501         }\r
502     },\r
503 \r
504     // private\r
505     render : function(bulkRender){\r
506         this.ui.render(bulkRender);\r
507         if(!this.rendered){\r
508             // make sure it is registered\r
509             this.getOwnerTree().registerNode(this);\r
510             this.rendered = true;\r
511             if(this.expanded){\r
512                 this.expanded = false;\r
513                 this.expand(false, false);\r
514             }\r
515         }\r
516     },\r
517 \r
518     // private\r
519     renderIndent : function(deep, refresh){\r
520         if(refresh){\r
521             this.ui.childIndent = null;\r
522         }\r
523         this.ui.renderIndent();\r
524         if(deep === true && this.childrenRendered){\r
525             var cs = this.childNodes;\r
526             for(var i = 0, len = cs.length; i < len; i++){\r
527                 cs[i].renderIndent(true, refresh);\r
528             }\r
529         }\r
530     },\r
531 \r
532     beginUpdate : function(){\r
533         this.childrenRendered = false;\r
534     },\r
535 \r
536     endUpdate : function(){\r
537         if(this.expanded && this.rendered){\r
538             this.renderChildren();\r
539         }\r
540     },\r
541 \r
542     destroy : function(){\r
543         this.unselect(true);\r
544         Ext.tree.TreeNode.superclass.destroy.call(this);\r
545         Ext.destroy(this.ui, this.loader);\r
546         this.ui = this.loader = null;\r
547     },\r
548 \r
549     // private\r
550     onIdChange : function(id){\r
551         this.ui.onIdChange(id);\r
552     }\r
553 });\r
554 \r
555 Ext.tree.TreePanel.nodeTypes.node = Ext.tree.TreeNode;