|
1 <!DOCTYPE html> |
|
2 <html lang="en"> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Example: Creating a Widget 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 |
|
24 <h1>Example: Creating a Widget Plugin</h1> |
|
25 <div class="yui3-g"> |
|
26 <div class="yui3-u-3-4"> |
|
27 <div id="main"> |
|
28 <div class="content"><style scoped> |
|
29 #demo { |
|
30 width: 50em; |
|
31 } |
|
32 .yui3-widget-content { |
|
33 border:1px solid #000; |
|
34 padding:1em; |
|
35 } |
|
36 .yui3-tab-loading { |
|
37 background: #fff url(../assets/widget/img/ajax-loader.gif) no-repeat center center; |
|
38 height:40px; |
|
39 } |
|
40 </style> |
|
41 |
|
42 <div class="intro"> |
|
43 <p>This example shows how you can use Widget's plugin infrastructure to add additional features to an existing widget.</p> |
|
44 <p> |
|
45 We use the <code>"gallery-widget-io"</code> plugin to add io capabilities bound to a widget instance. The plugin provides an <code>io</code> interface on Widget, which can be used to update |
|
46 the widget's contentBox contents. |
|
47 </p> |
|
48 <p> |
|
49 NOTE: This example uses io-xdr to retrieve content from pipes, and requires Flash. |
|
50 </p> |
|
51 </div> |
|
52 |
|
53 <div class="example"> |
|
54 <div id="demo"></div> |
|
55 <script type="text/javascript"> |
|
56 YUI().use("widget", "gallery-widget-io", "json-parse", "escape", function(Y) { |
|
57 |
|
58 var formatRSS = function (val) { |
|
59 var formatted = "Error parsing feed data"; |
|
60 try { |
|
61 var json = Y.JSON.parse(val); |
|
62 |
|
63 if (json && json.count) { |
|
64 var html = ['<ul class="yui3-feed-data">']; |
|
65 var linkTemplate = '<li><a href="{link}" target="_blank">{title}</a></li>'; |
|
66 |
|
67 Y.each(json.value.items, function(v, i) { |
|
68 if (i < 10) { |
|
69 v.title = Y.Escape.html(v.title); |
|
70 v.link = Y.Escape.html(v.link); |
|
71 html.push(Y.Lang.sub(linkTemplate, v)); |
|
72 } |
|
73 }); |
|
74 html.push("</ul>"); |
|
75 formatted = html.join(""); |
|
76 } else { |
|
77 formatted = "No Data Available"; |
|
78 } |
|
79 } catch(e) { |
|
80 formatted = "Error parsing feed data"; |
|
81 } |
|
82 return formatted; |
|
83 }; |
|
84 |
|
85 Y.on('io:xdrReady', function() { |
|
86 |
|
87 var widget = new Y.Widget(); |
|
88 |
|
89 widget.plug(Y.Plugin.WidgetIO, { |
|
90 uri : 'http:/' + '/pipes.yahooapis.com/pipes/pipe.run?_id=6b7b2c6a32f5a12e7259c36967052387&_render=json&url=http:/' + '/rss.news.yahoo.com/rss/us', |
|
91 cfg:{ |
|
92 xdr: { |
|
93 use:'flash' |
|
94 } |
|
95 }, |
|
96 formatter: formatRSS, |
|
97 loading: '<img class="yui3-loading" width="32px" height="32px" src="../assets/widget/img/ajax-loader.gif">' |
|
98 }); |
|
99 widget.render('#demo'); |
|
100 |
|
101 widget.io.refresh(); |
|
102 }); |
|
103 |
|
104 Y.io.transport({ |
|
105 id:'flash', |
|
106 yid: Y.id, |
|
107 src:'../../build/io-xdr/io.swf?stamp=' + (new Date()).getTime() |
|
108 }); |
|
109 |
|
110 }); |
|
111 </script> |
|
112 |
|
113 </div> |
|
114 |
|
115 <h2>Using The Gallery IO Plugin For Widget</h2> |
|
116 |
|
117 <h3>Setting Up The YUI Instance</h3> |
|
118 |
|
119 <p>For this example, we'll pull in <code>widget</code>; the <code>gallery-widget-io</code> plugin, and the <code>json-parse</code> modules to help us work with the JSON RSS data returned. |
|
120 The code to set up our sandbox instance is shown below:</p> |
|
121 |
|
122 <pre class="code prettyprint">YUI().use("widget", "gallery-widget-io", "json-parse", function(Y) { |
|
123 // We'll write our code here, after pulling in the default Widget module, |
|
124 // the IO plugin. |
|
125 });</pre> |
|
126 |
|
127 |
|
128 <h3>The Widget IO Plugin</h3> |
|
129 |
|
130 <p>The Widget IO plugin is a fairly simple plugin. It provides incremental functionality. It does not need to modify the behavior of any methods on the host Widget instance, or monitor any Widget events (unlike the <a href="../overlay/overlay-anim-plugin.html">AnimPlugin</a> example).</p> |
|
131 |
|
132 <p>It sets up the following attributes, which are used to control how the IO plugin's <code>refresh</code> method behaves:</p> |
|
133 |
|
134 <dl> |
|
135 <dt>uri</dt> |
|
136 <dd>The uri to use for the io request</dd> |
|
137 <dt>cfg</dt> |
|
138 <dd>The io configuration object, to pass to io when initiating a transaction</dd> |
|
139 <dt>formatter</dt> |
|
140 <dd>The formatter to use to formatting response data. The default implementation simply passes back the response data passed in, unchanged.</dd> |
|
141 <dt>loading</dt> |
|
142 <dd>The default content to display while an io transaction is in progress</dd> |
|
143 </dl> |
|
144 |
|
145 <h3>Using the Plugin</h3> |
|
146 |
|
147 <p>All objects derived from <a href="http://yuilibrary.com/yui/docs/api/Base.html">Base</a> are <a href="http://yuilibrary.com/yui/docs/api/Plugin.Host.html">Plugin Hosts</a>. |
|
148 They provide <a href="http://yuilibrary.com/yui/docs/api/Plugin.Host.html#method_plug"><code>plug</code></a> and <a href="http://yuilibrary.com/yui/docs/api/Plugin.Host.html#method_unplug"><code>unplug</code></a> methods to allow users to add/remove plugins to/from existing instances. |
|
149 They also allow the user to specify the set of plugins to be applied to a new instance, along with their configurations, as part of the constructor arguments.</p> |
|
150 |
|
151 <p>In this example, we'll create a new instance of a Widget:</p> |
|
152 |
|
153 <pre class="code prettyprint">/* Create a new Widget instance, with content generated from script */ |
|
154 var widget = new Y.Widget();</pre> |
|
155 |
|
156 |
|
157 <p>And then use the <code>plug</code> method to add the <code>WidgetIO</code> plugin, |
|
158 providing it with a configuration to use when sending out io transactions |
|
159 (The <a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a> example shows how |
|
160 you could do the same thing during construction), render the widget, and refresh |
|
161 the plugin to fetch the content.</p> |
|
162 |
|
163 <pre class="code prettyprint">/* |
|
164 * Add the Plugin, and configure it to use a news feed uri, and work cross-domain, using xdr |
|
165 */ |
|
166 widget.plug(Y.Plugin.WidgetIO, { |
|
167 uri : 'http:/' + '/pipes.yahooapis.com/pipes/pipe.run?_id=6b7b2c6a32f5a12e7259c36967052387&_render=json&url=http:/' + '/rss.news.yahoo.com/rss/us', |
|
168 cfg:{ |
|
169 xdr: { |
|
170 use:'flash' |
|
171 } |
|
172 }, |
|
173 formatter: formatRSS, |
|
174 loading: '<img class="yui3-loading" width="32px" height="32px" src="../assets/widget/img/ajax-loader.gif">' |
|
175 }); |
|
176 |
|
177 widget.render('#demo'); |
|
178 |
|
179 /* fetch the content */ |
|
180 widget.io.refresh();</pre> |
|
181 |
|
182 |
|
183 <p>We pass in a formatter to the io plugin, which is responsible for translating the JSON RSS from the uri to HTML:</p> |
|
184 |
|
185 <pre class="code prettyprint">var formatRSS = function (val) { |
|
186 var formatted = "Error parsing feed data"; |
|
187 try { |
|
188 var json = Y.JSON.parse(val); |
|
189 |
|
190 if (json && json.count) { |
|
191 var html = ['<ul class="yui3-feed-data">']; |
|
192 var linkTemplate = '<li><a href="{link}" target="_blank">{title}</a></li>'; |
|
193 |
|
194 Y.each(json.value.items, function(v, i) { |
|
195 if (i < 10) { |
|
196 v.title = Y.Escape.html(v.title); |
|
197 v.link = Y.Escape.html(v.link); |
|
198 html.push(Y.Lang.sub(linkTemplate, v)); |
|
199 } |
|
200 }); |
|
201 html.push("</ul>"); |
|
202 formatted = html.join(""); |
|
203 } else { |
|
204 formatted = "No Data Available"; |
|
205 } |
|
206 } catch(e) { |
|
207 formatted = "Error parsing feed data"; |
|
208 } |
|
209 return formatted; |
|
210 };</pre> |
|
211 |
|
212 |
|
213 <p>NOTE: Since we're using <code>IO</code>'s XDR functionality for this example, we wrap the widget construction in a callback which notifies us when the flash XDR module is ready to service requests. In your local implementations, |
|
214 if you're not sending cross-domain requests, you don't need to use the XDR functionality and leave out the code below:</p> |
|
215 |
|
216 <pre class="code prettyprint">Y.on('io:xdrReady', function() { |
|
217 // Setup Widget when io xdr is available |
|
218 }); |
|
219 |
|
220 // Set up io to use the flash XDR transport |
|
221 Y.io.transport({ |
|
222 id:'flash', |
|
223 yid: Y.id, |
|
224 src:'../../build/io-xdr/io.swf?stamp=' + (new Date()).getTime() |
|
225 });</pre> |
|
226 |
|
227 |
|
228 <h2>Complete Example Source</h2> |
|
229 <pre class="code prettyprint"><div id="demo"></div> |
|
230 <script type="text/javascript"> |
|
231 YUI().use("widget", "gallery-widget-io", "json-parse", "escape", function(Y) { |
|
232 |
|
233 var formatRSS = function (val) { |
|
234 var formatted = "Error parsing feed data"; |
|
235 try { |
|
236 var json = Y.JSON.parse(val); |
|
237 |
|
238 if (json && json.count) { |
|
239 var html = ['<ul class="yui3-feed-data">']; |
|
240 var linkTemplate = '<li><a href="{link}" target="_blank">{title}</a></li>'; |
|
241 |
|
242 Y.each(json.value.items, function(v, i) { |
|
243 if (i < 10) { |
|
244 v.title = Y.Escape.html(v.title); |
|
245 v.link = Y.Escape.html(v.link); |
|
246 html.push(Y.Lang.sub(linkTemplate, v)); |
|
247 } |
|
248 }); |
|
249 html.push("</ul>"); |
|
250 formatted = html.join(""); |
|
251 } else { |
|
252 formatted = "No Data Available"; |
|
253 } |
|
254 } catch(e) { |
|
255 formatted = "Error parsing feed data"; |
|
256 } |
|
257 return formatted; |
|
258 }; |
|
259 |
|
260 Y.on('io:xdrReady', function() { |
|
261 |
|
262 var widget = new Y.Widget(); |
|
263 |
|
264 widget.plug(Y.Plugin.WidgetIO, { |
|
265 uri : 'http:/' + '/pipes.yahooapis.com/pipes/pipe.run?_id=6b7b2c6a32f5a12e7259c36967052387&_render=json&url=http:/' + '/rss.news.yahoo.com/rss/us', |
|
266 cfg:{ |
|
267 xdr: { |
|
268 use:'flash' |
|
269 } |
|
270 }, |
|
271 formatter: formatRSS, |
|
272 loading: '<img class="yui3-loading" width="32px" height="32px" src="../assets/widget/img/ajax-loader.gif">' |
|
273 }); |
|
274 widget.render('#demo'); |
|
275 |
|
276 widget.io.refresh(); |
|
277 }); |
|
278 |
|
279 Y.io.transport({ |
|
280 id:'flash', |
|
281 yid: Y.id, |
|
282 src:'../../build/io-xdr/io.swf?stamp=' + (new Date()).getTime() |
|
283 }); |
|
284 |
|
285 }); |
|
286 </script></pre> |
|
287 |
|
288 </div> |
|
289 </div> |
|
290 </div> |
|
291 |
|
292 <div class="yui3-u-1-4"> |
|
293 <div class="sidebar"> |
|
294 |
|
295 |
|
296 |
|
297 <div class="sidebox"> |
|
298 <div class="hd"> |
|
299 <h2 class="no-toc">Examples</h2> |
|
300 </div> |
|
301 |
|
302 <div class="bd"> |
|
303 <ul class="examples"> |
|
304 |
|
305 |
|
306 <li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
307 <a href="widget-extend.html">Extending the Base Widget Class</a> |
|
308 </li> |
|
309 |
|
310 |
|
311 |
|
312 <li data-description="Shows how to use Base.create and mix/match extensions to create custom Widget classes."> |
|
313 <a href="widget-build.html">Creating Custom Widget Classes With Extensions</a> |
|
314 </li> |
|
315 |
|
316 |
|
317 |
|
318 <li data-description="Shows how to create an IO plugin for Widget."> |
|
319 <a href="widget-plugin.html">Creating a Widget Plugin</a> |
|
320 </li> |
|
321 |
|
322 |
|
323 |
|
324 <li data-description="Shows how to extend the Widget class, and add WidgetPosition and WidgetStack to create a Tooltip widget class."> |
|
325 <a href="widget-tooltip.html">Creating a Simple Tooltip Widget With Extensions</a> |
|
326 </li> |
|
327 |
|
328 |
|
329 |
|
330 <li data-description="Shows how to extend the Widget class, and add WidgetParent and WidgetChild to create a simple ListBox widget."> |
|
331 <a href="widget-parentchild-listbox.html">Creating a Hierarchical ListBox Widget</a> |
|
332 </li> |
|
333 |
|
334 |
|
335 </ul> |
|
336 </div> |
|
337 </div> |
|
338 |
|
339 |
|
340 |
|
341 </div> |
|
342 </div> |
|
343 </div> |
|
344 </div> |
|
345 |
|
346 <script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
347 <script>prettyPrint();</script> |
|
348 |
|
349 <script> |
|
350 YUI.Env.Tests = { |
|
351 examples: [], |
|
352 project: '../assets', |
|
353 assets: '../assets/widget', |
|
354 name: 'widget-plugin', |
|
355 title: 'Creating a Widget Plugin', |
|
356 newWindow: '', |
|
357 auto: false |
|
358 }; |
|
359 YUI.Env.Tests.examples.push('widget-extend'); |
|
360 YUI.Env.Tests.examples.push('widget-build'); |
|
361 YUI.Env.Tests.examples.push('widget-plugin'); |
|
362 YUI.Env.Tests.examples.push('widget-tooltip'); |
|
363 YUI.Env.Tests.examples.push('widget-parentchild-listbox'); |
|
364 |
|
365 </script> |
|
366 <script src="../assets/yui/test-runner.js"></script> |
|
367 |
|
368 |
|
369 |
|
370 </body> |
|
371 </html> |