PluginHost.js revision c938b255ef3c02ee132e52fbd15bb211c6f3f760
c4e6d94ea429e473a6732b6eb5e0fc980e822881Adam Moore /**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * <p>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * An augmentable class, which provides the augmented class with the ability to host plugins.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * It adds <a href="#method_plug">plug</a> and <a href="#method_unplug">unplug</a> methods to the augmented class, which can
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * be used to add or remove plugins from instances of the class.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * </p>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
ba2701ee03e94104edf19911ee0989f8cee11088Adam Moore * <p>Plugins can also be added through the constructor configuration object passed to the host class' constructor using
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * the "plugins" property. Supported values for the "plugins" property are those defined by the <a href="#method_plug">plug</a> method.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * For example the following code would add the AnimPlugin and IOPlugin to Overlay (the plugin host):
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * <xmp>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * var o = new Overlay({plugins: [ AnimPlugin, {fn:IOPlugin, cfg:{section:"header"}}]});
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * </xmp>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * </p>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * <p>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Plug.Host's protected <a href="#method_initPlugins">_initPlugins</a> and <a href="#method_destroyPlugins">_destroyPlugins</a>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * methods should be invoked by the host class at the appropriate point in the host's lifecyle. This is done by default for
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * the Base class, so developers extending Base or Widget don't need to do anything to enable plugin support.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * </p>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @class Plugin.Host
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var L = Y.Lang;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore function PluginHost(config) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore this._plugins = {};
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore PluginHost.prototype = {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore /**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Adds a plugin to the host object. This will instantiate the
ba2701ee03e94104edf19911ee0989f8cee11088Adam Moore * plugin and attach it to the configured namespace on the host object.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method plug
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @chainable
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param p {Function | Object |Array} Accepts the plugin class, or an
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * object with a "fn" property specifying the plugin class and
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * a "cfg" property specifying the configuration for the Plugin.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * <p>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Additionally an Array can also be passed in, with the above function or
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * object values, allowing the user to add multiple plugins in a single call.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * </p>
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param config (Optional) If the first argument is the plugin class, the second argument
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * can be the configuration for the plugin.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @return {Base} A reference to the host object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore plug: function(p, config) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (p) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (L.isFunction(p)) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore this._plug(p, config);
ba2701ee03e94104edf19911ee0989f8cee11088Adam Moore } else if (L.isArray(p)) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore for (var i = 0, ln = p.length; i < ln; i++) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore this.plug(p[i]);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore } else {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore this._plug(p.fn, p.cfg);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return this;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore },
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore /**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Removes a plugin from the host object. This will destroy the
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * plugin instance and delete the namepsace from the host object.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method unplug
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param {String | Function} plugin The namespace of the plugin, or the plugin class with the static NS namespace property defined. If not provided,
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * all registered plugins are unplugged.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @return {Base} A reference to the host object
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @chainable
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore unplug: function(plugin) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (plugin) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore this._unplug(plugin);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore } else {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var ns;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore for (ns in this._plugins) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (this._plugins.hasOwnProperty(ns)) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore this._unplug(ns);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return this;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore },
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore /**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Determines if a plugin has plugged into this host.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method hasPlugin
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param {String} ns The plugin's namespace
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @return {boolean} returns true, if the plugin has been plugged into this host, false otherwise.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore hasPlugin : function(ns) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return (this._plugins[ns] && this[ns]);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore },
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore /**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Initializes static plugins registered on the host (using the
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Base.plug static method) and any plugins passed to the
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * instance through the "plugins" configuration property.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore *
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method _initPlugins
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param {Config} config The configuration object with property name/value pairs.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @private
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore _initPlugins: function(config) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore // Class Configuration
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var classes = (this._getClasses) ? this._getClasses() : null,
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore plug = [],
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore unplug = {},
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore constructor, i, classPlug, classUnplug, pluginClassName;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore //TODO: Room for optimization. Can we apply statically/unplug in same pass?
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (classes) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore for (i = classes.length - 1; i >= 0; i--) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore constructor = classes[i];
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore classUnplug = constructor._UNPLUG;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (classUnplug) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore // subclasses over-write
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore Y.mix(unplug, classUnplug, true);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore classPlug = constructor._PLUG;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (classPlug) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore // subclasses over-write
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore Y.mix(plug, classPlug, true);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore for (pluginClassName in plug) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (plug.hasOwnProperty(pluginClassName)) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore if (!unplug[pluginClassName]) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore this.plug(plug[pluginClassName]);
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore }
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig // User Configuration
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig if (config && config.plugins) {
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig this.plug(config.plugins);
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig }
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig },
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig /**
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig * Unplugs and destroys all plugins on the host
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig * @method _destroyPlugins
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @private
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig _destroyPlugins: function() {
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig this._unplug();
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig },
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore /**
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore * Private method used to instantiate and attach plugins to the host
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore *
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore * @method _plug
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore * @param {Function} PluginClass The plugin class to instantiate
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore * @param {Object} config The configuration object for the plugin
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore * @private
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore */
c4e6d94ea429e473a6732b6eb5e0fc980e822881Adam Moore _plug: function(PluginClass, config) {
if (PluginClass && PluginClass.NS) {
var ns = PluginClass.NS;
config = config || {};
config.host = this;
if (this.hasPlugin(ns)) {
// Update config
this[ns].setAttrs(config);
} else {
// Create new instance
this[ns] = new PluginClass(config);
this._plugins[ns] = PluginClass;
}
}
},
/**
* Unplugs and destroys a plugin already instantiated with the host.
*
* @method _unplug
* @private
* @param {String | Function} plugin The namespace for the plugin, or a plugin class with the static NS property defined.
*/
_unplug : function(plugin) {
var ns = plugin,
plugins = this._plugins;
if (L.isFunction(plugin)) {
ns = plugin.NS;
if (ns && (!plugins[ns] || plugins[ns] !== plugin)) {
ns = null;
}
}
if (ns) {
if (this[ns]) {
this[ns].destroy();
delete this[ns];
}
if (plugins[ns]) {
delete plugins[ns];
}
}
}
};
/**
* Registers plugins to be instantiated at the class level (plugins
* which should be plugged into every instance of the class by default).
*
* @method Plugin.Host.plug
* @static
*
* @param {Function} hostClass The host class on which to register the plugins
* @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
* @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
*/
PluginHost.plug = function(hostClass, plugin, config) {
// Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
var p, i, l, name;
if (hostClass !== Y.Base) {
hostClass._PLUG = hostClass._PLUG || {};
if (!L.isArray(plugin)) {
if (config) {
plugin = {fn:plugin, cfg:config};
}
plugin = [plugin];
}
for (i = 0, l = plugin.length; i < l;i++) {
p = plugin[i];
name = p.NAME || p.fn.NAME;
hostClass._PLUG[name] = p;
}
}
};
/**
* Unregisters any class level plugins which have been registered by the host class, or any
* other class in the hierarchy.
*
* @method Plugin.Host.unplug
* @static
*
* @param {Function} hostClass The host class from which to unregister the plugins
* @param {Function | Array} plugin The plugin class, or an array of plugin classes
*/
PluginHost.unplug = function(hostClass, plugin) {
var p, i, l, name;
if (hostClass !== Y.Base) {
hostClass._UNPLUG = hostClass._UNPLUG || {};
if (!L.isArray(plugin)) {
plugin = [plugin];
}
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];
}
}
}
};
Y.namespace("Plugin").Host = PluginHost;