4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.tree.TreeNode
14 * @extends Roo.data.Node
15 * @cfg {String} text The text for this node
16 * @cfg {Boolean} expanded true to start the node expanded
17 * @cfg {Boolean} allowDrag false to make this node undraggable if DD is on (defaults to true)
18 * @cfg {Boolean} allowDrop false if this node cannot be drop on
19 * @cfg {Boolean} disabled true to start the node disabled
20 * @cfg {String} icon The path to an icon for the node. The preferred way to do this
21 * is to use the cls or iconCls attributes and add the icon via a CSS background image.
22 * @cfg {String} cls A css class to be added to the node
23 * @cfg {String} iconCls A css class to be added to the nodes icon element for applying css background images
24 * @cfg {String} href URL of the link used for the node (defaults to #)
25 * @cfg {String} hrefTarget target frame for the link
26 * @cfg {String} qtip An Ext QuickTip for the node
27 * @cfg {String} qtipCfg An Ext QuickTip config for the node (used instead of qtip)
28 * @cfg {Boolean} singleClickExpand True for single click expand on this node
29 * @cfg {Function} uiProvider A UI <b>class</b> to use for this node (defaults to Roo.tree.TreeNodeUI)
30 * @cfg {Boolean} checked True to render a checked checkbox for this node, false to render an unchecked checkbox
31 * @cfg {Array} children Array of Children to be added when created..
33 * (defaults to undefined with no checkbox rendered)
35 * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node
37 Roo.tree.TreeNode = function(attributes)
39 attributes = attributes || {};
40 if(typeof attributes == "string"){
41 attributes = {text: attributes};
43 this.childrenRendered = false;
44 this.rendered = false;
45 Roo.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 setText().
55 this.text = attributes.text;
57 * True if this node is disabled.
60 this.disabled = attributes.disabled === true;
65 * Fires when the text for this node is changed
66 * @param {Node} this This node
67 * @param {String} text The new text
68 * @param {String} oldText The old text
73 * Fires before this node is expanded, return false to cancel.
74 * @param {Node} this This node
75 * @param {Boolean} deep
76 * @param {Boolean} anim
78 "beforeexpand" : true,
80 * @event beforecollapse
81 * Fires before this node is collapsed, return false to cancel.
82 * @param {Node} this This node
83 * @param {Boolean} deep
84 * @param {Boolean} anim
86 "beforecollapse" : true,
89 * Fires when this node is expanded
90 * @param {Node} this This node
94 * @event disabledchange
95 * Fires when the disabled status of this node changes
96 * @param {Node} this This node
97 * @param {Boolean} disabled
99 "disabledchange" : true,
102 * Fires when this node is collapsed
103 * @param {Node} this This node
108 * Fires before click processing. Return false to cancel the default action.
109 * @param {Node} this This node
110 * @param {Roo.EventObject} e The event object
115 * Fires when a node with a checkbox's checked property changes
116 * @param {Node} this This node
117 * @param {Boolean} checked
122 * Fires when this node is clicked
123 * @param {Node} this This node
124 * @param {Roo.EventObject} e The event object
129 * Fires when this node is double clicked
130 * @param {Node} this This node
131 * @param {Roo.EventObject} e The event object
136 * Fires when this node is right clicked
137 * @param {Node} this This node
138 * @param {Roo.EventObject} e The event object
142 * @event beforechildrenrendered
143 * Fires right before the child nodes for this node are rendered
144 * @param {Node} this This node
146 "beforechildrenrendered":true
149 var uiClass = this.attributes.uiProvider || Roo.tree.TreeNodeUI;
152 * Read-only. The UI for this node
155 this.ui = new uiClass(this);
160 Roo.extend(Roo.tree.TreeNode, Roo.data.Node, {
161 preventHScroll: true,
163 * Returns true if this node is expanded
166 isExpanded : function(){
167 return this.expanded;
171 * Returns the UI object for this node
172 * @return {TreeNodeUI}
179 setFirstChild : function(node){
180 var of = this.firstChild;
181 Roo.tree.TreeNode.superclass.setFirstChild.call(this, node);
182 if(this.childrenRendered && of && node != of){
183 of.renderIndent(true, true);
186 this.renderIndent(true, true);
191 setLastChild : function(node){
192 var ol = this.lastChild;
193 Roo.tree.TreeNode.superclass.setLastChild.call(this, node);
194 if(this.childrenRendered && ol && node != ol){
195 ol.renderIndent(true, true);
198 this.renderIndent(true, true);
202 // these methods are overridden to provide lazy rendering support
204 appendChild : function(){
205 var node = Roo.tree.TreeNode.superclass.appendChild.apply(this, arguments);
206 if(node && this.childrenRendered){
209 this.ui.updateExpandIcon();
214 removeChild : function(node){
215 this.ownerTree.getSelectionModel().unselect(node);
216 Roo.tree.TreeNode.superclass.removeChild.apply(this, arguments);
217 // if it's been rendered remove dom node
218 if(this.childrenRendered){
221 if(this.childNodes.length < 1){
222 this.collapse(false, false);
224 this.ui.updateExpandIcon();
226 if(!this.firstChild) {
227 this.childrenRendered = false;
233 insertBefore : function(node, refNode){
234 var newNode = Roo.tree.TreeNode.superclass.insertBefore.apply(this, arguments);
235 if(newNode && refNode && this.childrenRendered){
238 this.ui.updateExpandIcon();
243 * Sets the text for this node
244 * @param {String} text
246 setText : function(text){
247 var oldText = this.text;
249 this.attributes.text = text;
250 if(this.rendered){ // event without subscribing
251 this.ui.onTextChange(this, text, oldText);
253 this.fireEvent("textchange", this, text, oldText);
257 * Triggers selection of this node
260 this.getOwnerTree().getSelectionModel().select(this);
264 * Triggers deselection of this node
266 unselect : function(){
267 this.getOwnerTree().getSelectionModel().unselect(this);
271 * Returns true if this node is selected
274 isSelected : function(){
275 return this.getOwnerTree().getSelectionModel().isSelected(this);
280 * @param {Boolean} deep (optional) True to expand all children as well
281 * @param {Boolean} anim (optional) false to cancel the default animation
282 * @param {Function} callback (optional) A callback to be called when
283 * expanding this node completes (does not wait for deep expand to complete).
284 * Called with 1 parameter, this node.
286 expand : function(deep, anim, callback){
288 if(this.fireEvent("beforeexpand", this, deep, anim) === false){
291 if(!this.childrenRendered){
292 this.renderChildren();
294 this.expanded = true;
295 if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
296 this.ui.animExpand(function(){
297 this.fireEvent("expand", this);
298 if(typeof callback == "function"){
302 this.expandChildNodes(true);
304 }.createDelegate(this));
308 this.fireEvent("expand", this);
309 if(typeof callback == "function"){
314 if(typeof callback == "function"){
319 this.expandChildNodes(true);
323 isHiddenRoot : function(){
324 return this.isRoot && !this.getOwnerTree().rootVisible;
328 * Collapse this node.
329 * @param {Boolean} deep (optional) True to collapse all children as well
330 * @param {Boolean} anim (optional) false to cancel the default animation
332 collapse : function(deep, anim){
333 if(this.expanded && !this.isHiddenRoot()){
334 if(this.fireEvent("beforecollapse", this, deep, anim) === false){
337 this.expanded = false;
338 if((this.getOwnerTree().animate && anim !== false) || anim){
339 this.ui.animCollapse(function(){
340 this.fireEvent("collapse", this);
342 this.collapseChildNodes(true);
344 }.createDelegate(this));
348 this.fireEvent("collapse", this);
352 var cs = this.childNodes;
353 for(var i = 0, len = cs.length; i < len; i++) {
354 cs[i].collapse(true, false);
360 delayedExpand : function(delay){
361 if(!this.expandProcId){
362 this.expandProcId = this.expand.defer(delay, this);
367 cancelExpand : function(){
368 if(this.expandProcId){
369 clearTimeout(this.expandProcId);
371 this.expandProcId = false;
375 * Toggles expanded/collapsed state of the node
386 * Ensures all parent nodes are expanded
388 ensureVisible : function(callback){
389 var tree = this.getOwnerTree();
390 tree.expandPath(this.parentNode.getPath(), false, function(){
391 tree.getTreeEl().scrollChildIntoView(this.ui.anchor);
392 Roo.callback(callback);
393 }.createDelegate(this));
397 * Expand all child nodes
398 * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
400 expandChildNodes : function(deep){
401 var cs = this.childNodes;
402 for(var i = 0, len = cs.length; i < len; i++) {
408 * Collapse all child nodes
409 * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
411 collapseChildNodes : function(deep){
412 var cs = this.childNodes;
413 for(var i = 0, len = cs.length; i < len; i++) {
414 cs[i].collapse(deep);
421 disable : function(){
422 this.disabled = true;
424 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
425 this.ui.onDisableChange(this, true);
427 this.fireEvent("disabledchange", this, true);
434 this.disabled = false;
435 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
436 this.ui.onDisableChange(this, false);
438 this.fireEvent("disabledchange", this, false);
442 renderChildren : function(suppressEvent){
443 if(suppressEvent !== false){
444 this.fireEvent("beforechildrenrendered", this);
446 var cs = this.childNodes;
447 for(var i = 0, len = cs.length; i < len; i++){
450 this.childrenRendered = true;
454 sort : function(fn, scope){
455 Roo.tree.TreeNode.superclass.sort.apply(this, arguments);
456 if(this.childrenRendered){
457 var cs = this.childNodes;
458 for(var i = 0, len = cs.length; i < len; i++){
465 render : function(bulkRender){
466 this.ui.render(bulkRender);
468 this.rendered = true;
470 this.expanded = false;
471 this.expand(false, false);
477 renderIndent : function(deep, refresh){
479 this.ui.childIndent = null;
481 this.ui.renderIndent();
482 if(deep === true && this.childrenRendered){
483 var cs = this.childNodes;
484 for(var i = 0, len = cs.length; i < len; i++){
485 cs[i].renderIndent(true, refresh);