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 console.log("COMPONENT CONSTRUCTOR");
28 config = config || {};
29 if(config.tagName || config.dom || typeof config == "string"){ // element object
30 config = {el: config, id: config.id || config};
32 this.initialConfig = config;
34 Roo.apply(this, config);
38 * Fires after the component is disabled.
39 * @param {Roo.Component} this
44 * Fires after the component is enabled.
45 * @param {Roo.Component} this
50 * Fires before the component is shown. Return false to stop the show.
51 * @param {Roo.Component} this
56 * Fires after the component is shown.
57 * @param {Roo.Component} this
62 * Fires before the component is hidden. Return false to stop the hide.
63 * @param {Roo.Component} this
68 * Fires after the component is hidden.
69 * @param {Roo.Component} this
74 * Fires before the component is rendered. Return false to stop the render.
75 * @param {Roo.Component} this
80 * Fires after the component is rendered.
81 * @param {Roo.Component} this
85 * @event beforedestroy
86 * Fires before the component is destroyed. Return false to stop the destroy.
87 * @param {Roo.Component} this
92 * Fires after the component is destroyed.
93 * @param {Roo.Component} this
98 this.id = "roo-comp-" + (++Roo.Component.AUTO_ID);
100 Roo.ComponentMgr.register(this);
101 Roo.Component.superclass.constructor.call(this);
102 this.initComponent();
103 if(this.renderTo){ // not supported by all components yet. use at your own risk!
104 this.render(this.renderTo);
105 delete this.renderTo;
110 Roo.Component.AUTO_ID = 1000;
112 Roo.extend(Roo.Component, Roo.util.Observable, {
114 * @scope Roo.Component.prototype
116 * true if this component is hidden. Read-only.
121 * true if this component is disabled. Read-only.
126 * true if this component has been rendered. Read-only.
130 /** @cfg {String} disableClass
131 * CSS class added to the component when it is disabled (defaults to "x-item-disabled").
133 disabledClass : "x-item-disabled",
134 /** @cfg {Boolean} allowDomMove
135 * Whether the component can move the Dom node when rendering (defaults to true).
138 /** @cfg {String} hideMode (display|visibility)
139 * How this component should hidden. Supported values are
140 * "visibility" (css visibility), "offsets" (negative offset position) and
141 * "display" (css display) - defaults to "display".
146 ctype : "Roo.Component",
149 * @cfg {String} actionMode
150 * which property holds the element that used for hide() / show() / disable() / enable()
151 * default is 'el' for forms you probably want to set this to fieldEl
156 getActionEl : function(){
157 return this[this.actionMode];
160 initComponent : Roo.emptyFn,
162 * If this is a lazy rendering component, render it to its container element.
163 * @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.
165 render : function(container, position){
171 if(this.fireEvent("beforerender", this) === false){
175 if(!container && this.el){
176 this.el = Roo.get(this.el);
177 container = this.el.dom.parentNode;
178 this.allowDomMove = false;
180 this.container = Roo.get(container);
181 this.rendered = true;
182 if(position !== undefined){
183 if(typeof position == 'number'){
184 position = this.container.dom.childNodes[position];
186 position = Roo.getDom(position);
189 this.onRender(this.container, position || null);
191 this.el.addClass(this.cls);
195 this.el.applyStyles(this.style);
198 this.fireEvent("render", this);
199 this.afterRender(this.container);
212 // default function is not really useful
213 onRender : function(ct, position){
215 this.el = Roo.get(this.el);
216 if(this.allowDomMove !== false){
217 ct.dom.insertBefore(this.el.dom, position);
223 getAutoCreate : function(){
224 var cfg = typeof this.autoCreate == "object" ?
225 this.autoCreate : Roo.apply({}, this.defaultAutoCreate);
226 if(this.id && !cfg.id){
233 afterRender : Roo.emptyFn,
236 * Destroys this component by purging any event listeners, removing the component's element from the DOM,
237 * removing the component from its {@link Roo.Container} (if applicable) and unregistering it from {@link Roo.ComponentMgr}.
239 destroy : function(){
240 if(this.fireEvent("beforedestroy", this) !== false){
241 this.purgeListeners();
242 this.beforeDestroy();
244 this.el.removeAllListeners();
246 if(this.actionMode == "container"){
247 this.container.remove();
251 Roo.ComponentMgr.unregister(this);
252 this.fireEvent("destroy", this);
257 beforeDestroy : function(){
262 onDestroy : function(){
267 * Returns the underlying {@link Roo.Element}.
268 * @return {Roo.Element} The element
275 * Returns the id of this component.
283 * Try to focus this component.
284 * @param {Boolean} selectText True to also select the text in this component (if applicable)
285 * @return {Roo.Component} this
287 focus : function(selectText){
290 if(selectText === true){
291 this.el.dom.select();
306 * Disable this component.
307 * @return {Roo.Component} this
309 disable : function(){
313 this.disabled = true;
314 this.fireEvent("disable", this);
319 onDisable : function(){
320 this.getActionEl().addClass(this.disabledClass);
321 this.el.dom.disabled = true;
325 * Enable this component.
326 * @return {Roo.Component} this
332 this.disabled = false;
333 this.fireEvent("enable", this);
338 onEnable : function(){
339 this.getActionEl().removeClass(this.disabledClass);
340 this.el.dom.disabled = false;
344 * Convenience function for setting disabled/enabled by boolean.
345 * @param {Boolean} disabled
347 setDisabled : function(disabled){
348 this[disabled ? "disable" : "enable"]();
352 * Show this component.
353 * @return {Roo.Component} this
356 if(this.fireEvent("beforeshow", this) !== false){
361 this.fireEvent("show", this);
368 var ae = this.getActionEl();
369 if(this.hideMode == 'visibility'){
370 ae.dom.style.visibility = "visible";
371 }else if(this.hideMode == 'offsets'){
372 ae.removeClass('x-hidden');
374 ae.dom.style.display = "";
379 * Hide this component.
380 * @return {Roo.Component} this
383 if(this.fireEvent("beforehide", this) !== false){
388 this.fireEvent("hide", this);
395 var ae = this.getActionEl();
396 if(this.hideMode == 'visibility'){
397 ae.dom.style.visibility = "hidden";
398 }else if(this.hideMode == 'offsets'){
399 ae.addClass('x-hidden');
401 ae.dom.style.display = "none";
406 * Convenience function to hide or show this component by boolean.
407 * @param {Boolean} visible True to show, false to hide
408 * @return {Roo.Component} this
410 setVisible: function(visible){
420 * Returns true if this component is visible.
422 isVisible : function(){
423 return this.getActionEl().isVisible();
426 cloneConfig : function(overrides){
427 overrides = overrides || {};
428 var id = overrides.id || Roo.id();
429 var cfg = Roo.applyIf(overrides, this.initialConfig);
430 cfg.id = id; // prevent dup id
431 return new this.constructor(cfg);