3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('pluginhost-config', function(Y) {
10
* Adds pluginhost constructor configuration and static configuration support
11
* @submodule pluginhost-config
14
var PluginHost = Y.Plugin.Host,
18
* A protected initialization method, used by the host class to initialize
19
* plugin configurations passed the constructor, through the config object.
21
* Host objects should invoke this method at the appropriate time in their
22
* construction lifecycle.
24
* @method _initConfigPlugins
25
* @param {Object} config The configuration object passed to the constructor
29
PluginHost.prototype._initConfigPlugins = function(config) {
31
// Class Configuration
32
var classes = (this._getClasses) ? this._getClasses() : [this.constructor],
35
constructor, i, classPlug, classUnplug, pluginClassName;
37
// TODO: Room for optimization. Can we apply statically/unplug in same pass?
38
for (i = classes.length - 1; i >= 0; i--) {
39
constructor = classes[i];
41
classUnplug = constructor._UNPLUG;
43
// subclasses over-write
44
Y.mix(unplug, classUnplug, true);
47
classPlug = constructor._PLUG;
49
// subclasses over-write
50
Y.mix(plug, classPlug, true);
54
for (pluginClassName in plug) {
55
if (plug.hasOwnProperty(pluginClassName)) {
56
if (!unplug[pluginClassName]) {
57
this.plug(plug[pluginClassName]);
63
if (config && config.plugins) {
64
this.plug(config.plugins);
69
* Registers plugins to be instantiated at the class level (plugins
70
* which should be plugged into every instance of the class by default).
75
* @param {Function} hostClass The host class on which to register the plugins
76
* @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
77
* @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
80
PluginHost.plug = function(hostClass, plugin, config) {
81
// Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
84
if (hostClass !== Y.Base) {
85
hostClass._PLUG = hostClass._PLUG || {};
87
if (!L.isArray(plugin)) {
89
plugin = {fn:plugin, cfg:config};
94
for (i = 0, l = plugin.length; i < l;i++) {
96
name = p.NAME || p.fn.NAME;
97
hostClass._PLUG[name] = p;
103
* Unregisters any class level plugins which have been registered by the host class, or any
104
* other class in the hierarchy.
109
* @param {Function} hostClass The host class from which to unregister the plugins
110
* @param {Function | Array} plugin The plugin class, or an array of plugin classes
113
PluginHost.unplug = function(hostClass, plugin) {
116
if (hostClass !== Y.Base) {
117
hostClass._UNPLUG = hostClass._UNPLUG || {};
119
if (!L.isArray(plugin)) {
123
for (i = 0, l = plugin.length; i < l; i++) {
126
if (!hostClass._PLUG[name]) {
127
hostClass._UNPLUG[name] = p;
129
delete hostClass._PLUG[name];
136
}, '3.4.1' ,{requires:['pluginhost-base']});