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.
18 Ext.define('Ext.AbstractManager', {
20 /* Begin Definitions */
22 requires: ['Ext.util.HashMap'],
28 constructor: function(config) {
29 Ext.apply(this, config || {});
32 * @property {Ext.util.HashMap} all
33 * Contains all of the items currently managed
35 this.all = Ext.create('Ext.util.HashMap');
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.
47 return this.all.get(id);
51 * Registers an item to be managed
52 * @param {Object} item The item to register
54 register: function(item) {
57 key = all.getKey(item);
59 if (all.containsKey(key)) {
60 Ext.Error.raise('Registering duplicate id "' + key + '" with this manager');
67 * Unregisters an item by removing it from this manager
68 * @param {Object} item The item to unregister
70 unregister: function(item) {
71 this.all.remove(item);
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.
79 registerType : function(type, cls) {
80 this.types[type] = cls;
81 cls[this.typeName] = type;
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.
89 isRegistered : function(type){
90 return this.types[type] !== undefined;
94 * Creates and returns an instance of whatever this manager manages, based on the supplied type and
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
100 create: function(config, defaultType) {
101 var type = config[this.typeName] || config.type || defaultType,
102 Constructor = this.types[type];
105 if (Constructor === undefined) {
106 Ext.Error.raise("The '" + type + "' type has not been registered with this manager");
110 return new Constructor(config);
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.
121 onAvailable : function(id, fn, scope){
125 if (all.containsKey(id)) {
127 fn.call(scope || item, item);
129 all.on('add', function(map, key, item){
131 fn.call(scope || item, item);
132 all.un('add', fn, scope);
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`.
147 each: function(fn, scope){
148 this.all.each(fn, scope || this);
152 * Gets the number of items in the collection.
153 * @return {Number} The number of items in the collection.
155 getCount: function(){
156 return this.all.getCount();