Upgrade to ExtJS 4.0.7 - Released 10/19/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  * @singleton
17  *
18  * Provides a registry of available Plugin classes indexed by a mnemonic code known as the Plugin's ptype.
19  *
20  * A plugin may be specified simply as a *config object* as long as the correct `ptype` is specified:
21  *
22  *     {
23  *         ptype: 'gridviewdragdrop',
24  *         dragText: 'Drag and drop to reorganize'
25  *     }
26  *
27  * Or just use the ptype on its own:
28  *
29  *     'gridviewdragdrop'
30  *
31  * Alternatively you can instantiate the plugin with Ext.create:
32  *
33  *     Ext.create('Ext.view.plugin.AutoComplete', {
34  *         ptype: 'gridviewdragdrop',
35  *         dragText: 'Drag and drop to reorganize'
36  *     })
37  */
38 Ext.define('Ext.PluginManager', {
39     extend: 'Ext.AbstractManager',
40     alternateClassName: 'Ext.PluginMgr',
41     singleton: true,
42     typeName: 'ptype',
43
44     /**
45      * Creates a new Plugin from the specified config object using the config object's ptype to determine the class to
46      * instantiate.
47      * @param {Object} config A configuration object for the Plugin you wish to create.
48      * @param {Function} defaultType (optional) The constructor to provide the default Plugin type if the config object does not
49      * contain a `ptype`. (Optional if the config contains a `ptype`).
50      * @return {Ext.Component} The newly instantiated Plugin.
51      */
52     //create: function(plugin, defaultType) {
53     //    if (plugin instanceof this) {
54     //        return plugin;
55     //    } else {
56     //        var type, config = {};
57     //
58     //        if (Ext.isString(plugin)) {
59     //            type = plugin;
60     //        }
61     //        else {
62     //            type = plugin[this.typeName] || defaultType;
63     //            config = plugin;
64     //        }
65     //
66     //        return Ext.createByAlias('plugin.' + type, config);
67     //    }
68     //},
69
70     create : function(config, defaultType){
71         if (config.init) {
72             return config;
73         } else {
74             return Ext.createByAlias('plugin.' + (config.ptype || defaultType), config);
75         }
76
77         // Prior system supported Singleton plugins.
78         //var PluginCls = this.types[config.ptype || defaultType];
79         //if (PluginCls.init) {
80         //    return PluginCls;
81         //} else {
82         //    return new PluginCls(config);
83         //}
84     },
85
86     /**
87      * Returns all plugins registered with the given type. Here, 'type' refers to the type of plugin, not its ptype.
88      * @param {String} type The type to search for
89      * @param {Boolean} defaultsOnly True to only return plugins of this type where the plugin's isDefault property is
90      * truthy
91      * @return {Ext.AbstractPlugin[]} All matching plugins
92      */
93     findByType: function(type, defaultsOnly) {
94         var matches = [],
95             types   = this.types;
96
97         for (var name in types) {
98             if (!types.hasOwnProperty(name)) {
99                 continue;
100             }
101             var item = types[name];
102
103             if (item.type == type && (!defaultsOnly || (defaultsOnly === true && item.isDefault))) {
104                 matches.push(item);
105             }
106         }
107
108         return matches;
109     }
110 }, function() {
111     /**
112      * Shorthand for {@link Ext.PluginManager#registerType}
113      * @param {String} ptype The ptype mnemonic string by which the Plugin class
114      * may be looked up.
115      * @param {Function} cls The new Plugin class.
116      * @member Ext
117      * @method preg
118      */
119     Ext.preg = function() {
120         return Ext.PluginManager.registerType.apply(Ext.PluginManager, arguments);
121     };
122 });
123