1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 3.0.0pr2
*/
YUI.add('plugin', function(Y) {
/**
* Provides the base Plugin class for building widget plugins.
*
* @module plugin
*/
/**
* Plugin provides a base class for all Plugin classes.
*
* @class Plugin
* @extends Base
* @param {Object} config The configuration object for the
* plugin.
*/
function Plugin(config) {
Plugin.superclass.constructor.apply(this, arguments);
}
/**
* Static property provides a string to identify the class.
*
* @property Plugin.NAME
* @type {String}
* @static
*/
Plugin.NAME = 'plugin';
/**
* Static property provides the namespace the plugin will be
* registered under.
*
* @property Plugin.NS
* @type {String}
* @static
*/
Plugin.NS = 'plugin';
var proto = {
_handles: null,
/**
* Initializer lifecycle implementation.
*
* @method initializer
* @param {Object} config Configuration object literal for the plugin
*/
initializer : function(config) {
if (config.owner) {
this._owner = config.owner;
} else {
}
this._handles = [];
},
/**
* desctructor lifecycle implementation.
*
* Removes any listeners attached by the Plugin and restores
* and over-ridden methods.
*
* @method destructor
*/
destructor: function() {
// remove all handles
if (this._handles) {
for (var i = 0, l = this._handles.length; i < l; i++) {
this._handles[i].detach();
}
}
},
/**
* Listens for events and methods fired by the owner widget.
* The handler is called before the event handler or method is called.
* @method doBefore
* @param sFn The event of method to listen for.
* @param fn The handler function to call when the listener fires.
* @param context An optional context to call the handler with.
* Default context is the plugin instance.
* @return Handle A handle that can be used to detach the handler (e.g. "handle.detach()").
*/
doBefore: function(sFn, fn, context) {
var owner = this._owner,
handle;
context = context || this;
if (sFn in owner) { // method
handle = Y.Do.before(fn, this._owner, sFn, context);
} else if (owner.on) { // event
handle = owner.on(sFn, fn, context);
}
this._handles.push(handle);
return handle;
},
/**
* Listens for events and methods fired by the owner widget.
* The handler is called after the event handler or method is called.
* @method doAfter
* @param sFn The event of method to listen for.
* @param fn The handler function to call when the listener fires.
* @param context An optional context to call the handler with.
* Default context is the plugin instance.
* @return Handle A handle that can be used to detach the handler (e.g. "handle.detach()").
*/
doAfter: function(sFn, fn, context) {
var owner = this._owner,
handle;
context = context || this;
if (sFn in owner) { // method
handle = Y.Do.after(fn, this._owner, sFn, context);
} else if (owner.after) { // event
handle = owner.after(sFn, fn, context);
}
this._handles.push(handle);
return handle;
},
toString: function() {
return this.constructor.NAME + '[' + this.constructor.NS + ']';
}
};
Y.extend(Plugin, Y.Base, proto);
Y.Plugin = Plugin;
}, '3.0.0pr2' ,{requires:['base']});
|