]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/pluginhost/pluginhost-config.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / pluginhost / pluginhost-config.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('pluginhost-config', function(Y) {
9
10     /**
11      * Adds pluginhost constructor configuration and static configuration support
12      * @submodule pluginhost-config
13      */
14
15     /**
16      * Constructor and static configuration support for plugins
17      * 
18      * @for Plugin.Host
19      */
20     var PluginHost = Y.Plugin.Host,
21         L = Y.Lang;
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 Plugin.Host.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      */
73     PluginHost.plug = function(hostClass, plugin, config) {
74         // Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
75         var p, i, l, name;
76     
77         if (hostClass !== Y.Base) {
78             hostClass._PLUG = hostClass._PLUG || {};
79     
80             if (!L.isArray(plugin)) {
81                 if (config) {
82                     plugin = {fn:plugin, cfg:config};
83                 }
84                 plugin = [plugin];
85             }
86     
87             for (i = 0, l = plugin.length; i < l;i++) {
88                 p = plugin[i];
89                 name = p.NAME || p.fn.NAME;
90                 hostClass._PLUG[name] = p;
91             }
92         }
93     };
94
95     /**
96      * Unregisters any class level plugins which have been registered by the host class, or any
97      * other class in the hierarchy.
98      *
99      * @method Plugin.Host.unplug
100      * @static
101      *
102      * @param {Function} hostClass The host class from which to unregister the plugins
103      * @param {Function | Array} plugin The plugin class, or an array of plugin classes
104      */
105     PluginHost.unplug = function(hostClass, plugin) {
106         var p, i, l, name;
107     
108         if (hostClass !== Y.Base) {
109             hostClass._UNPLUG = hostClass._UNPLUG || {};
110     
111             if (!L.isArray(plugin)) {
112                 plugin = [plugin];
113             }
114     
115             for (i = 0, l = plugin.length; i < l; i++) {
116                 p = plugin[i];
117                 name = p.NAME;
118                 if (!hostClass._PLUG[name]) {
119                     hostClass._UNPLUG[name] = p;
120                 } else {
121                     delete hostClass._PLUG[name];
122                 }
123             }
124         }
125     };
126
127
128 }, '3.3.0' ,{requires:['pluginhost-base']});