Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / AbstractManager.js
1 /**
2  * @class Ext.AbstractManager
3  * @extends Object
4  * @ignore
5  * Base Manager class
6  */
7
8 Ext.define('Ext.AbstractManager', {
9
10     /* Begin Definitions */
11
12     requires: ['Ext.util.HashMap'],
13
14     /* End Definitions */
15
16     typeName: 'type',
17
18     constructor: function(config) {
19         Ext.apply(this, config || {});
20
21         /**
22          * Contains all of the items currently managed
23          * @property all
24          * @type Ext.util.MixedCollection
25          */
26         this.all = Ext.create('Ext.util.HashMap');
27
28         this.types = {};
29     },
30
31     /**
32      * Returns an item by id.
33      * For additional details see {@link Ext.util.HashMap#get}.
34      * @param {String} id The id of the item
35      * @return {Mixed} The item, <code>undefined</code> if not found.
36      */
37     get : function(id) {
38         return this.all.get(id);
39     },
40
41     /**
42      * Registers an item to be managed
43      * @param {Mixed} item The item to register
44      */
45     register: function(item) {
46         this.all.add(item);
47     },
48
49     /**
50      * Unregisters an item by removing it from this manager
51      * @param {Mixed} item The item to unregister
52      */
53     unregister: function(item) {
54         this.all.remove(item);
55     },
56
57     /**
58      * <p>Registers a new item constructor, keyed by a type key.
59      * @param {String} type The mnemonic string by which the class may be looked up.
60      * @param {Constructor} cls The new instance class.
61      */
62     registerType : function(type, cls) {
63         this.types[type] = cls;
64         cls[this.typeName] = type;
65     },
66
67     /**
68      * Checks if an item type is registered.
69      * @param {String} type The mnemonic string by which the class may be looked up
70      * @return {Boolean} Whether the type is registered.
71      */
72     isRegistered : function(type){
73         return this.types[type] !== undefined;
74     },
75
76     /**
77      * Creates and returns an instance of whatever this manager manages, based on the supplied type and config object
78      * @param {Object} config The config object
79      * @param {String} defaultType If no type is discovered in the config object, we fall back to this type
80      * @return {Mixed} The instance of whatever this manager is managing
81      */
82     create: function(config, defaultType) {
83         var type        = config[this.typeName] || config.type || defaultType,
84             Constructor = this.types[type];
85
86         //<debug>
87         if (Constructor == undefined) {
88             Ext.Error.raise("The '" + type + "' type has not been registered with this manager");
89         }
90         //</debug>
91
92         return new Constructor(config);
93     },
94
95     /**
96      * Registers a function that will be called when an item with the specified id is added to the manager. This will happen on instantiation.
97      * @param {String} id The item id
98      * @param {Function} fn The callback function. Called with a single parameter, the item.
99      * @param {Object} scope The scope (<code>this</code> reference) in which the callback is executed. Defaults to the item.
100      */
101     onAvailable : function(id, fn, scope){
102         var all = this.all,
103             item;
104         
105         if (all.containsKey(id)) {
106             item = all.get(id);
107             fn.call(scope || item, item);
108         } else {
109             all.on('add', function(map, key, item){
110                 if (key == id) {
111                     fn.call(scope || item, item);
112                     all.un('add', fn, scope);
113                 }
114             });
115         }
116     },
117     
118     /**
119      * Executes the specified function once for each item in the collection.
120      * Returning false from the function will cease iteration.
121      * 
122      * The paramaters passed to the function are:
123      * <div class="mdetail-params"><ul>
124      * <li><b>key</b> : String<p class="sub-desc">The key of the item</p></li>
125      * <li><b>value</b> : Number<p class="sub-desc">The value of the item</p></li>
126      * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the collection</p></li>
127      * </ul></div>
128      * @param {Object} fn The function to execute.
129      * @param {Object} scope The scope to execute in. Defaults to <tt>this</tt>.
130      */
131     each: function(fn, scope){
132         this.all.each(fn, scope || this);    
133     },
134     
135     /**
136      * Gets the number of items in the collection.
137      * @return {Number} The number of items in the collection.
138      */
139     getCount: function(){
140         return this.all.getCount();
141     }
142 });