PluginHostConfig.js revision 6c96442b6a372e466ea59417a4e147d4fbd4a006
4458N/A /**
4458N/A * Adds pluginhost constructor configuration and static configuration support
4458N/A * @submodule pluginhost-config
4458N/A */
4458N/A
4458N/A /**
4458N/A * Constructor and static configuration support for plugins
4458N/A *
4458N/A * @for Plugin.Host
4458N/A */
4458N/A var PluginHost = Y.Plugin.Host,
4458N/A L = Y.Lang;
4458N/A
4458N/A PluginHost.prototype._initConfigPlugins = function(config) {
4458N/A
4458N/A // Class Configuration
4458N/A var classes = (this._getClasses) ? this._getClasses() : [this.constructor],
4458N/A plug = [],
4458N/A unplug = {},
4458N/A constructor, i, classPlug, classUnplug, pluginClassName;
4458N/A
4458N/A // TODO: Room for optimization. Can we apply statically/unplug in same pass?
5403N/A for (i = classes.length - 1; i >= 0; i--) {
4458N/A constructor = classes[i];
4458N/A
4458N/A classUnplug = constructor._UNPLUG;
4458N/A if (classUnplug) {
4458N/A // subclasses over-write
6853N/A Y.mix(unplug, classUnplug, true);
6853N/A }
6853N/A
4458N/A classPlug = constructor._PLUG;
4458N/A if (classPlug) {
4458N/A // subclasses over-write
6853N/A Y.mix(plug, classPlug, true);
6853N/A }
6853N/A }
6853N/A
4458N/A for (pluginClassName in plug) {
4458N/A if (plug.hasOwnProperty(pluginClassName)) {
4458N/A if (!unplug[pluginClassName]) {
6853N/A this.plug(plug[pluginClassName]);
4458N/A }
5425N/A }
5425N/A }
4458N/A
4458N/A // User Configuration
4458N/A if (config && config.plugins) {
4458N/A this.plug(config.plugins);
4458N/A }
4458N/A };
4458N/A
4458N/A /**
4458N/A * Registers plugins to be instantiated at the class level (plugins
4458N/A * which should be plugged into every instance of the class by default).
4458N/A *
4458N/A * @method Plugin.Host.plug
4458N/A * @static
4458N/A *
5403N/A * @param {Function} hostClass The host class on which to register the plugins
5403N/A * @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
5403N/A * @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
4458N/A */
4458N/A PluginHost.plug = function(hostClass, plugin, config) {
5445N/A // Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
5445N/A var p, i, l, name;
5445N/A
5445N/A if (hostClass !== Y.Base) {
5445N/A hostClass._PLUG = hostClass._PLUG || {};
5445N/A
5445N/A if (!L.isArray(plugin)) {
5445N/A if (config) {
5445N/A plugin = {fn:plugin, cfg:config};
5445N/A }
5445N/A plugin = [plugin];
5445N/A }
5445N/A
5445N/A for (i = 0, l = plugin.length; i < l;i++) {
5445N/A p = plugin[i];
4458N/A name = p.NAME || p.fn.NAME;
4458N/A hostClass._PLUG[name] = p;
4458N/A }
4458N/A }
4458N/A };
4458N/A
4458N/A /**
4458N/A * Unregisters any class level plugins which have been registered by the host class, or any
4458N/A * other class in the hierarchy.
4458N/A *
4458N/A * @method Plugin.Host.unplug
4458N/A * @static
4458N/A *
4458N/A * @param {Function} hostClass The host class from which to unregister the plugins
4458N/A * @param {Function | Array} plugin The plugin class, or an array of plugin classes
5403N/A */
5445N/A PluginHost.unplug = function(hostClass, plugin) {
5403N/A var p, i, l, name;
4458N/A
4458N/A if (hostClass !== Y.Base) {
4458N/A hostClass._UNPLUG = hostClass._UNPLUG || {};
4458N/A
5445N/A if (!L.isArray(plugin)) {
4458N/A plugin = [plugin];
4458N/A }
for (i = 0, l = plugin.length; i < l; i++) {
p = plugin[i];
name = p.NAME;
if (!hostClass._PLUG[name]) {
hostClass._UNPLUG[name] = p;
} else {
delete hostClass._PLUG[name];
}
}
}
};