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 = "ext-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
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' 
150      */
151     actionMode : "el",
152
153     /** @private */
154     getActionEl : function(){
155         return this[this.actionMode];
156     },
157
158     initComponent : Roo.emptyFn,
159     /**
160      * If this is a lazy rendering component, render it to its container element.
161      * @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.
162      */
163     render : function(container, position){
164         Roo.log('render the component');
165         if(!this.rendered && this.fireEvent("beforerender", this) !== false){
166             if(!container && this.el){
167                 this.el = Roo.get(this.el);
168                 container = this.el.dom.parentNode;
169                 this.allowDomMove = false;
170             }
171             this.container = Roo.get(container);
172             this.rendered = true;
173             if(position !== undefined){
174                 if(typeof position == 'number'){
175                     position = this.container.dom.childNodes[position];
176                 }else{
177                     position = Roo.getDom(position);
178                 }
179             }
180             this.onRender(this.container, position || null);
181             if(this.cls){
182                 this.el.addClass(this.cls);
183                 delete this.cls;
184             }
185             if(this.style){
186                 this.el.applyStyles(this.style);
187                 delete this.style;
188             }
189             this.fireEvent("render", this);
190             this.afterRender(this.container);
191             if(this.hidden){
192                 this.hide();
193             }
194             if(this.disabled){
195                 this.disable();
196             }
197         }
198         Roo.log('render the component');
199         Roo.log(this);
200         return this;
201     },
202
203     /** @private */
204     // default function is not really useful
205     onRender : function(ct, position){
206         if(this.el){
207             this.el = Roo.get(this.el);
208             if(this.allowDomMove !== false){
209                 ct.dom.insertBefore(this.el.dom, position);
210             }
211         }
212     },
213
214     /** @private */
215     getAutoCreate : function(){
216         var cfg = typeof this.autoCreate == "object" ?
217                       this.autoCreate : Roo.apply({}, this.defaultAutoCreate);
218         if(this.id && !cfg.id){
219             cfg.id = this.id;
220         }
221         return cfg;
222     },
223
224     /** @private */
225     afterRender : Roo.emptyFn,
226
227     /**
228      * Destroys this component by purging any event listeners, removing the component's element from the DOM,
229      * removing the component from its {@link Roo.Container} (if applicable) and unregistering it from {@link Roo.ComponentMgr}.
230      */
231     destroy : function(){
232         if(this.fireEvent("beforedestroy", this) !== false){
233             this.purgeListeners();
234             this.beforeDestroy();
235             if(this.rendered){
236                 this.el.removeAllListeners();
237                 this.el.remove();
238                 if(this.actionMode == "container"){
239                     this.container.remove();
240                 }
241             }
242             this.onDestroy();
243             Roo.ComponentMgr.unregister(this);
244             this.fireEvent("destroy", this);
245         }
246     },
247
248         /** @private */
249     beforeDestroy : function(){
250
251     },
252
253         /** @private */
254         onDestroy : function(){
255
256     },
257
258     /**
259      * Returns the underlying {@link Roo.Element}.
260      * @return {Roo.Element} The element
261      */
262     getEl : function(){
263         return this.el;
264     },
265
266     /**
267      * Returns the id of this component.
268      * @return {String}
269      */
270     getId : function(){
271         return this.id;
272     },
273
274     /**
275      * Try to focus this component.
276      * @param {Boolean} selectText True to also select the text in this component (if applicable)
277      * @return {Roo.Component} this
278      */
279     focus : function(selectText){
280         if(this.rendered){
281             this.el.focus();
282             if(selectText === true){
283                 this.el.dom.select();
284             }
285         }
286         return this;
287     },
288
289     /** @private */
290     blur : function(){
291         if(this.rendered){
292             this.el.blur();
293         }
294         return this;
295     },
296
297     /**
298      * Disable this component.
299      * @return {Roo.Component} this
300      */
301     disable : function(){
302         if(this.rendered){
303             this.onDisable();
304         }
305         this.disabled = true;
306         this.fireEvent("disable", this);
307         return this;
308     },
309
310         // private
311     onDisable : function(){
312         this.getActionEl().addClass(this.disabledClass);
313         this.el.dom.disabled = true;
314     },
315
316     /**
317      * Enable this component.
318      * @return {Roo.Component} this
319      */
320     enable : function(){
321         if(this.rendered){
322             this.onEnable();
323         }
324         this.disabled = false;
325         this.fireEvent("enable", this);
326         return this;
327     },
328
329         // private
330     onEnable : function(){
331         this.getActionEl().removeClass(this.disabledClass);
332         this.el.dom.disabled = false;
333     },
334
335     /**
336      * Convenience function for setting disabled/enabled by boolean.
337      * @param {Boolean} disabled
338      */
339     setDisabled : function(disabled){
340         this[disabled ? "disable" : "enable"]();
341     },
342
343     /**
344      * Show this component.
345      * @return {Roo.Component} this
346      */
347     show: function(){
348         if(this.fireEvent("beforeshow", this) !== false){
349             this.hidden = false;
350             if(this.rendered){
351                 this.onShow();
352             }
353             this.fireEvent("show", this);
354         }
355         return this;
356     },
357
358     // private
359     onShow : function(){
360         var ae = this.getActionEl();
361         if(this.hideMode == 'visibility'){
362             ae.dom.style.visibility = "visible";
363         }else if(this.hideMode == 'offsets'){
364             ae.removeClass('x-hidden');
365         }else{
366             ae.dom.style.display = "";
367         }
368     },
369
370     /**
371      * Hide this component.
372      * @return {Roo.Component} this
373      */
374     hide: function(){
375         if(this.fireEvent("beforehide", this) !== false){
376             this.hidden = true;
377             if(this.rendered){
378                 this.onHide();
379             }
380             this.fireEvent("hide", this);
381         }
382         return this;
383     },
384
385     // private
386     onHide : function(){
387         var ae = this.getActionEl();
388         if(this.hideMode == 'visibility'){
389             ae.dom.style.visibility = "hidden";
390         }else if(this.hideMode == 'offsets'){
391             ae.addClass('x-hidden');
392         }else{
393             ae.dom.style.display = "none";
394         }
395     },
396
397     /**
398      * Convenience function to hide or show this component by boolean.
399      * @param {Boolean} visible True to show, false to hide
400      * @return {Roo.Component} this
401      */
402     setVisible: function(visible){
403         if(visible) {
404             this.show();
405         }else{
406             this.hide();
407         }
408         return this;
409     },
410
411     /**
412      * Returns true if this component is visible.
413      */
414     isVisible : function(){
415         return this.getActionEl().isVisible();
416     },
417
418     cloneConfig : function(overrides){
419         overrides = overrides || {};
420         var id = overrides.id || Roo.id();
421         var cfg = Roo.applyIf(overrides, this.initialConfig);
422         cfg.id = id; // prevent dup id
423         return new this.constructor(cfg);
424     }
425 });