2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('pluginhost-config', function(Y) {
11 * Adds pluginhost constructor configuration and static configuration support
12 * @submodule pluginhost-config
16 * Constructor and static configuration support for plugins
20 var PluginHost = Y.Plugin.Host,
23 PluginHost.prototype._initConfigPlugins = function(config) {
25 // Class Configuration
26 var classes = (this._getClasses) ? this._getClasses() : [this.constructor],
29 constructor, i, classPlug, classUnplug, pluginClassName;
31 // TODO: Room for optimization. Can we apply statically/unplug in same pass?
32 for (i = classes.length - 1; i >= 0; i--) {
33 constructor = classes[i];
35 classUnplug = constructor._UNPLUG;
37 // subclasses over-write
38 Y.mix(unplug, classUnplug, true);
41 classPlug = constructor._PLUG;
43 // subclasses over-write
44 Y.mix(plug, classPlug, true);
48 for (pluginClassName in plug) {
49 if (plug.hasOwnProperty(pluginClassName)) {
50 if (!unplug[pluginClassName]) {
51 this.plug(plug[pluginClassName]);
57 if (config && config.plugins) {
58 this.plug(config.plugins);
63 * Registers plugins to be instantiated at the class level (plugins
64 * which should be plugged into every instance of the class by default).
66 * @method Plugin.Host.plug
69 * @param {Function} hostClass The host class on which to register the plugins
70 * @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
71 * @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
73 PluginHost.plug = function(hostClass, plugin, config) {
74 // Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
77 if (hostClass !== Y.Base) {
78 hostClass._PLUG = hostClass._PLUG || {};
80 if (!L.isArray(plugin)) {
82 plugin = {fn:plugin, cfg:config};
87 for (i = 0, l = plugin.length; i < l;i++) {
89 name = p.NAME || p.fn.NAME;
90 hostClass._PLUG[name] = p;
96 * Unregisters any class level plugins which have been registered by the host class, or any
97 * other class in the hierarchy.
99 * @method Plugin.Host.unplug
102 * @param {Function} hostClass The host class from which to unregister the plugins
103 * @param {Function | Array} plugin The plugin class, or an array of plugin classes
105 PluginHost.unplug = function(hostClass, plugin) {
108 if (hostClass !== Y.Base) {
109 hostClass._UNPLUG = hostClass._UNPLUG || {};
111 if (!L.isArray(plugin)) {
115 for (i = 0, l = plugin.length; i < l; i++) {
118 if (!hostClass._PLUG[name]) {
119 hostClass._UNPLUG[name] = p;
121 delete hostClass._PLUG[name];
128 }, '3.3.0' ,{requires:['pluginhost-base']});