Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / src / util / AbstractManager.js
1 /*!
2  * Ext JS Library 3.3.1
3  * Copyright(c) 2006-2010 Sencha Inc.
4  * licensing@sencha.com
5  * http://www.sencha.com/license
6  */
7 /**
8  * @class Ext.AbstractManager
9  * @extends Object
10  * Base Manager class - extended by ComponentMgr and PluginMgr
11  */
12 Ext.AbstractManager = Ext.extend(Object, {
13     typeName: 'type',
14     
15     constructor: function(config) {
16         Ext.apply(this, config || {});
17         
18         /**
19          * Contains all of the items currently managed
20          * @property all
21          * @type Ext.util.MixedCollection
22          */
23         this.all = new Ext.util.MixedCollection();
24         
25         this.types = {};
26     },
27     
28     /**
29      * Returns a component by {@link Ext.Component#id id}.
30      * For additional details see {@link Ext.util.MixedCollection#get}.
31      * @param {String} id The component {@link Ext.Component#id id}
32      * @return Ext.Component The Component, <code>undefined</code> if not found, or <code>null</code> if a
33      * Class was found.
34      */
35     get : function(id){
36         return this.all.get(id);
37     },
38     
39     /**
40      * Registers an item to be managed
41      * @param {Mixed} item The item to register
42      */
43     register: function(item) {
44         this.all.add(item);
45     },
46     
47     /**
48      * Unregisters a component by removing it from this manager
49      * @param {Mixed} item The item to unregister
50      */
51     unregister: function(item) {
52         this.all.remove(item);        
53     },
54     
55     /**
56      * <p>Registers a new Component constructor, keyed by a new
57      * {@link Ext.Component#xtype}.</p>
58      * <p>Use this method (or its alias {@link Ext#reg Ext.reg}) to register new
59      * subclasses of {@link Ext.Component} so that lazy instantiation may be used when specifying
60      * child Components.
61      * see {@link Ext.Container#items}</p>
62      * @param {String} xtype The mnemonic string by which the Component class may be looked up.
63      * @param {Constructor} cls The new Component class.
64      */
65     registerType : function(type, cls){
66         this.types[type] = cls;
67         cls[this.typeName] = type;
68     },
69     
70     /**
71      * Checks if a Component type is registered.
72      * @param {Ext.Component} xtype The mnemonic string by which the Component class may be looked up
73      * @return {Boolean} Whether the type is registered.
74      */
75     isRegistered : function(type){
76         return this.types[type] !== undefined;    
77     },
78     
79     /**
80      * Creates and returns an instance of whatever this manager manages, based on the supplied type and config object
81      * @param {Object} config The config object
82      * @param {String} defaultType If no type is discovered in the config object, we fall back to this type
83      * @return {Mixed} The instance of whatever this manager is managing
84      */
85     create: function(config, defaultType) {
86         var type        = config[this.typeName] || config.type || defaultType,
87             Constructor = this.types[type];
88         
89         if (Constructor == undefined) {
90             throw new Error(String.format("The '{0}' type has not been registered with this manager", type));
91         }
92         
93         return new Constructor(config);
94     },
95     
96     /**
97      * Registers a function that will be called when a Component with the specified id is added to the manager. This will happen on instantiation.
98      * @param {String} id The component {@link Ext.Component#id id}
99      * @param {Function} fn The callback function
100      * @param {Object} scope The scope (<code>this</code> reference) in which the callback is executed. Defaults to the Component.
101      */
102     onAvailable : function(id, fn, scope){
103         var all = this.all;
104         
105         all.on("add", function(index, o){
106             if (o.id == id) {
107                 fn.call(scope || o, o);
108                 all.un("add", fn, scope);
109             }
110         });
111     }
112 });