3 * Copyright(c) 2006-2010 Ext JS, LLC
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){
220 if(!n.render && !Ext.isArray(n)){
221 n = this.getLoader().createNode(n);
223 exists = !n.parentNode;
225 node = Ext.tree.TreeNode.superclass.appendChild.call(this, n);
227 this.afterAdd(node, exists);
229 this.ui.updateExpandIcon();
234 removeChild : function(node, destroy){
235 this.ownerTree.getSelectionModel().unselect(node);
236 Ext.tree.TreeNode.superclass.removeChild.apply(this, arguments);
237 // if it's been rendered remove dom node
238 if(node.ui.rendered){
241 if(this.childNodes.length < 1){
242 this.collapse(false, false);
244 this.ui.updateExpandIcon();
246 if(!this.firstChild && !this.isHiddenRoot()) {
247 this.childrenRendered = false;
253 insertBefore : function(node, refNode){
256 node = this.getLoader().createNode(node);
258 exists = Ext.isObject(node.parentNode);
260 newNode = Ext.tree.TreeNode.superclass.insertBefore.call(this, node, refNode);
261 if(newNode && refNode){
262 this.afterAdd(newNode, exists);
264 this.ui.updateExpandIcon();
269 afterAdd : function(node, exists){
270 if(this.childrenRendered){
271 // bulk render if the node already exists
274 // make sure we update the indent
275 node.renderIndent(true, true);
280 * Sets the text for this node
281 * @param {String} text
283 setText : function(text){
284 var oldText = this.text;
285 this.text = this.attributes.text = text;
286 if(this.rendered){ // event without subscribing
287 this.ui.onTextChange(this, text, oldText);
289 this.fireEvent('textchange', this, text, oldText);
293 * Triggers selection of this node
296 var t = this.getOwnerTree();
298 t.getSelectionModel().select(this);
303 * Triggers deselection of this node
304 * @param {Boolean} silent (optional) True to stop selection change events from firing.
306 unselect : function(silent){
307 var t = this.getOwnerTree();
309 t.getSelectionModel().unselect(this, silent);
314 * Returns true if this node is selected
317 isSelected : function(){
318 var t = this.getOwnerTree();
319 return t ? t.getSelectionModel().isSelected(this) : false;
324 * @param {Boolean} deep (optional) True to expand all children as well
325 * @param {Boolean} anim (optional) false to cancel the default animation
326 * @param {Function} callback (optional) A callback to be called when
327 * expanding this node completes (does not wait for deep expand to complete).
328 * Called with 1 parameter, this node.
329 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
331 expand : function(deep, anim, callback, scope){
333 if(this.fireEvent('beforeexpand', this, deep, anim) === false){
336 if(!this.childrenRendered){
337 this.renderChildren();
339 this.expanded = true;
340 if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
341 this.ui.animExpand(function(){
342 this.fireEvent('expand', this);
343 this.runCallback(callback, scope || this, [this]);
345 this.expandChildNodes(true);
347 }.createDelegate(this));
351 this.fireEvent('expand', this);
352 this.runCallback(callback, scope || this, [this]);
355 this.runCallback(callback, scope || this, [this]);
358 this.expandChildNodes(true);
362 runCallback : function(cb, scope, args){
363 if(Ext.isFunction(cb)){
364 cb.apply(scope, args);
368 isHiddenRoot : function(){
369 return this.isRoot && !this.getOwnerTree().rootVisible;
373 * Collapse this node.
374 * @param {Boolean} deep (optional) True to collapse 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 collapse : function(deep, anim, callback, scope){
382 if(this.expanded && !this.isHiddenRoot()){
383 if(this.fireEvent('beforecollapse', this, deep, anim) === false){
386 this.expanded = false;
387 if((this.getOwnerTree().animate && anim !== false) || anim){
388 this.ui.animCollapse(function(){
389 this.fireEvent('collapse', this);
390 this.runCallback(callback, scope || this, [this]);
392 this.collapseChildNodes(true);
394 }.createDelegate(this));
398 this.fireEvent('collapse', this);
399 this.runCallback(callback, scope || this, [this]);
401 }else if(!this.expanded){
402 this.runCallback(callback, scope || this, [this]);
405 var cs = this.childNodes;
406 for(var i = 0, len = cs.length; i < len; i++) {
407 cs[i].collapse(true, false);
413 delayedExpand : function(delay){
414 if(!this.expandProcId){
415 this.expandProcId = this.expand.defer(delay, this);
420 cancelExpand : function(){
421 if(this.expandProcId){
422 clearTimeout(this.expandProcId);
424 this.expandProcId = false;
428 * Toggles expanded/collapsed state of the node
439 * Ensures all parent nodes are expanded, and if necessary, scrolls
440 * the node into view.
441 * @param {Function} callback (optional) A function to call when the node has been made visible.
442 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this TreeNode.
444 ensureVisible : function(callback, scope){
445 var tree = this.getOwnerTree();
446 tree.expandPath(this.parentNode ? this.parentNode.getPath() : this.getPath(), false, function(){
447 var node = tree.getNodeById(this.id); // Somehow if we don't do this, we lose changes that happened to node in the meantime
448 tree.getTreeEl().scrollChildIntoView(node.ui.anchor);
449 this.runCallback(callback, scope || this, [this]);
450 }.createDelegate(this));
454 * Expand all child nodes
455 * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
457 expandChildNodes : function(deep){
458 var cs = this.childNodes;
459 for(var i = 0, len = cs.length; i < len; i++) {
465 * Collapse all child nodes
466 * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
468 collapseChildNodes : function(deep){
469 var cs = this.childNodes;
470 for(var i = 0, len = cs.length; i < len; i++) {
471 cs[i].collapse(deep);
478 disable : function(){
479 this.disabled = true;
481 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
482 this.ui.onDisableChange(this, true);
484 this.fireEvent('disabledchange', this, true);
491 this.disabled = false;
492 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
493 this.ui.onDisableChange(this, false);
495 this.fireEvent('disabledchange', this, false);
499 renderChildren : function(suppressEvent){
500 if(suppressEvent !== false){
501 this.fireEvent('beforechildrenrendered', this);
503 var cs = this.childNodes;
504 for(var i = 0, len = cs.length; i < len; i++){
507 this.childrenRendered = true;
511 sort : function(fn, scope){
512 Ext.tree.TreeNode.superclass.sort.apply(this, arguments);
513 if(this.childrenRendered){
514 var cs = this.childNodes;
515 for(var i = 0, len = cs.length; i < len; i++){
522 render : function(bulkRender){
523 this.ui.render(bulkRender);
525 // make sure it is registered
526 this.getOwnerTree().registerNode(this);
527 this.rendered = true;
529 this.expanded = false;
530 this.expand(false, false);
536 renderIndent : function(deep, refresh){
538 this.ui.childIndent = null;
540 this.ui.renderIndent();
541 if(deep === true && this.childrenRendered){
542 var cs = this.childNodes;
543 for(var i = 0, len = cs.length; i < len; i++){
544 cs[i].renderIndent(true, refresh);
549 beginUpdate : function(){
550 this.childrenRendered = false;
553 endUpdate : function(){
554 if(this.expanded && this.rendered){
555 this.renderChildren();
559 destroy : function(){
561 Ext.tree.TreeNode.superclass.destroy.call(this);
562 Ext.destroy(this.ui, this.loader);
563 this.ui = this.loader = null;
567 onIdChange : function(id){
568 this.ui.onIdChange(id);
572 Ext.tree.TreePanel.nodeTypes.node = Ext.tree.TreeNode;