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