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);
157 if (attributes.children) {
158 Roo.each(attributes.children, function(c) {
159 this.appendChild(new Roo.tree.TreeNode(c));
164 Roo.extend(Roo.tree.TreeNode, Roo.data.Node, {
167 preventHScroll: true,
169 * Returns true if this node is expanded
172 isExpanded : function(){
173 return this.expanded;
177 * Returns the UI object for this node
178 * @return {TreeNodeUI}
185 setFirstChild : function(node){
186 var of = this.firstChild;
187 Roo.tree.TreeNode.superclass.setFirstChild.call(this, node);
188 if(this.childrenRendered && of && node != of){
189 of.renderIndent(true, true);
192 this.renderIndent(true, true);
197 setLastChild : function(node){
198 var ol = this.lastChild;
199 Roo.tree.TreeNode.superclass.setLastChild.call(this, node);
200 if(this.childrenRendered && ol && node != ol){
201 ol.renderIndent(true, true);
204 this.renderIndent(true, true);
208 // these methods are overridden to provide lazy rendering support
210 appendChild : function(){
211 var node = Roo.tree.TreeNode.superclass.appendChild.apply(this, arguments);
212 if(node && this.childrenRendered){
215 this.ui.updateExpandIcon();
220 removeChild : function(node){
221 this.ownerTree.getSelectionModel().unselect(node);
222 Roo.tree.TreeNode.superclass.removeChild.apply(this, arguments);
223 // if it's been rendered remove dom node
224 if(this.childrenRendered){
227 if(this.childNodes.length < 1){
228 this.collapse(false, false);
230 this.ui.updateExpandIcon();
232 if(!this.firstChild) {
233 this.childrenRendered = false;
239 insertBefore : function(node, refNode){
240 var newNode = Roo.tree.TreeNode.superclass.insertBefore.apply(this, arguments);
241 if(newNode && refNode && this.childrenRendered){
244 this.ui.updateExpandIcon();
249 * Sets the text for this node
250 * @param {String} text
252 setText : function(text){
253 var oldText = this.text;
255 this.attributes.text = text;
256 if(this.rendered){ // event without subscribing
257 this.ui.onTextChange(this, text, oldText);
259 this.fireEvent("textchange", this, text, oldText);
263 * Triggers selection of this node
266 this.getOwnerTree().getSelectionModel().select(this);
270 * Triggers deselection of this node
272 unselect : function(){
273 this.getOwnerTree().getSelectionModel().unselect(this);
277 * Returns true if this node is selected
280 isSelected : function(){
281 return this.getOwnerTree().getSelectionModel().isSelected(this);
286 * @param {Boolean} deep (optional) True to expand all children as well
287 * @param {Boolean} anim (optional) false to cancel the default animation
288 * @param {Function} callback (optional) A callback to be called when
289 * expanding this node completes (does not wait for deep expand to complete).
290 * Called with 1 parameter, this node.
292 expand : function(deep, anim, callback){
294 if(this.fireEvent("beforeexpand", this, deep, anim) === false){
297 if(!this.childrenRendered){
298 this.renderChildren();
300 this.expanded = true;
301 if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
302 this.ui.animExpand(function(){
303 this.fireEvent("expand", this);
304 if(typeof callback == "function"){
308 this.expandChildNodes(true);
310 }.createDelegate(this));
314 this.fireEvent("expand", this);
315 if(typeof callback == "function"){
320 if(typeof callback == "function"){
325 this.expandChildNodes(true);
329 isHiddenRoot : function(){
330 return this.isRoot && !this.getOwnerTree().rootVisible;
334 * Collapse this node.
335 * @param {Boolean} deep (optional) True to collapse all children as well
336 * @param {Boolean} anim (optional) false to cancel the default animation
338 collapse : function(deep, anim){
339 if(this.expanded && !this.isHiddenRoot()){
340 if(this.fireEvent("beforecollapse", this, deep, anim) === false){
343 this.expanded = false;
344 if((this.getOwnerTree().animate && anim !== false) || anim){
345 this.ui.animCollapse(function(){
346 this.fireEvent("collapse", this);
348 this.collapseChildNodes(true);
350 }.createDelegate(this));
354 this.fireEvent("collapse", this);
358 var cs = this.childNodes;
359 for(var i = 0, len = cs.length; i < len; i++) {
360 cs[i].collapse(true, false);
366 delayedExpand : function(delay){
367 if(!this.expandProcId){
368 this.expandProcId = this.expand.defer(delay, this);
373 cancelExpand : function(){
374 if(this.expandProcId){
375 clearTimeout(this.expandProcId);
377 this.expandProcId = false;
381 * Toggles expanded/collapsed state of the node
392 * Ensures all parent nodes are expanded
394 ensureVisible : function(callback){
395 var tree = this.getOwnerTree();
396 tree.expandPath(this.parentNode.getPath(), false, function(){
397 tree.getTreeEl().scrollChildIntoView(this.ui.anchor);
398 Roo.callback(callback);
399 }.createDelegate(this));
403 * Expand all child nodes
404 * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
406 expandChildNodes : function(deep){
407 var cs = this.childNodes;
408 for(var i = 0, len = cs.length; i < len; i++) {
414 * Collapse all child nodes
415 * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
417 collapseChildNodes : function(deep){
418 var cs = this.childNodes;
419 for(var i = 0, len = cs.length; i < len; i++) {
420 cs[i].collapse(deep);
427 disable : function(){
428 this.disabled = true;
430 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
431 this.ui.onDisableChange(this, true);
433 this.fireEvent("disabledchange", this, true);
440 this.disabled = false;
441 if(this.rendered && this.ui.onDisableChange){ // event without subscribing
442 this.ui.onDisableChange(this, false);
444 this.fireEvent("disabledchange", this, false);
448 renderChildren : function(suppressEvent){
449 if(suppressEvent !== false){
450 this.fireEvent("beforechildrenrendered", this);
452 var cs = this.childNodes;
453 for(var i = 0, len = cs.length; i < len; i++){
456 this.childrenRendered = true;
460 sort : function(fn, scope){
461 Roo.tree.TreeNode.superclass.sort.apply(this, arguments);
462 if(this.childrenRendered){
463 var cs = this.childNodes;
464 for(var i = 0, len = cs.length; i < len; i++){
471 render : function(bulkRender){
472 this.ui.render(bulkRender);
474 this.rendered = true;
476 this.expanded = false;
477 this.expand(false, false);
483 renderIndent : function(deep, refresh){
485 this.ui.childIndent = null;
487 this.ui.renderIndent();
488 if(deep === true && this.childrenRendered){
489 var cs = this.childNodes;
490 for(var i = 0, len = cs.length; i < len; i++){
491 cs[i].renderIndent(true, refresh);