76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassYUI.add('pluginhost-config', function(Y) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Adds pluginhost constructor configuration and static configuration support
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @submodule pluginhost-config
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai var PluginHost = Y.Plugin.Host,
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai L = Y.Lang;
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai * A protected initialization method, used by the host class to initialize
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai * plugin configurations passed the constructor, through the config object.
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai *
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai * Host objects should invoke this method at the appropriate time in their
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai * construction lifecycle.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai * @method _initConfigPlugins
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai * @param {Object} config The configuration object passed to the constructor
d34e98574c42591f6e01ee482f163c7817a81324Satyen Desai * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for Plugin.Host
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass PluginHost.prototype._initConfigPlugins = function(config) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Class Configuration
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var classes = (this._getClasses) ? this._getClasses() : [this.constructor],
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass plug = [],
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass unplug = {},
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass constructor, i, classPlug, classUnplug, pluginClassName;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // TODO: Room for optimization. Can we apply statically/unplug in same pass?
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass for (i = classes.length - 1; i >= 0; i--) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass constructor = classes[i];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass classUnplug = constructor._UNPLUG;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (classUnplug) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // subclasses over-write
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.mix(unplug, classUnplug, true);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass classPlug = constructor._PLUG;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (classPlug) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // subclasses over-write
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.mix(plug, classPlug, true);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass for (pluginClassName in plug) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (plug.hasOwnProperty(pluginClassName)) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (!unplug[pluginClassName]) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.plug(plug[pluginClassName]);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // User Configuration
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (config && config.plugins) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.plug(config.plugins);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass };
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Registers plugins to be instantiated at the class level (plugins
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * which should be plugged into every instance of the class by default).
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @method plug
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @static
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {Function} hostClass The host class on which to register the plugins
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @for Plugin.Host
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass PluginHost.plug = function(hostClass, plugin, config) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var p, i, l, name;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (hostClass !== Y.Base) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass hostClass._PLUG = hostClass._PLUG || {};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (!L.isArray(plugin)) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (config) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass plugin = {fn:plugin, cfg:config};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass plugin = [plugin];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass for (i = 0, l = plugin.length; i < l;i++) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass p = plugin[i];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass name = p.NAME || p.fn.NAME;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass hostClass._PLUG[name] = p;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass };
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Unregisters any class level plugins which have been registered by the host class, or any
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * other class in the hierarchy.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @method unplug
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @static
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {Function} hostClass The host class from which to unregister the plugins
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {Function | Array} plugin The plugin class, or an array of plugin classes
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @for Plugin.Host
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass PluginHost.unplug = function(hostClass, plugin) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var p, i, l, name;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (hostClass !== Y.Base) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass hostClass._UNPLUG = hostClass._UNPLUG || {};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (!L.isArray(plugin)) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass plugin = [plugin];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass for (i = 0, l = plugin.length; i < l; i++) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass p = plugin[i];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass name = p.NAME;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (!hostClass._PLUG[name]) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass hostClass._UNPLUG[name] = p;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass } else {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass delete hostClass._PLUG[name];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass };
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}, '@VERSION@' ,{requires:['pluginhost-base']});