3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.AbstractManager
20 Ext.define('Ext.AbstractManager', {
22 /* Begin Definitions */
24 requires: ['Ext.util.HashMap'],
30 constructor: function(config) {
31 Ext.apply(this, config || {});
34 * Contains all of the items currently managed
36 * @type Ext.util.MixedCollection
38 this.all = Ext.create('Ext.util.HashMap');
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.
50 return this.all.get(id);
54 * Registers an item to be managed
55 * @param {Mixed} item The item to register
57 register: function(item) {
62 * Unregisters an item by removing it from this manager
63 * @param {Mixed} item The item to unregister
65 unregister: function(item) {
66 this.all.remove(item);
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.
74 registerType : function(type, cls) {
75 this.types[type] = cls;
76 cls[this.typeName] = type;
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.
84 isRegistered : function(type){
85 return this.types[type] !== undefined;
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
94 create: function(config, defaultType) {
95 var type = config[this.typeName] || config.type || defaultType,
96 Constructor = this.types[type];
99 if (Constructor == undefined) {
100 Ext.Error.raise("The '" + type + "' type has not been registered with this manager");
104 return new Constructor(config);
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.
113 onAvailable : function(id, fn, scope){
117 if (all.containsKey(id)) {
119 fn.call(scope || item, item);
121 all.on('add', function(map, key, item){
123 fn.call(scope || item, item);
124 all.un('add', fn, scope);
131 * Executes the specified function once for each item in the collection.
132 * Returning false from the function will cease iteration.
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>
140 * @param {Object} fn The function to execute.
141 * @param {Object} scope The scope to execute in. Defaults to <tt>this</tt>.
143 each: function(fn, scope){
144 this.all.each(fn, scope || this);
148 * Gets the number of items in the collection.
149 * @return {Number} The number of items in the collection.
151 getCount: function(){
152 return this.all.getCount();