Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / ComponentManager.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.ComponentManager
17  * @extends Ext.AbstractManager
18  * <p>Provides a registry of all Components (instances of {@link Ext.Component} or any subclass
19  * thereof) on a page so that they can be easily accessed by {@link Ext.Component component}
20  * {@link Ext.Component#id id} (see {@link #get}, or the convenience method {@link Ext#getCmp Ext.getCmp}).</p>
21  * <p>This object also provides a registry of available Component <i>classes</i>
22  * indexed by a mnemonic code known as the Component's {@link Ext.Component#xtype xtype}.
23  * The <code>xtype</code> provides a way to avoid instantiating child Components
24  * when creating a full, nested config object for a complete Ext page.</p>
25  * <p>A child Component may be specified simply as a <i>config object</i>
26  * as long as the correct <code>{@link Ext.Component#xtype xtype}</code> is specified so that if and when the Component
27  * needs rendering, the correct type can be looked up for lazy instantiation.</p>
28  * <p>For a list of all available <code>{@link Ext.Component#xtype xtypes}</code>, see {@link Ext.Component}.</p>
29  * @singleton
30  */
31 Ext.define('Ext.ComponentManager', {
32     extend: 'Ext.AbstractManager',
33     alternateClassName: 'Ext.ComponentMgr',
34     
35     singleton: true,
36     
37     typeName: 'xtype',
38     
39     /**
40      * Creates a new Component from the specified config object using the
41      * config object's xtype to determine the class to instantiate.
42      * @param {Object} config A configuration object for the Component you wish to create.
43      * @param {Function} defaultType (optional) The constructor to provide the default Component type if
44      * the config object does not contain a <code>xtype</code>. (Optional if the config contains a <code>xtype</code>).
45      * @return {Ext.Component} The newly instantiated Component.
46      */
47     create: function(component, defaultType){
48         if (component instanceof Ext.AbstractComponent) {
49             return component;
50         }
51         else if (Ext.isString(component)) {
52             return Ext.createByAlias('widget.' + component);
53         }
54         else {
55             var type = component.xtype || defaultType,
56                 config = component;
57             
58             return Ext.createByAlias('widget.' + type, config);
59         }
60     },
61
62     registerType: function(type, cls) {
63         this.types[type] = cls;
64         cls[this.typeName] = type;
65         cls.prototype[this.typeName] = type;
66     }
67 });