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