|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Find Photos on Flickr (Custom Formatting, YQL Source)</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: Find Photos on Flickr (Custom Formatting, YQL Source)</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p> |
|
|
30 |
This example uses the Flickr Search API (via YQL) to allow the user to select photo results based on a search query. After each photo is selected, it's added to the collection of photos displayed below the input field. |
|
|
31 |
</p> |
|
|
32 |
|
|
|
33 |
<p> |
|
|
34 |
A custom <code>resultFormatter</code> is used to format the Flickr results in the AutoComplete dropdown list, and a custom <code>select</code> event handler is used to override the default selection logic. |
|
|
35 |
</p> |
|
|
36 |
</div> |
|
|
37 |
|
|
|
38 |
<div class="example"> |
|
|
39 |
<style scoped> |
|
|
40 |
#ac-input { width: 300px; } |
|
|
41 |
|
|
|
42 |
#photos { |
|
|
43 |
border: 1px solid #cfcfcf; |
|
|
44 |
list-style: none; |
|
|
45 |
margin: 1.5em 0 0; |
|
|
46 |
padding: 6px; |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
#photos li { |
|
|
50 |
display: inline-block; |
|
|
51 |
list-style: none; |
|
|
52 |
|
|
|
53 |
/* fake inline-block for IE<8 */ |
|
|
54 |
zoom: 1; |
|
|
55 |
*display: inline; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
#photos .empty { font-style: italic; } |
|
|
59 |
|
|
|
60 |
#photos .photo { |
|
|
61 |
margin: 5px; |
|
|
62 |
text-align: center; |
|
|
63 |
width: 100px; |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
#photos .photo img { |
|
|
67 |
border: 1px solid #000; |
|
|
68 |
vertical-align: top; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
.result { |
|
|
72 |
margin: 2px 0; |
|
|
73 |
zoom: 1; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
.result:after { |
|
|
77 |
clear: both; |
|
|
78 |
content: '.'; |
|
|
79 |
display: block; |
|
|
80 |
height: 0; |
|
|
81 |
visibility: hidden; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
.result .photo { |
|
|
85 |
height: 32px; |
|
|
86 |
float: left; |
|
|
87 |
margin-right: 6px; |
|
|
88 |
width: 32px; |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
.result .title { vertical-align: top; } |
|
|
92 |
</style> |
|
|
93 |
|
|
|
94 |
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
95 |
<label for="ac-input">Search Flickr:</label><br> |
|
|
96 |
<input id="ac-input" type="text"> |
|
|
97 |
|
|
|
98 |
<ul id="photos"> |
|
|
99 |
<li class="empty">No photos selected.</li> |
|
|
100 |
</ul> |
|
|
101 |
</div> |
|
|
102 |
|
|
|
103 |
<script> |
|
|
104 |
YUI().use('autocomplete', 'autocomplete-highlighters', function (Y) { |
|
|
105 |
var inputNode = Y.one('#ac-input'), |
|
|
106 |
photosNode = Y.one('#photos'); |
|
|
107 |
|
|
|
108 |
// Constructs an image URL for the Flickr photo represented by the given |
|
|
109 |
// AutoComplete result object. |
|
|
110 |
function getImageUrl(result, size) { |
|
|
111 |
if (!size) { size = 's'; } |
|
|
112 |
|
|
|
113 |
return Y.Lang.sub( |
|
|
114 |
'http://farm{farm}.static.flickr.com/{server}/{id}_{secret}_' + size + '.jpg', |
|
|
115 |
result.raw |
|
|
116 |
); |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
inputNode.plug(Y.Plugin.AutoComplete, { |
|
|
120 |
maxResults: 5, |
|
|
121 |
resultHighlighter: 'wordMatch', |
|
|
122 |
resultTextLocator: 'title', |
|
|
123 |
source: 'select * from flickr.photos.search where text="{query}" and api_key="1895311ec0d2e23431a6407f3e8dffcc" limit {maxResults}', |
|
|
124 |
|
|
|
125 |
// Custom result formatter for Flickr results. |
|
|
126 |
resultFormatter: function (query, results) { |
|
|
127 |
return Y.Array.map(results, function (result) { |
|
|
128 |
return '<div class="result">' + |
|
|
129 |
'<img class="photo" src="' + getImageUrl(result) + '" alt="thumbnail">' + |
|
|
130 |
'<span class="title">' + result.highlighted + '</span>' + |
|
|
131 |
'</div>'; |
|
|
132 |
}); |
|
|
133 |
} |
|
|
134 |
}); |
|
|
135 |
|
|
|
136 |
// After a photo is selected, add it to the collection. |
|
|
137 |
inputNode.ac.on('select', function (e) { |
|
|
138 |
var result = e.result; |
|
|
139 |
|
|
|
140 |
// Prevent the default select handler so we can provide our own selection |
|
|
141 |
// behavior instead. |
|
|
142 |
e.preventDefault(); |
|
|
143 |
|
|
|
144 |
inputNode.select(); |
|
|
145 |
inputNode.ac.hide(); |
|
|
146 |
|
|
|
147 |
// Remove the 'No photos selected' message if it exists. |
|
|
148 |
photosNode.all('.empty').remove(); |
|
|
149 |
|
|
|
150 |
// Append a new list item containing the selected photo. |
|
|
151 |
photosNode.append( |
|
|
152 |
'<li class="photo">' + |
|
|
153 |
'<img src="' + getImageUrl(result, 't') + |
|
|
154 |
'" alt="' + result.raw.title + '" title="' + result.raw.title + '">' + |
|
|
155 |
'</li>' |
|
|
156 |
); |
|
|
157 |
}); |
|
|
158 |
}); |
|
|
159 |
</script> |
|
|
160 |
|
|
|
161 |
</div> |
|
|
162 |
|
|
|
163 |
<h2>HTML</h2> |
|
|
164 |
<p> |
|
|
165 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
166 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
167 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
168 |
</p> |
|
|
169 |
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
170 |
<label for="ac-input">Search Flickr:</label><br> |
|
|
171 |
<input id="ac-input" type="text"> |
|
|
172 |
|
|
|
173 |
<ul id="photos"> |
|
|
174 |
<li class="empty">No photos selected.</li> |
|
|
175 |
</ul> |
|
|
176 |
</div></pre> |
|
|
177 |
|
|
|
178 |
|
|
|
179 |
<h2>CSS</h2> |
|
|
180 |
|
|
|
181 |
<pre class="code prettyprint">#ac-input { width: 300px; } |
|
|
182 |
|
|
|
183 |
#photos { |
|
|
184 |
border: 1px solid #cfcfcf; |
|
|
185 |
list-style: none; |
|
|
186 |
margin: 1.5em 0 0; |
|
|
187 |
padding: 6px; |
|
|
188 |
} |
|
|
189 |
|
|
|
190 |
#photos li { |
|
|
191 |
display: inline-block; |
|
|
192 |
list-style: none; |
|
|
193 |
|
|
|
194 |
/* fake inline-block for IE<8 */ |
|
|
195 |
zoom: 1; |
|
|
196 |
*display: inline; |
|
|
197 |
} |
|
|
198 |
|
|
|
199 |
#photos .empty { font-style: italic; } |
|
|
200 |
|
|
|
201 |
#photos .photo { |
|
|
202 |
margin: 5px; |
|
|
203 |
text-align: center; |
|
|
204 |
width: 100px; |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
#photos .photo img { |
|
|
208 |
border: 1px solid #000; |
|
|
209 |
vertical-align: top; |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
.result { |
|
|
213 |
margin: 2px 0; |
|
|
214 |
zoom: 1; |
|
|
215 |
} |
|
|
216 |
|
|
|
217 |
.result:after { |
|
|
218 |
clear: both; |
|
|
219 |
content: '.'; |
|
|
220 |
display: block; |
|
|
221 |
height: 0; |
|
|
222 |
visibility: hidden; |
|
|
223 |
} |
|
|
224 |
|
|
|
225 |
.result .photo { |
|
|
226 |
height: 32px; |
|
|
227 |
float: left; |
|
|
228 |
margin-right: 6px; |
|
|
229 |
width: 32px; |
|
|
230 |
} |
|
|
231 |
|
|
|
232 |
.result .title { vertical-align: top; }</pre> |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
<h2>JavaScript</h2> |
|
|
236 |
|
|
|
237 |
<pre class="code prettyprint">YUI().use('autocomplete', 'autocomplete-highlighters', function (Y) { |
|
|
238 |
var inputNode = Y.one('#ac-input'), |
|
|
239 |
photosNode = Y.one('#photos'); |
|
|
240 |
|
|
|
241 |
// Constructs an image URL for the Flickr photo represented by the given |
|
|
242 |
// AutoComplete result object. |
|
|
243 |
function getImageUrl(result, size) { |
|
|
244 |
if (!size) { size = 's'; } |
|
|
245 |
|
|
|
246 |
return Y.Lang.sub( |
|
|
247 |
'http://farm{farm}.static.flickr.com/{server}/{id}_{secret}_' + size + '.jpg', |
|
|
248 |
result.raw |
|
|
249 |
); |
|
|
250 |
} |
|
|
251 |
|
|
|
252 |
inputNode.plug(Y.Plugin.AutoComplete, { |
|
|
253 |
maxResults: 5, |
|
|
254 |
resultHighlighter: 'wordMatch', |
|
|
255 |
resultTextLocator: 'title', |
|
|
256 |
source: 'select * from flickr.photos.search where text="{query}" and api_key="1895311ec0d2e23431a6407f3e8dffcc" limit {maxResults}', |
|
|
257 |
|
|
|
258 |
// Custom result formatter for Flickr results. |
|
|
259 |
resultFormatter: function (query, results) { |
|
|
260 |
return Y.Array.map(results, function (result) { |
|
|
261 |
return '<div class="result">' + |
|
|
262 |
'<img class="photo" src="' + getImageUrl(result) + '" alt="thumbnail">' + |
|
|
263 |
'<span class="title">' + result.highlighted + '</span>' + |
|
|
264 |
'</div>'; |
|
|
265 |
}); |
|
|
266 |
} |
|
|
267 |
}); |
|
|
268 |
|
|
|
269 |
// After a photo is selected, add it to the collection. |
|
|
270 |
inputNode.ac.on('select', function (e) { |
|
|
271 |
var result = e.result; |
|
|
272 |
|
|
|
273 |
// Prevent the default select handler so we can provide our own selection |
|
|
274 |
// behavior instead. |
|
|
275 |
e.preventDefault(); |
|
|
276 |
|
|
|
277 |
inputNode.select(); |
|
|
278 |
inputNode.ac.hide(); |
|
|
279 |
|
|
|
280 |
// Remove the 'No photos selected' message if it exists. |
|
|
281 |
photosNode.all('.empty').remove(); |
|
|
282 |
|
|
|
283 |
// Append a new list item containing the selected photo. |
|
|
284 |
photosNode.append( |
|
|
285 |
'<li class="photo">' + |
|
|
286 |
'<img src="' + getImageUrl(result, 't') + |
|
|
287 |
'" alt="' + result.raw.title + '" title="' + result.raw.title + '">' + |
|
|
288 |
'</li>' |
|
|
289 |
); |
|
|
290 |
}); |
|
|
291 |
});</pre> |
|
|
292 |
|
|
|
293 |
|
|
|
294 |
<h2>Complete Example Source</h2> |
|
|
295 |
|
|
|
296 |
<pre class="code prettyprint"><style scoped> |
|
|
297 |
#ac-input { width: 300px; } |
|
|
298 |
|
|
|
299 |
#photos { |
|
|
300 |
border: 1px solid #cfcfcf; |
|
|
301 |
list-style: none; |
|
|
302 |
margin: 1.5em 0 0; |
|
|
303 |
padding: 6px; |
|
|
304 |
} |
|
|
305 |
|
|
|
306 |
#photos li { |
|
|
307 |
display: inline-block; |
|
|
308 |
list-style: none; |
|
|
309 |
|
|
|
310 |
/* fake inline-block for IE<8 */ |
|
|
311 |
zoom: 1; |
|
|
312 |
*display: inline; |
|
|
313 |
} |
|
|
314 |
|
|
|
315 |
#photos .empty { font-style: italic; } |
|
|
316 |
|
|
|
317 |
#photos .photo { |
|
|
318 |
margin: 5px; |
|
|
319 |
text-align: center; |
|
|
320 |
width: 100px; |
|
|
321 |
} |
|
|
322 |
|
|
|
323 |
#photos .photo img { |
|
|
324 |
border: 1px solid #000; |
|
|
325 |
vertical-align: top; |
|
|
326 |
} |
|
|
327 |
|
|
|
328 |
.result { |
|
|
329 |
margin: 2px 0; |
|
|
330 |
zoom: 1; |
|
|
331 |
} |
|
|
332 |
|
|
|
333 |
.result:after { |
|
|
334 |
clear: both; |
|
|
335 |
content: '.'; |
|
|
336 |
display: block; |
|
|
337 |
height: 0; |
|
|
338 |
visibility: hidden; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
.result .photo { |
|
|
342 |
height: 32px; |
|
|
343 |
float: left; |
|
|
344 |
margin-right: 6px; |
|
|
345 |
width: 32px; |
|
|
346 |
} |
|
|
347 |
|
|
|
348 |
.result .title { vertical-align: top; } |
|
|
349 |
</style> |
|
|
350 |
|
|
|
351 |
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
352 |
<label for="ac-input">Search Flickr:</label><br> |
|
|
353 |
<input id="ac-input" type="text"> |
|
|
354 |
|
|
|
355 |
<ul id="photos"> |
|
|
356 |
<li class="empty">No photos selected.</li> |
|
|
357 |
</ul> |
|
|
358 |
</div> |
|
|
359 |
|
|
|
360 |
<script> |
|
|
361 |
YUI().use('autocomplete', 'autocomplete-highlighters', function (Y) { |
|
|
362 |
var inputNode = Y.one('#ac-input'), |
|
|
363 |
photosNode = Y.one('#photos'); |
|
|
364 |
|
|
|
365 |
// Constructs an image URL for the Flickr photo represented by the given |
|
|
366 |
// AutoComplete result object. |
|
|
367 |
function getImageUrl(result, size) { |
|
|
368 |
if (!size) { size = 's'; } |
|
|
369 |
|
|
|
370 |
return Y.Lang.sub( |
|
|
371 |
'http://farm{farm}.static.flickr.com/{server}/{id}_{secret}_' + size + '.jpg', |
|
|
372 |
result.raw |
|
|
373 |
); |
|
|
374 |
} |
|
|
375 |
|
|
|
376 |
inputNode.plug(Y.Plugin.AutoComplete, { |
|
|
377 |
maxResults: 5, |
|
|
378 |
resultHighlighter: 'wordMatch', |
|
|
379 |
resultTextLocator: 'title', |
|
|
380 |
source: 'select * from flickr.photos.search where text="{query}" and api_key="1895311ec0d2e23431a6407f3e8dffcc" limit {maxResults}', |
|
|
381 |
|
|
|
382 |
// Custom result formatter for Flickr results. |
|
|
383 |
resultFormatter: function (query, results) { |
|
|
384 |
return Y.Array.map(results, function (result) { |
|
|
385 |
return '<div class="result">' + |
|
|
386 |
'<img class="photo" src="' + getImageUrl(result) + '" alt="thumbnail">' + |
|
|
387 |
'<span class="title">' + result.highlighted + '</span>' + |
|
|
388 |
'</div>'; |
|
|
389 |
}); |
|
|
390 |
} |
|
|
391 |
}); |
|
|
392 |
|
|
|
393 |
// After a photo is selected, add it to the collection. |
|
|
394 |
inputNode.ac.on('select', function (e) { |
|
|
395 |
var result = e.result; |
|
|
396 |
|
|
|
397 |
// Prevent the default select handler so we can provide our own selection |
|
|
398 |
// behavior instead. |
|
|
399 |
e.preventDefault(); |
|
|
400 |
|
|
|
401 |
inputNode.select(); |
|
|
402 |
inputNode.ac.hide(); |
|
|
403 |
|
|
|
404 |
// Remove the 'No photos selected' message if it exists. |
|
|
405 |
photosNode.all('.empty').remove(); |
|
|
406 |
|
|
|
407 |
// Append a new list item containing the selected photo. |
|
|
408 |
photosNode.append( |
|
|
409 |
'<li class="photo">' + |
|
|
410 |
'<img src="' + getImageUrl(result, 't') + |
|
|
411 |
'" alt="' + result.raw.title + '" title="' + result.raw.title + '">' + |
|
|
412 |
'</li>' |
|
|
413 |
); |
|
|
414 |
}); |
|
|
415 |
}); |
|
|
416 |
</script></pre> |
|
|
417 |
|
|
|
418 |
</div> |
|
|
419 |
</div> |
|
|
420 |
</div> |
|
|
421 |
|
|
|
422 |
<div class="yui3-u-1-4"> |
|
|
423 |
<div class="sidebar"> |
|
|
424 |
|
|
|
425 |
|
|
|
426 |
|
|
|
427 |
<div class="sidebox"> |
|
|
428 |
<div class="hd"> |
|
|
429 |
<h2 class="no-toc">Examples</h2> |
|
|
430 |
</div> |
|
|
431 |
|
|
|
432 |
<div class="bd"> |
|
|
433 |
<ul class="examples"> |
|
|
434 |
|
|
|
435 |
|
|
|
436 |
<li data-description="How to provide autocomplete suggestions from a local array."> |
|
|
437 |
<a href="ac-local.html">Basic Local Data</a> |
|
|
438 |
</li> |
|
|
439 |
|
|
|
440 |
|
|
|
441 |
|
|
|
442 |
<li data-description="How to provide autocomplete suggestions using a remote JSONP source."> |
|
|
443 |
<a href="ac-jsonp.html">Remote Data via JSONP</a> |
|
|
444 |
</li> |
|
|
445 |
|
|
|
446 |
|
|
|
447 |
|
|
|
448 |
<li data-description="How to provide autocomplete suggestions using a YQL query source."> |
|
|
449 |
<a href="ac-yql.html">Remote Data via YQL</a> |
|
|
450 |
</li> |
|
|
451 |
|
|
|
452 |
|
|
|
453 |
|
|
|
454 |
<li data-description="How to provide autocomplete suggestions using a DataSource instance."> |
|
|
455 |
<a href="ac-datasource.html">Remote Data via DataSource</a> |
|
|
456 |
</li> |
|
|
457 |
|
|
|
458 |
|
|
|
459 |
|
|
|
460 |
<li data-description="How to implement delimited tag completion."> |
|
|
461 |
<a href="ac-tagging.html">Tag Completion Using Query Delimiters</a> |
|
|
462 |
</li> |
|
|
463 |
|
|
|
464 |
|
|
|
465 |
|
|
|
466 |
<li data-description="How to find and select Flickr photos using a YQL source and a custom autocomplete result formatter."> |
|
|
467 |
<a href="ac-flickr.html">Find Photos on Flickr (Custom Formatting, YQL Source)</a> |
|
|
468 |
</li> |
|
|
469 |
|
|
|
470 |
|
|
|
471 |
|
|
|
472 |
<li data-description="How to use autocomplete-base to filter a set of existing items on a page."> |
|
|
473 |
<a href="ac-filter.html">Filter a Set of Existing Items on a Page</a> |
|
|
474 |
</li> |
|
|
475 |
|
|
|
476 |
|
|
|
477 |
|
|
|
478 |
<li data-description="How to find an address using a YQL source calling Google's Geocoding Service"> |
|
|
479 |
<a href="ac-geocode.html">Address Completion on Google's Geocoding Service</a> |
|
|
480 |
</li> |
|
|
481 |
|
|
|
482 |
|
|
|
483 |
</ul> |
|
|
484 |
</div> |
|
|
485 |
</div> |
|
|
486 |
|
|
|
487 |
|
|
|
488 |
|
|
|
489 |
</div> |
|
|
490 |
</div> |
|
|
491 |
</div> |
|
|
492 |
</div> |
|
|
493 |
|
|
|
494 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
495 |
<script>prettyPrint();</script> |
|
|
496 |
|
|
|
497 |
<script> |
|
|
498 |
YUI.Env.Tests = { |
|
|
499 |
examples: [], |
|
|
500 |
project: '../assets', |
|
|
501 |
assets: '../assets/autocomplete', |
|
|
502 |
name: 'ac-flickr', |
|
|
503 |
title: 'Find Photos on Flickr (Custom Formatting, YQL Source)', |
|
|
504 |
newWindow: '', |
|
|
505 |
auto: false |
|
|
506 |
}; |
|
|
507 |
YUI.Env.Tests.examples.push('ac-local'); |
|
|
508 |
YUI.Env.Tests.examples.push('ac-jsonp'); |
|
|
509 |
YUI.Env.Tests.examples.push('ac-yql'); |
|
|
510 |
YUI.Env.Tests.examples.push('ac-datasource'); |
|
|
511 |
YUI.Env.Tests.examples.push('ac-tagging'); |
|
|
512 |
YUI.Env.Tests.examples.push('ac-flickr'); |
|
|
513 |
YUI.Env.Tests.examples.push('ac-filter'); |
|
|
514 |
YUI.Env.Tests.examples.push('ac-geocode'); |
|
|
515 |
|
|
|
516 |
</script> |
|
|
517 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
518 |
|
|
|
519 |
|
|
|
520 |
|
|
|
521 |
</body> |
|
|
522 |
</html> |