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