PluginHost.js revision 64953b0596d07dbee9a9b521ed61c434ce4709b5
/**
* The base module provides the Base class, which objects requiring managed attributes
* and custom event support can extend. The base module also provides two ways to reuse
* code - An augmentable PluginHost interface which provides plugin support
* (which is augmented to the Base class) and Base.build which provides a way to
* build custom classes using extensions.
*
* @module base
*/
/**
* <p>
* An augmentable class, which when added to a "Base" based class, allows
* the class to support Plugins, providing plug and unplug methods and the ability
* to add Plugins through the configuration literal passed to the constructor.
* </p>
* <p>
* The PlugHost's _initPlugins and _destroyPlugins should be invoked by the
* host class at the appropriate point in the instances lifecyle. This is done
* by default for Base class, so developers extending base don't need to do
* anything to get plugin support.
* </p>
* @class PluginHost
*/
var L = Y.Lang;
function PluginHost(config) {
this._plugins = {};
}
PluginHost.prototype = {
/**
* Adds a plugin to the host object. This will instantiate the
* plugin and attach it to the configured namespace on the host object.
*
* @method plug
* @chainable
* @param p {Function | Object |Array} Accepts the plugin class, or an
* object literal with a "fn" property specifying the plugin class and
* a "cfg" property specifying the configuration for the Plugin.
* <p>
* Additionally an Array can also be passed in, with the above function or
* object literal values, allowing the user to add multiple plugins in a single call.
* </p>
* @param config (Optional) If the first argument is the plugin class, the second argument
* can be the configuration for the plugin.
* @return {Base} A reference to the host object
*/
if (p) {
if (L.isFunction(p)) {
} else if (L.isArray(p)) {
this.plug(p[i]);
}
} else {
}
}
return this;
},
/**
* Removes a plugin from the host object. This will destroy the
* plugin instance and delete the namepsace from the host object.
*
* @method unplug
* @param {String | Function} plugin The namespace of the plugin, or the plugin class with the static NS namespace property defined. If not provided,
* all registered plugins are unplugged.
* @return {Base} A reference to the host object
* @chainable
*/
if (plugin) {
} else {
var ns;
}
}
}
return this;
},
/**
* Determines if a plugin has plugged into this host.
*
* @method hasPlugin
* @param The plugin's namespace
* @return {boolean} returns true, if the plugin has been plugged into this host, false otherwise.
*/
},
/**
* Initializes static plugins registered on the host (using the
* Base.plug static method) and any plugins passed to the
* instance through the "plugins" configuration property.
*
* @method _initPlugins
* @param {Config} config The configuration object literal for the host.
* @private
*/
_initPlugins: function(config) {
// Class Configuration
var classes = this._getClasses(),
plug = [],
unplug = {},
//TODO: Room for optimization. Can we apply statically/unplug in same pass?
constructor = classes[i];
if (classUnplug) {
// subclasses over-write
}
if (classPlug) {
// subclasses over-write
}
}
for (pluginClassName in plug) {
if (!unplug[pluginClassName]) {
}
}
}
// User Configuration
}
},
/**
* Unplugs and destroys all plugins on the host
* @method _destroyPlugins
* @private
*/
_destroyPlugins: function() {
this._unplug();
},
/**
* Private method used to instantiate and attach plugins to the host
*
* @method _plug
* @param {Function} PluginClass The plugin class to instantiate
* @param {Object} config The configuration object for the plugin
* @private
*/
// Update config
} else {
// Create new instance
}
}
},
/**
* 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.
*/
if (L.isFunction(plugin)) {
ns = null;
}
}
if (ns) {
if (this[ns]) {
delete this[ns];
}
}
}
}
};
/**
* Registers plugins to be instantiated at the class level (plugins
* which should be plugged into every instance of the class by default).
*
* @method PluginHost.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 and array of object literals (with fn and cfg properties defined)
* @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
*/
// Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
var p, i, l, name;
if (config) {
}
}
p = plugin[i];
}
}
};
/**
* Unregisters any class level plugins which have been registered by the host class, or any
* other class in the hierarchy.
*
* @method PluginHost.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
*/
var p, i, l, name;
}
p = plugin[i];
} else {
}
}
}
};