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