Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / PluginManager.js
1 /**
2  * @class Ext.PluginManager
3  * @extends Ext.AbstractManager
4  * <p>Provides a registry of available Plugin <i>classes</i> indexed by a mnemonic code known as the Plugin's ptype.
5  * The <code>{@link Ext.Component#xtype xtype}</code> provides a way to avoid instantiating child Components
6  * when creating a full, nested config object for a complete Ext page.</p>
7  * <p>A child Component may be specified simply as a <i>config object</i>
8  * as long as the correct <code>{@link Ext.Component#xtype xtype}</code> is specified so that if and when the Component
9  * needs rendering, the correct type can be looked up for lazy instantiation.</p>
10  * <p>For a list of all available <code>{@link Ext.Component#xtype xtypes}</code>, see {@link Ext.Component}.</p>
11  * @singleton
12  */
13 Ext.define('Ext.PluginManager', {
14     extend: 'Ext.AbstractManager',
15     alternateClassName: 'Ext.PluginMgr',
16     singleton: true,
17     typeName: 'ptype',
18
19     /**
20      * Creates a new Plugin from the specified config object using the
21      * config object's ptype to determine the class to instantiate.
22      * @param {Object} config A configuration object for the Plugin you wish to create.
23      * @param {Constructor} defaultType The constructor to provide the default Plugin type if
24      * the config object does not contain a <code>ptype</code>. (Optional if the config contains a <code>ptype</code>).
25      * @return {Ext.Component} The newly instantiated Plugin.
26      */
27     //create: function(plugin, defaultType) {
28     //    if (plugin instanceof this) {
29     //        return plugin;
30     //    } else {
31     //        var type, config = {};
32     //
33     //        if (Ext.isString(plugin)) {
34     //            type = plugin;
35     //        }
36     //        else {
37     //            type = plugin[this.typeName] || defaultType;
38     //            config = plugin;
39     //        }
40     //
41     //        return Ext.createByAlias('plugin.' + type, config);
42     //    }
43     //},
44
45     create : function(config, defaultType){
46         if (config.init) {
47             return config;
48         } else {
49             return Ext.createByAlias('plugin.' + (config.ptype || defaultType), config);
50         }
51         
52         // Prior system supported Singleton plugins.
53         //var PluginCls = this.types[config.ptype || defaultType];
54         //if (PluginCls.init) {
55         //    return PluginCls;
56         //} else {
57         //    return new PluginCls(config);
58         //}
59     },
60
61     /**
62      * Returns all plugins registered with the given type. Here, 'type' refers to the type of plugin, not its ptype.
63      * @param {String} type The type to search for
64      * @param {Boolean} defaultsOnly True to only return plugins of this type where the plugin's isDefault property is truthy
65      * @return {Array} All matching plugins
66      */
67     findByType: function(type, defaultsOnly) {
68         var matches = [],
69             types   = this.types;
70
71         for (var name in types) {
72             if (!types.hasOwnProperty(name)) {
73                 continue;
74             }
75             var item = types[name];
76
77             if (item.type == type && (!defaultsOnly || (defaultsOnly === true && item.isDefault))) {
78                 matches.push(item);
79             }
80         }
81
82         return matches;
83     }
84 }, function() {    
85     /**
86      * Shorthand for {@link Ext.PluginManager#registerType}
87      * @param {String} ptype The ptype mnemonic string by which the Plugin class
88      * may be looked up.
89      * @param {Constructor} cls The new Plugin class.
90      * @member Ext
91      * @method preg
92      */
93     Ext.preg = function() {
94         return Ext.PluginManager.registerType.apply(Ext.PluginManager, arguments);
95     };
96 });