|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>IO Plugin</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<!--begin custom header content for this example--> |
|
|
25 |
<style type="text/css"> |
|
|
26 |
.yui-overlay-content { |
|
|
27 |
padding:5px; |
|
|
28 |
background-color:#ccc; |
|
|
29 |
border:1px solid #000; |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
.yui-overlay .yui-widget-hd, |
|
|
33 |
.yui-overlay .yui-widget-bd, |
|
|
34 |
.yui-overlay .yui-widget-ft { |
|
|
35 |
background-color:#eee; |
|
|
36 |
padding:2px; |
|
|
37 |
border:1px solid #999; |
|
|
38 |
text-align:left; |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
.yui-overlay .yui-widget-bd { |
|
|
42 |
background-color:#fff; |
|
|
43 |
text-align:center; |
|
|
44 |
vertical-align:middle; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
.yui-overlay .yui-widget-bd .yui-feed-data { |
|
|
48 |
text-align:left; |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
.yui-overlay .yui-feed-selector .yui-prompt { |
|
|
52 |
font-weight:bold; |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
</style> |
|
|
56 |
<!--end custom header content for this example--> |
|
|
57 |
|
|
|
58 |
</head> |
|
|
59 |
|
|
|
60 |
<body class=" yui-skin-sam"> |
|
|
61 |
|
|
|
62 |
<h1>IO Plugin</h1> |
|
|
63 |
|
|
|
64 |
<div class="exampleIntro"> |
|
|
65 |
<p>This example shows how you can use Widget's plugin infrastructure to add additional features to an existing widget.</p> |
|
|
66 |
<p>We create an IO plugin class for <code>Overlay</code> called <code>StdModIOPlugin</code>. The plugin adds IO capabilities to the Overlay, bound to one of its standard module sections <em>(header, body or footer)</em>.</p> |
|
|
67 |
|
|
|
68 |
</div> |
|
|
69 |
|
|
|
70 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
71 |
|
|
|
72 |
<button type="button" id="show">Show Overlay</button> |
|
|
73 |
<button type="button" id="hide">Hide Overlay</button> |
|
|
74 |
|
|
|
75 |
<script type="text/javascript"> |
|
|
76 |
YUI({base:"../../build/", timeout: 10000}).use("overlay", "substitute", "io", "json", "plugin", function(Y) { |
|
|
77 |
|
|
|
78 |
/* Setup local variable for Y.WidgetStdMod, since we use it multiple times */ |
|
|
79 |
var StdMod = Y.WidgetStdMod; |
|
|
80 |
|
|
|
81 |
/* Standard Module IO Plugin Constructor */ |
|
|
82 |
function StdModIOPlugin(config) { |
|
|
83 |
StdModIOPlugin.superclass.constructor.apply(this, arguments); |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
/* |
|
|
87 |
* The namespace for the plugin. This will be the property on the widget, which will |
|
|
88 |
* reference the plugin instance, when it's plugged in |
|
|
89 |
*/ |
|
|
90 |
StdModIOPlugin.NS = "io"; |
|
|
91 |
|
|
|
92 |
/* |
|
|
93 |
* The NAME of the StdModIOPlugin class. Used to prefix events generated |
|
|
94 |
* by the plugin class. |
|
|
95 |
*/ |
|
|
96 |
StdModIOPlugin.NAME = "stdModIOPlugin"; |
|
|
97 |
|
|
|
98 |
/* |
|
|
99 |
* The default set of attributes for the StdModIOPlugin class. |
|
|
100 |
*/ |
|
|
101 |
StdModIOPlugin.ATTRS = { |
|
|
102 |
|
|
|
103 |
/* |
|
|
104 |
* The uri to use for the io request |
|
|
105 |
*/ |
|
|
106 |
uri : { |
|
|
107 |
value:null |
|
|
108 |
}, |
|
|
109 |
|
|
|
110 |
/* |
|
|
111 |
* The io configuration object, to pass to io when intiating a transaction |
|
|
112 |
*/ |
|
|
113 |
cfg : { |
|
|
114 |
value:null |
|
|
115 |
}, |
|
|
116 |
|
|
|
117 |
/* |
|
|
118 |
* The default formatter to use when formatting response data. The default |
|
|
119 |
* implementation simply passes back the response data passed in. |
|
|
120 |
*/ |
|
|
121 |
formatter : { |
|
|
122 |
valueFn: function() { |
|
|
123 |
return this._defFormatter; |
|
|
124 |
} |
|
|
125 |
}, |
|
|
126 |
|
|
|
127 |
/* |
|
|
128 |
* The Standard Module section to which the io plugin instance is bound. |
|
|
129 |
* Response data will be used to populate this section, after passing through |
|
|
130 |
* the configured formatter. |
|
|
131 |
*/ |
|
|
132 |
section: { |
|
|
133 |
value:StdMod.BODY, |
|
|
134 |
validator: function(val) { |
|
|
135 |
return (!val || val == StdMod.BODY || val == StdMod.HEADER || val == StdMod.FOOTER); |
|
|
136 |
} |
|
|
137 |
}, |
|
|
138 |
|
|
|
139 |
/* |
|
|
140 |
* The default loading indicator to use, when an io transaction is in progress. |
|
|
141 |
*/ |
|
|
142 |
loading: { |
|
|
143 |
value: '<img class="yui-loading" width="32px" height="32px" src="assets/img/ajax-loader.gif">' |
|
|
144 |
} |
|
|
145 |
}; |
|
|
146 |
|
|
|
147 |
/* Extend the base plugin class */ |
|
|
148 |
Y.extend(StdModIOPlugin, Y.Plugin.Base, { |
|
|
149 |
|
|
|
150 |
/* |
|
|
151 |
* Initialization code. Called when the |
|
|
152 |
* plugin is instantiated (whenever it's |
|
|
153 |
* plugged into the host) |
|
|
154 |
*/ |
|
|
155 |
initializer: function(config) { |
|
|
156 |
Y.io.transport({ |
|
|
157 |
id:'flash', |
|
|
158 |
yid: Y.id, |
|
|
159 |
src:'../../build/io/io.swf?stamp=' + (new Date()).getTime() |
|
|
160 |
}); |
|
|
161 |
}, |
|
|
162 |
|
|
|
163 |
/* |
|
|
164 |
* Destruction code. Terminates the activeIO transaction if it exists |
|
|
165 |
*/ |
|
|
166 |
destructor : function() { |
|
|
167 |
if (this._activeIO) { |
|
|
168 |
Y.io.abort(this._activeIO); |
|
|
169 |
this._activeIO = null; |
|
|
170 |
} |
|
|
171 |
}, |
|
|
172 |
|
|
|
173 |
/* |
|
|
174 |
* IO Plugin specific method, use to initiate a new io request using the current |
|
|
175 |
* io configuration settings. |
|
|
176 |
*/ |
|
|
177 |
refresh : function() { |
|
|
178 |
section = this.get("section"); |
|
|
179 |
|
|
|
180 |
if (section && !this._activeIO) { |
|
|
181 |
var uri = this.get("uri"); |
|
|
182 |
|
|
|
183 |
if (uri) { |
|
|
184 |
|
|
|
185 |
cfg = this.get("cfg") || {}; |
|
|
186 |
cfg.on = cfg.on || {}; |
|
|
187 |
|
|
|
188 |
cfg.on.start = cfg.on.start || Y.bind(this._defStartHandler, this); |
|
|
189 |
cfg.on.complete = cfg.on.complete || Y.bind(this._defCompleteHandler, this); |
|
|
190 |
|
|
|
191 |
cfg.on.success = cfg.on.success || Y.bind(this._defSuccessHandler, this); |
|
|
192 |
cfg.on.failure = cfg.on.failure || Y.bind(this._defFailureHandler, this); |
|
|
193 |
|
|
|
194 |
cfg.method = cfg.method; // io defaults to "GET" if not defined |
|
|
195 |
|
|
|
196 |
Y.io(uri, cfg); |
|
|
197 |
} |
|
|
198 |
} |
|
|
199 |
}, |
|
|
200 |
|
|
|
201 |
/* |
|
|
202 |
* The default io transaction success handler |
|
|
203 |
*/ |
|
|
204 |
_defSuccessHandler : function(id, o) { |
|
|
205 |
var response = o.responseXML || o.responseText; |
|
|
206 |
var section = this.get("section"); |
|
|
207 |
var formatter = this.get("formatter"); |
|
|
208 |
|
|
|
209 |
this.get("host").setStdModContent(section, formatter(response)); |
|
|
210 |
}, |
|
|
211 |
|
|
|
212 |
/* |
|
|
213 |
* The default io transaction failure handler |
|
|
214 |
*/ |
|
|
215 |
_defFailureHandler : function(id, o) { |
|
|
216 |
this.get("host").setStdModContent(this.get("section"), "Failed to retrieve content"); |
|
|
217 |
}, |
|
|
218 |
|
|
|
219 |
/* |
|
|
220 |
* The default io transaction start handler |
|
|
221 |
*/ |
|
|
222 |
_defStartHandler : function(id, o) { |
|
|
223 |
this._activeIO = o; |
|
|
224 |
this.get("host").setStdModContent(this.get("section"), this.get("loading")); |
|
|
225 |
}, |
|
|
226 |
|
|
|
227 |
/* |
|
|
228 |
* The default io transaction complete handler |
|
|
229 |
*/ |
|
|
230 |
_defCompleteHandler : function(id, o) { |
|
|
231 |
this._activeIO = null; |
|
|
232 |
}, |
|
|
233 |
|
|
|
234 |
/* |
|
|
235 |
* The default response formatter |
|
|
236 |
*/ |
|
|
237 |
_defFormatter : function(val) { |
|
|
238 |
return val |
|
|
239 |
} |
|
|
240 |
}); |
|
|
241 |
|
|
|
242 |
/* The Pipes feed URIs to be used to dispatch io transactions */ |
|
|
243 |
var pipes = { |
|
|
244 |
|
|
|
245 |
// uri data |
|
|
246 |
baseUri : 'http:/'+'/pipes.yahooapis.com/pipes/pipe.run?_id=6b7b2c6a32f5a12e7259c36967052387&_render=json&url=http:/'+'/', |
|
|
247 |
feeds : { |
|
|
248 |
ynews : { |
|
|
249 |
title: 'Yahoo! US News', |
|
|
250 |
uri: 'rss.news.yahoo.com/rss/us' |
|
|
251 |
}, |
|
|
252 |
yui : { |
|
|
253 |
title: 'YUI Blog', |
|
|
254 |
uri: 'feeds.yuiblog.com/YahooUserInterfaceBlog' |
|
|
255 |
}, |
|
|
256 |
slashdot : { |
|
|
257 |
title: 'Slashdot', |
|
|
258 |
uri: 'rss.slashdot.org/Slashdot/slashdot' |
|
|
259 |
}, |
|
|
260 |
ajaxian: { |
|
|
261 |
title: 'Ajaxian', |
|
|
262 |
uri: 'feeds.feedburner.com/ajaxian' |
|
|
263 |
}, |
|
|
264 |
daringfireball: { |
|
|
265 |
title: 'Daring Fireball', |
|
|
266 |
uri: 'daringfireball.net/index.xml' |
|
|
267 |
}, |
|
|
268 |
wiredtech: { |
|
|
269 |
title: 'Wire: Tech Biz', |
|
|
270 |
uri: 'www.wired.com/rss/techbiz.xml' |
|
|
271 |
}, |
|
|
272 |
techcrunch: { |
|
|
273 |
title: 'TechCrunch', |
|
|
274 |
uri: 'feedproxy.google.com/Techcrunch' |
|
|
275 |
} |
|
|
276 |
}, |
|
|
277 |
|
|
|
278 |
// The default formatter, responsible for converting the JSON responses recieved, |
|
|
279 |
// into HTML, using JSON for the parsing step, and substitute for some basic templating functionality |
|
|
280 |
formatter : function (val) { |
|
|
281 |
var formatted = "Error parsing feed data"; |
|
|
282 |
try { |
|
|
283 |
var json = Y.JSON.parse(val); |
|
|
284 |
if (json && json.count) { |
|
|
285 |
var html = ['<ul class="yui-feed-data">']; |
|
|
286 |
var linkTemplate = '<li><a href="{link}" target="_blank">{title}</a></li>'; |
|
|
287 |
|
|
|
288 |
Y.each(json.value.items, function(v, i) { |
|
|
289 |
if (i < 10) { |
|
|
290 |
html.push(Y.substitute(linkTemplate, v)); |
|
|
291 |
} |
|
|
292 |
}); |
|
|
293 |
html.push("</ul>"); |
|
|
294 |
formatted = html.join(""); |
|
|
295 |
} else { |
|
|
296 |
formatted = "No Data Available"; |
|
|
297 |
} |
|
|
298 |
} catch(e) { |
|
|
299 |
formatted = "Error parsing feed data"; |
|
|
300 |
} |
|
|
301 |
return formatted; |
|
|
302 |
} |
|
|
303 |
}; |
|
|
304 |
|
|
|
305 |
/* Helper function, to generate the select dropdown markup from the pipes feed data */ |
|
|
306 |
function generateHeaderMarkup() { |
|
|
307 |
var optTemplate = '<option value="{id}">{title}</option>', |
|
|
308 |
html = ['<select id="feedSelector" class="yui-feed-selector"><option value="-1" class="yui-prompt">Select a Feed...</option>']; |
|
|
309 |
|
|
|
310 |
Y.Object.each(pipes.feeds, function(v, k, o) { |
|
|
311 |
html.push(Y.substitute(optTemplate, {id:k, title:v.title})); |
|
|
312 |
}); |
|
|
313 |
html.push('</select>'); |
|
|
314 |
|
|
|
315 |
return html.join(""); |
|
|
316 |
} |
|
|
317 |
|
|
|
318 |
/* Create a new Overlay instance, with content generated from script */ |
|
|
319 |
var overlay = new Y.Overlay({ |
|
|
320 |
width:"40em", |
|
|
321 |
visible:false, |
|
|
322 |
align: { |
|
|
323 |
node:"#show", |
|
|
324 |
points: [Y.WidgetPositionExt.TL, Y.WidgetPositionExt.BL] |
|
|
325 |
}, |
|
|
326 |
zIndex:10, |
|
|
327 |
headerContent: generateHeaderMarkup(), |
|
|
328 |
bodyContent: "Feed data will be displayed here" |
|
|
329 |
}); |
|
|
330 |
|
|
|
331 |
/* |
|
|
332 |
* Add the Standard Module IO Plugin, and configure it to use flash, and a formatter specific |
|
|
333 |
* to the pipes response we're expecting from the uri request we'll send out. |
|
|
334 |
*/ |
|
|
335 |
overlay.plug(StdModIOPlugin, { |
|
|
336 |
uri : pipes.baseUri + pipes.feeds["ynews"].uri, |
|
|
337 |
cfg:{ |
|
|
338 |
xdr: { |
|
|
339 |
use:'flash' |
|
|
340 |
} |
|
|
341 |
}, |
|
|
342 |
formatter: pipes.formatter |
|
|
343 |
}); |
|
|
344 |
overlay.render(); |
|
|
345 |
|
|
|
346 |
Y.on("change", function(e) { |
|
|
347 |
var val = this.get("value"); |
|
|
348 |
if (val != -1) { |
|
|
349 |
overlay.io.set("uri", pipes.baseUri + pipes.feeds[val].uri); |
|
|
350 |
overlay.io.refresh(); |
|
|
351 |
} |
|
|
352 |
}, "#feedSelector"); |
|
|
353 |
|
|
|
354 |
Y.on("click", function(e) { |
|
|
355 |
overlay.show(); |
|
|
356 |
}, "#show"); |
|
|
357 |
|
|
|
358 |
Y.on("click", function(e) { |
|
|
359 |
overlay.hide(); |
|
|
360 |
}, "#hide"); |
|
|
361 |
|
|
|
362 |
}); |
|
|
363 |
</script> |
|
|
364 |
|
|
|
365 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
366 |
|
|
|
367 |
</body> |
|
|
368 |
</html> |