3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.tree.TreeNode
9 * @extends Ext.data.Node
10 * @cfg {String} text The text for this node
11 * @cfg {Boolean} expanded true to start the node expanded
12 * @cfg {Boolean} allowDrag False to make this node undraggable if {@link #draggable} = true (defaults to true)
13 * @cfg {Boolean} allowDrop False if this node cannot have child nodes dropped on it (defaults to true)
14 * @cfg {Boolean} disabled true to start the node disabled
15 * @cfg {String} icon The path to an icon for the node. The preferred way to do this
16 * is to use the cls or iconCls attributes and add the icon via a CSS background image.
17 * @cfg {String} cls A css class to be added to the node
18 * @cfg {String} iconCls A css class to be added to the nodes icon element for applying css background images
19 * @cfg {String} href URL of the link used for the node (defaults to #)
20 * @cfg {String} hrefTarget target frame for the link
21 * @cfg {Boolean} hidden True to render hidden. (Defaults to false).
22 * @cfg {String} qtip An Ext QuickTip for the node
23 * @cfg {Boolean} expandable If set to true, the node will always show a plus/minus icon, even when empty
24 * @cfg {String} qtipCfg An Ext QuickTip config for the node (used instead of qtip)
25 * @cfg {Boolean} singleClickExpand True for single click expand on this node
26 * @cfg {Function} uiProvider A UI <b>class</b> to use for this node (defaults to Ext.tree.TreeNodeUI)
27 * @cfg {Boolean} checked True to render a checked checkbox for this node, false to render an unchecked checkbox
28 * (defaults to undefined with no checkbox rendered)
29 * @cfg {Boolean} draggable True to make this node draggable (defaults to false)
30 * @cfg {Boolean} isTarget False to not allow this node to act as a drop target (defaults to true)
31 * @cfg {Boolean} allowChildren False to not allow this node to have child nodes (defaults to true)
32 * @cfg {Boolean} editable False to not allow this node to be edited by an {@link Ext.tree.TreeEditor} (defaults to true)
34 * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node
36 Ext.tree.TreeNode = function(attributes){
37 attributes = attributes || {};
38 if(Ext.isString(attributes)){
39 attributes = {text: attributes};
41 this.childrenRendered = false;
42 this.rendered = false;
43 Ext.tree.TreeNode.superclass.constructor.call(this, attributes);
44 this.expanded = attributes.expanded === true;
45 this.isTarget = attributes.isTarget !== false;
46 this.draggable = attributes.draggable !== false && attributes.allowDrag !== false;
47 this.allowChildren = attributes.allowChildren !== false && attributes.allowDrop !== false;
50 * Read-only. The text for this node. To change it use <code>{@link #setText}</code>.
53 this.text = attributes.text;
55 * True if this node is disabled.
58 this.disabled = attributes.disabled === true;
60 * True if this node is hidden.
63 this.hidden = attributes.hidden === true;
68 * Fires when the text for this node is changed
69 * @param {Node} this This node
70 * @param {String} text The new text
71 * @param {String} oldText The old text
76 * Fires before this node is expanded, return false to cancel.
77 * @param {Node} this This node
78 * @param {Boolean} deep
79 * @param {Boolean} anim
83 * @event beforecollapse
84 * Fires before this node is collapsed, return false to cancel.
85 * @param {Node} this This node
86 * @param {Boolean} deep
87 * @param {Boolean} anim
92 * Fires when this node is expanded
93 * @param {Node} this This node
97 * @event disabledchange
98 * Fires when the disabled status of this node changes
99 * @param {Node} this This node
100 * @param {Boolean} disabled
105 * Fires when this node is collapsed
106 * @param {Node} this This node
111 * Fires before click processing. Return false to cancel the default action.
112 * @param {Node} this This node
113 * @param {Ext.EventObject} e The event object
118 * Fires when this node is clicked
119 * @param {Node} this This node
120 * @param {Ext.EventObject} e The event object
125 * Fires when a node with a checkbox's checked property changes
126 * @param {Node} this This node
127 * @param {Boolean} checked
131 * @event beforedblclick
132 * Fires before double click processing. Return false to cancel the default action.
133 * @param {Node} this This node
134 * @param {Ext.EventObject} e The event object
139 * Fires when this node is double clicked
140 * @param {Node} this This node
141 * @param {Ext.EventObject} e The event object
146 * Fires when this node is right clicked
147 * @param {Node} this This node
148 * @param {Ext.EventObject} e The event object
152 * @event beforechildrenrendered
153 * Fires right before the child nodes for this node are rendered
154 * @param {Node} this This node
156 'beforechildrenrendered'
159 var uiClass = this.attributes.uiProvider || this.defaultUI || Ext.tree.TreeNodeUI;
162 * Read-only. The UI for this node
165 this.ui = new uiClass(this);
167 Ext.extend(Ext.tree.TreeNode, Ext.data.Node, {
168 preventHScroll : true,
170 * Returns true if this node is expanded
173 isExpanded : function(){
174 return this.expanded;
178 * Returns the UI object for this node.
179 * @return {TreeNodeUI} The object which is providing the user interface for this tree
180 * node. Unless otherwise specified in the {@link #uiProvider}, this will be an instance
181 * of {@link Ext.tree.TreeNodeUI}
187 getLoader : function(){
189 return this.loader || ((owner = this.getOwnerTree()) && owner.loader ? owner.loader : (this.loader = new Ext.tree.TreeLoader()));
193 setFirstChild : function(node){
194 var of = this.firstChild;
195 Ext.tree.TreeNode.superclass.setFirstChild.call(this, node);
196 if(this.childrenRendered && of && node != of){
197 of.renderIndent(true, true);
200 this.renderIndent(true, true);
205 setLastChild : function(node){
206 var ol = this.lastChild;
207 Ext.tree.TreeNode.superclass.setLastChild.call(this, node);
208 if(this.childrenRendered && ol && node != ol){
209 ol.renderIndent(true, true);
212 this.renderIndent(true, true);
216 // these methods are overridden to provide lazy rendering support
218 appendChild : function(n){
219 if(!n.render && !Ext.isArray(n)){
220 n = this.getLoader().createNode(n);
222 var node = Ext.tree.TreeNode.superclass.appendChild.call(this, n);
223 if(node && this.childrenRendered){
226 this.ui.updateExpandIcon();
231 removeChild : function(node, destroy){
232 this.ownerTree.getSelectionModel().unselect(node);
233 Ext.tree.TreeNode.superclass.removeChild.apply(this, arguments);
234 // only update the ui if we're not destroying
236 // if it's been rendered remove dom node
237 if(node.ui.rendered){
240 if(this.childNodes.length < 1){
241 this.collapse(false, false);
243 this.ui.updateExpandIcon();
245 if(!this.firstChild && !this.isHiddenRoot()){
246 this.childrenRendered = false;
253 insertBefore : function(node, refNode){
255 node = this.getLoader().createNode(node);
257 var newNode = Ext.tree.TreeNode.superclass.insertBefore.call(this, node, refNode);
258 if(newNode && refNode && this.childrenRendered){
261 this.ui.updateExpandIcon();
266 * Sets the text for this node
267 * @param {String} text
269 setText : function(text){
270 var oldText = this.text;
271 this.text = this.attributes.text = text;
272 if(this.rendered){ // event without subscribing
273 this.ui.onTextChange(this, text, oldText);
275 this.fireEvent('textchange', this, text, oldText);
279 * Triggers selection of this node
282 var t = this.getOwnerTree();
284 t.getSelectionModel().select(this);
289 * Triggers deselection of this node
290 * @param {Boolean} silent (optional) True to stop selection change events from firing.
292 unselect : function(silent){
293 var t = this.getOwnerTree();
295 t.getSelectionModel().unselect(this, silent);
300 * Returns true if this node is selected
303 isSelected : function(){
304 var t = this.getOwnerTree();
305 return t ? t.getSelectionModel().isSelected(this) : false;
310 * @param {Boolean} deep (optional) True to expand all children as well
311 * @param {Boolean} anim (optional) false to cancel the default animation
312 * @param {Function} callback (optional) A callback to be called when
313 * expanding this node completes (does not wait for deep expand to complete).
314 * Called with 1 parameter, this node.
315 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
317 expand : function(deep, anim, callback, scope){
319 if(this.fireEvent('beforeexpand', this, deep, anim) === false){
322 if(!this.childrenRendered){
323 this.renderChildren();
325 this.expanded = true;
326 if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
327 this.ui.animExpand(function(){
328 this.fireEvent('expand', this);
329 this.runCallback(callback, scope || this, [this]);
331 this.expandChildNodes(true);
333 }.createDelegate(this));
337 this.fireEvent('expand', this);
338 this.runCallback(callback, scope || this, [this]);
341 this.runCallback(callback, scope || this, [this]);
344 this.expandChildNodes(true);
348 runCallback : function(cb, scope, args){
349 if(Ext.isFunction(cb)){
350 cb.apply(scope, args);
354 isHiddenRoot : function(){
355 return this.isRoot && !this.getOwnerTree().rootVisible;
359 * Collapse this node.
360 * @param {Boolean} deep (optional) True to collapse all children as well
361 * @param {Boolean} anim (optional) false to cancel the default animation
362 * @param {Function} callback (optional) A callback to be called when
363 * expanding this node completes (does not wait for deep expand to complete).
364 * Called with 1 parameter, this node.
365 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
367 collapse : function(deep, anim, callback, scope){
368 if(this.expanded && !this.isHiddenRoot()){
369 if(this.fireEvent('beforecollapse', this, deep, anim) === false){
372 this.expanded = false;
373 if((this.getOwnerTree().animate && anim !== false) || anim){
374 this.ui.animCollapse(function(){
375 this.fireEvent('collapse', this);
376 this.runCallback(callback, scope || this, [this]);
378 this.collapseChildNodes(true);
380 }.createDelegate(this));
384 this.fireEvent('collapse', this);
385 this.runCallback(callback, scope || this, [this]);
387 }else if(!this.expanded){
388 this.runCallback(callback, scope || this, [this]);
391 var cs = this.childNodes;
392 for(var i = 0, len = cs.length; i < len; i++) {
393 cs[i].collapse(true, false);
399 delayedExpand : function(delay){
400 if(!this.expandProcId){
401 this.expandProcId = this.expand.defer(delay, this);
406 cancelExpand : function(){
407 if(this.expandProcId){
408 clearTimeout(this.expandProcId);
410 this.expandProcId = false;
414 * Toggles expanded/collapsed state of the node
425 * Ensures all parent nodes are expanded, and if necessary, scrolls
426 * the node into view.
427 * @param {Function} callback (optional) A function to call when the node has been made visible.
428 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
430 ensureVisible : function(callback, scope){
431 var tree = this.getOwnerTree();
432 tree.expandPath(this.parentNode ? this.parentNode.getPath() : this.getPath(), false, function(){
433 var node = tree.getNodeById(this.id); // Somehow if we don't do this, we lose changes that happened to node in the meantime
434 tree.getTreeEl().scrollChildIntoView(node.ui.anchor);
435 this.runCallback(callback, scope || this, [this]);
436 }.createDelegate(this));
440 * Expand all child nodes
441 * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
443 expandChildNodes : function(deep){
444 var cs = this.childNodes;
445 for(var i = 0, len = cs.length; i < len; i++) {
451 * Collapse all child nodes
452 * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
454 collapseChildNodes : function(deep){
455 var cs = this.childNodes;
456 for(var i = 0, len = cs.length; i < len; i++) {
457 cs[i].collapse(deep);
464 disable : function(){
465 this.disabled = true;
467 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
468 this.ui.onDisableChange(this, true);
470 this.fireEvent('disabledchange', this, true);
477 this.disabled = false;
478 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
479 this.ui.onDisableChange(this, false);
481 this.fireEvent('disabledchange', this, false);
485 renderChildren : function(suppressEvent){
486 if(suppressEvent !== false){
487 this.fireEvent('beforechildrenrendered', this);
489 var cs = this.childNodes;
490 for(var i = 0, len = cs.length; i < len; i++){
493 this.childrenRendered = true;
497 sort : function(fn, scope){
498 Ext.tree.TreeNode.superclass.sort.apply(this, arguments);
499 if(this.childrenRendered){
500 var cs = this.childNodes;
501 for(var i = 0, len = cs.length; i < len; i++){
508 render : function(bulkRender){
509 this.ui.render(bulkRender);
511 // make sure it is registered
512 this.getOwnerTree().registerNode(this);
513 this.rendered = true;
515 this.expanded = false;
516 this.expand(false, false);
522 renderIndent : function(deep, refresh){
524 this.ui.childIndent = null;
526 this.ui.renderIndent();
527 if(deep === true && this.childrenRendered){
528 var cs = this.childNodes;
529 for(var i = 0, len = cs.length; i < len; i++){
530 cs[i].renderIndent(true, refresh);
535 beginUpdate : function(){
536 this.childrenRendered = false;
539 endUpdate : function(){
540 if(this.expanded && this.rendered){
541 this.renderChildren();
546 destroy : function(silent){
550 Ext.tree.TreeNode.superclass.destroy.call(this, silent);
551 Ext.destroy(this.ui, this.loader);
552 this.ui = this.loader = null;
556 onIdChange : function(id){
557 this.ui.onIdChange(id);
561 Ext.tree.TreePanel.nodeTypes.node = Ext.tree.TreeNode;