Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / ComponentMgr.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.ComponentMgr
9  * <p>Provides a registry of all Components (instances of {@link Ext.Component} or any subclass
10  * thereof) on a page so that they can be easily accessed by {@link Ext.Component component}
11  * {@link Ext.Component#id id} (see {@link #get}, or the convenience method {@link Ext#getCmp Ext.getCmp}).</p>
12  * <p>This object also provides a registry of available Component <i>classes</i>
13  * indexed by a mnemonic code known as the Component's {@link Ext.Component#xtype xtype}.
14  * The <code>{@link Ext.Component#xtype xtype}</code> provides a way to avoid instantiating child Components
15  * when creating a full, nested config object for a complete Ext page.</p>
16  * <p>A child Component may be specified simply as a <i>config object</i>
17  * as long as the correct <code>{@link Ext.Component#xtype xtype}</code> is specified so that if and when the Component
18  * needs rendering, the correct type can be looked up for lazy instantiation.</p>
19  * <p>For a list of all available <code>{@link Ext.Component#xtype xtypes}</code>, see {@link Ext.Component}.</p>
20  * @singleton
21  */
22 Ext.ComponentMgr = function(){
23     var all = new Ext.util.MixedCollection();
24     var types = {};
25     var ptypes = {};
26
27     return {
28         /**
29          * Registers a component.
30          * @param {Ext.Component} c The component
31          */
32         register : function(c){
33             all.add(c);
34         },
35
36         /**
37          * Unregisters a component.
38          * @param {Ext.Component} c The component
39          */
40         unregister : function(c){
41             all.remove(c);
42         },
43
44         /**
45          * Returns a component by {@link Ext.Component#id id}.
46          * For additional details see {@link Ext.util.MixedCollection#get}.
47          * @param {String} id The component {@link Ext.Component#id id}
48          * @return Ext.Component The Component, <code>undefined</code> if not found, or <code>null</code> if a
49          * Class was found.
50          */
51         get : function(id){
52             return all.get(id);
53         },
54
55         /**
56          * Registers a function that will be called when a specified component is added to ComponentMgr
57          * @param {String} id The component {@link Ext.Component#id id}
58          * @param {Function} fn The callback function
59          * @param {Object} scope The scope of the callback
60          */
61         onAvailable : function(id, fn, scope){
62             all.on("add", function(index, o){
63                 if(o.id == id){
64                     fn.call(scope || o, o);
65                     all.un("add", fn, scope);
66                 }
67             });
68         },
69
70         /**
71          * The MixedCollection used internally for the component cache. An example usage may be subscribing to
72          * events on the MixedCollection to monitor addition or removal.  Read-only.
73          * @type {MixedCollection}
74          */
75         all : all,
76         
77         /**
78          * Checks if a Component type is registered.
79          * @param {Ext.Component} xtype The mnemonic string by which the Component class may be looked up
80          * @return {Boolean} Whether the type is registered.
81          */
82         isRegistered : function(xtype){
83             return types[xtype] !== undefined;    
84         },
85
86         /**
87          * <p>Registers a new Component constructor, keyed by a new
88          * {@link Ext.Component#xtype}.</p>
89          * <p>Use this method (or its alias {@link Ext#reg Ext.reg}) to register new
90          * subclasses of {@link Ext.Component} so that lazy instantiation may be used when specifying
91          * child Components.
92          * see {@link Ext.Container#items}</p>
93          * @param {String} xtype The mnemonic string by which the Component class may be looked up.
94          * @param {Constructor} cls The new Component class.
95          */
96         registerType : function(xtype, cls){
97             types[xtype] = cls;
98             cls.xtype = xtype;
99         },
100
101         /**
102          * Creates a new Component from the specified config object using the
103          * config object's {@link Ext.component#xtype xtype} to determine the class to instantiate.
104          * @param {Object} config A configuration object for the Component you wish to create.
105          * @param {Constructor} defaultType The constructor to provide the default Component type if
106          * the config object does not contain a <code>xtype</code>. (Optional if the config contains a <code>xtype</code>).
107          * @return {Ext.Component} The newly instantiated Component.
108          */
109         create : function(config, defaultType){
110             return config.render ? config : new types[config.xtype || defaultType](config);
111         },
112
113         /**
114          * <p>Registers a new Plugin constructor, keyed by a new
115          * {@link Ext.Component#ptype}.</p>
116          * <p>Use this method (or its alias {@link Ext#preg Ext.preg}) to register new
117          * plugins for {@link Ext.Component}s so that lazy instantiation may be used when specifying
118          * Plugins.</p>
119          * @param {String} ptype The mnemonic string by which the Plugin class may be looked up.
120          * @param {Constructor} cls The new Plugin class.
121          */
122         registerPlugin : function(ptype, cls){
123             ptypes[ptype] = cls;
124             cls.ptype = ptype;
125         },
126
127         /**
128          * Creates a new Plugin from the specified config object using the
129          * config object's {@link Ext.component#ptype ptype} to determine the class to instantiate.
130          * @param {Object} config A configuration object for the Plugin you wish to create.
131          * @param {Constructor} defaultType The constructor to provide the default Plugin type if
132          * the config object does not contain a <code>ptype</code>. (Optional if the config contains a <code>ptype</code>).
133          * @return {Ext.Component} The newly instantiated Plugin.
134          */
135         createPlugin : function(config, defaultType){
136             return new ptypes[config.ptype || defaultType](config);
137         }
138     };
139 }();
140
141 /**
142  * Shorthand for {@link Ext.ComponentMgr#registerType}
143  * @param {String} xtype The {@link Ext.component#xtype mnemonic string} by which the Component class
144  * may be looked up.
145  * @param {Constructor} cls The new Component class.
146  * @member Ext
147  * @method reg
148  */
149 Ext.reg = Ext.ComponentMgr.registerType; // this will be called a lot internally, shorthand to keep the bytes down
150 /**
151  * Shorthand for {@link Ext.ComponentMgr#registerPlugin}
152  * @param {String} ptype The {@link Ext.component#ptype mnemonic string} by which the Plugin class
153  * may be looked up.
154  * @param {Constructor} cls The new Plugin class.
155  * @member Ext
156  * @method preg
157  */
158 Ext.preg = Ext.ComponentMgr.registerPlugin;
159 /**
160  * Shorthand for {@link Ext.ComponentMgr#create}
161  * Creates a new Component from the specified config object using the
162  * config object's {@link Ext.component#xtype xtype} to determine the class to instantiate.
163  * @param {Object} config A configuration object for the Component you wish to create.
164  * @param {Constructor} defaultType The constructor to provide the default Component type if
165  * the config object does not contain a <code>xtype</code>. (Optional if the config contains a <code>xtype</code>).
166  * @return {Ext.Component} The newly instantiated Component.
167  * @member Ext
168  * @method create
169  */
170 Ext.create = Ext.ComponentMgr.create;