2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
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 Plugin.superclass.constructor.apply(this, arguments);
28 * Object defining the set of attributes supported by the Plugin.Base class
30 * @property Plugin.Base.ATTRS
37 * The plugin's host object.
49 * The string identifying the Plugin.Base class. Plugins extending
50 * Plugin.Base should set their own NAME value.
52 * @property Plugin.Base.NAME
56 Plugin.NAME = 'plugin';
59 * The name of the property the the plugin will be attached to
60 * when plugged into a Plugin Host. Plugins extending Plugin.Base,
61 * should set their own NS value.
69 Y.extend(Plugin, Y.Base, {
72 * The list of event handles for event listeners or AOP injected methods
73 * applied by the plugin to the host object.
83 * Initializer lifecycle implementation.
86 * @param {Object} config Configuration object with property name/value pairs.
88 initializer : function(config) {
93 * Destructor lifecycle implementation.
95 * Removes any event listeners or injected methods applied by the Plugin
99 destructor: function() {
100 // remove all handles
102 for (var i = 0, l = this._handles.length; i < l; i++) {
103 this._handles[i].detach();
109 * Listens for the "on" moment of events fired by the host,
110 * or injects code "before" a given method on the host.
114 * @param sFn {String} The event to listen for, or method to inject logic before.
115 * @param fn {Function} The handler function. For events, the "on" moment listener. For methods, the function to execute before the given method is executed.
116 * @param context {Object} An optional context to call the handler with. The default context is the plugin instance.
117 * @return handle {EventHandle} The detach handle for the handler.
119 doBefore: function(sFn, fn, context) {
120 var host = this.get("host"),
123 context = context || this;
125 if (sFn in host) { // method
126 handle = Y.Do.before(fn, host, sFn, context);
127 } else if (host.on) { // event
128 handle = host.on(sFn, fn, context);
131 this._handles.push(handle);
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 sFn {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 handler.
146 doAfter: function(sFn, fn, context) {
147 var host = this.get("host"),
150 context = context || this;
152 if (sFn in host) { // method
153 handle = Y.Do.after(fn, host, sFn, context);
154 } else if (host.after) { // event
155 handle = host.after(sFn, fn, context);
158 this._handles.push(handle);
162 toString: function() {
163 return this.constructor.NAME + '[' + this.constructor.NS + ']';
167 Y.namespace("Plugin").Base = Plugin;
170 }, '3.0.0' ,{requires:['base-base']});