Roo/Component.js
[roojs1] / Roo / Component.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.Component
14  * @extends Roo.util.Observable
15  * Base class for all major Roo components.  All subclasses of Component can automatically participate in the standard
16  * Roo component lifecycle of creation, rendering and destruction.  They also have automatic support for basic hide/show
17  * and enable/disable behavior.  Component allows any subclass to be lazy-rendered into any {@link Roo.Container} and
18  * to be automatically registered with the {@link Roo.ComponentMgr} so that it can be referenced at any time via {@link Roo.getCmp}.
19  * All visual components (widgets) that require rendering into a layout should subclass Component.
20  * @constructor
21  * @param {Roo.Element/String/Object} config The configuration options.  If an element is passed, it is set as the internal
22  * element and its id used as the component id.  If a string is passed, it is assumed to be the id of an existing element
23  * and is used as the component id.  Otherwise, it is assumed to be a standard config object and is applied to the component.
24  */
25 Roo.Component = function(config){
26     config = config || {};
27     if(config.tagName || config.dom || typeof config == "string"){ // element object
28         config = {el: config, id: config.id || config};
29     }
30     this.initialConfig = config;
31
32     Roo.apply(this, config);
33     this.addEvents({
34         /**
35          * @event disable
36          * Fires after the component is disabled.
37              * @param {Roo.Component} this
38              */
39         disable : true,
40         /**
41          * @event enable
42          * Fires after the component is enabled.
43              * @param {Roo.Component} this
44              */
45         enable : true,
46         /**
47          * @event beforeshow
48          * Fires before the component is shown.  Return false to stop the show.
49              * @param {Roo.Component} this
50              */
51         beforeshow : true,
52         /**
53          * @event show
54          * Fires after the component is shown.
55              * @param {Roo.Component} this
56              */
57         show : true,
58         /**
59          * @event beforehide
60          * Fires before the component is hidden. Return false to stop the hide.
61              * @param {Roo.Component} this
62              */
63         beforehide : true,
64         /**
65          * @event hide
66          * Fires after the component is hidden.
67              * @param {Roo.Component} this
68              */
69         hide : true,
70         /**
71          * @event beforerender
72          * Fires before the component is rendered. Return false to stop the render.
73              * @param {Roo.Component} this
74              */
75         beforerender : true,
76         /**
77          * @event render
78          * Fires after the component is rendered.
79              * @param {Roo.Component} this
80              */
81         render : true,
82         /**
83          * @event beforedestroy
84          * Fires before the component is destroyed. Return false to stop the destroy.
85              * @param {Roo.Component} this
86              */
87         beforedestroy : true,
88         /**
89          * @event destroy
90          * Fires after the component is destroyed.
91              * @param {Roo.Component} this
92              */
93         destroy : true
94     });
95     if(!this.id){
96         this.id = "roo-comp-" + (++Roo.Component.AUTO_ID);
97     }
98     Roo.ComponentMgr.register(this);
99     Roo.Component.superclass.constructor.call(this);
100     this.initComponent();
101     if(this.renderTo){ // not supported by all components yet. use at your own risk!
102         this.render(this.renderTo);
103         delete this.renderTo;
104     }
105 };
106
107 /** @private */
108 Roo.Component.AUTO_ID = 1000;
109
110 Roo.extend(Roo.Component, Roo.util.Observable, {
111     /**
112      * @scope Roo.Component.prototype
113      * @type {Boolean}
114      * true if this component is hidden. Read-only.
115      */
116     hidden : false,
117     /**
118      * @type {Boolean}
119      * true if this component is disabled. Read-only.
120      */
121     disabled : false,
122     /**
123      * @type {Boolean}
124      * true if this component has been rendered. Read-only.
125      */
126     rendered : false,
127     
128     /** @cfg {String} disableClass
129      * CSS class added to the component when it is disabled (defaults to "x-item-disabled").
130      */
131     disabledClass : "x-item-disabled",
132         /** @cfg {Boolean} allowDomMove
133          * Whether the component can move the Dom node when rendering (defaults to true).
134          */
135     allowDomMove : true,
136     /** @cfg {String} hideMode (display|visibility)
137      * How this component should hidden. Supported values are
138      * "visibility" (css visibility), "offsets" (negative offset position) and
139      * "display" (css display) - defaults to "display".
140      */
141     hideMode: 'display',
142
143     /** @private */
144     ctype : "Roo.Component",
145
146     /**
147      * @cfg {String} actionMode 
148      * which property holds the element that used for  hide() / show() / disable() / enable()
149      * default is 'el' for forms you probably want to set this to fieldEl 
150      */
151     actionMode : "el",
152
153          /**
154      * @cfg {String} style
155      * css styles to add to component
156      * eg. text-align:right;
157      */
158     style : false,
159         
160     /** @private */
161     getActionEl : function(){
162         return this[this.actionMode];
163     },
164
165     initComponent : Roo.emptyFn,
166     /**
167      * If this is a lazy rendering component, render it to its container element.
168      * @param {String/HTMLElement/Element} container (optional) The element this component should be rendered into. If it is being applied to existing markup, this should be left off.
169      */
170     render : function(container, position){
171         Roo.log('render');
172         Roo.log(this);
173         if(this.rendered){
174             return this;
175         }
176         
177         if(this.fireEvent("beforerender", this) === false){
178             return false;
179         }
180         
181         if(!container && this.el){
182             this.el = Roo.get(this.el);
183             container = this.el.dom.parentNode;
184             this.allowDomMove = false;
185         }
186         this.container = Roo.get(container);
187         this.rendered = true;
188         if(position !== undefined){
189             if(typeof position == 'number'){
190                 position = this.container.dom.childNodes[position];
191             }else{
192                 position = Roo.getDom(position);
193             }
194         }
195         this.onRender(this.container, position || null);
196         if(this.cls){
197             this.el.addClass(this.cls);
198             delete this.cls;
199         }
200         if(this.style){
201             this.el.applyStyles(this.style);
202             delete this.style;
203         }
204         this.fireEvent("render", this);
205         this.afterRender(this.container);
206         if(this.hidden){
207             this.hide();
208         }
209         if(this.disabled){
210             this.disable();
211         }
212
213         return this;
214         
215     },
216
217     /** @private */
218     // default function is not really useful
219     onRender : function(ct, position){
220         if(this.el){
221             this.el = Roo.get(this.el);
222             if(this.allowDomMove !== false){
223                 ct.dom.insertBefore(this.el.dom, position);
224             }
225         }
226     },
227
228     /** @private */
229     getAutoCreate : function(){
230         var cfg = typeof this.autoCreate == "object" ?
231                       this.autoCreate : Roo.apply({}, this.defaultAutoCreate);
232         if(this.id && !cfg.id){
233             cfg.id = this.id;
234         }
235         return cfg;
236     },
237
238     /** @private */
239     afterRender : Roo.emptyFn,
240
241     /**
242      * Destroys this component by purging any event listeners, removing the component's element from the DOM,
243      * removing the component from its {@link Roo.Container} (if applicable) and unregistering it from {@link Roo.ComponentMgr}.
244      */
245     destroy : function(){
246         if(this.fireEvent("beforedestroy", this) !== false){
247             this.purgeListeners();
248             this.beforeDestroy();
249             if(this.rendered){
250                 this.el.removeAllListeners();
251                 this.el.remove();
252                 if(this.actionMode == "container"){
253                     this.container.remove();
254                 }
255             }
256             this.onDestroy();
257             Roo.ComponentMgr.unregister(this);
258             this.fireEvent("destroy", this);
259         }
260     },
261
262         /** @private */
263     beforeDestroy : function(){
264
265     },
266
267         /** @private */
268         onDestroy : function(){
269
270     },
271
272     /**
273      * Returns the underlying {@link Roo.Element}.
274      * @return {Roo.Element} The element
275      */
276     getEl : function(){
277         return this.el;
278     },
279
280     /**
281      * Returns the id of this component.
282      * @return {String}
283      */
284     getId : function(){
285         return this.id;
286     },
287
288     /**
289      * Try to focus this component.
290      * @param {Boolean} selectText True to also select the text in this component (if applicable)
291      * @return {Roo.Component} this
292      */
293     focus : function(selectText){
294         if(this.rendered){
295             this.el.focus();
296             if(selectText === true){
297                 this.el.dom.select();
298             }
299         }
300         return this;
301     },
302
303     /** @private */
304     blur : function(){
305         if(this.rendered){
306             this.el.blur();
307         }
308         return this;
309     },
310
311     /**
312      * Disable this component.
313      * @return {Roo.Component} this
314      */
315     disable : function(){
316         if(this.rendered){
317             this.onDisable();
318         }
319         this.disabled = true;
320         this.fireEvent("disable", this);
321         return this;
322     },
323
324         // private
325     onDisable : function(){
326         this.getActionEl().addClass(this.disabledClass);
327         this.el.dom.disabled = true;
328     },
329
330     /**
331      * Enable this component.
332      * @return {Roo.Component} this
333      */
334     enable : function(){
335         if(this.rendered){
336             this.onEnable();
337         }
338         this.disabled = false;
339         this.fireEvent("enable", this);
340         return this;
341     },
342
343         // private
344     onEnable : function(){
345         this.getActionEl().removeClass(this.disabledClass);
346         this.el.dom.disabled = false;
347     },
348
349     /**
350      * Convenience function for setting disabled/enabled by boolean.
351      * @param {Boolean} disabled
352      */
353     setDisabled : function(disabled){
354         this[disabled ? "disable" : "enable"]();
355     },
356
357     /**
358      * Show this component.
359      * @return {Roo.Component} this
360      */
361     show: function(){
362         if(this.fireEvent("beforeshow", this) !== false){
363             this.hidden = false;
364             if(this.rendered){
365                 this.onShow();
366             }
367             this.fireEvent("show", this);
368         }
369         return this;
370     },
371
372     // private
373     onShow : function(){
374         var ae = this.getActionEl();
375         if(this.hideMode == 'visibility'){
376             ae.dom.style.visibility = "visible";
377         }else if(this.hideMode == 'offsets'){
378             ae.removeClass('x-hidden');
379         }else{
380             ae.dom.style.display = "";
381         }
382     },
383
384     /**
385      * Hide this component.
386      * @return {Roo.Component} this
387      */
388     hide: function(){
389         if(this.fireEvent("beforehide", this) !== false){
390             this.hidden = true;
391             if(this.rendered){
392                 this.onHide();
393             }
394             this.fireEvent("hide", this);
395         }
396         return this;
397     },
398
399     // private
400     onHide : function(){
401         var ae = this.getActionEl();
402         if(this.hideMode == 'visibility'){
403             ae.dom.style.visibility = "hidden";
404         }else if(this.hideMode == 'offsets'){
405             ae.addClass('x-hidden');
406         }else{
407             ae.dom.style.display = "none";
408         }
409     },
410
411     /**
412      * Convenience function to hide or show this component by boolean.
413      * @param {Boolean} visible True to show, false to hide
414      * @return {Roo.Component} this
415      */
416     setVisible: function(visible){
417         if(visible) {
418             this.show();
419         }else{
420             this.hide();
421         }
422         return this;
423     },
424
425     /**
426      * Returns true if this component is visible.
427      */
428     isVisible : function(){
429         return this.getActionEl().isVisible();
430     },
431
432     cloneConfig : function(overrides){
433         overrides = overrides || {};
434         var id = overrides.id || Roo.id();
435         var cfg = Roo.applyIf(overrides, this.initialConfig);
436         cfg.id = id; // prevent dup id
437         return new this.constructor(cfg);
438     }
439 });