4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
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.
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.
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};
30 this.initialConfig = config;
32 Roo.apply(this, config);
36 * Fires after the component is disabled.
37 * @param {Roo.Component} this
42 * Fires after the component is enabled.
43 * @param {Roo.Component} this
48 * Fires before the component is shown. Return false to stop the show.
49 * @param {Roo.Component} this
54 * Fires after the component is shown.
55 * @param {Roo.Component} this
60 * Fires before the component is hidden. Return false to stop the hide.
61 * @param {Roo.Component} this
66 * Fires after the component is hidden.
67 * @param {Roo.Component} this
72 * Fires before the component is rendered. Return false to stop the render.
73 * @param {Roo.Component} this
78 * Fires after the component is rendered.
79 * @param {Roo.Component} this
83 * @event beforedestroy
84 * Fires before the component is destroyed. Return false to stop the destroy.
85 * @param {Roo.Component} this
90 * Fires after the component is destroyed.
91 * @param {Roo.Component} this
96 this.id = "roo-comp-" + (++Roo.Component.AUTO_ID);
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;
108 Roo.Component.AUTO_ID = 1000;
110 Roo.extend(Roo.Component, Roo.util.Observable, {
112 * @scope Roo.Component.prototype
114 * true if this component is hidden. Read-only.
119 * true if this component is disabled. Read-only.
124 * true if this component has been rendered. Read-only.
128 /** @cfg {String} disableClass
129 * CSS class added to the component when it is disabled (defaults to "x-item-disabled").
131 disabledClass : "x-item-disabled",
132 /** @cfg {Boolean} allowDomMove
133 * Whether the component can move the Dom node when rendering (defaults to 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".
144 ctype : "Roo.Component",
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
154 getActionEl : function(){
155 return this[this.actionMode];
158 initComponent : Roo.emptyFn,
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.
163 render : function(container, position){
169 if(this.fireEvent("beforerender", this) === false){
173 if(!container && this.el){
174 this.el = Roo.get(this.el);
175 container = this.el.dom.parentNode;
176 this.allowDomMove = false;
178 this.container = Roo.get(container);
179 this.rendered = true;
180 if(position !== undefined){
181 if(typeof position == 'number'){
182 position = this.container.dom.childNodes[position];
184 position = Roo.getDom(position);
187 this.onRender(this.container, position || null);
189 this.el.addClass(this.cls);
193 this.el.applyStyles(this.style);
196 this.fireEvent("render", this);
197 this.afterRender(this.container);
210 // default function is not really useful
211 onRender : function(ct, position){
213 this.el = Roo.get(this.el);
214 if(this.allowDomMove !== false){
215 ct.dom.insertBefore(this.el.dom, position);
221 getAutoCreate : function(){
222 var cfg = typeof this.autoCreate == "object" ?
223 this.autoCreate : Roo.apply({}, this.defaultAutoCreate);
224 if(this.id && !cfg.id){
231 afterRender : Roo.emptyFn,
234 * Destroys this component by purging any event listeners, removing the component's element from the DOM,
235 * removing the component from its {@link Roo.Container} (if applicable) and unregistering it from {@link Roo.ComponentMgr}.
237 destroy : function(){
238 if(this.fireEvent("beforedestroy", this) !== false){
239 this.purgeListeners();
240 this.beforeDestroy();
242 this.el.removeAllListeners();
244 if(this.actionMode == "container"){
245 this.container.remove();
249 Roo.ComponentMgr.unregister(this);
250 this.fireEvent("destroy", this);
255 beforeDestroy : function(){
260 onDestroy : function(){
265 * Returns the underlying {@link Roo.Element}.
266 * @return {Roo.Element} The element
273 * Returns the id of this component.
281 * Try to focus this component.
282 * @param {Boolean} selectText True to also select the text in this component (if applicable)
283 * @return {Roo.Component} this
285 focus : function(selectText){
288 if(selectText === true){
289 this.el.dom.select();
304 * Disable this component.
305 * @return {Roo.Component} this
307 disable : function(){
311 this.disabled = true;
312 this.fireEvent("disable", this);
317 onDisable : function(){
318 this.getActionEl().addClass(this.disabledClass);
319 this.el.dom.disabled = true;
323 * Enable this component.
324 * @return {Roo.Component} this
330 this.disabled = false;
331 this.fireEvent("enable", this);
336 onEnable : function(){
337 this.getActionEl().removeClass(this.disabledClass);
338 this.el.dom.disabled = false;
342 * Convenience function for setting disabled/enabled by boolean.
343 * @param {Boolean} disabled
345 setDisabled : function(disabled){
346 this[disabled ? "disable" : "enable"]();
350 * Show this component.
351 * @return {Roo.Component} this
354 if(this.fireEvent("beforeshow", this) !== false){
359 this.fireEvent("show", this);
366 var ae = this.getActionEl();
367 if(this.hideMode == 'visibility'){
368 ae.dom.style.visibility = "visible";
369 }else if(this.hideMode == 'offsets'){
370 ae.removeClass('x-hidden');
372 ae.dom.style.display = "";
377 * Hide this component.
378 * @return {Roo.Component} this
381 if(this.fireEvent("beforehide", this) !== false){
386 this.fireEvent("hide", this);
393 var ae = this.getActionEl();
394 if(this.hideMode == 'visibility'){
395 ae.dom.style.visibility = "hidden";
396 }else if(this.hideMode == 'offsets'){
397 ae.addClass('x-hidden');
399 ae.dom.style.display = "none";
404 * Convenience function to hide or show this component by boolean.
405 * @param {Boolean} visible True to show, false to hide
406 * @return {Roo.Component} this
408 setVisible: function(visible){
418 * Returns true if this component is visible.
420 isVisible : function(){
421 return this.getActionEl().isVisible();
424 cloneConfig : function(overrides){
425 overrides = overrides || {};
426 var id = overrides.id || Roo.id();
427 var cfg = Roo.applyIf(overrides, this.initialConfig);
428 cfg.id = id; // prevent dup id
429 return new this.constructor(cfg);