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