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