commit extjs-2.2.1
[extjs.git] / source / widgets / ComponentMgr.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.ComponentMgr\r
11  * <p>Provides a registry of all Components (instances of {@link Ext.Component} or any subclass\r
12  * thereof) on a page so that they can be easily accessed by component id (see {@link #get}, or\r
13  * the convenience method {@link Ext#getCmp Ext.getCmp}).</p>\r
14  * <p>This object also provides a registry of available Component <i>classes</i>\r
15  * indexed by a mnemonic code known as the Component's {@link Ext.Component#xtype xtype}.\r
16  * The <tt>xtype</tt> provides a way to avoid instantiating child Components\r
17  * when creating a full, nested config object for a complete Ext page.</p>\r
18  * <p>A child Component may be specified simply as a <i>config object</i>\r
19  * as long as the correct xtype is specified so that if and when the Component\r
20  * needs rendering, the correct type can be looked up for lazy instantiation.</p>\r
21  * <p>For a list of all available xtypes, see {@link Ext.Component}.</p>\r
22  * @singleton\r
23  */\r
24 Ext.ComponentMgr = function(){\r
25     var all = new Ext.util.MixedCollection();\r
26     var types = {};\r
27 \r
28     return {\r
29         /**\r
30          * Registers a component.\r
31          * @param {Ext.Component} c The component\r
32          */\r
33         register : function(c){\r
34             all.add(c);\r
35         },\r
36 \r
37         /**\r
38          * Unregisters a component.\r
39          * @param {Ext.Component} c The component\r
40          */\r
41         unregister : function(c){\r
42             all.remove(c);\r
43         },\r
44 \r
45         /**\r
46          * Returns a component by id\r
47          * @param {String} id The component id\r
48          * @return Ext.Component\r
49          */\r
50         get : function(id){\r
51             return all.get(id);\r
52         },\r
53 \r
54         /**\r
55          * Registers a function that will be called when a specified component is added to ComponentMgr\r
56          * @param {String} id The component id\r
57          * @param {Function} fn The callback function\r
58          * @param {Object} scope The scope of the callback\r
59          */\r
60         onAvailable : function(id, fn, scope){\r
61             all.on("add", function(index, o){\r
62                 if(o.id == id){\r
63                     fn.call(scope || o, o);\r
64                     all.un("add", fn, scope);\r
65                 }\r
66             });\r
67         },\r
68 \r
69         /**\r
70          * The MixedCollection used internally for the component cache. An example usage may be subscribing to\r
71          * events on the MixedCollection to monitor addition or removal.  Read-only.\r
72          * @type {MixedCollection}\r
73          */\r
74         all : all,\r
75 \r
76         /**\r
77          * <p>Registers a new Component constructor, keyed by a new\r
78          * {@link Ext.Component#xtype}.</p>\r
79          * <p>Use this method to register new subclasses of {@link Ext.Component} so\r
80          * that lazy instantiation may be used when specifying child Components.\r
81          * see {@link Ext.Container#items}</p>\r
82          * @param {String} xtype The mnemonic string by which the Component class\r
83          * may be looked up.\r
84          * @param {Constructor} cls The new Component class.\r
85          */\r
86         registerType : function(xtype, cls){\r
87             types[xtype] = cls;\r
88             cls.xtype = xtype;\r
89         },\r
90 \r
91         /**\r
92          * Creates a new Component from the specified config object using the\r
93          * config object's {@link Ext.component#xtype xtype} to determine the class to instantiate.\r
94          * @param config {Object} A configuration object for the Component you wish to create.\r
95          * @param defaultType {Constructor} The constructor to provide the default Component type if\r
96          * the config object does not contain an xtype. (Optional if the config contains an xtype).\r
97          * @return {Ext.Component} The newly instantiated Component.\r
98          */\r
99         create : function(config, defaultType){\r
100             return new types[config.xtype || defaultType](config);\r
101         }\r
102     };\r
103 }();\r
104 \r
105 /**\r
106  * Shorthand for {@link Ext.ComponentMgr#registerType}\r
107  * @param {String} xtype The mnemonic string by which the Component class\r
108  * may be looked up.\r
109  * @param {Constructor} cls The new Component class.\r
110  * @member Ext\r
111  * @method reg\r
112  */\r
113 Ext.reg = Ext.ComponentMgr.registerType; // this will be called a lot internally, shorthand to keep the bytes down