3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('plugin', function(Y) {
10
* Provides the base Plugin class, which plugin developers should extend, when creating custom plugins
16
* The base class for all Plugin instances.
20
* @param {Object} config Configuration object with property name/value pairs.
22
function Plugin(config) {
23
if (! (this.hasImpl && this.hasImpl(Y.Plugin.Base)) ) {
24
Plugin.superclass.constructor.apply(this, arguments);
26
Plugin.prototype.initializer.apply(this, arguments);
31
* Object defining the set of attributes supported by the Plugin.Base class
40
* The plugin's host object.
52
* The string identifying the Plugin.Base class. Plugins extending
53
* Plugin.Base should set their own NAME value.
59
Plugin.NAME = 'plugin';
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.
72
Y.extend(Plugin, Y.Base, {
75
* The list of event handles for event listeners or AOP injected methods
76
* applied by the plugin to the host object.
86
* Initializer lifecycle implementation.
89
* @param {Object} config Configuration object with property name/value pairs.
91
initializer : function(config) {
92
if (!this.get("host")) { Y.log('No host defined for plugin ' + this, 'warn', 'Plugin');}
94
Y.log('Initializing: ' + this.constructor.NAME, 'info', 'Plugin');
98
* Destructor lifecycle implementation.
100
* Removes any event listeners or injected methods applied by the Plugin
104
destructor: function() {
105
// remove all handles
107
for (var i = 0, l = this._handles.length; i < l; i++) {
108
this._handles[i].detach();
114
* Listens for the "on" moment of events fired by the host,
115
* or injects code "before" a given method on the host.
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.
124
doBefore: function(strMethod, fn, context) {
125
var host = this.get("host"), handle;
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);
137
* Listens for the "after" moment of events fired by the host,
138
* or injects code "after" a given method on the host.
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.
147
doAfter: function(strMethod, fn, context) {
148
var host = this.get("host"), handle;
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);
160
* Listens for the "on" moment of events fired by the host object.
162
* Listeners attached through this method will be detached when the plugin is unplugged.
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.
170
onHostEvent : function(type, fn, context) {
171
var handle = this.get("host").on(type, fn, context || this);
172
this._handles.push(handle);
177
* Listens for the "after" moment of events fired by the host object.
179
* Listeners attached through this method will be detached when the plugin is unplugged.
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.
187
afterHostEvent : function(type, fn, context) {
188
var handle = this.get("host").after(type, fn, context || this);
189
this._handles.push(handle);
194
* Injects a function to be executed before a given method on host object.
196
* The function will be detached when the plugin is unplugged.
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.
204
beforeHostMethod : function(strMethod, fn, context) {
205
var handle = Y.Do.before(fn, this.get("host"), strMethod, context || this);
206
this._handles.push(handle);
211
* Injects a function to be executed after a given method on host object.
213
* The function will be detached when the plugin is unplugged.
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.
221
afterHostMethod : function(strMethod, fn, context) {
222
var handle = Y.Do.after(fn, this.get("host"), strMethod, context || this);
223
this._handles.push(handle);
227
toString: function() {
228
return this.constructor.NAME + '[' + this.constructor.NS + ']';
232
Y.namespace("Plugin").Base = Plugin;
235
}, '3.5.1' ,{requires:['base-base']});