1 |
efrain |
1 |
YUI.add('pluginhost-config', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Adds pluginhost constructor configuration and static configuration support
|
|
|
5 |
* @submodule pluginhost-config
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
var PluginHost = Y.Plugin.Host,
|
|
|
9 |
L = Y.Lang;
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* A protected initialization method, used by the host class to initialize
|
|
|
13 |
* plugin configurations passed the constructor, through the config object.
|
|
|
14 |
*
|
|
|
15 |
* Host objects should invoke this method at the appropriate time in their
|
|
|
16 |
* construction lifecycle.
|
|
|
17 |
*
|
|
|
18 |
* @method _initConfigPlugins
|
|
|
19 |
* @param {Object} config The configuration object passed to the constructor
|
|
|
20 |
* @protected
|
|
|
21 |
* @for Plugin.Host
|
|
|
22 |
*/
|
|
|
23 |
PluginHost.prototype._initConfigPlugins = function(config) {
|
|
|
24 |
|
|
|
25 |
// Class Configuration
|
|
|
26 |
var classes = (this._getClasses) ? this._getClasses() : [this.constructor],
|
|
|
27 |
plug = [],
|
|
|
28 |
unplug = {},
|
|
|
29 |
constructor, i, classPlug, classUnplug, pluginClassName;
|
|
|
30 |
|
|
|
31 |
// TODO: Room for optimization. Can we apply statically/unplug in same pass?
|
|
|
32 |
for (i = classes.length - 1; i >= 0; i--) {
|
|
|
33 |
constructor = classes[i];
|
|
|
34 |
|
|
|
35 |
classUnplug = constructor._UNPLUG;
|
|
|
36 |
if (classUnplug) {
|
|
|
37 |
// subclasses over-write
|
|
|
38 |
Y.mix(unplug, classUnplug, true);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
classPlug = constructor._PLUG;
|
|
|
42 |
if (classPlug) {
|
|
|
43 |
// subclasses over-write
|
|
|
44 |
Y.mix(plug, classPlug, true);
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
for (pluginClassName in plug) {
|
|
|
49 |
if (plug.hasOwnProperty(pluginClassName)) {
|
|
|
50 |
if (!unplug[pluginClassName]) {
|
|
|
51 |
this.plug(plug[pluginClassName]);
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
// User Configuration
|
|
|
57 |
if (config && config.plugins) {
|
|
|
58 |
this.plug(config.plugins);
|
|
|
59 |
}
|
|
|
60 |
};
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Registers plugins to be instantiated at the class level (plugins
|
|
|
64 |
* which should be plugged into every instance of the class by default).
|
|
|
65 |
*
|
|
|
66 |
* @method plug
|
|
|
67 |
* @static
|
|
|
68 |
*
|
|
|
69 |
* @param {Function} hostClass The host class on which to register the plugins
|
|
|
70 |
* @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
|
|
|
71 |
* @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
|
|
|
72 |
* @for Plugin.Host
|
|
|
73 |
*/
|
|
|
74 |
PluginHost.plug = function(hostClass, plugin, config) {
|
|
|
75 |
// Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
|
|
|
76 |
var p, i, l, name;
|
|
|
77 |
|
|
|
78 |
if (hostClass !== Y.Base) {
|
|
|
79 |
hostClass._PLUG = hostClass._PLUG || {};
|
|
|
80 |
|
|
|
81 |
if (!L.isArray(plugin)) {
|
|
|
82 |
if (config) {
|
|
|
83 |
plugin = {fn:plugin, cfg:config};
|
|
|
84 |
}
|
|
|
85 |
plugin = [plugin];
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
for (i = 0, l = plugin.length; i < l;i++) {
|
|
|
89 |
p = plugin[i];
|
|
|
90 |
name = p.NAME || p.fn.NAME;
|
|
|
91 |
hostClass._PLUG[name] = p;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
};
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Unregisters any class level plugins which have been registered by the host class, or any
|
|
|
98 |
* other class in the hierarchy.
|
|
|
99 |
*
|
|
|
100 |
* @method unplug
|
|
|
101 |
* @static
|
|
|
102 |
*
|
|
|
103 |
* @param {Function} hostClass The host class from which to unregister the plugins
|
|
|
104 |
* @param {Function | Array} plugin The plugin class, or an array of plugin classes
|
|
|
105 |
* @for Plugin.Host
|
|
|
106 |
*/
|
|
|
107 |
PluginHost.unplug = function(hostClass, plugin) {
|
|
|
108 |
var p, i, l, name;
|
|
|
109 |
|
|
|
110 |
if (hostClass !== Y.Base) {
|
|
|
111 |
hostClass._UNPLUG = hostClass._UNPLUG || {};
|
|
|
112 |
|
|
|
113 |
if (!L.isArray(plugin)) {
|
|
|
114 |
plugin = [plugin];
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
for (i = 0, l = plugin.length; i < l; i++) {
|
|
|
118 |
p = plugin[i];
|
|
|
119 |
name = p.NAME;
|
|
|
120 |
if (!hostClass._PLUG[name]) {
|
|
|
121 |
hostClass._UNPLUG[name] = p;
|
|
|
122 |
} else {
|
|
|
123 |
delete hostClass._PLUG[name];
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
};
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
}, '3.18.1', {"requires": ["pluginhost-base"]});
|