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