3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
9 * @extends Ext.util.Observable
10 * <p>Base class for all Ext components. All subclasses of Component may participate in the automated
11 * Ext component lifecycle of creation, rendering and destruction which is provided by the {@link Ext.Container Container} class.
12 * Components may be added to a Container through the {@link Ext.Container#items items} config option at the time the Container is created,
13 * or they may be added dynamically via the {@link Ext.Container#add add} method.</p>
14 * <p>The Component base class has built-in support for basic hide/show and enable/disable behavior.</p>
15 * <p>All Components are registered with the {@link Ext.ComponentMgr} on construction so that they can be referenced at any time via
16 * {@link Ext#getCmp}, passing the {@link #id}.</p>
17 * <p>All user-developed visual widgets that are required to participate in automated lifecycle and size management should subclass Component (or
18 * {@link Ext.BoxComponent} if managed box model handling is required, ie height and width management).</p>
19 * <p>See the <a href="http://extjs.com/learn/Tutorial:Creating_new_UI_controls">Creating new UI controls</a> tutorial for details on how
20 * and to either extend or augment ExtJs base classes to create custom Components.</p>
21 * <p>Every component has a specific xtype, which is its Ext-specific type name, along with methods for checking the
22 * xtype like {@link #getXType} and {@link #isXType}. This is the list of all valid xtypes:</p>
25 ------------- ------------------
26 box {@link Ext.BoxComponent}
27 button {@link Ext.Button}
28 buttongroup {@link Ext.ButtonGroup}
29 colorpalette {@link Ext.ColorPalette}
30 component {@link Ext.Component}
31 container {@link Ext.Container}
32 cycle {@link Ext.CycleButton}
33 dataview {@link Ext.DataView}
34 datepicker {@link Ext.DatePicker}
35 editor {@link Ext.Editor}
36 editorgrid {@link Ext.grid.EditorGridPanel}
37 flash {@link Ext.FlashComponent}
38 grid {@link Ext.grid.GridPanel}
39 listview {@link Ext.ListView}
40 panel {@link Ext.Panel}
41 progress {@link Ext.ProgressBar}
42 propertygrid {@link Ext.grid.PropertyGrid}
43 slider {@link Ext.Slider}
44 spacer {@link Ext.Spacer}
45 splitbutton {@link Ext.SplitButton}
46 tabpanel {@link Ext.TabPanel}
47 treepanel {@link Ext.tree.TreePanel}
48 viewport {@link Ext.ViewPort}
49 window {@link Ext.Window}
52 ---------------------------------------
53 paging {@link Ext.PagingToolbar}
54 toolbar {@link Ext.Toolbar}
55 tbbutton {@link Ext.Toolbar.Button} (deprecated; use button)
56 tbfill {@link Ext.Toolbar.Fill}
57 tbitem {@link Ext.Toolbar.Item}
58 tbseparator {@link Ext.Toolbar.Separator}
59 tbspacer {@link Ext.Toolbar.Spacer}
60 tbsplit {@link Ext.Toolbar.SplitButton} (deprecated; use splitbutton)
61 tbtext {@link Ext.Toolbar.TextItem}
64 ---------------------------------------
65 menu {@link Ext.menu.Menu}
66 colormenu {@link Ext.menu.ColorMenu}
67 datemenu {@link Ext.menu.DateMenu}
68 menubaseitem {@link Ext.menu.BaseItem}
69 menucheckitem {@link Ext.menu.CheckItem}
70 menuitem {@link Ext.menu.Item}
71 menuseparator {@link Ext.menu.Separator}
72 menutextitem {@link Ext.menu.TextItem}
75 ---------------------------------------
76 form {@link Ext.FormPanel}
77 checkbox {@link Ext.form.Checkbox}
78 checkboxgroup {@link Ext.form.CheckboxGroup}
79 combo {@link Ext.form.ComboBox}
80 datefield {@link Ext.form.DateField}
81 displayfield {@link Ext.form.DisplayField}
82 field {@link Ext.form.Field}
83 fieldset {@link Ext.form.FieldSet}
84 hidden {@link Ext.form.Hidden}
85 htmleditor {@link Ext.form.HtmlEditor}
86 label {@link Ext.form.Label}
87 numberfield {@link Ext.form.NumberField}
88 radio {@link Ext.form.Radio}
89 radiogroup {@link Ext.form.RadioGroup}
90 textarea {@link Ext.form.TextArea}
91 textfield {@link Ext.form.TextField}
92 timefield {@link Ext.form.TimeField}
93 trigger {@link Ext.form.TriggerField}
96 ---------------------------------------
97 chart {@link Ext.chart.Chart}
98 barchart {@link Ext.chart.BarChart}
99 cartesianchart {@link Ext.chart.CartesianChart}
100 columnchart {@link Ext.chart.ColumnChart}
101 linechart {@link Ext.chart.LineChart}
102 piechart {@link Ext.chart.PieChart}
105 ---------------------------------------
106 arraystore {@link Ext.data.ArrayStore}
107 directstore {@link Ext.data.DirectStore}
108 groupingstore {@link Ext.data.GroupingStore}
109 jsonstore {@link Ext.data.JsonStore}
110 simplestore {@link Ext.data.SimpleStore} (deprecated; use arraystore)
111 store {@link Ext.data.Store}
112 xmlstore {@link Ext.data.XmlStore}
115 * @param {Ext.Element/String/Object} config The configuration options may be specified as either:
116 * <div class="mdetail-params"><ul>
117 * <li><b>an element</b> :
118 * <p class="sub-desc">it is set as the internal element and its id used as the component id</p></li>
119 * <li><b>a string</b> :
120 * <p class="sub-desc">it is assumed to be the id of an existing element and is used as the component id</p></li>
121 * <li><b>anything else</b> :
122 * <p class="sub-desc">it is assumed to be a standard config object and is applied to the component</p></li>
125 Ext.Component = function(config){
126 config = config || {};
127 if(config.initialConfig){
128 if(config.isAction){ // actions
129 this.baseAction = config;
131 config = config.initialConfig; // component cloning / action set up
132 }else if(config.tagName || config.dom || Ext.isString(config)){ // element object
133 config = {applyTo: config, id: config.id || config};
137 * This Component's initial configuration specification. Read-only.
139 * @property initialConfig
141 this.initialConfig = config;
143 Ext.apply(this, config);
147 * Fires after the component is disabled.
148 * @param {Ext.Component} this
153 * Fires after the component is enabled.
154 * @param {Ext.Component} this
159 * Fires before the component is shown by calling the {@link #show} method.
160 * Return false from an event handler to stop the show.
161 * @param {Ext.Component} this
166 * Fires after the component is shown when calling the {@link #show} method.
167 * @param {Ext.Component} this
172 * Fires before the component is hidden by calling the {@link #hide} method.
173 * Return false from an event handler to stop the hide.
174 * @param {Ext.Component} this
179 * Fires after the component is hidden.
180 * Fires after the component is hidden when calling the {@link #hide} method.
181 * @param {Ext.Component} this
185 * @event beforerender
186 * Fires before the component is {@link #rendered}. Return false from an
187 * event handler to stop the {@link #render}.
188 * @param {Ext.Component} this
193 * Fires after the component markup is {@link #rendered}.
194 * @param {Ext.Component} this
199 * <p>Fires after the component rendering is finished.</p>
200 * <p>The afterrender event is fired after this Component has been {@link #rendered}, been postprocesed
201 * by any afterRender method defined for the Component, and, if {@link #stateful}, after state
202 * has been restored.</p>
203 * @param {Ext.Component} this
207 * @event beforedestroy
208 * Fires before the component is {@link #destroy}ed. Return false from an event handler to stop the {@link #destroy}.
209 * @param {Ext.Component} this
214 * Fires after the component is {@link #destroy}ed.
215 * @param {Ext.Component} this
219 * @event beforestaterestore
220 * Fires before the state of the component is restored. Return false from an event handler to stop the restore.
221 * @param {Ext.Component} this
222 * @param {Object} state The hash of state values returned from the StateProvider. If this
223 * event is not vetoed, then the state object is passed to <b><tt>applyState</tt></b>. By default,
224 * that simply copies property values into this Component. The method maybe overriden to
225 * provide custom state restoration.
227 'beforestaterestore',
229 * @event staterestore
230 * Fires after the state of the component is restored.
231 * @param {Ext.Component} this
232 * @param {Object} state The hash of state values returned from the StateProvider. This is passed
233 * to <b><tt>applyState</tt></b>. By default, that simply copies property values into this
234 * Component. The method maybe overriden to provide custom state restoration.
238 * @event beforestatesave
239 * Fires before the state of the component is saved to the configured state provider. Return false to stop the save.
240 * @param {Ext.Component} this
241 * @param {Object} state The hash of state values. This is determined by calling
242 * <b><tt>getState()</tt></b> on the Component. This method must be provided by the
243 * developer to return whetever representation of state is required, by default, Ext.Component
244 * has a null implementation.
249 * Fires after the state of the component is saved to the configured state provider.
250 * @param {Ext.Component} this
251 * @param {Object} state The hash of state values. This is determined by calling
252 * <b><tt>getState()</tt></b> on the Component. This method must be provided by the
253 * developer to return whetever representation of state is required, by default, Ext.Component
254 * has a null implementation.
259 Ext.ComponentMgr.register(this);
260 Ext.Component.superclass.constructor.call(this);
263 this.baseAction.addComponent(this);
266 this.initComponent();
269 if(Ext.isArray(this.plugins)){
270 for(var i = 0, len = this.plugins.length; i < len; i++){
271 this.plugins[i] = this.initPlugin(this.plugins[i]);
274 this.plugins = this.initPlugin(this.plugins);
278 if(this.stateful !== false){
279 this.initState(config);
283 this.applyToMarkup(this.applyTo);
285 }else if(this.renderTo){
286 this.render(this.renderTo);
287 delete this.renderTo;
292 Ext.Component.AUTO_ID = 1000;
294 Ext.extend(Ext.Component, Ext.util.Observable, {
295 // Configs below are used for all Components when rendered by FormLayout.
297 * @cfg {String} fieldLabel <p>The label text to display next to this Component (defaults to '').</p>
298 * <br><p><b>Note</b>: this config is only used when this Component is rendered by a Container which
299 * has been configured to use the <b>{@link Ext.layout.FormLayout FormLayout}</b> layout manager (e.g.
300 * {@link Ext.form.FormPanel} or specifying <tt>layout:'form'</tt>).</p><br>
301 * <p>Also see <tt>{@link #hideLabel}</tt> and
302 * {@link Ext.layout.FormLayout}.{@link Ext.layout.FormLayout#fieldTpl fieldTpl}.</p>
303 * Example use:<pre><code>
306 renderTo: Ext.getBody(),
315 * @cfg {String} labelStyle <p>A CSS style specification string to apply directly to this field's
316 * label. Defaults to the container's labelStyle value if set (e.g.,
317 * <tt>{@link Ext.layout.FormLayout#labelStyle}</tt> , or '').</p>
318 * <br><p><b>Note</b>: see the note for <code>{@link #clearCls}</code>.</p><br>
319 * <p>Also see <code>{@link #hideLabel}</code> and
320 * <code>{@link Ext.layout.FormLayout}.{@link Ext.layout.FormLayout#fieldTpl fieldTpl}.</code></p>
321 * Example use:<pre><code>
324 renderTo: Ext.getBody(),
328 labelStyle: 'font-weight:bold;'
334 * @cfg {String} labelSeparator <p>The separator to display after the text of each
335 * <tt>{@link #fieldLabel}</tt>. This property may be configured at various levels.
336 * The order of precedence is:
337 * <div class="mdetail-params"><ul>
338 * <li>field / component level</li>
339 * <li>container level</li>
340 * <li>{@link Ext.layout.FormLayout#labelSeparator layout level} (defaults to colon <tt>':'</tt>)</li>
342 * To display no separator for this field's label specify empty string ''.</p>
343 * <br><p><b>Note</b>: see the note for <tt>{@link #clearCls}</tt>.</p><br>
344 * <p>Also see <tt>{@link #hideLabel}</tt> and
345 * {@link Ext.layout.FormLayout}.{@link Ext.layout.FormLayout#fieldTpl fieldTpl}.</p>
346 * Example use:<pre><code>
349 renderTo: Ext.getBody(),
351 labelSeparator: '~' // layout config has lowest priority (defaults to ':')
353 {@link Ext.layout.FormLayout#labelSeparator labelSeparator}: '>>', // config at container level
356 fieldLabel: 'Field 1',
357 labelSeparator: '...' // field/component level config supersedes others
360 fieldLabel: 'Field 2' // labelSeparator will be '='
366 * @cfg {Boolean} hideLabel <p><tt>true</tt> to completely hide the label element
367 * ({@link #fieldLabel label} and {@link #labelSeparator separator}). Defaults to <tt>false</tt>.
368 * By default, even if you do not specify a <tt>{@link #fieldLabel}</tt> the space will still be
369 * reserved so that the field will line up with other fields that do have labels.
370 * Setting this to <tt>true</tt> will cause the field to not reserve that space.</p>
371 * <br><p><b>Note</b>: see the note for <tt>{@link #clearCls}</tt>.</p><br>
372 * Example use:<pre><code>
375 renderTo: Ext.getBody(),
384 * @cfg {String} clearCls <p>The CSS class used to to apply to the special clearing div rendered
385 * directly after each form field wrapper to provide field clearing (defaults to
386 * <tt>'x-form-clear-left'</tt>).</p>
387 * <br><p><b>Note</b>: this config is only used when this Component is rendered by a Container
388 * which has been configured to use the <b>{@link Ext.layout.FormLayout FormLayout}</b> layout
389 * manager (e.g. {@link Ext.form.FormPanel} or specifying <tt>layout:'form'</tt>) and either a
390 * <tt>{@link #fieldLabel}</tt> is specified or <tt>isFormField=true</tt> is specified.</p><br>
391 * <p>See {@link Ext.layout.FormLayout}.{@link Ext.layout.FormLayout#fieldTpl fieldTpl} also.</p>
394 * @cfg {String} itemCls
395 * <p><b>Note</b>: this config is only used when this Component is rendered by a Container which
396 * has been configured to use the <b>{@link Ext.layout.FormLayout FormLayout}</b> layout manager (e.g.
397 * {@link Ext.form.FormPanel} or specifying <tt>layout:'form'</tt>).</p><br>
398 * <p>An additional CSS class to apply to the div wrapping the form item
399 * element of this field. If supplied, <tt>itemCls</tt> at the <b>field</b> level will override
400 * the default <tt>itemCls</tt> supplied at the <b>container</b> level. The value specified for
401 * <tt>itemCls</tt> will be added to the default class (<tt>'x-form-item'</tt>).</p>
402 * <p>Since it is applied to the item wrapper (see
403 * {@link Ext.layout.FormLayout}.{@link Ext.layout.FormLayout#fieldTpl fieldTpl}), it allows
404 * you to write standard CSS rules that can apply to the field, the label (if specified), or
405 * any other element within the markup for the field.</p>
406 * <br><p><b>Note</b>: see the note for <tt>{@link #fieldLabel}</tt>.</p><br>
407 * Example use:<pre><code>
408 // Apply a style to the field's label:
410 .required .x-form-item-label {font-weight:bold;color:red;}
415 renderTo: Ext.getBody(),
419 itemCls: 'required' //this label will be styled
422 fieldLabel: 'Favorite Color'
428 // Configs below are used for all Components when rendered by AnchorLayout.
430 * @cfg {String} anchor <p><b>Note</b>: this config is only used when this Component is rendered
431 * by a Container which has been configured to use an <b>{@link Ext.layout.AnchorLayout AnchorLayout}</b>
432 * based layout manager, for example:<div class="mdetail-params"><ul>
433 * <li>{@link Ext.form.FormPanel}</li>
434 * <li>specifying <code>layout: 'anchor' // or 'form', or 'absolute'</code></li>
436 * <p>See {@link Ext.layout.AnchorLayout}.{@link Ext.layout.AnchorLayout#anchor anchor} also.</p>
441 * <p>The <b>unique</b> id of this component (defaults to an {@link #getId auto-assigned id}).
442 * You should assign an id if you need to be able to access the component later and you do
443 * not have an object reference available (e.g., using {@link Ext}.{@link Ext#getCmp getCmp}).</p>
444 * <p>Note that this id will also be used as the element id for the containing HTML element
445 * that is rendered to the page for this component. This allows you to write id-based CSS
446 * rules to style the specific instance of this component uniquely, and also to select
447 * sub-elements using this component's id as the parent.</p>
448 * <p><b>Note</b>: to avoid complications imposed by a unique <tt>id</tt> also see
449 * <code>{@link #itemId}</code> and <code>{@link #ref}</code>.</p>
450 * <p><b>Note</b>: to access the container of an item see <code>{@link #ownerCt}</code>.</p>
453 * @cfg {String} itemId
454 * <p>An <tt>itemId</tt> can be used as an alternative way to get a reference to a component
455 * when no object reference is available. Instead of using an <code>{@link #id}</code> with
456 * {@link Ext}.{@link Ext#getCmp getCmp}, use <code>itemId</code> with
457 * {@link Ext.Container}.{@link Ext.Container#getComponent getComponent} which will retrieve
458 * <code>itemId</code>'s or <tt>{@link #id}</tt>'s. Since <code>itemId</code>'s are an index to the
459 * container's internal MixedCollection, the <code>itemId</code> is scoped locally to the container --
460 * avoiding potential conflicts with {@link Ext.ComponentMgr} which requires a <b>unique</b>
461 * <code>{@link #id}</code>.</p>
463 var c = new Ext.Panel({ //
464 {@link Ext.BoxComponent#height height}: 300,
465 {@link #renderTo}: document.body,
466 {@link Ext.Container#layout layout}: 'auto',
467 {@link Ext.Container#items items}: [
470 {@link Ext.Panel#title title}: 'Panel 1',
471 {@link Ext.BoxComponent#height height}: 150
475 {@link Ext.Panel#title title}: 'Panel 2',
476 {@link Ext.BoxComponent#height height}: 150
480 p1 = c.{@link Ext.Container#getComponent getComponent}('p1'); // not the same as {@link Ext#getCmp Ext.getCmp()}
481 p2 = p1.{@link #ownerCt}.{@link Ext.Container#getComponent getComponent}('p2'); // reference via a sibling
483 * <p>Also see <tt>{@link #id}</tt> and <code>{@link #ref}</code>.</p>
484 * <p><b>Note</b>: to access the container of an item see <tt>{@link #ownerCt}</tt>.</p>
487 * @cfg {String} xtype
488 * The registered <tt>xtype</tt> to create. This config option is not used when passing
489 * a config object into a constructor. This config option is used only when
490 * lazy instantiation is being used, and a child item of a Container is being
491 * specified not as a fully instantiated Component, but as a <i>Component config
492 * object</i>. The <tt>xtype</tt> will be looked up at render time up to determine what
493 * type of child Component to create.<br><br>
494 * The predefined xtypes are listed {@link Ext.Component here}.
496 * If you subclass Components to create your own Components, you may register
497 * them using {@link Ext.ComponentMgr#registerType} in order to be able to
498 * take advantage of lazy instantiation and rendering.
501 * @cfg {String} ptype
502 * The registered <tt>ptype</tt> to create. This config option is not used when passing
503 * a config object into a constructor. This config option is used only when
504 * lazy instantiation is being used, and a Plugin is being
505 * specified not as a fully instantiated Component, but as a <i>Component config
506 * object</i>. The <tt>ptype</tt> will be looked up at render time up to determine what
507 * type of Plugin to create.<br><br>
508 * If you create your own Plugins, you may register them using
509 * {@link Ext.ComponentMgr#registerPlugin} in order to be able to
510 * take advantage of lazy instantiation and rendering.
514 * An optional extra CSS class that will be added to this component's Element (defaults to ''). This can be
515 * useful for adding customized styles to the component or any of its children using standard CSS rules.
518 * @cfg {String} overCls
519 * An optional extra CSS class that will be added to this component's Element when the mouse moves
520 * over the Element, and removed when the mouse moves out. (defaults to ''). This can be
521 * useful for adding customized 'active' or 'hover' styles to the component or any of its children using standard CSS rules.
524 * @cfg {String} style
525 * A custom style specification to be applied to this component's Element. Should be a valid argument to
526 * {@link Ext.Element#applyStyles}.
530 renderTo: Ext.getBody(),
531 width: 400, height: 300,
552 * @cfg {String} ctCls
553 * <p>An optional extra CSS class that will be added to this component's container. This can be useful for
554 * adding customized styles to the container or any of its children using standard CSS rules. See
555 * {@link Ext.layout.ContainerLayout}.{@link Ext.layout.ContainerLayout#extraCls extraCls} also.</p>
556 * <p><b>Note</b>: <tt>ctCls</tt> defaults to <tt>''</tt> except for the following class
557 * which assigns a value by default:
558 * <div class="mdetail-params"><ul>
559 * <li>{@link Ext.layout.Box Box Layout} : <tt>'x-box-layout-ct'</tt></li>
561 * To configure the above Class with an extra CSS class append to the default. For example,
562 * for BoxLayout (Hbox and Vbox):<pre><code>
563 * ctCls: 'x-box-layout-ct custom-class'
568 * @cfg {Boolean} disabled
569 * Render this component disabled (default is false).
573 * @cfg {Boolean} hidden
574 * Render this component hidden (default is false). If <tt>true</tt>, the
575 * {@link #hide} method will be called internally.
579 * @cfg {Object/Array} plugins
580 * An object or array of objects that will provide custom functionality for this component. The only
581 * requirement for a valid plugin is that it contain an init method that accepts a reference of type Ext.Component.
582 * When a component is created, if any plugins are available, the component will call the init method on each
583 * plugin, passing a reference to itself. Each plugin can then call methods or respond to events on the
584 * component as needed to provide its functionality.
587 * @cfg {Mixed} applyTo
588 * <p>Specify the id of the element, a DOM element or an existing Element corresponding to a DIV
589 * that is already present in the document that specifies some structural markup for this
590 * component.</p><div><ul>
591 * <li><b>Description</b> : <ul>
592 * <div class="sub-desc">When <tt>applyTo</tt> is used, constituent parts of the component can also be specified
593 * by id or CSS class name within the main element, and the component being created may attempt
594 * to create its subcomponents from that markup if applicable.</div>
596 * <li><b>Notes</b> : <ul>
597 * <div class="sub-desc">When using this config, a call to render() is not required.</div>
598 * <div class="sub-desc">If applyTo is specified, any value passed for {@link #renderTo} will be ignored and the target
599 * element's parent node will automatically be used as the component's container.</div>
604 * @cfg {Mixed} renderTo
605 * <p>Specify the id of the element, a DOM element or an existing Element that this component
606 * will be rendered into.</p><div><ul>
607 * <li><b>Notes</b> : <ul>
608 * <div class="sub-desc">Do <u>not</u> use this option if the Component is to be a child item of
609 * a {@link Ext.Container Container}. It is the responsibility of the
610 * {@link Ext.Container Container}'s {@link Ext.Container#layout layout manager}
611 * to render and manage its child items.</div>
612 * <div class="sub-desc">When using this config, a call to render() is not required.</div>
615 * <p>See <tt>{@link #render}</tt> also.</p>
618 * @cfg {Boolean} stateful
619 * <p>A flag which causes the Component to attempt to restore the state of
620 * internal properties from a saved state on startup. The component must have
621 * either a <code>{@link #stateId}</code> or <code>{@link #id}</code> assigned
622 * for state to be managed. Auto-generated ids are not guaranteed to be stable
623 * across page loads and cannot be relied upon to save and restore the same
624 * state for a component.<p>
625 * <p>For state saving to work, the state manager's provider must have been
626 * set to an implementation of {@link Ext.state.Provider} which overrides the
627 * {@link Ext.state.Provider#set set} and {@link Ext.state.Provider#get get}
628 * methods to save and recall name/value pairs. A built-in implementation,
629 * {@link Ext.state.CookieProvider} is available.</p>
630 * <p>To set the state provider for the current page:</p>
632 Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
633 expires: new Date(new Date().getTime()+(1000*60*60*24*7)), //7 days from now
636 * <p>A stateful Component attempts to save state when one of the events
637 * listed in the <code>{@link #stateEvents}</code> configuration fires.</p>
638 * <p>To save state, a stateful Component first serializes its state by
639 * calling <b><code>getState</code></b>. By default, this function does
640 * nothing. The developer must provide an implementation which returns an
641 * object hash which represents the Component's restorable state.</p>
642 * <p>The value yielded by getState is passed to {@link Ext.state.Manager#set}
643 * which uses the configured {@link Ext.state.Provider} to save the object
644 * keyed by the Component's <code>{@link stateId}</code>, or, if that is not
645 * specified, its <code>{@link #id}</code>.</p>
646 * <p>During construction, a stateful Component attempts to <i>restore</i>
647 * its state by calling {@link Ext.state.Manager#get} passing the
648 * <code>{@link #stateId}</code>, or, if that is not specified, the
649 * <code>{@link #id}</code>.</p>
650 * <p>The resulting object is passed to <b><code>applyState</code></b>.
651 * The default implementation of <code>applyState</code> simply copies
652 * properties into the object, but a developer may override this to support
653 * more behaviour.</p>
654 * <p>You can perform extra processing on state save and restore by attaching
655 * handlers to the {@link #beforestaterestore}, {@link #staterestore},
656 * {@link #beforestatesave} and {@link #statesave} events.</p>
659 * @cfg {String} stateId
660 * The unique id for this component to use for state management purposes
661 * (defaults to the component id if one was set, otherwise null if the
662 * component is using a generated id).
663 * <p>See <code>{@link #stateful}</code> for an explanation of saving and
664 * restoring Component state.</p>
667 * @cfg {Array} stateEvents
668 * <p>An array of events that, when fired, should trigger this component to
669 * save its state (defaults to none). <code>stateEvents</code> may be any type
670 * of event supported by this component, including browser or custom events
671 * (e.g., <tt>['click', 'customerchange']</tt>).</p>
672 * <p>See <code>{@link #stateful}</code> for an explanation of saving and
673 * restoring Component state.</p>
676 * @cfg {Mixed} autoEl
677 * <p>A tag name or {@link Ext.DomHelper DomHelper} spec used to create the {@link #getEl Element} which will
678 * encapsulate this Component.</p>
679 * <p>You do not normally need to specify this. For the base classes {@link Ext.Component}, {@link Ext.BoxComponent},
680 * and {@link Ext.Container}, this defaults to <b><tt>'div'</tt></b>. The more complex Ext classes use a more complex
681 * DOM structure created by their own onRender methods.</p>
682 * <p>This is intended to allow the developer to create application-specific utility Components encapsulated by
683 * different DOM elements. Example usage:</p><pre><code>
688 src: 'http://www.example.com/example.jpg'
694 html: 'autoEl is cool!'
699 cls: 'ux-unordered-list',
703 html: 'First list item'
711 * @cfg {String} disabledClass
712 * CSS class added to the component when it is disabled (defaults to 'x-item-disabled').
714 disabledClass : 'x-item-disabled',
716 * @cfg {Boolean} allowDomMove
717 * Whether the component can move the Dom node when rendering (defaults to true).
721 * @cfg {Boolean} autoShow
722 * True if the component should check for hidden classes (e.g. 'x-hidden' or 'x-hide-display') and remove
723 * them on render (defaults to false).
727 * @cfg {String} hideMode
728 * <p>How this component should be hidden. Supported values are <tt>'visibility'</tt>
729 * (css visibility), <tt>'offsets'</tt> (negative offset position) and <tt>'display'</tt>
731 * <br><p><b>Note</b>: the default of <tt>'display'</tt> is generally preferred
732 * since items are automatically laid out when they are first shown (no sizing
733 * is done while hidden).</p>
735 hideMode : 'display',
737 * @cfg {Boolean} hideParent
738 * True to hide and show the component's container when hide/show is called on the component, false to hide
739 * and show the component itself (defaults to false). For example, this can be used as a shortcut for a hide
740 * button on a window by setting hide:true on the button when adding it to its parent container.
744 * <p>The {@link Ext.Element} which encapsulates this Component. Read-only.</p>
745 * <p>This will <i>usually</i> be a <DIV> element created by the class's onRender method, but
746 * that may be overridden using the <code>{@link #autoEl}</code> config.</p>
747 * <br><p><b>Note</b>: this element will not be available until this Component has been rendered.</p><br>
748 * <p>To add listeners for <b>DOM events</b> to this Component (as opposed to listeners
749 * for this Component's own Observable events), see the {@link Ext.util.Observable#listeners listeners}
750 * config for a suggestion, or use a render listener directly:</p><pre><code>
752 title: 'The Clickable Panel',
754 render: function(p) {
755 // Append the Panel to the click handler's argument list.
756 p.getEl().on('click', handlePanelClick.createDelegate(null, [p], true));
758 single: true // Remove the listener after first invocation
762 * <p>See also <tt>{@link #getEl getEl}</p>
767 * This Component's owner {@link Ext.Container Container} (defaults to undefined, and is set automatically when
768 * this Component is added to a Container). Read-only.
769 * <p><b>Note</b>: to access items within the Container see <tt>{@link #itemId}</tt>.</p>
770 * @type Ext.Container
774 * True if this component is hidden. Read-only.
779 * True if this component is disabled. Read-only.
784 * True if this component has been rendered. Read-only.
791 ctype : 'Ext.Component',
797 getActionEl : function(){
798 return this[this.actionMode];
801 initPlugin : function(p){
802 if(p.ptype && !Ext.isFunction(p.init)){
803 p = Ext.ComponentMgr.createPlugin(p);
804 }else if(Ext.isString(p)){
805 p = Ext.ComponentMgr.createPlugin({
814 * Function to be implemented by Component subclasses to be part of standard component initialization flow (it is empty by default).
816 // Traditional constructor:
817 Ext.Foo = function(config){
818 // call superclass constructor:
819 Ext.Foo.superclass.constructor.call(this, config);
825 Ext.extend(Ext.Foo, Ext.Bar, {
829 // initComponent replaces the constructor:
830 Ext.Foo = Ext.extend(Ext.Bar, {
831 initComponent : function(){
832 // call superclass initComponent
833 Ext.Container.superclass.initComponent.call(this);
842 initComponent : Ext.emptyFn,
845 * <p>Render this Component into the passed HTML element.</p>
846 * <p><b>If you are using a {@link Ext.Container Container} object to house this Component, then
847 * do not use the render method.</b></p>
848 * <p>A Container's child Components are rendered by that Container's
849 * {@link Ext.Container#layout layout} manager when the Container is first rendered.</p>
850 * <p>Certain layout managers allow dynamic addition of child components. Those that do
851 * include {@link Ext.layout.CardLayout}, {@link Ext.layout.AnchorLayout},
852 * {@link Ext.layout.FormLayout}, {@link Ext.layout.TableLayout}.</p>
853 * <p>If the Container is already rendered when a new child Component is added, you may need to call
854 * the Container's {@link Ext.Container#doLayout doLayout} to refresh the view which causes any
855 * unrendered child Components to be rendered. This is required so that you can add multiple
856 * child components if needed while only refreshing the layout once.</p>
857 * <p>When creating complex UIs, it is important to remember that sizing and positioning
858 * of child items is the responsibility of the Container's {@link Ext.Container#layout layout} manager.
859 * If you expect child items to be sized in response to user interactions, you must
860 * configure the Container with a layout manager which creates and manages the type of layout you
862 * <p><b>Omitting the Container's {@link Ext.Container#layout layout} config means that a basic
863 * layout manager is used which does nothing but render child components sequentially into the
864 * Container. No sizing or positioning will be performed in this situation.</b></p>
865 * @param {Element/HTMLElement/String} container (optional) The element this Component should be
866 * rendered into. If it is being created from existing markup, this should be omitted.
867 * @param {String/Number} position (optional) The element ID or DOM node index within the container <b>before</b>
868 * which this component will be inserted (defaults to appending to the end of the container)
870 render : function(container, position){
871 if(!this.rendered && this.fireEvent('beforerender', this) !== false){
872 if(!container && this.el){
873 this.el = Ext.get(this.el);
874 container = this.el.dom.parentNode;
875 this.allowDomMove = false;
877 this.container = Ext.get(container);
879 this.container.addClass(this.ctCls);
881 this.rendered = true;
882 if(position !== undefined){
883 if(Ext.isNumber(position)){
884 position = this.container.dom.childNodes[position];
886 position = Ext.getDom(position);
889 this.onRender(this.container, position || null);
891 this.el.removeClass(['x-hidden','x-hide-' + this.hideMode]);
894 this.el.addClass(this.cls);
898 this.el.applyStyles(this.style);
902 this.el.addClassOnOver(this.overCls);
904 this.fireEvent('render', this);
905 this.afterRender(this.container);
907 // call this so we don't fire initial hide events.
911 // pass silent so the event doesn't fire the first time.
915 if(this.stateful !== false){
916 this.initStateEvents();
919 this.fireEvent('afterrender', this);
924 initRef : function(){
927 * <p>A path specification, relative to the Component's {@link #ownerCt} specifying into which
928 * ancestor Container to place a named reference to this Component.</p>
929 * <p>The ancestor axis can be traversed by using '/' characters in the path.
930 * For example, to put a reference to a Toolbar Button into <i>the Panel which owns the Toolbar</i>:</p><pre><code>
931 var myGrid = new Ext.grid.EditorGridPanel({
932 title: 'My EditorGridPanel',
934 colModel: myColModel,
937 handler: saveChanges,
942 afteredit: function() {
943 // The button reference is in the GridPanel
944 myGrid.saveButton.enable();
949 * <p>In the code above, if the ref had been <code>'saveButton'</code> the reference would
950 * have been placed into the Toolbar. Each '/' in the ref moves up one level from the
951 * Component's {@link #ownerCt}.</p>
954 var levels = this.ref.split('/');
955 var last = levels.length, i = 0;
963 t[levels[--i]] = this;
968 initState : function(config){
969 if(Ext.state.Manager){
970 var id = this.getStateId();
972 var state = Ext.state.Manager.get(id);
974 if(this.fireEvent('beforestaterestore', this, state) !== false){
975 this.applyState(state);
976 this.fireEvent('staterestore', this, state);
984 getStateId : function(){
985 return this.stateId || ((this.id.indexOf('ext-comp-') == 0 || this.id.indexOf('ext-gen') == 0) ? null : this.id);
989 initStateEvents : function(){
990 if(this.stateEvents){
991 for(var i = 0, e; e = this.stateEvents[i]; i++){
992 this.on(e, this.saveState, this, {delay:100});
998 applyState : function(state){
1000 Ext.apply(this, state);
1005 getState : function(){
1010 saveState : function(){
1011 if(Ext.state.Manager && this.stateful !== false){
1012 var id = this.getStateId();
1014 var state = this.getState();
1015 if(this.fireEvent('beforestatesave', this, state) !== false){
1016 Ext.state.Manager.set(id, state);
1017 this.fireEvent('statesave', this, state);
1024 * Apply this component to existing markup that is valid. With this function, no call to render() is required.
1025 * @param {String/HTMLElement} el
1027 applyToMarkup : function(el){
1028 this.allowDomMove = false;
1029 this.el = Ext.get(el);
1030 this.render(this.el.dom.parentNode);
1034 * Adds a CSS class to the component's underlying element.
1035 * @param {string} cls The CSS class name to add
1036 * @return {Ext.Component} this
1038 addClass : function(cls){
1040 this.el.addClass(cls);
1042 this.cls = this.cls ? this.cls + ' ' + cls : cls;
1048 * Removes a CSS class from the component's underlying element.
1049 * @param {string} cls The CSS class name to remove
1050 * @return {Ext.Component} this
1052 removeClass : function(cls){
1054 this.el.removeClass(cls);
1056 this.cls = this.cls.split(' ').remove(cls).join(' ');
1062 // default function is not really useful
1063 onRender : function(ct, position){
1064 if(!this.el && this.autoEl){
1065 if(Ext.isString(this.autoEl)){
1066 this.el = document.createElement(this.autoEl);
1068 var div = document.createElement('div');
1069 Ext.DomHelper.overwrite(div, this.autoEl);
1070 this.el = div.firstChild;
1073 this.el.id = this.getId();
1077 this.el = Ext.get(this.el);
1078 if(this.allowDomMove !== false){
1079 ct.dom.insertBefore(this.el.dom, position);
1085 getAutoCreate : function(){
1086 var cfg = Ext.isObject(this.autoCreate) ?
1087 this.autoCreate : Ext.apply({}, this.defaultAutoCreate);
1088 if(this.id && !cfg.id){
1095 afterRender : Ext.emptyFn,
1098 * Destroys this component by purging any event listeners, removing the component's element from the DOM,
1099 * removing the component from its {@link Ext.Container} (if applicable) and unregistering it from
1100 * {@link Ext.ComponentMgr}. Destruction is generally handled automatically by the framework and this method
1101 * should usually not need to be called directly.
1104 destroy : function(){
1105 if(!this.isDestroyed){
1106 if(this.fireEvent('beforedestroy', this) !== false){
1107 this.beforeDestroy();
1109 this.el.removeAllListeners();
1111 if(this.actionMode == 'container' || this.removeMode == 'container'){
1112 this.container.remove();
1116 Ext.ComponentMgr.unregister(this);
1117 this.fireEvent('destroy', this);
1118 this.purgeListeners();
1119 this.isDestroyed = true;
1125 beforeDestroy : Ext.emptyFn,
1128 onDestroy : Ext.emptyFn,
1131 * <p>Returns the {@link Ext.Element} which encapsulates this Component.</p>
1132 * <p>This will <i>usually</i> be a <DIV> element created by the class's onRender method, but
1133 * that may be overridden using the {@link #autoEl} config.</p>
1134 * <br><p><b>Note</b>: this element will not be available until this Component has been rendered.</p><br>
1135 * <p>To add listeners for <b>DOM events</b> to this Component (as opposed to listeners
1136 * for this Component's own Observable events), see the {@link #listeners} config for a suggestion,
1137 * or use a render listener directly:</p><pre><code>
1139 title: 'The Clickable Panel',
1141 render: function(p) {
1142 // Append the Panel to the click handler's argument list.
1143 p.getEl().on('click', handlePanelClick.createDelegate(null, [p], true));
1145 single: true // Remove the listener after first invocation
1149 * @return {Ext.Element} The Element which encapsulates this Component.
1156 * Returns the <code>id</code> of this component or automatically generates and
1157 * returns an <code>id</code> if an <code>id</code> is not defined yet:<pre><code>
1158 * 'ext-comp-' + (++Ext.Component.AUTO_ID)
1160 * @return {String} id
1163 return this.id || (this.id = 'ext-comp-' + (++Ext.Component.AUTO_ID));
1167 * Returns the <code>{@link #itemId}</code> of this component. If an
1168 * <code>{@link #itemId}</code> was not assigned through configuration the
1169 * <code>id</code> is returned using <code>{@link #getId}</code>.
1172 getItemId : function(){
1173 return this.itemId || this.getId();
1177 * Try to focus this component.
1178 * @param {Boolean} selectText (optional) If applicable, true to also select the text in this component
1179 * @param {Boolean/Number} delay (optional) Delay the focus this number of milliseconds (true for 10 milliseconds)
1180 * @return {Ext.Component} this
1182 focus : function(selectText, delay){
1184 this.focus.defer(Ext.isNumber(delay) ? delay : 10, this, [selectText, false]);
1189 if(selectText === true){
1190 this.el.dom.select();
1205 * Disable this component and fire the 'disable' event.
1206 * @return {Ext.Component} this
1208 disable : function(/* private */ silent){
1212 this.disabled = true;
1213 if(silent !== true){
1214 this.fireEvent('disable', this);
1220 onDisable : function(){
1221 this.getActionEl().addClass(this.disabledClass);
1222 this.el.dom.disabled = true;
1226 * Enable this component and fire the 'enable' event.
1227 * @return {Ext.Component} this
1229 enable : function(){
1233 this.disabled = false;
1234 this.fireEvent('enable', this);
1239 onEnable : function(){
1240 this.getActionEl().removeClass(this.disabledClass);
1241 this.el.dom.disabled = false;
1245 * Convenience function for setting disabled/enabled by boolean.
1246 * @param {Boolean} disabled
1247 * @return {Ext.Component} this
1249 setDisabled : function(disabled){
1250 return this[disabled ? 'disable' : 'enable']();
1254 * Show this component. Listen to the '{@link #beforeshow}' event and return
1255 * <tt>false</tt> to cancel showing the component. Fires the '{@link #show}'
1256 * event after showing the component.
1257 * @return {Ext.Component} this
1260 if(this.fireEvent('beforeshow', this) !== false){
1261 this.hidden = false;
1262 if(this.autoRender){
1263 this.render(Ext.isBoolean(this.autoRender) ? Ext.getBody() : this.autoRender);
1268 this.fireEvent('show', this);
1274 onShow : function(){
1275 this.getVisibilityEl().removeClass('x-hide-' + this.hideMode);
1279 * Hide this component. Listen to the '{@link #beforehide}' event and return
1280 * <tt>false</tt> to cancel hiding the component. Fires the '{@link #hide}'
1281 * event after hiding the component. Note this method is called internally if
1282 * the component is configured to be <code>{@link #hidden}</code>.
1283 * @return {Ext.Component} this
1286 if(this.fireEvent('beforehide', this) !== false){
1288 this.fireEvent('hide', this);
1302 onHide : function(){
1303 this.getVisibilityEl().addClass('x-hide-' + this.hideMode);
1307 getVisibilityEl : function(){
1308 return this.hideParent ? this.container : this.getActionEl();
1312 * Convenience function to hide or show this component by boolean.
1313 * @param {Boolean} visible True to show, false to hide
1314 * @return {Ext.Component} this
1316 setVisible : function(visible){
1317 return this[visible ? 'show' : 'hide']();
1321 * Returns true if this component is visible.
1322 * @return {Boolean} True if this component is visible, false otherwise.
1324 isVisible : function(){
1325 return this.rendered && this.getVisibilityEl().isVisible();
1329 * Clone the current component using the original config values passed into this instance by default.
1330 * @param {Object} overrides A new config containing any properties to override in the cloned version.
1331 * An id property can be passed on this object, otherwise one will be generated to avoid duplicates.
1332 * @return {Ext.Component} clone The cloned copy of this component
1334 cloneConfig : function(overrides){
1335 overrides = overrides || {};
1336 var id = overrides.id || Ext.id();
1337 var cfg = Ext.applyIf(overrides, this.initialConfig);
1338 cfg.id = id; // prevent dup id
1339 return new this.constructor(cfg);
1343 * Gets the xtype for this component as registered with {@link Ext.ComponentMgr}. For a list of all
1344 * available xtypes, see the {@link Ext.Component} header. Example usage:
1346 var t = new Ext.form.TextField();
1347 alert(t.getXType()); // alerts 'textfield'
1349 * @return {String} The xtype
1351 getXType : function(){
1352 return this.constructor.xtype;
1356 * <p>Tests whether or not this Component is of a specific xtype. This can test whether this Component is descended
1357 * from the xtype (default) or whether it is directly of the xtype specified (shallow = true).</p>
1358 * <p><b>If using your own subclasses, be aware that a Component must register its own xtype
1359 * to participate in determination of inherited xtypes.</b></p>
1360 * <p>For a list of all available xtypes, see the {@link Ext.Component} header.</p>
1361 * <p>Example usage:</p>
1363 var t = new Ext.form.TextField();
1364 var isText = t.isXType('textfield'); // true
1365 var isBoxSubclass = t.isXType('box'); // true, descended from BoxComponent
1366 var isBoxInstance = t.isXType('box', true); // false, not a direct BoxComponent instance
1368 * @param {String} xtype The xtype to check for this Component
1369 * @param {Boolean} shallow (optional) False to check whether this Component is descended from the xtype (this is
1370 * the default), or true to check whether this Component is directly of the specified xtype.
1371 * @return {Boolean} True if this component descends from the specified xtype, false otherwise.
1373 isXType : function(xtype, shallow){
1374 //assume a string by default
1375 if (Ext.isFunction(xtype)){
1376 xtype = xtype.xtype; //handle being passed the class, e.g. Ext.Component
1377 }else if (Ext.isObject(xtype)){
1378 xtype = xtype.constructor.xtype; //handle being passed an instance
1381 return !shallow ? ('/' + this.getXTypes() + '/').indexOf('/' + xtype + '/') != -1 : this.constructor.xtype == xtype;
1385 * <p>Returns this Component's xtype hierarchy as a slash-delimited string. For a list of all
1386 * available xtypes, see the {@link Ext.Component} header.</p>
1387 * <p><b>If using your own subclasses, be aware that a Component must register its own xtype
1388 * to participate in determination of inherited xtypes.</b></p>
1389 * <p>Example usage:</p>
1391 var t = new Ext.form.TextField();
1392 alert(t.getXTypes()); // alerts 'component/box/field/textfield'
1394 * @return {String} The xtype hierarchy string
1396 getXTypes : function(){
1397 var tc = this.constructor;
1399 var c = [], sc = this;
1400 while(sc && sc.constructor.xtype){
1401 c.unshift(sc.constructor.xtype);
1402 sc = sc.constructor.superclass;
1405 tc.xtypes = c.join('/');
1411 * Find a container above this component at any level by a custom function. If the passed function returns
1412 * true, the container will be returned.
1413 * @param {Function} fn The custom function to call with the arguments (container, this component).
1414 * @return {Ext.Container} The first Container for which the custom function returns true
1416 findParentBy : function(fn) {
1417 for (var p = this.ownerCt; (p != null) && !fn(p, this); p = p.ownerCt);
1422 * Find a container above this component at any level by xtype or class
1423 * @param {String/Class} xtype The xtype string for a component, or the class of the component directly
1424 * @return {Ext.Container} The first Container which matches the given xtype or class
1426 findParentByType : function(xtype) {
1427 return Ext.isFunction(xtype) ?
1428 this.findParentBy(function(p){
1429 return p.constructor === xtype;
1431 this.findParentBy(function(p){
1432 return p.constructor.xtype === xtype;
1436 getDomPositionEl : function(){
1437 return this.getPositionEl ? this.getPositionEl() : this.getEl();
1441 purgeListeners : function(){
1442 Ext.Component.superclass.purgeListeners.call(this);
1444 this.on('beforedestroy', this.clearMons, this, {single: true});
1449 clearMons : function(){
1450 Ext.each(this.mons, function(m){
1451 m.item.un(m.ename, m.fn, m.scope);
1457 createMons: function(){
1460 this.on('beforedestroy', this.clearMons, this, {single: true});
1464 // internal function for auto removal of assigned event handlers on destruction
1465 mon : function(item, ename, fn, scope, opt){
1467 if(Ext.isObject(ename)){
1468 var propRe = /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/;
1475 if(Ext.isFunction(o[e])){
1478 item: item, ename: e, fn: o[e], scope: o.scope
1480 item.on(e, o[e], o.scope, o);
1482 // individual options
1484 item: item, ename: e, fn: o[e], scope: o.scope
1493 item: item, ename: ename, fn: fn, scope: scope
1495 item.on(ename, fn, scope, opt);
1498 // protected, opposite of mon
1499 mun : function(item, ename, fn, scope){
1502 for(var i = 0, len = this.mons.length; i < len; ++i){
1504 if(item === mon.item && ename == mon.ename && fn === mon.fn && scope === mon.scope){
1505 this.mons.splice(i, 1);
1506 item.un(ename, fn, scope);
1515 * Returns the next component in the owning container
1516 * @return Ext.Component
1518 nextSibling : function(){
1520 var index = this.ownerCt.items.indexOf(this);
1521 if(index != -1 && index+1 < this.ownerCt.items.getCount()){
1522 return this.ownerCt.items.itemAt(index+1);
1529 * Returns the previous component in the owning container
1530 * @return Ext.Component
1532 previousSibling : function(){
1534 var index = this.ownerCt.items.indexOf(this);
1536 return this.ownerCt.items.itemAt(index-1);
1543 * Provides the link for Observable's fireEvent method to bubble up the ownership hierarchy.
1544 * @return {Ext.Container} the Container which owns this Component.
1546 getBubbleTarget : function(){
1547 return this.ownerCt;
1551 Ext.reg('component', Ext.Component);