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