Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('pluginhost-base', function (Y, NAME) {
2
 
3
    /**
4
     * Provides the augmentable PluginHost interface, which can be added to any class.
5
     * @module pluginhost
6
     */
7
 
8
    /**
9
     * Provides the augmentable PluginHost interface, which can be added to any class.
10
     * @module pluginhost-base
11
     */
12
 
13
    /**
14
     * <p>
15
     * An augmentable class, which provides the augmented class with the ability to host plugins.
16
     * It adds <a href="#method_plug">plug</a> and <a href="#method_unplug">unplug</a> methods to the augmented class, which can
17
     * be used to add or remove plugins from instances of the class.
18
     * </p>
19
     *
20
     * <p>Plugins can also be added through the constructor configuration object passed to the host class' constructor using
21
     * the "plugins" property. Supported values for the "plugins" property are those defined by the <a href="#method_plug">plug</a> method.
22
     *
23
     * For example the following code would add the AnimPlugin and IOPlugin to Overlay (the plugin host):
24
     * <xmp>
25
     * var o = new Overlay({plugins: [ AnimPlugin, {fn:IOPlugin, cfg:{section:"header"}}]});
26
     * </xmp>
27
     * </p>
28
     * <p>
29
     * Plug.Host's protected <a href="#method_initPlugins">_initPlugins</a> and <a href="#method_destroyPlugins">_destroyPlugins</a>
30
     * methods should be invoked by the host class at the appropriate point in the host's lifecyle.
31
     * </p>
32
     *
33
     * @class Plugin.Host
34
     */
35
 
36
    var L = Y.Lang;
37
 
38
    function PluginHost() {
39
        this._plugins = {};
40
    }
41
 
42
    PluginHost.prototype = {
43
 
44
        /**
45
         * Adds a plugin to the host object. This will instantiate the
46
         * plugin and attach it to the configured namespace on the host object.
47
         *
48
         * @method plug
49
         * @chainable
50
         * @param P {Function | Object |Array} Accepts the plugin class, or an
51
         * object with a "fn" property specifying the plugin class and
52
         * a "cfg" property specifying the configuration for the Plugin.
53
         * <p>
54
         * Additionally an Array can also be passed in, with the above function or
55
         * object values, allowing the user to add multiple plugins in a single call.
56
         * </p>
57
         * @param config (Optional) If the first argument is the plugin class, the second argument
58
         * can be the configuration for the plugin.
59
         * @return {Base} A reference to the host object
60
         */
61
        plug: function(Plugin, config) {
62
            var i, ln, ns;
63
 
64
            if (L.isArray(Plugin)) {
65
                for (i = 0, ln = Plugin.length; i < ln; i++) {
66
                    this.plug(Plugin[i]);
67
                }
68
            } else {
69
                if (Plugin && !L.isFunction(Plugin)) {
70
                    config = Plugin.cfg;
71
                    Plugin = Plugin.fn;
72
                }
73
 
74
                // Plugin should be fn by now
75
                if (Plugin && Plugin.NS) {
76
                    ns = Plugin.NS;
77
 
78
                    config = config || {};
79
                    config.host = this;
80
 
81
                    if (this.hasPlugin(ns)) {
82
                        // Update config
83
                        if (this[ns].setAttrs) {
84
                            this[ns].setAttrs(config);
85
                        }
86
                        else {
87
                            Y.log("Attempt to replug an already attached plugin, and we can't setAttrs, because it's not Attribute based: " + ns, "warn", "PluginHost");
88
                        }
89
                    } else {
90
                        // Create new instance
91
                        this[ns] = new Plugin(config);
92
                        this._plugins[ns] = Plugin;
93
                    }
94
                }
95
                else {
96
                    Y.log("Attempt to plug in an invalid plugin. Host:" + this + ", Plugin:" + Plugin, "error", "PluginHost");
97
                }
98
            }
99
            return this;
100
        },
101
 
102
        /**
103
         * Removes a plugin from the host object. This will destroy the
104
         * plugin instance and delete the namespace from the host object.
105
         *
106
         * @method unplug
107
         * @param {String | Function} plugin The namespace of the plugin, or the plugin class with the static NS namespace property defined. If not provided,
108
         * all registered plugins are unplugged.
109
         * @return {Base} A reference to the host object
110
         * @chainable
111
         */
112
        unplug: function(plugin) {
113
            var ns = plugin,
114
                plugins = this._plugins;
115
 
116
            if (plugin) {
117
                if (L.isFunction(plugin)) {
118
                    ns = plugin.NS;
119
                    if (ns && (!plugins[ns] || plugins[ns] !== plugin)) {
120
                        ns = null;
121
                    }
122
                }
123
 
124
                if (ns) {
125
                    if (this[ns]) {
126
                        if (this[ns].destroy) {
127
                            this[ns].destroy();
128
                        }
129
                        delete this[ns];
130
                    }
131
                    if (plugins[ns]) {
132
                        delete plugins[ns];
133
                    }
134
                }
135
            } else {
136
                for (ns in this._plugins) {
137
                    if (this._plugins.hasOwnProperty(ns)) {
138
                        this.unplug(ns);
139
                    }
140
                }
141
            }
142
            return this;
143
        },
144
 
145
        /**
146
         * Determines if a plugin has plugged into this host.
147
         *
148
         * @method hasPlugin
149
         * @param {String} ns The plugin's namespace
150
         * @return {Plugin} Returns a truthy value (the plugin instance) if present, or undefined if not.
151
         */
152
        hasPlugin : function(ns) {
153
            return (this._plugins[ns] && this[ns]);
154
        },
155
 
156
        /**
157
         * Initializes static plugins registered on the host (using the
158
         * Base.plug static method) and any plugins passed to the
159
         * instance through the "plugins" configuration property.
160
         *
161
         * @method _initPlugins
162
         * @param {Object} config The configuration object with property name/value pairs.
163
         * @private
164
         */
165
 
166
        _initPlugins: function(config) {
167
            this._plugins = this._plugins || {};
168
 
169
            if (this._initConfigPlugins) {
170
                this._initConfigPlugins(config);
171
            }
172
        },
173
 
174
        /**
175
         * Unplugs and destroys all plugins on the host
176
         * @method _destroyPlugins
177
         * @private
178
         */
179
        _destroyPlugins: function() {
180
            this.unplug();
181
        }
182
    };
183
 
184
    Y.namespace("Plugin").Host = PluginHost;
185
 
186
 
187
}, '3.18.1', {"requires": ["yui-base"]});