]> git.proxmox.com Git - sencha-touch.git/blob - src/src/ComponentManager.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / ComponentManager.js
1 /**
2 * @private
3 *
4 * Provides a registry of all Components (instances of {@link Ext.Component} or any subclass
5 * thereof) on a page so that they can be easily accessed by {@link Ext.Component component}
6 * {@link Ext.Component#getId id} (see {@link #get}, or the convenience method {@link Ext#getCmp Ext.getCmp}).
7 *
8 * This object also provides a registry of available Component _classes_
9 * indexed by a mnemonic code known as the Component's `xtype`.
10 * The `xtype` provides a way to avoid instantiating child Components
11 * when creating a full, nested config object for a complete Ext page.
12 *
13 * A child Component may be specified simply as a _config object_
14 * as long as the correct `xtype` is specified so that if and when the Component
15 * needs rendering, the correct type can be looked up for lazy instantiation.
16 *
17 * For a list of all available `xtype`, see {@link Ext.Component}.
18 */
19 Ext.define('Ext.ComponentManager', {
20 alternateClassName: 'Ext.ComponentMgr',
21 singleton: true,
22
23 constructor: function() {
24 var map = {};
25
26 // The sole reason for this is just to support the old code of ComponentQuery
27 this.all = {
28 map: map,
29
30 getArray: function() {
31 var list = [],
32 id;
33
34 for (id in map) {
35 if (map.hasOwnProperty(id)) {
36 list.push(map[id]);
37 }
38 }
39 return list;
40 }
41 };
42
43 this.map = map;
44 },
45
46 /**
47 * Registers an item to be managed.
48 * @param {Object} component The item to register.
49 */
50 register: function(component) {
51 var id = component.getId();
52
53 // <debug>
54 if (this.map[id]) {
55 Ext.Logger.warn('Registering a component with a id (`' + id + '`) which has already been used. Please ensure the existing component has been destroyed (`Ext.Component#destroy()`.');
56 }
57 // </debug>
58
59 this.map[component.getId()] = component;
60 },
61
62 /**
63 * Unregisters an item by removing it from this manager.
64 * @param {Object} component The item to unregister.
65 */
66 unregister: function(component) {
67 delete this.map[component.getId()];
68 },
69
70 /**
71 * Checks if an item type is registered.
72 * @param {String} component The mnemonic string by which the class may be looked up.
73 * @return {Boolean} Whether the type is registered.
74 */
75 isRegistered : function(component){
76 return this.map[component] !== undefined;
77 },
78
79 /**
80 * Returns an item by id.
81 * For additional details see {@link Ext.util.HashMap#get}.
82 * @param {String} id The `id` of the item.
83 * @return {Object} The item, or `undefined` if not found.
84 */
85 get: function(id) {
86 return this.map[id];
87 },
88
89 /**
90 * Creates a new Component from the specified config object using the
91 * config object's `xtype` to determine the class to instantiate.
92 * @param {Object} component A configuration object for the Component you wish to create.
93 * @param {Function} [defaultType] The constructor to provide the default Component type if
94 * the config object does not contain a `xtype`. (Optional if the config contains an `xtype`).
95 * @return {Ext.Component} The newly instantiated Component.
96 */
97 create: function(component, defaultType) {
98 if (component.isComponent) {
99 return component;
100 }
101 else if (Ext.isString(component)) {
102 return Ext.createByAlias('widget.' + component);
103 }
104 else {
105 var type = component.xtype || defaultType;
106
107 return Ext.createByAlias('widget.' + type, component);
108 }
109 },
110
111 registerType: Ext.emptyFn
112 });