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     if (attributes.children) {
158         Roo.each(attributes.children, function(c) {
159             this.appendChild(new Roo.tree.TreeNode(c));
160         }, this);
161         
162     }
163     
164 };
165 Roo.extend(Roo.tree.TreeNode, Roo.data.Node, {
166     
167     
168     preventHScroll: true,
169     /**
170      * Returns true if this node is expanded
171      * @return {Boolean}
172      */
173     isExpanded : function(){
174         return this.expanded;
175     },
176
177     /**
178      * Returns the UI object for this node
179      * @return {TreeNodeUI}
180      */
181     getUI : function(){
182         return this.ui;
183     },
184
185     // private override
186     setFirstChild : function(node){
187         var of = this.firstChild;
188         Roo.tree.TreeNode.superclass.setFirstChild.call(this, node);
189         if(this.childrenRendered && of && node != of){
190             of.renderIndent(true, true);
191         }
192         if(this.rendered){
193             this.renderIndent(true, true);
194         }
195     },
196
197     // private override
198     setLastChild : function(node){
199         var ol = this.lastChild;
200         Roo.tree.TreeNode.superclass.setLastChild.call(this, node);
201         if(this.childrenRendered && ol && node != ol){
202             ol.renderIndent(true, true);
203         }
204         if(this.rendered){
205             this.renderIndent(true, true);
206         }
207     },
208
209     // these methods are overridden to provide lazy rendering support
210     // private override
211     appendChild : function(){
212         var node = Roo.tree.TreeNode.superclass.appendChild.apply(this, arguments);
213         if(node && this.childrenRendered){
214             node.render();
215         }
216         this.ui.updateExpandIcon();
217         return node;
218     },
219
220     // private override
221     removeChild : function(node){
222         this.ownerTree.getSelectionModel().unselect(node);
223         Roo.tree.TreeNode.superclass.removeChild.apply(this, arguments);
224         // if it's been rendered remove dom node
225         if(this.childrenRendered){
226             node.ui.remove();
227         }
228         if(this.childNodes.length < 1){
229             this.collapse(false, false);
230         }else{
231             this.ui.updateExpandIcon();
232         }
233         if(!this.firstChild) {
234             this.childrenRendered = false;
235         }
236         return node;
237     },
238
239     // private override
240     insertBefore : function(node, refNode){
241         var newNode = Roo.tree.TreeNode.superclass.insertBefore.apply(this, arguments);
242         if(newNode && refNode && this.childrenRendered){
243             node.render();
244         }
245         this.ui.updateExpandIcon();
246         return newNode;
247     },
248
249     /**
250      * Sets the text for this node
251      * @param {String} text
252      */
253     setText : function(text){
254         var oldText = this.text;
255         this.text = text;
256         this.attributes.text = text;
257         if(this.rendered){ // event without subscribing
258             this.ui.onTextChange(this, text, oldText);
259         }
260         this.fireEvent("textchange", this, text, oldText);
261     },
262
263     /**
264      * Triggers selection of this node
265      */
266     select : function(){
267         this.getOwnerTree().getSelectionModel().select(this);
268     },
269
270     /**
271      * Triggers deselection of this node
272      */
273     unselect : function(){
274         this.getOwnerTree().getSelectionModel().unselect(this);
275     },
276
277     /**
278      * Returns true if this node is selected
279      * @return {Boolean}
280      */
281     isSelected : function(){
282         return this.getOwnerTree().getSelectionModel().isSelected(this);
283     },
284
285     /**
286      * Expand this node.
287      * @param {Boolean} deep (optional) True to expand all children as well
288      * @param {Boolean} anim (optional) false to cancel the default animation
289      * @param {Function} callback (optional) A callback to be called when
290      * expanding this node completes (does not wait for deep expand to complete).
291      * Called with 1 parameter, this node.
292      */
293     expand : function(deep, anim, callback){
294         if(!this.expanded){
295             if(this.fireEvent("beforeexpand", this, deep, anim) === false){
296                 return;
297             }
298             if(!this.childrenRendered){
299                 this.renderChildren();
300             }
301             this.expanded = true;
302             if(!this.isHiddenRoot() && (this.getOwnerTree().animate && anim !== false) || anim){
303                 this.ui.animExpand(function(){
304                     this.fireEvent("expand", this);
305                     if(typeof callback == "function"){
306                         callback(this);
307                     }
308                     if(deep === true){
309                         this.expandChildNodes(true);
310                     }
311                 }.createDelegate(this));
312                 return;
313             }else{
314                 this.ui.expand();
315                 this.fireEvent("expand", this);
316                 if(typeof callback == "function"){
317                     callback(this);
318                 }
319             }
320         }else{
321            if(typeof callback == "function"){
322                callback(this);
323            }
324         }
325         if(deep === true){
326             this.expandChildNodes(true);
327         }
328     },
329
330     isHiddenRoot : function(){
331         return this.isRoot && !this.getOwnerTree().rootVisible;
332     },
333
334     /**
335      * Collapse this node.
336      * @param {Boolean} deep (optional) True to collapse all children as well
337      * @param {Boolean} anim (optional) false to cancel the default animation
338      */
339     collapse : function(deep, anim){
340         if(this.expanded && !this.isHiddenRoot()){
341             if(this.fireEvent("beforecollapse", this, deep, anim) === false){
342                 return;
343             }
344             this.expanded = false;
345             if((this.getOwnerTree().animate && anim !== false) || anim){
346                 this.ui.animCollapse(function(){
347                     this.fireEvent("collapse", this);
348                     if(deep === true){
349                         this.collapseChildNodes(true);
350                     }
351                 }.createDelegate(this));
352                 return;
353             }else{
354                 this.ui.collapse();
355                 this.fireEvent("collapse", this);
356             }
357         }
358         if(deep === true){
359             var cs = this.childNodes;
360             for(var i = 0, len = cs.length; i < len; i++) {
361                 cs[i].collapse(true, false);
362             }
363         }
364     },
365
366     // private
367     delayedExpand : function(delay){
368         if(!this.expandProcId){
369             this.expandProcId = this.expand.defer(delay, this);
370         }
371     },
372
373     // private
374     cancelExpand : function(){
375         if(this.expandProcId){
376             clearTimeout(this.expandProcId);
377         }
378         this.expandProcId = false;
379     },
380
381     /**
382      * Toggles expanded/collapsed state of the node
383      */
384     toggle : function(){
385         if(this.expanded){
386             this.collapse();
387         }else{
388             this.expand();
389         }
390     },
391
392     /**
393      * Ensures all parent nodes are expanded
394      */
395     ensureVisible : function(callback){
396         var tree = this.getOwnerTree();
397         tree.expandPath(this.parentNode.getPath(), false, function(){
398             tree.getTreeEl().scrollChildIntoView(this.ui.anchor);
399             Roo.callback(callback);
400         }.createDelegate(this));
401     },
402
403     /**
404      * Expand all child nodes
405      * @param {Boolean} deep (optional) true if the child nodes should also expand their child nodes
406      */
407     expandChildNodes : function(deep){
408         var cs = this.childNodes;
409         for(var i = 0, len = cs.length; i < len; i++) {
410                 cs[i].expand(deep);
411         }
412     },
413
414     /**
415      * Collapse all child nodes
416      * @param {Boolean} deep (optional) true if the child nodes should also collapse their child nodes
417      */
418     collapseChildNodes : function(deep){
419         var cs = this.childNodes;
420         for(var i = 0, len = cs.length; i < len; i++) {
421                 cs[i].collapse(deep);
422         }
423     },
424
425     /**
426      * Disables this node
427      */
428     disable : function(){
429         this.disabled = true;
430         this.unselect();
431         if(this.rendered && this.ui.onDisableChange){ // event without subscribing
432             this.ui.onDisableChange(this, true);
433         }
434         this.fireEvent("disabledchange", this, true);
435     },
436
437     /**
438      * Enables this node
439      */
440     enable : function(){
441         this.disabled = false;
442         if(this.rendered && this.ui.onDisableChange){ // event without subscribing
443             this.ui.onDisableChange(this, false);
444         }
445         this.fireEvent("disabledchange", this, false);
446     },
447
448     // private
449     renderChildren : function(suppressEvent){
450         if(suppressEvent !== false){
451             this.fireEvent("beforechildrenrendered", this);
452         }
453         var cs = this.childNodes;
454         for(var i = 0, len = cs.length; i < len; i++){
455             cs[i].render(true);
456         }
457         this.childrenRendered = true;
458     },
459
460     // private
461     sort : function(fn, scope){
462         Roo.tree.TreeNode.superclass.sort.apply(this, arguments);
463         if(this.childrenRendered){
464             var cs = this.childNodes;
465             for(var i = 0, len = cs.length; i < len; i++){
466                 cs[i].render(true);
467             }
468         }
469     },
470
471     // private
472     render : function(bulkRender){
473         this.ui.render(bulkRender);
474         if(!this.rendered){
475             this.rendered = true;
476             if(this.expanded){
477                 this.expanded = false;
478                 this.expand(false, false);
479             }
480         }
481     },
482
483     // private
484     renderIndent : function(deep, refresh){
485         if(refresh){
486             this.ui.childIndent = null;
487         }
488         this.ui.renderIndent();
489         if(deep === true && this.childrenRendered){
490             var cs = this.childNodes;
491             for(var i = 0, len = cs.length; i < len; i++){
492                 cs[i].renderIndent(true, refresh);
493             }
494         }
495     }
496 });