Roo/tree/TreeNode.js
[roojs1] / Roo / tree / TreeNode.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
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..
32   
33  * (defaults to undefined with no checkbox rendered)
34  * @constructor
35  * @param {Object/String} attributes The attributes/config for the node or just a string with the text for the node
36  */
37 Roo.tree.TreeNode = function(attributes)
38 {
39     attributes = attributes || {};
40     if(typeof attributes == "string"){
41         attributes = {text: attributes};
42     }
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;
50
51     /**
52      * Read-only. The text for this node. To change it use setText().
53      * @type String
54      */
55     this.text = attributes.text;
56     /**
57      * True if this node is disabled.
58      * @type Boolean
59      */
60     this.disabled = attributes.disabled === true;
61
62     this.addEvents({
63         /**
64         * @event textchange
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
69         */
70         "textchange" : true,
71         /**
72         * @event beforeexpand
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
77         */
78         "beforeexpand" : true,
79         /**
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
85         */
86         "beforecollapse" : true,
87         /**
88         * @event expand
89         * Fires when this node is expanded
90         * @param {Node} this This node
91         */
92         "expand" : true,
93         /**
94         * @event disabledchange
95         * Fires when the disabled status of this node changes
96         * @param {Node} this This node
97         * @param {Boolean} disabled
98         */
99         "disabledchange" : true,
100         /**
101         * @event collapse
102         * Fires when this node is collapsed
103         * @param {Node} this This node
104         */
105         "collapse" : true,
106         /**
107         * @event beforeclick
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
111         */
112         "beforeclick":true,
113         /**
114         * @event checkchange
115         * Fires when a node with a checkbox's checked property changes
116         * @param {Node} this This node
117         * @param {Boolean} checked
118         */
119         "checkchange":true,
120         /**
121         * @event click
122         * Fires when this node is clicked
123         * @param {Node} this This node
124         * @param {Roo.EventObject} e The event object
125         */
126         "click":true,
127         /**
128         * @event dblclick
129         * Fires when this node is double clicked
130         * @param {Node} this This node
131         * @param {Roo.EventObject} e The event object
132         */
133         "dblclick":true,
134         /**
135         * @event contextmenu
136         * Fires when this node is right clicked
137         * @param {Node} this This node
138         * @param {Roo.EventObject} e The event object
139         */
140         "contextmenu":true,
141         /**
142         * @event beforechildrenrendered
143         * Fires right before the child nodes for this node are rendered
144         * @param {Node} this This node
145         */
146         "beforechildrenrendered":true
147     });
148
149     var uiClass = this.attributes.uiProvider || Roo.tree.TreeNodeUI;
150
151     /**
152      * Read-only. The UI for this node
153      * @type TreeNodeUI
154      */
155     this.ui = new uiClass(this);
156      
157     
158     
159 };
160 Roo.extend(Roo.tree.TreeNode, Roo.data.Node, {
161     
162     children : false,
163     
164     preventHScroll: true,
165     /**
166      * Returns true if this node is expanded
167      * @return {Boolean}
168      */
169     isExpanded : function(){
170         return this.expanded;
171     },
172
173     /**
174      * Returns the UI object for this node
175      * @return {TreeNodeUI}
176      */
177     getUI : function(){
178         return this.ui;
179     },
180
181     // private override
182     setFirstChild : function(node){
183         var of = this.firstChild;
184         Roo.tree.TreeNode.superclass.setFirstChild.call(this, node);
185         if(this.childrenRendered && of && node != of){
186             of.renderIndent(true, true);
187         }
188         if(this.rendered){
189             this.renderIndent(true, true);
190         }
191     },
192
193     // private override
194     setLastChild : function(node){
195         var ol = this.lastChild;
196         Roo.tree.TreeNode.superclass.setLastChild.call(this, node);
197         if(this.childrenRendered && ol && node != ol){
198             ol.renderIndent(true, true);
199         }
200         if(this.rendered){
201             this.renderIndent(true, true);
202         }
203     },
204
205     // these methods are overridden to provide lazy rendering support
206     // private override
207     appendChild : function(){
208         var node = Roo.tree.TreeNode.superclass.appendChild.apply(this, arguments);
209         if(node && this.childrenRendered){
210             node.render();
211         }
212         this.ui.updateExpandIcon();
213         return node;
214     },
215
216     // private override
217     removeChild : function(node){
218         this.ownerTree.getSelectionModel().unselect(node);
219         Roo.tree.TreeNode.superclass.removeChild.apply(this, arguments);
220         // if it's been rendered remove dom node
221         if(this.childrenRendered){
222             node.ui.remove();
223         }
224         if(this.childNodes.length < 1){
225             this.collapse(false, false);
226         }else{
227             this.ui.updateExpandIcon();
228         }
229         if(!this.firstChild) {
230             this.childrenRendered = false;
231         }
232         return node;
233     },
234
235     // private override
236     insertBefore : function(node, refNode){
237         var newNode = Roo.tree.TreeNode.superclass.insertBefore.apply(this, arguments);
238         if(newNode && refNode && this.childrenRendered){
239             node.render();
240         }
241         this.ui.updateExpandIcon();
242         return newNode;
243     },
244
245     /**
246      * Sets the text for this node
247      * @param {String} text
248      */
249     setText : function(text){
250         var oldText = this.text;
251         this.text = text;
252         this.attributes.text = text;
253         if(this.rendered){ // event without subscribing
254             this.ui.onTextChange(this, text, oldText);
255         }
256         this.fireEvent("textchange", this, text, oldText);
257     },
258
259     /**
260      * Triggers selection of this node
261      */
262     select : function(){
263         this.getOwnerTree().getSelectionModel().select(this);
264     },
265
266     /**
267      * Triggers deselection of this node
268      */
269     unselect : function(){
270         this.getOwnerTree().getSelectionModel().unselect(this);
271     },
272
273     /**
274      * Returns true if this node is selected
275      * @return {Boolean}
276      */
277     isSelected : function(){
278         return this.getOwnerTree().getSelectionModel().isSelected(this);
279     },
280
281     /**
282      * Expand this node.
283      * @param {Boolean} deep (optional) True to expand all children as well
284      * @param {Boolean} anim (optional) false to cancel the default animation
285      * @param {Function} callback (optional) A callback to be called when
286      * expanding this node completes (does not wait for deep expand to complete).
287      * Called with 1 parameter, this node.
288      */
289     expand : function(deep, anim, callback){
290         if(!this.expanded){
291             if(this.fireEvent("beforeexpand", this, deep, anim) === false){
292                 return;
293             }
294             if(!this.childrenRendered){
295                 this.renderChildren();
296             }
297             this.expanded = true;
298             if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
299                 this.ui.animExpand(function(){
300                     this.fireEvent("expand", this);
301                     if(typeof callback == "function"){
302                         callback(this);
303                     }
304                     if(deep === true){
305                         this.expandChildNodes(true);
306                     }
307                 }.createDelegate(this));
308                 return;
309             }else{
310                 this.ui.expand();
311                 this.fireEvent("expand", this);
312                 if(typeof callback == "function"){
313                     callback(this);
314                 }
315             }
316         }else{
317            if(typeof callback == "function"){
318                callback(this);
319            }
320         }
321         if(deep === true){
322             this.expandChildNodes(true);
323         }
324     },
325
326     isHiddenRoot : function(){
327         return this.isRoot && !this.getOwnerTree().rootVisible;
328     },
329
330     /**
331      * Collapse this node.
332      * @param {Boolean} deep (optional) True to collapse all children as well
333      * @param {Boolean} anim (optional) false to cancel the default animation
334      */
335     collapse : function(deep, anim){
336         if(this.expanded && !this.isHiddenRoot()){
337             if(this.fireEvent("beforecollapse", this, deep, anim) === false){
338                 return;
339             }
340             this.expanded = false;
341             if((this.getOwnerTree().animate && anim !== false) || anim){
342                 this.ui.animCollapse(function(){
343                     this.fireEvent("collapse", this);
344                     if(deep === true){
345                         this.collapseChildNodes(true);
346                     }
347                 }.createDelegate(this));
348                 return;
349             }else{
350                 this.ui.collapse();
351                 this.fireEvent("collapse", this);
352             }
353         }
354         if(deep === true){
355             var cs = this.childNodes;
356             for(var i = 0, len = cs.length; i < len; i++) {
357                 cs[i].collapse(true, false);
358             }
359         }
360     },
361
362     // private
363     delayedExpand : function(delay){
364         if(!this.expandProcId){
365             this.expandProcId = this.expand.defer(delay, this);
366         }
367     },
368
369     // private
370     cancelExpand : function(){
371         if(this.expandProcId){
372             clearTimeout(this.expandProcId);
373         }
374         this.expandProcId = false;
375     },
376
377     /**
378      * Toggles expanded/collapsed state of the node
379      */
380     toggle : function(){
381         if(this.expanded){
382             this.collapse();
383         }else{
384             this.expand();
385         }
386     },
387
388     /**
389      * Ensures all parent nodes are expanded
390      */
391     ensureVisible : function(callback){
392         var tree = this.getOwnerTree();
393         tree.expandPath(this.parentNode.getPath(), false, function(){
394             tree.getTreeEl().scrollChildIntoView(this.ui.anchor);
395             Roo.callback(callback);
396         }.createDelegate(this));
397     },
398
399     /**
400      * Expand all child nodes
401      * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
402      */
403     expandChildNodes : function(deep){
404         var cs = this.childNodes;
405         for(var i = 0, len = cs.length; i < len; i++) {
406                 cs[i].expand(deep);
407         }
408     },
409
410     /**
411      * Collapse all child nodes
412      * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
413      */
414     collapseChildNodes : function(deep){
415         var cs = this.childNodes;
416         for(var i = 0, len = cs.length; i < len; i++) {
417                 cs[i].collapse(deep);
418         }
419     },
420
421     /**
422      * Disables this node
423      */
424     disable : function(){
425         this.disabled = true;
426         this.unselect();
427         if(this.rendered && this.ui.onDisableChange){ // event without subscribing
428             this.ui.onDisableChange(this, true);
429         }
430         this.fireEvent("disabledchange", this, true);
431     },
432
433     /**
434      * Enables this node
435      */
436     enable : function(){
437         this.disabled = false;
438         if(this.rendered && this.ui.onDisableChange){ // event without subscribing
439             this.ui.onDisableChange(this, false);
440         }
441         this.fireEvent("disabledchange", this, false);
442     },
443
444     // private
445     renderChildren : function(suppressEvent){
446         if(suppressEvent !== false){
447             this.fireEvent("beforechildrenrendered", this);
448         }
449         var cs = this.childNodes;
450         for(var i = 0, len = cs.length; i < len; i++){
451             cs[i].render(true);
452         }
453         this.childrenRendered = true;
454     },
455
456     // private
457     sort : function(fn, scope){
458         Roo.tree.TreeNode.superclass.sort.apply(this, arguments);
459         if(this.childrenRendered){
460             var cs = this.childNodes;
461             for(var i = 0, len = cs.length; i < len; i++){
462                 cs[i].render(true);
463             }
464         }
465     },
466
467     // private
468     render : function(bulkRender){
469         this.ui.render(bulkRender);
470         if(!this.rendered){
471             this.rendered = true;
472             if(this.expanded){
473                 this.expanded = false;
474                 this.expand(false, false);
475             }
476         }
477     },
478
479     // private
480     renderIndent : function(deep, refresh){
481         if(refresh){
482             this.ui.childIndent = null;
483         }
484         this.ui.renderIndent();
485         if(deep === true && this.childrenRendered){
486             var cs = this.childNodes;
487             for(var i = 0, len = cs.length; i < len; i++){
488                 cs[i].renderIndent(true, refresh);
489             }
490         }
491     }
492 });