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