~bac/juju-gui/trunkcopy

« back to all changes in this revision

Viewing changes to lib/yui/build/plugin/plugin-debug.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
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('plugin', function(Y) {
8
 
 
9
 
    /**
10
 
     * Provides the base Plugin class, which plugin developers should extend, when creating custom plugins
11
 
     *
12
 
     * @module plugin
13
 
     */
14
 
 
15
 
    /**
16
 
     * The base class for all Plugin instances.
17
 
     *
18
 
     * @class Plugin.Base 
19
 
     * @extends Base
20
 
     * @param {Object} config Configuration object with property name/value pairs.
21
 
     */
22
 
    function Plugin(config) {
23
 
        if (! (this.hasImpl && this.hasImpl(Y.Plugin.Base)) ) {
24
 
            Plugin.superclass.constructor.apply(this, arguments);
25
 
        } else {
26
 
            Plugin.prototype.initializer.apply(this, arguments);
27
 
        }
28
 
    }
29
 
 
30
 
    /**
31
 
     * Object defining the set of attributes supported by the Plugin.Base class
32
 
     * 
33
 
     * @property ATTRS
34
 
     * @type Object
35
 
     * @static
36
 
     */
37
 
    Plugin.ATTRS = {
38
 
 
39
 
        /**
40
 
         * The plugin's host object.
41
 
         *
42
 
         * @attribute host
43
 
         * @writeonce
44
 
         * @type Plugin.Host
45
 
         */
46
 
        host : {
47
 
            writeOnce: true
48
 
        }
49
 
    };
50
 
 
51
 
    /**
52
 
     * The string identifying the Plugin.Base class. Plugins extending
53
 
     * Plugin.Base should set their own NAME value.
54
 
     *
55
 
     * @property NAME
56
 
     * @type String
57
 
     * @static
58
 
     */
59
 
    Plugin.NAME = 'plugin';
60
 
 
61
 
    /**
62
 
     * The name of the property the the plugin will be attached to
63
 
     * when plugged into a Plugin Host. Plugins extending Plugin.Base,
64
 
     * should set their own NS value.
65
 
     *
66
 
     * @property NS
67
 
     * @type String
68
 
     * @static
69
 
     */
70
 
    Plugin.NS = 'plugin';
71
 
 
72
 
    Y.extend(Plugin, Y.Base, {
73
 
 
74
 
        /**
75
 
         * The list of event handles for event listeners or AOP injected methods
76
 
         * applied by the plugin to the host object.
77
 
         *
78
 
         * @property _handles
79
 
         * @private
80
 
         * @type Array
81
 
         * @value null
82
 
         */
83
 
        _handles: null,
84
 
 
85
 
        /**
86
 
         * Initializer lifecycle implementation.
87
 
         *
88
 
         * @method initializer
89
 
         * @param {Object} config Configuration object with property name/value pairs.
90
 
         */
91
 
        initializer : function(config) {
92
 
            if (!this.get("host")) { Y.log('No host defined for plugin ' + this, 'warn', 'Plugin');}
93
 
            this._handles = [];
94
 
            Y.log('Initializing: ' + this.constructor.NAME, 'info', 'Plugin');
95
 
        },
96
 
 
97
 
        /**
98
 
         * Destructor lifecycle implementation.
99
 
         *
100
 
         * Removes any event listeners or injected methods applied by the Plugin
101
 
         *
102
 
         * @method destructor
103
 
         */
104
 
        destructor: function() {
105
 
            // remove all handles
106
 
            if (this._handles) {
107
 
                for (var i = 0, l = this._handles.length; i < l; i++) {
108
 
                   this._handles[i].detach();
109
 
                }
110
 
            }
111
 
        },
112
 
 
113
 
        /**
114
 
         * Listens for the "on" moment of events fired by the host, 
115
 
         * or injects code "before" a given method on the host.
116
 
         *
117
 
         * @method doBefore
118
 
         *
119
 
         * @param strMethod {String} The event to listen for, or method to inject logic before.
120
 
         * @param fn {Function} The handler function. For events, the "on" moment listener. For methods, the function to execute before the given method is executed.
121
 
         * @param context {Object} An optional context to call the handler with. The default context is the plugin instance.
122
 
         * @return handle {EventHandle} The detach handle for the handler.
123
 
         */
124
 
        doBefore: function(strMethod, fn, context) {
125
 
            var host = this.get("host"), handle;
126
 
 
127
 
            if (strMethod in host) { // method
128
 
                handle = this.beforeHostMethod(strMethod, fn, context);
129
 
            } else if (host.on) { // event
130
 
                handle = this.onHostEvent(strMethod, fn, context);
131
 
            }
132
 
 
133
 
            return handle;
134
 
        },
135
 
 
136
 
        /**
137
 
         * Listens for the "after" moment of events fired by the host, 
138
 
         * or injects code "after" a given method on the host.
139
 
         *
140
 
         * @method doAfter
141
 
         *
142
 
         * @param strMethod {String} The event to listen for, or method to inject logic after.
143
 
         * @param fn {Function} The handler function. For events, the "after" moment listener. For methods, the function to execute after the given method is executed.
144
 
         * @param context {Object} An optional context to call the handler with. The default context is the plugin instance.
145
 
         * @return handle {EventHandle} The detach handle for the listener.
146
 
         */
147
 
        doAfter: function(strMethod, fn, context) {
148
 
            var host = this.get("host"), handle;
149
 
 
150
 
            if (strMethod in host) { // method
151
 
                handle = this.afterHostMethod(strMethod, fn, context);
152
 
            } else if (host.after) { // event
153
 
                handle = this.afterHostEvent(strMethod, fn, context);
154
 
            }
155
 
 
156
 
            return handle;
157
 
        },
158
 
 
159
 
        /**
160
 
         * Listens for the "on" moment of events fired by the host object.
161
 
         *
162
 
         * Listeners attached through this method will be detached when the plugin is unplugged.
163
 
         * 
164
 
         * @method onHostEvent
165
 
         * @param {String | Object} type The event type.
166
 
         * @param {Function} fn The listener.
167
 
         * @param {Object} context The execution context. Defaults to the plugin instance.
168
 
         * @return handle {EventHandle} The detach handle for the listener. 
169
 
         */
170
 
        onHostEvent : function(type, fn, context) {
171
 
            var handle = this.get("host").on(type, fn, context || this);
172
 
            this._handles.push(handle);
173
 
            return handle;
174
 
        },
175
 
 
176
 
        /**
177
 
         * Listens for the "after" moment of events fired by the host object.
178
 
         *
179
 
         * Listeners attached through this method will be detached when the plugin is unplugged.
180
 
         * 
181
 
         * @method afterHostEvent
182
 
         * @param {String | Object} type The event type.
183
 
         * @param {Function} fn The listener.
184
 
         * @param {Object} context The execution context. Defaults to the plugin instance.
185
 
         * @return handle {EventHandle} The detach handle for the listener. 
186
 
         */
187
 
        afterHostEvent : function(type, fn, context) {
188
 
            var handle = this.get("host").after(type, fn, context || this);
189
 
            this._handles.push(handle);
190
 
            return handle;
191
 
        },
192
 
 
193
 
        /**
194
 
         * Injects a function to be executed before a given method on host object.
195
 
         *
196
 
         * The function will be detached when the plugin is unplugged.
197
 
         *
198
 
         * @method beforeHostMethod
199
 
         * @param {String} method The name of the method to inject the function before.
200
 
         * @param {Function} fn The function to inject.
201
 
         * @param {Object} context The execution context. Defaults to the plugin instance.
202
 
         * @return handle {EventHandle} The detach handle for the injected function. 
203
 
         */
204
 
        beforeHostMethod : function(strMethod, fn, context) {
205
 
            var handle = Y.Do.before(fn, this.get("host"), strMethod, context || this);
206
 
            this._handles.push(handle);
207
 
            return handle;
208
 
        },
209
 
 
210
 
        /**
211
 
         * Injects a function to be executed after a given method on host object.
212
 
         *
213
 
         * The function will be detached when the plugin is unplugged.
214
 
         *
215
 
         * @method afterHostMethod
216
 
         * @param {String} method The name of the method to inject the function after.
217
 
         * @param {Function} fn The function to inject.
218
 
         * @param {Object} context The execution context. Defaults to the plugin instance.
219
 
         * @return handle {EventHandle} The detach handle for the injected function. 
220
 
         */
221
 
        afterHostMethod : function(strMethod, fn, context) {
222
 
            var handle = Y.Do.after(fn, this.get("host"), strMethod, context || this);
223
 
            this._handles.push(handle);
224
 
            return handle;
225
 
        },
226
 
 
227
 
        toString: function() {
228
 
            return this.constructor.NAME + '[' + this.constructor.NS + ']';
229
 
        }
230
 
    });
231
 
 
232
 
    Y.namespace("Plugin").Base = Plugin;
233
 
 
234
 
 
235
 
}, '3.5.1' ,{requires:['base-base']});