3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.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 = Ext.extend(Ext.data.Node, {
38 constructor : function(attributes){
39 attributes = attributes || {};
40 if(Ext.isString(attributes)){
41 attributes = {text: attributes};
43 this.childrenRendered = false;
44 this.rendered = false;
45 Ext.tree.TreeNode.superclass.constructor.call(this, attributes);
46 this.expanded = attributes.expanded === true;
47 this.isTarget = attributes.isTarget !== false;
48 this.draggable = attributes.draggable !== false && attributes.allowDrag !== false;
49 this.allowChildren = attributes.allowChildren !== false && attributes.allowDrop !== false;
52 * Read-only. The text for this node. To change it use <code>{@link #setText}</code>.
55 this.text = attributes.text;
57 * True if this node is disabled.
60 this.disabled = attributes.disabled === true;
62 * True if this node is hidden.
65 this.hidden = attributes.hidden === true;
70 * Fires when the text for this node is changed
71 * @param {Node} this This node
72 * @param {String} text The new text
73 * @param {String} oldText The old text
78 * Fires before this node is expanded, return false to cancel.
79 * @param {Node} this This node
80 * @param {Boolean} deep
81 * @param {Boolean} anim
85 * @event beforecollapse
86 * Fires before this node is collapsed, return false to cancel.
87 * @param {Node} this This node
88 * @param {Boolean} deep
89 * @param {Boolean} anim
94 * Fires when this node is expanded
95 * @param {Node} this This node
99 * @event disabledchange
100 * Fires when the disabled status of this node changes
101 * @param {Node} this This node
102 * @param {Boolean} disabled
107 * Fires when this node is collapsed
108 * @param {Node} this This node
113 * Fires before click processing. Return false to cancel the default action.
114 * @param {Node} this This node
115 * @param {Ext.EventObject} e The event object
120 * Fires when this node is clicked
121 * @param {Node} this This node
122 * @param {Ext.EventObject} e The event object
127 * Fires when a node with a checkbox's checked property changes
128 * @param {Node} this This node
129 * @param {Boolean} checked
133 * @event beforedblclick
134 * Fires before double click processing. Return false to cancel the default action.
135 * @param {Node} this This node
136 * @param {Ext.EventObject} e The event object
141 * Fires when this node is double clicked
142 * @param {Node} this This node
143 * @param {Ext.EventObject} e The event object
148 * Fires when this node is right clicked
149 * @param {Node} this This node
150 * @param {Ext.EventObject} e The event object
154 * @event beforechildrenrendered
155 * Fires right before the child nodes for this node are rendered
156 * @param {Node} this This node
158 'beforechildrenrendered'
161 var uiClass = this.attributes.uiProvider || this.defaultUI || Ext.tree.TreeNodeUI;
164 * Read-only. The UI for this node
167 this.ui = new uiClass(this);
170 preventHScroll : true,
172 * Returns true if this node is expanded
175 isExpanded : function(){
176 return this.expanded;
180 * Returns the UI object for this node.
181 * @return {TreeNodeUI} The object which is providing the user interface for this tree
182 * node. Unless otherwise specified in the {@link #uiProvider}, this will be an instance
183 * of {@link Ext.tree.TreeNodeUI}
189 getLoader : function(){
191 return this.loader || ((owner = this.getOwnerTree()) && owner.loader ? owner.loader : (this.loader = new Ext.tree.TreeLoader()));
195 setFirstChild : function(node){
196 var of = this.firstChild;
197 Ext.tree.TreeNode.superclass.setFirstChild.call(this, node);
198 if(this.childrenRendered && of && node != of){
199 of.renderIndent(true, true);
202 this.renderIndent(true, true);
207 setLastChild : function(node){
208 var ol = this.lastChild;
209 Ext.tree.TreeNode.superclass.setLastChild.call(this, node);
210 if(this.childrenRendered && ol && node != ol){
211 ol.renderIndent(true, true);
214 this.renderIndent(true, true);
218 // these methods are overridden to provide lazy rendering support
220 appendChild : function(n){
221 if(!n.render && !Ext.isArray(n)){
222 n = this.getLoader().createNode(n);
224 var node = Ext.tree.TreeNode.superclass.appendChild.call(this, n);
225 if(node && this.childrenRendered){
228 this.ui.updateExpandIcon();
233 removeChild : function(node, destroy){
234 this.ownerTree.getSelectionModel().unselect(node);
235 Ext.tree.TreeNode.superclass.removeChild.apply(this, arguments);
236 // only update the ui if we're not destroying
238 var rendered = node.ui.rendered;
239 // if it's been rendered remove dom node
243 if(rendered && this.childNodes.length < 1){
244 this.collapse(false, false);
246 this.ui.updateExpandIcon();
248 if(!this.firstChild && !this.isHiddenRoot()){
249 this.childrenRendered = false;
256 insertBefore : function(node, refNode){
258 node = this.getLoader().createNode(node);
260 var newNode = Ext.tree.TreeNode.superclass.insertBefore.call(this, node, refNode);
261 if(newNode && refNode && this.childrenRendered){
264 this.ui.updateExpandIcon();
269 * Sets the text for this node
270 * @param {String} text
272 setText : function(text){
273 var oldText = this.text;
274 this.text = this.attributes.text = text;
275 if(this.rendered){ // event without subscribing
276 this.ui.onTextChange(this, text, oldText);
278 this.fireEvent('textchange', this, text, oldText);
282 * Sets the icon class for this node.
283 * @param {String} cls
285 setIconCls : function(cls){
286 var old = this.attributes.iconCls;
287 this.attributes.iconCls = cls;
289 this.ui.onIconClsChange(this, cls, old);
294 * Sets the tooltip for this node.
295 * @param {String} tip The text for the tip
296 * @param {String} title (Optional) The title for the tip
298 setTooltip : function(tip, title){
299 this.attributes.qtip = tip;
300 this.attributes.qtipTitle = title;
302 this.ui.onTipChange(this, tip, title);
307 * Sets the icon for this node.
308 * @param {String} icon
310 setIcon : function(icon){
311 this.attributes.icon = icon;
313 this.ui.onIconChange(this, icon);
318 * Sets the href for the node.
319 * @param {String} href The href to set
320 * @param {String} (Optional) target The target of the href
322 setHref : function(href, target){
323 this.attributes.href = href;
324 this.attributes.hrefTarget = target;
326 this.ui.onHrefChange(this, href, target);
331 * Sets the class on this node.
332 * @param {String} cls
334 setCls : function(cls){
335 var old = this.attributes.cls;
336 this.attributes.cls = cls;
338 this.ui.onClsChange(this, cls, old);
343 * Triggers selection of this node
346 var t = this.getOwnerTree();
348 t.getSelectionModel().select(this);
353 * Triggers deselection of this node
354 * @param {Boolean} silent (optional) True to stop selection change events from firing.
356 unselect : function(silent){
357 var t = this.getOwnerTree();
359 t.getSelectionModel().unselect(this, silent);
364 * Returns true if this node is selected
367 isSelected : function(){
368 var t = this.getOwnerTree();
369 return t ? t.getSelectionModel().isSelected(this) : false;
374 * @param {Boolean} deep (optional) True to expand all children as well
375 * @param {Boolean} anim (optional) false to cancel the default animation
376 * @param {Function} callback (optional) A callback to be called when
377 * expanding this node completes (does not wait for deep expand to complete).
378 * Called with 1 parameter, this node.
379 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
381 expand : function(deep, anim, callback, scope){
383 if(this.fireEvent('beforeexpand', this, deep, anim) === false){
386 if(!this.childrenRendered){
387 this.renderChildren();
389 this.expanded = true;
390 if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
391 this.ui.animExpand(function(){
392 this.fireEvent('expand', this);
393 this.runCallback(callback, scope || this, [this]);
395 this.expandChildNodes(true, true);
397 }.createDelegate(this));
401 this.fireEvent('expand', this);
402 this.runCallback(callback, scope || this, [this]);
405 this.runCallback(callback, scope || this, [this]);
408 this.expandChildNodes(true);
412 runCallback : function(cb, scope, args){
413 if(Ext.isFunction(cb)){
414 cb.apply(scope, args);
418 isHiddenRoot : function(){
419 return this.isRoot && !this.getOwnerTree().rootVisible;
423 * Collapse this node.
424 * @param {Boolean} deep (optional) True to collapse all children as well
425 * @param {Boolean} anim (optional) false to cancel the default animation
426 * @param {Function} callback (optional) A callback to be called when
427 * expanding this node completes (does not wait for deep expand to complete).
428 * Called with 1 parameter, this node.
429 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
431 collapse : function(deep, anim, callback, scope){
432 if(this.expanded && !this.isHiddenRoot()){
433 if(this.fireEvent('beforecollapse', this, deep, anim) === false){
436 this.expanded = false;
437 if((this.getOwnerTree().animate && anim !== false) || anim){
438 this.ui.animCollapse(function(){
439 this.fireEvent('collapse', this);
440 this.runCallback(callback, scope || this, [this]);
442 this.collapseChildNodes(true);
444 }.createDelegate(this));
448 this.fireEvent('collapse', this);
449 this.runCallback(callback, scope || this, [this]);
451 }else if(!this.expanded){
452 this.runCallback(callback, scope || this, [this]);
455 var cs = this.childNodes;
456 for(var i = 0, len = cs.length; i < len; i++) {
457 cs[i].collapse(true, false);
463 delayedExpand : function(delay){
464 if(!this.expandProcId){
465 this.expandProcId = this.expand.defer(delay, this);
470 cancelExpand : function(){
471 if(this.expandProcId){
472 clearTimeout(this.expandProcId);
474 this.expandProcId = false;
478 * Toggles expanded/collapsed state of the node
489 * Ensures all parent nodes are expanded, and if necessary, scrolls
490 * the node into view.
491 * @param {Function} callback (optional) A function to call when the node has been made visible.
492 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
494 ensureVisible : function(callback, scope){
495 var tree = this.getOwnerTree();
496 tree.expandPath(this.parentNode ? this.parentNode.getPath() : this.getPath(), false, function(){
497 var node = tree.getNodeById(this.id); // Somehow if we don't do this, we lose changes that happened to node in the meantime
498 tree.getTreeEl().scrollChildIntoView(node.ui.anchor);
499 this.runCallback(callback, scope || this, [this]);
500 }.createDelegate(this));
504 * Expand all child nodes
505 * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
507 expandChildNodes : function(deep, anim) {
508 var cs = this.childNodes,
511 for (i = 0; i < len; i++) {
512 cs[i].expand(deep, anim);
517 * Collapse all child nodes
518 * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
520 collapseChildNodes : function(deep){
521 var cs = this.childNodes;
522 for(var i = 0, len = cs.length; i < len; i++) {
523 cs[i].collapse(deep);
530 disable : function(){
531 this.disabled = true;
533 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
534 this.ui.onDisableChange(this, true);
536 this.fireEvent('disabledchange', this, true);
543 this.disabled = false;
544 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
545 this.ui.onDisableChange(this, false);
547 this.fireEvent('disabledchange', this, false);
551 renderChildren : function(suppressEvent){
552 if(suppressEvent !== false){
553 this.fireEvent('beforechildrenrendered', this);
555 var cs = this.childNodes;
556 for(var i = 0, len = cs.length; i < len; i++){
559 this.childrenRendered = true;
563 sort : function(fn, scope){
564 Ext.tree.TreeNode.superclass.sort.apply(this, arguments);
565 if(this.childrenRendered){
566 var cs = this.childNodes;
567 for(var i = 0, len = cs.length; i < len; i++){
574 render : function(bulkRender){
575 this.ui.render(bulkRender);
577 // make sure it is registered
578 this.getOwnerTree().registerNode(this);
579 this.rendered = true;
581 this.expanded = false;
582 this.expand(false, false);
588 renderIndent : function(deep, refresh){
590 this.ui.childIndent = null;
592 this.ui.renderIndent();
593 if(deep === true && this.childrenRendered){
594 var cs = this.childNodes;
595 for(var i = 0, len = cs.length; i < len; i++){
596 cs[i].renderIndent(true, refresh);
601 beginUpdate : function(){
602 this.childrenRendered = false;
605 endUpdate : function(){
606 if(this.expanded && this.rendered){
607 this.renderChildren();
612 destroy : function(silent){
616 Ext.tree.TreeNode.superclass.destroy.call(this, silent);
617 Ext.destroy(this.ui, this.loader);
618 this.ui = this.loader = null;
622 onIdChange : function(id){
623 this.ui.onIdChange(id);
627 Ext.tree.TreePanel.nodeTypes.node = Ext.tree.TreeNode;