PluginHost.js revision c938b255ef3c02ee132e52fbd15bb211c6f3f760
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.
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 * For example the following code would add the AnimPlugin and IOPlugin to Overlay (the plugin host):
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * var o = new Overlay({plugins: [ AnimPlugin, {fn:IOPlugin, cfg:{section:"header"}}]});
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 * @class Plugin.Host
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 * @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 * 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 * @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
ba2701ee03e94104edf19911ee0989f8cee11088Adam Moore } else if (L.isArray(p)) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore return this;
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 * @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 return this;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Determines if a plugin has plugged into this host.
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 * 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 * @method _initPlugins
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param {Config} config The configuration object with property name/value pairs.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore // Class Configuration
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var classes = (this._getClasses) ? this._getClasses() : null,
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore constructor, i, classPlug, classUnplug, pluginClassName;
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore //TODO: Room for optimization. Can we apply statically/unplug in same pass?
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore // subclasses over-write
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore // subclasses over-write
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig // User Configuration
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig * Unplugs and destroys all plugins on the host
a3e6db667fcd33ac653355e76d3660ecf232193bJeff Craig * @method _destroyPlugins
eb86457f85638a9eb7c4d5f84eb367d24061abfbAdam Moore * Private method used to instantiate and attach plugins to the host
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
* @param {String | Function} plugin The namespace for the plugin, or a plugin class with the static NS property defined.
ns = null;
if (ns) {
if (this[ns]) {
delete this[ns];
* @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
var p, i, l, name;
if (config) {
p = plugin[i];
var p, i, l, name;
p = plugin[i];