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