2
Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
8
YUI.add('pluginhost', function(Y) {
11
* Provides the augmentable PluginHost interface, which can be added to any class.
17
* An augmentable class, which provides the augmented class with the ability to host plugins.
18
* It adds <a href="#method_plug">plug</a> and <a href="#method_unplug">unplug</a> methods to the augmented class, which can
19
* be used to add or remove plugins from instances of the class.
22
* <p>Plugins can also be added through the constructor configuration object passed to the host class' constructor using
23
* the "plugins" property. Supported values for the "plugins" property are those defined by the <a href="#method_plug">plug</a> method.
25
* For example the following code would add the AnimPlugin and IOPlugin to Overlay (the plugin host):
27
* var o = new Overlay({plugins: [ AnimPlugin, {fn:IOPlugin, cfg:{section:"header"}}]});
31
* Plug.Host's protected <a href="#method_initPlugins">_initPlugins</a> and <a href="#method_destroyPlugins">_destroyPlugins</a>
32
* methods should be invoked by the host class at the appropriate point in the host's lifecyle.
40
function PluginHost() {
44
PluginHost.prototype = {
47
* Adds a plugin to the host object. This will instantiate the
48
* plugin and attach it to the configured namespace on the host object.
52
* @param p {Function | Object |Array} Accepts the plugin class, or an
53
* object with a "fn" property specifying the plugin class and
54
* a "cfg" property specifying the configuration for the Plugin.
56
* Additionally an Array can also be passed in, with the above function or
57
* object values, allowing the user to add multiple plugins in a single call.
59
* @param config (Optional) If the first argument is the plugin class, the second argument
60
* can be the configuration for the plugin.
61
* @return {Base} A reference to the host object
64
plug: function(p, config) {
66
if (L.isFunction(p)) {
67
this._plug(p, config);
68
} else if (L.isArray(p)) {
69
for (var i = 0, ln = p.length; i < ln; i++) {
73
this._plug(p.fn, p.cfg);
80
* Removes a plugin from the host object. This will destroy the
81
* plugin instance and delete the namepsace from the host object.
84
* @param {String | Function} plugin The namespace of the plugin, or the plugin class with the static NS namespace property defined. If not provided,
85
* all registered plugins are unplugged.
86
* @return {Base} A reference to the host object
89
unplug: function(plugin) {
94
for (ns in this._plugins) {
95
if (this._plugins.hasOwnProperty(ns)) {
104
* Determines if a plugin has plugged into this host.
107
* @param {String} ns The plugin's namespace
108
* @return {boolean} returns true, if the plugin has been plugged into this host, false otherwise.
110
hasPlugin : function(ns) {
111
return (this._plugins[ns] && this[ns]);
115
* Initializes static plugins registered on the host (using the
116
* Base.plug static method) and any plugins passed to the
117
* instance through the "plugins" configuration property.
119
* @method _initPlugins
120
* @param {Config} config The configuration object with property name/value pairs.
123
_initPlugins: function(config) {
124
this._plugins = this._plugins || {};
126
// Class Configuration
127
var classes = (this._getClasses) ? this._getClasses() : [this.constructor],
130
constructor, i, classPlug, classUnplug, pluginClassName;
132
//TODO: Room for optimization. Can we apply statically/unplug in same pass?
133
for (i = classes.length - 1; i >= 0; i--) {
134
constructor = classes[i];
136
classUnplug = constructor._UNPLUG;
138
// subclasses over-write
139
Y.mix(unplug, classUnplug, true);
142
classPlug = constructor._PLUG;
144
// subclasses over-write
145
Y.mix(plug, classPlug, true);
149
for (pluginClassName in plug) {
150
if (plug.hasOwnProperty(pluginClassName)) {
151
if (!unplug[pluginClassName]) {
152
this.plug(plug[pluginClassName]);
157
// User Configuration
158
if (config && config.plugins) {
159
this.plug(config.plugins);
164
* Unplugs and destroys all plugins on the host
165
* @method _destroyPlugins
168
_destroyPlugins: function() {
173
* Private method used to instantiate and attach plugins to the host
176
* @param {Function} PluginClass The plugin class to instantiate
177
* @param {Object} config The configuration object for the plugin
180
_plug: function(PluginClass, config) {
181
if (PluginClass && PluginClass.NS) {
182
var ns = PluginClass.NS;
184
config = config || {};
187
if (this.hasPlugin(ns)) {
189
this[ns].setAttrs(config);
191
// Create new instance
192
this[ns] = new PluginClass(config);
193
this._plugins[ns] = PluginClass;
199
* Unplugs and destroys a plugin already instantiated with the host.
203
* @param {String | Function} plugin The namespace for the plugin, or a plugin class with the static NS property defined.
205
_unplug : function(plugin) {
207
plugins = this._plugins;
209
if (L.isFunction(plugin)) {
211
if (ns && (!plugins[ns] || plugins[ns] !== plugin)) {
229
* Registers plugins to be instantiated at the class level (plugins
230
* which should be plugged into every instance of the class by default).
232
* @method Plugin.Host.plug
235
* @param {Function} hostClass The host class on which to register the plugins
236
* @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
237
* @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
239
PluginHost.plug = function(hostClass, plugin, config) {
240
// Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
243
if (hostClass !== Y.Base) {
244
hostClass._PLUG = hostClass._PLUG || {};
246
if (!L.isArray(plugin)) {
248
plugin = {fn:plugin, cfg:config};
253
for (i = 0, l = plugin.length; i < l;i++) {
255
name = p.NAME || p.fn.NAME;
256
hostClass._PLUG[name] = p;
262
* Unregisters any class level plugins which have been registered by the host class, or any
263
* other class in the hierarchy.
265
* @method Plugin.Host.unplug
268
* @param {Function} hostClass The host class from which to unregister the plugins
269
* @param {Function | Array} plugin The plugin class, or an array of plugin classes
271
PluginHost.unplug = function(hostClass, plugin) {
274
if (hostClass !== Y.Base) {
275
hostClass._UNPLUG = hostClass._UNPLUG || {};
277
if (!L.isArray(plugin)) {
281
for (i = 0, l = plugin.length; i < l; i++) {
284
if (!hostClass._PLUG[name]) {
285
hostClass._UNPLUG[name] = p;
287
delete hostClass._PLUG[name];
293
Y.namespace("Plugin").Host = PluginHost;
296
}, '3.0.0' ,{requires:['yui-base']});