Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / ComponentManager.js
1 /**
2  * @class Ext.ComponentManager
3  * @extends Ext.AbstractManager
4  * <p>Provides a registry of all Components (instances of {@link Ext.Component} or any subclass
5  * thereof) on a page so that they can be easily accessed by {@link Ext.Component component}
6  * {@link Ext.Component#id id} (see {@link #get}, or the convenience method {@link Ext#getCmp Ext.getCmp}).</p>
7  * <p>This object also provides a registry of available Component <i>classes</i>
8  * indexed by a mnemonic code known as the Component's {@link Ext.Component#xtype xtype}.
9  * The <code>xtype</code> provides a way to avoid instantiating child Components
10  * when creating a full, nested config object for a complete Ext page.</p>
11  * <p>A child Component may be specified simply as a <i>config object</i>
12  * as long as the correct <code>{@link Ext.Component#xtype xtype}</code> is specified so that if and when the Component
13  * needs rendering, the correct type can be looked up for lazy instantiation.</p>
14  * <p>For a list of all available <code>{@link Ext.Component#xtype xtypes}</code>, see {@link Ext.Component}.</p>
15  * @singleton
16  */
17 Ext.define('Ext.ComponentManager', {
18     extend: 'Ext.AbstractManager',
19     alternateClassName: 'Ext.ComponentMgr',
20     
21     singleton: true,
22     
23     typeName: 'xtype',
24     
25     /**
26      * Creates a new Component from the specified config object using the
27      * config object's xtype to determine the class to instantiate.
28      * @param {Object} config A configuration object for the Component you wish to create.
29      * @param {Constructor} defaultType The constructor to provide the default Component type if
30      * the config object does not contain a <code>xtype</code>. (Optional if the config contains a <code>xtype</code>).
31      * @return {Ext.Component} The newly instantiated Component.
32      */
33     create: function(component, defaultType){
34         if (component instanceof Ext.AbstractComponent) {
35             return component;
36         }
37         else if (Ext.isString(component)) {
38             return Ext.createByAlias('widget.' + component);
39         }
40         else {
41             var type = component.xtype || defaultType,
42                 config = component;
43             
44             return Ext.createByAlias('widget.' + type, config);
45         }
46     },
47
48     registerType: function(type, cls) {
49         this.types[type] = cls;
50         cls[this.typeName] = type;
51         cls.prototype[this.typeName] = type;
52     }
53 });