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");
27 config = config || {};
28 if(config.tagName || config.dom || typeof config == "string"){ // element object
29 config = {el: config, id: config.id || config};
31 this.initialConfig = config;
33 Roo.apply(this, config);
37 * Fires after the component is disabled.
38 * @param {Roo.Component} this
43 * Fires after the component is enabled.
44 * @param {Roo.Component} this
49 * Fires before the component is shown. Return false to stop the show.
50 * @param {Roo.Component} this
55 * Fires after the component is shown.
56 * @param {Roo.Component} this
61 * Fires before the component is hidden. Return false to stop the hide.
62 * @param {Roo.Component} this
67 * Fires after the component is hidden.
68 * @param {Roo.Component} this
73 * Fires before the component is rendered. Return false to stop the render.
74 * @param {Roo.Component} this
79 * Fires after the component is rendered.
80 * @param {Roo.Component} this
84 * @event beforedestroy
85 * Fires before the component is destroyed. Return false to stop the destroy.
86 * @param {Roo.Component} this
91 * Fires after the component is destroyed.
92 * @param {Roo.Component} this
97 this.id = "roo-comp-" + (++Roo.Component.AUTO_ID);
99 Roo.ComponentMgr.register(this);
100 Roo.Component.superclass.constructor.call(this);
101 this.initComponent();
102 if(this.renderTo){ // not supported by all components yet. use at your own risk!
103 this.render(this.renderTo);
104 delete this.renderTo;
109 Roo.Component.AUTO_ID = 1000;
111 Roo.extend(Roo.Component, Roo.util.Observable, {
113 * @scope Roo.Component.prototype
115 * true if this component is hidden. Read-only.
120 * true if this component is disabled. Read-only.
125 * true if this component has been rendered. Read-only.
129 /** @cfg {String} disableClass
130 * CSS class added to the component when it is disabled (defaults to "x-item-disabled").
132 disabledClass : "x-item-disabled",
133 /** @cfg {Boolean} allowDomMove
134 * Whether the component can move the Dom node when rendering (defaults to true).
137 /** @cfg {String} hideMode (display|visibility)
138 * How this component should hidden. Supported values are
139 * "visibility" (css visibility), "offsets" (negative offset position) and
140 * "display" (css display) - defaults to "display".
145 ctype : "Roo.Component",
148 * @cfg {String} actionMode
149 * which property holds the element that used for hide() / show() / disable() / enable()
150 * default is 'el' for forms you probably want to set this to fieldEl
155 getActionEl : function(){
156 return this[this.actionMode];
159 initComponent : Roo.emptyFn,
161 * If this is a lazy rendering component, render it to its container element.
162 * @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.
164 render : function(container, position){
170 if(this.fireEvent("beforerender", this) === false){
174 if(!container && this.el){
175 this.el = Roo.get(this.el);
176 container = this.el.dom.parentNode;
177 this.allowDomMove = false;
179 this.container = Roo.get(container);
180 this.rendered = true;
181 if(position !== undefined){
182 if(typeof position == 'number'){
183 position = this.container.dom.childNodes[position];
185 position = Roo.getDom(position);
188 this.onRender(this.container, position || null);
190 this.el.addClass(this.cls);
194 this.el.applyStyles(this.style);
197 this.fireEvent("render", this);
198 this.afterRender(this.container);
211 // default function is not really useful
212 onRender : function(ct, position){
213 console.log("THIS EL");
214 console.log(this.el);
216 this.el = Roo.get(this.el);
217 if(this.allowDomMove !== false){
218 ct.dom.insertBefore(this.el.dom, position);
224 getAutoCreate : function(){
225 var cfg = typeof this.autoCreate == "object" ?
226 this.autoCreate : Roo.apply({}, this.defaultAutoCreate);
227 if(this.id && !cfg.id){
234 afterRender : Roo.emptyFn,
237 * Destroys this component by purging any event listeners, removing the component's element from the DOM,
238 * removing the component from its {@link Roo.Container} (if applicable) and unregistering it from {@link Roo.ComponentMgr}.
240 destroy : function(){
241 if(this.fireEvent("beforedestroy", this) !== false){
242 this.purgeListeners();
243 this.beforeDestroy();
245 this.el.removeAllListeners();
247 if(this.actionMode == "container"){
248 this.container.remove();
252 Roo.ComponentMgr.unregister(this);
253 this.fireEvent("destroy", this);
258 beforeDestroy : function(){
263 onDestroy : function(){
268 * Returns the underlying {@link Roo.Element}.
269 * @return {Roo.Element} The element
276 * Returns the id of this component.
284 * Try to focus this component.
285 * @param {Boolean} selectText True to also select the text in this component (if applicable)
286 * @return {Roo.Component} this
288 focus : function(selectText){
291 if(selectText === true){
292 this.el.dom.select();
307 * Disable this component.
308 * @return {Roo.Component} this
310 disable : function(){
314 this.disabled = true;
315 this.fireEvent("disable", this);
320 onDisable : function(){
321 this.getActionEl().addClass(this.disabledClass);
322 this.el.dom.disabled = true;
326 * Enable this component.
327 * @return {Roo.Component} this
333 this.disabled = false;
334 this.fireEvent("enable", this);
339 onEnable : function(){
340 this.getActionEl().removeClass(this.disabledClass);
341 this.el.dom.disabled = false;
345 * Convenience function for setting disabled/enabled by boolean.
346 * @param {Boolean} disabled
348 setDisabled : function(disabled){
349 this[disabled ? "disable" : "enable"]();
353 * Show this component.
354 * @return {Roo.Component} this
357 if(this.fireEvent("beforeshow", this) !== false){
362 this.fireEvent("show", this);
369 var ae = this.getActionEl();
370 if(this.hideMode == 'visibility'){
371 ae.dom.style.visibility = "visible";
372 }else if(this.hideMode == 'offsets'){
373 ae.removeClass('x-hidden');
375 ae.dom.style.display = "";
380 * Hide this component.
381 * @return {Roo.Component} this
384 if(this.fireEvent("beforehide", this) !== false){
389 this.fireEvent("hide", this);
396 var ae = this.getActionEl();
397 if(this.hideMode == 'visibility'){
398 ae.dom.style.visibility = "hidden";
399 }else if(this.hideMode == 'offsets'){
400 ae.addClass('x-hidden');
402 ae.dom.style.display = "none";
407 * Convenience function to hide or show this component by boolean.
408 * @param {Boolean} visible True to show, false to hide
409 * @return {Roo.Component} this
411 setVisible: function(visible){
421 * Returns true if this component is visible.
423 isVisible : function(){
424 return this.getActionEl().isVisible();
427 cloneConfig : function(overrides){
428 overrides = overrides || {};
429 var id = overrides.id || Roo.id();
430 var cfg = Roo.applyIf(overrides, this.initialConfig);
431 cfg.id = id; // prevent dup id
432 return new this.constructor(cfg);