2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('plugin', function(Y) {
11 * Provides the base Plugin class, which plugin developers should extend, when creating custom plugins
17 * The base class for all Plugin instances.
21 * @param {Object} config Configuration object with property name/value pairs.
23 function Plugin(config) {
24 if (! (this.hasImpl && this.hasImpl(Y.Plugin.Base)) ) {
25 Plugin.superclass.constructor.apply(this, arguments);
27 Plugin.prototype.initializer.apply(this, arguments);
32 * Object defining the set of attributes supported by the Plugin.Base class
34 * @property Plugin.Base.ATTRS
41 * The plugin's host object.
53 * The string identifying the Plugin.Base class. Plugins extending
54 * Plugin.Base should set their own NAME value.
56 * @property Plugin.Base.NAME
60 Plugin.NAME = 'plugin';
63 * The name of the property the the plugin will be attached to
64 * when plugged into a Plugin Host. Plugins extending Plugin.Base,
65 * should set their own NS value.
73 Y.extend(Plugin, Y.Base, {
76 * The list of event handles for event listeners or AOP injected methods
77 * applied by the plugin to the host object.
87 * Initializer lifecycle implementation.
90 * @param {Object} config Configuration object with property name/value pairs.
92 initializer : function(config) {
97 * Destructor lifecycle implementation.
99 * Removes any event listeners or injected methods applied by the Plugin
103 destructor: function() {
104 // remove all handles
106 for (var i = 0, l = this._handles.length; i < l; i++) {
107 this._handles[i].detach();
113 * Listens for the "on" moment of events fired by the host,
114 * or injects code "before" a given method on the host.
118 * @param strMethod {String} The event to listen for, or method to inject logic before.
119 * @param fn {Function} The handler function. For events, the "on" moment listener. For methods, the function to execute before the given method is executed.
120 * @param context {Object} An optional context to call the handler with. The default context is the plugin instance.
121 * @return handle {EventHandle} The detach handle for the handler.
123 doBefore: function(strMethod, fn, context) {
124 var host = this.get("host"), handle;
126 if (strMethod in host) { // method
127 handle = this.beforeHostMethod(strMethod, fn, context);
128 } else if (host.on) { // event
129 handle = this.onHostEvent(strMethod, fn, context);
136 * Listens for the "after" moment of events fired by the host,
137 * or injects code "after" a given method on the host.
141 * @param strMethod {String} The event to listen for, or method to inject logic after.
142 * @param fn {Function} The handler function. For events, the "after" moment listener. For methods, the function to execute after the given method is executed.
143 * @param context {Object} An optional context to call the handler with. The default context is the plugin instance.
144 * @return handle {EventHandle} The detach handle for the listener.
146 doAfter: function(strMethod, fn, context) {
147 var host = this.get("host"), handle;
149 if (strMethod in host) { // method
150 handle = this.afterHostMethod(strMethod, fn, context);
151 } else if (host.after) { // event
152 handle = this.afterHostEvent(strMethod, fn, context);
159 * Listens for the "on" moment of events fired by the host object.
161 * Listeners attached through this method will be detached when the plugin is unplugged.
163 * @method onHostEvent
164 * @param {String | Object} type The event type.
165 * @param {Function} fn The listener.
166 * @param {Object} context The execution context. Defaults to the plugin instance.
167 * @return handle {EventHandle} The detach handle for the listener.
169 onHostEvent : function(type, fn, context) {
170 var handle = this.get("host").on(type, fn, context || this);
171 this._handles.push(handle);
176 * Listens for the "after" moment of events fired by the host object.
178 * Listeners attached through this method will be detached when the plugin is unplugged.
180 * @method afterHostEvent
181 * @param {String | Object} type The event type.
182 * @param {Function} fn The listener.
183 * @param {Object} context The execution context. Defaults to the plugin instance.
184 * @return handle {EventHandle} The detach handle for the listener.
186 afterHostEvent : function(type, fn, context) {
187 var handle = this.get("host").after(type, fn, context || this);
188 this._handles.push(handle);
193 * Injects a function to be executed before a given method on host object.
195 * The function will be detached when the plugin is unplugged.
197 * @method beforeHostMethod
198 * @param {String} method The name of the method to inject the function before.
199 * @param {Function} fn The function to inject.
200 * @param {Object} context The execution context. Defaults to the plugin instance.
201 * @return handle {EventHandle} The detach handle for the injected function.
203 beforeHostMethod : function(strMethod, fn, context) {
204 var handle = Y.Do.before(fn, this.get("host"), strMethod, context || this);
205 this._handles.push(handle);
210 * Injects a function to be executed after a given method on host object.
212 * The function will be detached when the plugin is unplugged.
214 * @method afterHostMethod
215 * @param {String} method The name of the method to inject the function after.
216 * @param {Function} fn The function to inject.
217 * @param {Object} context The execution context. Defaults to the plugin instance.
218 * @return handle {EventHandle} The detach handle for the injected function.
220 afterHostMethod : function(strMethod, fn, context) {
221 var handle = Y.Do.after(fn, this.get("host"), strMethod, context || this);
222 this._handles.push(handle);
226 toString: function() {
227 return this.constructor.NAME + '[' + this.constructor.NS + ']';
231 Y.namespace("Plugin").Base = Plugin;
234 }, '3.3.0' ,{requires:['base-base']});