|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Plugin</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>Plugin</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p> |
|
|
32 |
Plugins allow you to unobtrusively add functionality to objects (referred to as the "host" object) such as nodes and widgets. |
|
|
33 |
Plugins can inherit from the <code>Plugin.Base</code> class, but this is not a hard requirement as we'll see later. |
|
|
34 |
</p> |
|
|
35 |
|
|
|
36 |
<p> |
|
|
37 |
Plugins are used to add atomic pieces of functionality or features to component instances (hosts), without having to bake support or even |
|
|
38 |
knowledge of the feature into the component class. This allows features to be mixed and matched per component instance, without having to build all |
|
|
39 |
features into a monolithic component class or having to ship multiple versions of the component class with varying combinations of features. |
|
|
40 |
</p> |
|
|
41 |
</div> |
|
|
42 |
|
|
|
43 |
<h2 id="getting-started">Getting Started</h2> |
|
|
44 |
|
|
|
45 |
<p> |
|
|
46 |
To include the source files for Plugin and its dependencies, first load |
|
|
47 |
the YUI seed file if you haven't already loaded it. |
|
|
48 |
</p> |
|
|
49 |
|
|
|
50 |
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script></pre> |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
<p> |
|
|
54 |
Next, create a new YUI instance for your application and populate it with the |
|
|
55 |
modules you need by specifying them as arguments to the <code>YUI().use()</code> method. |
|
|
56 |
YUI will automatically load any dependencies required by the modules you |
|
|
57 |
specify. |
|
|
58 |
</p> |
|
|
59 |
|
|
|
60 |
<pre class="code prettyprint"><script> |
|
|
61 |
// Create a new YUI instance and populate it with the required modules. |
|
|
62 |
YUI().use('plugin', function (Y) { |
|
|
63 |
// Plugin is available and ready for use. Add implementation |
|
|
64 |
// code here. |
|
|
65 |
}); |
|
|
66 |
</script></pre> |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
<p> |
|
|
70 |
For more information on creating YUI instances and on the |
|
|
71 |
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the |
|
|
72 |
documentation for the <a href="../yui/index.html">YUI Global Object</a>. |
|
|
73 |
</p> |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
<h2 id="using">Creating Plugins</h2> |
|
|
77 |
|
|
|
78 |
<h3 id="simple">Simple Plugins</h3> |
|
|
79 |
|
|
|
80 |
<p> |
|
|
81 |
For the most basic plugins, which don't have any events or attributes of their own, and which don't modify the behavior |
|
|
82 |
of the host by listening for any host events, or overriding any of the host's methods, plugins can simply be basic JavaScript classes. |
|
|
83 |
</p> |
|
|
84 |
|
|
|
85 |
<p> |
|
|
86 |
The only requirement for the class is that it has a static namespace property <code>NS</code> with a value assigned to it. |
|
|
87 |
The value of the <code>NS</code> property is used to define the property on the host instance which will refer to |
|
|
88 |
the plugin when it's plugged into the host. |
|
|
89 |
</p> |
|
|
90 |
|
|
|
91 |
<p> |
|
|
92 |
When plugins are plugged into a host instance a new instance of the plugin is created, |
|
|
93 |
and a reference to the host is added to the configuration object passed to the plugin's constructor, |
|
|
94 |
so that the plugin has a way to reference the host object. (similarly, when plugins are unplugged from a host |
|
|
95 |
object they are destroyed). |
|
|
96 |
</p> |
|
|
97 |
|
|
|
98 |
<p>So, putting this all together, a simple plugin class may look something like the following:</p> |
|
|
99 |
|
|
|
100 |
<pre class="code prettyprint">// This AnchorPlugin is designed to be added to Node instances (the host will be a Node instance) |
|
|
101 |
|
|
|
102 |
function AnchorPlugin(config) { |
|
|
103 |
|
|
|
104 |
// Hold onto the host instance (a Node in this case), |
|
|
105 |
// for other plugin methods to use. |
|
|
106 |
|
|
|
107 |
this._node = config.host; |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
// When plugged into a node instance, the plugin will be |
|
|
111 |
// available on the "anchors" property. |
|
|
112 |
AnchorPlugin.NS = "anchors" |
|
|
113 |
|
|
|
114 |
AnchorPlugin.prototype = { |
|
|
115 |
disable: function() { |
|
|
116 |
var node = this._node; |
|
|
117 |
var anchors = node.queryAll("a"); |
|
|
118 |
anchors.addClass("disabled"); |
|
|
119 |
anchors.setAttribute("disabled", true); |
|
|
120 |
} |
|
|
121 |
};</pre> |
|
|
122 |
|
|
|
123 |
|
|
|
124 |
<p>To use the <code>AnchorPlugin</code>, the user would plug it into a Node reference they were holding on to:</p> |
|
|
125 |
|
|
|
126 |
<pre class="code prettyprint">var container = Y.one("div.actions"); |
|
|
127 |
container.plug(AnchorPlugin);</pre> |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
<p>And invoke methods on the plugin, through the namespace it is bound to:</p> |
|
|
131 |
|
|
|
132 |
<pre class="code prettyprint">container.anchors.disable();</pre> |
|
|
133 |
|
|
|
134 |
|
|
|
135 |
<h3 id="advanced">Advanced Plugins</h3> |
|
|
136 |
|
|
|
137 |
<p>For basic features, simple plugin classes as described above may suffice. However, when you have more complex features which you'd like to encapsulate, the ability to use |
|
|
138 |
attributes and events for your plugin implementation becomes useful. More importantly, for many plugins, you'll be looking to modify the default |
|
|
139 |
behavior of the host instance in some way (for example an Animation Plugin may want to change the default show/hide behavior of a Widget, to be animated).</p> |
|
|
140 |
|
|
|
141 |
<p>For these richer plugins, you should extend the base plugin class <a href="http://yuilibrary.com/yui/docs/api/Plugin.Base.html"><code>Plugin.Base</code></a>. </p> |
|
|
142 |
|
|
|
143 |
<p><code>Plugin</code> is a subclass of <code>Base</code>, thereby providing managed attributes, lifecycle methods, and custom event support. Additionally it allows the plugin code to |
|
|
144 |
either listen for and react to events fired by the host or inject custom logic before or after methods invoked on the host object (through the YUI 3 <a href="http://yuilibrary.com/yui/docs/api/Do.html">AOP</a> infrastructure). |
|
|
145 |
<code>Plugin.Base</code> also sets up <code>host</code> as an attribute, so you can access it through <code>this.get("host")</code> in your plugin implementation code. |
|
|
146 |
</p> |
|
|
147 |
|
|
|
148 |
<h4 id="extendingplugin">Extending Plugin.Base</h4> |
|
|
149 |
|
|
|
150 |
<p>You can extend the <code>Plugin.Base</code> class, just as you would extend the <a href="../base/index.html"><code>Base</code></a> class. One thing to note when comparing this to simple plugins |
|
|
151 |
is that the host reference is automatically set up as an attribute by the <code>Plugin.Base</code> class, so a reference to it does not need to be set up explicitly.</p> |
|
|
152 |
|
|
|
153 |
<p>The class structure for an advanced plugin follows the pattern for all classes derived from Base, with the addition of the <code>NS</code> property to define |
|
|
154 |
the namespace for the plugin (see the <a href="../base/index.html">Base</a> documentation for details about NAME and ATTRS).</p> |
|
|
155 |
|
|
|
156 |
|
|
|
157 |
<pre class="code prettyprint">// A plugin class designed to animate Widget's show and hide methods. |
|
|
158 |
function WidgetAnimPlugin(config) { |
|
|
159 |
WidgetAnimPlugin.superclass.constructor.apply(this, arguments); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
// Define Static properties NAME (to identify the class) and NS (to identify the namespace) |
|
|
163 |
WidgetAnimPlugin.NAME = 'widgetAnimPlugin'; |
|
|
164 |
WidgetAnimPlugin.NS = 'fx'; |
|
|
165 |
|
|
|
166 |
// Attribute definitions for the plugin |
|
|
167 |
WidgetAnimPlugin.ATTRS = { |
|
|
168 |
|
|
|
169 |
animHidden : { |
|
|
170 |
... |
|
|
171 |
}, |
|
|
172 |
|
|
|
173 |
animVisible: { |
|
|
174 |
... |
|
|
175 |
} |
|
|
176 |
}; |
|
|
177 |
|
|
|
178 |
// Extend Plugin.Base |
|
|
179 |
Y.extend(WidgetAnimPlugin, Y.Plugin.Base, { |
|
|
180 |
|
|
|
181 |
// Add any required prototype methods |
|
|
182 |
|
|
|
183 |
});</pre> |
|
|
184 |
|
|
|
185 |
|
|
|
186 |
<p>The plugin class structure described above is captured in this <a href="../assets/plugin/myplugin.js.txt">"MyPlugin" Template File</a>, which you can use as a starting point to create your own plugins derived from <code>Plugin.Base</code>.</p> |
|
|
187 |
|
|
|
188 |
<h4 id="pluginlisteners">Plugin Listeners</h4> |
|
|
189 |
|
|
|
190 |
<p>The main value obtained by extending <code>Plugin.Base</code> is the ability to react to events fired by the host |
|
|
191 |
using <code>Plugin.Base</code>'s <a href="http://yuilibrary.com/yui/docs/api/Plugin.Base.html#method_onHostEvent"><code>onHostEvent</code></a> and <a href="http://yuilibrary.com/yui/docs/api/Plugin.Base.html#method_afterHostEvent"><code>afterHostEvent</code></a> methods, or |
|
|
192 |
modify methods on the host, using <a href="http://yuilibrary.com/yui/docs/api/Plugin.Base.html#method_beforeHostMethod"><code>beforeHostMethod</code></a> and <a href="http://yuilibrary.com/yui/docs/api/Plugin.Base.html#method_afterHostMethod"><code>afterHostMethod</code></a>.</p> |
|
|
193 |
|
|
|
194 |
<p>The value of doing this through the above <code>Plugin.Base</code> methods as opposed to working with the host directly, is that any listeners added by the plugin using the above methods are removed when the plugin is unplugged. |
|
|
195 |
This is important. Plugins should clean up after themselves when unplugged from the host.</p> |
|
|
196 |
|
|
|
197 |
<h5 id="events">Events</h5> |
|
|
198 |
|
|
|
199 |
<p>As mentioned, plugins which derive from <code>Plugin.Base</code> have the ability to listen for events on the host object and react to them.</p> |
|
|
200 |
|
|
|
201 |
<p>For example, all widgets fire a <code>render</code> event when they are rendered. Your widget-specific plugin may need to know when this occurs, |
|
|
202 |
so that it can inject custom elements into the markup the host renders. It can do this through the <code>afterHostEvent</code> method:</p> |
|
|
203 |
|
|
|
204 |
<pre class="code prettyprint">// A plugin which introduces rounded corners to a widget. |
|
|
205 |
function RoundedCornersPlugin(config) { |
|
|
206 |
//... |
|
|
207 |
} |
|
|
208 |
|
|
|
209 |
RoundedCornersPlugin.NAME = 'roundedCornersPlugin'; |
|
|
210 |
RoundedCornersPlugin.NS = 'corners'; |
|
|
211 |
|
|
|
212 |
Y.extend(RoundedCornersPlugin, Y.Plugin.Base, { |
|
|
213 |
|
|
|
214 |
// Automatically called by Base, during construction |
|
|
215 |
initializer: function(config) { |
|
|
216 |
// "render" is a widget event |
|
|
217 |
this.afterHostEvent('render', this.insertCornerElements); |
|
|
218 |
}, |
|
|
219 |
|
|
|
220 |
insertCornerElements: function() { |
|
|
221 |
var widget = this.get("host"); |
|
|
222 |
var boundingBox = widget.get("boundingBox"); |
|
|
223 |
|
|
|
224 |
var tl = Y.Node.create(TL_TEMPLATE); |
|
|
225 |
//... |
|
|
226 |
|
|
|
227 |
boundingBox.appendChild(tlNode); |
|
|
228 |
boundingBox.appendChild(trNode); |
|
|
229 |
boundingBox.appendChild(blNode); |
|
|
230 |
boundingBox.appendChild(brNode); |
|
|
231 |
} |
|
|
232 |
});</pre> |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
<h5 id="methods">Methods</h5> |
|
|
236 |
|
|
|
237 |
<p>In some cases, your plugin may need to override the logic in the host class' methods. The <code>beforeHostMethod</code> and <code>afterHostMethod</code> methods provided by <code>Plugin.Base</code> |
|
|
238 |
allow you to insert custom plugin logic before or after a method is executed on the host object.</p> |
|
|
239 |
|
|
|
240 |
<p>For example, to animate the way a widget is shown or hidden, we may need to override the method |
|
|
241 |
which actually flips the visibility style attribute on the widget's bounding box and replace it with an animated opacity implementation, |
|
|
242 |
as shown below:</p> |
|
|
243 |
|
|
|
244 |
<pre class="code prettyprint">// A plugin class designed to animate Widget's show and hide methods. |
|
|
245 |
function WidgetAnimPlugin(config) { |
|
|
246 |
//... |
|
|
247 |
} |
|
|
248 |
|
|
|
249 |
WidgetAnimPlugin.NAME = 'widgetAnimPlugin'; |
|
|
250 |
WidgetAnimPlugin.NS = 'fx'; |
|
|
251 |
|
|
|
252 |
WidgetAnimPlugin.ATTRS = { |
|
|
253 |
|
|
|
254 |
animHidden : { |
|
|
255 |
//... |
|
|
256 |
}, |
|
|
257 |
|
|
|
258 |
animVisible: { |
|
|
259 |
//... |
|
|
260 |
} |
|
|
261 |
}; |
|
|
262 |
|
|
|
263 |
// Extend Plugin.Base, and override the default |
|
|
264 |
// method _uiSetVisible, used by Widget to flip the visibility |
|
|
265 |
Y.extend(WidgetAnimPlugin, Y.Plugin.Base, { |
|
|
266 |
|
|
|
267 |
initializer : function(config) { |
|
|
268 |
|
|
|
269 |
// Override Widget's _uiSetVisible method, with the custom animated method |
|
|
270 |
this.beforeHostMethod("_uiSetVisible", this._uiAnimSetVisible); |
|
|
271 |
}, |
|
|
272 |
|
|
|
273 |
_uiAnimSetVisible : function(show) { |
|
|
274 |
// Instead of flipping visibility, use the animation |
|
|
275 |
// instances configured for the plugin to animate |
|
|
276 |
// hide/show. |
|
|
277 |
if (this.get("host").get("rendered")) { |
|
|
278 |
if (show) { |
|
|
279 |
this.get("animHidden").stop(); |
|
|
280 |
this.get("animVisible").run(); |
|
|
281 |
} else { |
|
|
282 |
this.get("animVisible").stop(); |
|
|
283 |
this.get("animHidden").run(); |
|
|
284 |
} |
|
|
285 |
|
|
|
286 |
// Prevent the default method from being invoked. |
|
|
287 |
return new Y.Do.Prevent(); |
|
|
288 |
} |
|
|
289 |
} |
|
|
290 |
});</pre> |
|
|
291 |
|
|
|
292 |
</div> |
|
|
293 |
</div> |
|
|
294 |
</div> |
|
|
295 |
|
|
|
296 |
<div class="yui3-u-1-4"> |
|
|
297 |
<div class="sidebar"> |
|
|
298 |
|
|
|
299 |
<div id="toc" class="sidebox"> |
|
|
300 |
<div class="hd"> |
|
|
301 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
302 |
</div> |
|
|
303 |
|
|
|
304 |
<div class="bd"> |
|
|
305 |
<ul class="toc"> |
|
|
306 |
<li> |
|
|
307 |
<a href="#getting-started">Getting Started</a> |
|
|
308 |
</li> |
|
|
309 |
<li> |
|
|
310 |
<a href="#using">Creating Plugins</a> |
|
|
311 |
<ul class="toc"> |
|
|
312 |
<li> |
|
|
313 |
<a href="#simple">Simple Plugins</a> |
|
|
314 |
</li> |
|
|
315 |
<li> |
|
|
316 |
<a href="#advanced">Advanced Plugins</a> |
|
|
317 |
<ul class="toc"> |
|
|
318 |
<li> |
|
|
319 |
<a href="#extendingplugin">Extending Plugin.Base</a> |
|
|
320 |
</li> |
|
|
321 |
<li> |
|
|
322 |
<a href="#pluginlisteners">Plugin Listeners</a> |
|
|
323 |
<ul class="toc"> |
|
|
324 |
<li> |
|
|
325 |
<a href="#events">Events</a> |
|
|
326 |
</li> |
|
|
327 |
<li> |
|
|
328 |
<a href="#methods">Methods</a> |
|
|
329 |
</li> |
|
|
330 |
</ul> |
|
|
331 |
</li> |
|
|
332 |
</ul> |
|
|
333 |
</li> |
|
|
334 |
</ul> |
|
|
335 |
</li> |
|
|
336 |
</ul> |
|
|
337 |
</div> |
|
|
338 |
</div> |
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
|
344 |
<div class="sidebox"> |
|
|
345 |
<div class="hd"> |
|
|
346 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
347 |
</div> |
|
|
348 |
|
|
|
349 |
<div class="bd"> |
|
|
350 |
<ul class="examples"> |
|
|
351 |
|
|
|
352 |
|
|
|
353 |
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility."> |
|
|
354 |
<a href="../overlay/overlay-io-plugin.html">IO Plugin</a> |
|
|
355 |
</li> |
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility."> |
|
|
360 |
<a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a> |
|
|
361 |
</li> |
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
<li data-description="Shows how to create an IO plugin for Widget."> |
|
|
366 |
<a href="../widget/widget-plugin.html">Creating a Widget Plugin</a> |
|
|
367 |
</li> |
|
|
368 |
|
|
|
369 |
|
|
|
370 |
</ul> |
|
|
371 |
</div> |
|
|
372 |
</div> |
|
|
373 |
|
|
|
374 |
</div> |
|
|
375 |
</div> |
|
|
376 |
</div> |
|
|
377 |
</div> |
|
|
378 |
|
|
|
379 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
380 |
<script>prettyPrint();</script> |
|
|
381 |
|
|
|
382 |
<script> |
|
|
383 |
YUI.Env.Tests = { |
|
|
384 |
examples: [], |
|
|
385 |
project: '../assets', |
|
|
386 |
assets: '../assets/plugin', |
|
|
387 |
name: 'plugin', |
|
|
388 |
title: 'Plugin', |
|
|
389 |
newWindow: '', |
|
|
390 |
auto: false |
|
|
391 |
}; |
|
|
392 |
YUI.Env.Tests.examples.push('overlay-io-plugin'); |
|
|
393 |
YUI.Env.Tests.examples.push('overlay-anim-plugin'); |
|
|
394 |
YUI.Env.Tests.examples.push('widget-plugin'); |
|
|
395 |
|
|
|
396 |
</script> |
|
|
397 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
|
401 |
</body> |
|
|
402 |
</html> |