This example uses AutoComplete to find images by tag from the Flickr webservice. A simple PHP proxy is used to access the remote server via XHR. The generateRequest()
method has been customized in order send additional required parameters to the Flickr application. The formatResult()
method has been customized in order to display images in the results container, and the default CSS has been enhanced so the results container can scroll. Finally, a itemSelectEvent
handler has been defined to collect selected images in a separate container.
CSS:
1 | #flickrselections { |
2 | float:right; |
3 | width:240px; |
4 | padding:10px; |
5 | background-color:#FFA928; |
6 | } |
7 | |
8 | #flickrselections h5 { |
9 | color:#009; |
10 | margin:0; |
11 | } |
12 | |
13 | /* custom styles for scrolling container */ |
14 | #flickrautocomplete { |
15 | width:15em; /* set width of widget here */ |
16 | padding-bottom:2em; |
17 | } |
18 | #flickrautocomplete .yui-ac-content { |
19 | max-height:30em;overflow:auto;overflow-x:hidden; /* set scrolling */ |
20 | _height:30em; /* ie6 */ |
21 | } |
22 | #flickrautocomplete .flickrImg { |
23 | width:6em;height:6em;padding:.1em;vertical-align:middle; |
24 | } |
view plain | print | ? |
Markup:
1 | <h3>Find photos by tag and collect your selections:</h3> |
2 | <div id="flickrselections"> |
3 | <h5>Selections</h5> |
4 | <div id="photos"></div> |
5 | </div> |
6 | |
7 | <div id="flickrautocomplete"> |
8 | <input id="flickrinput" type="text"> |
9 | <div id="flickrcontainer"></div> |
10 | </div> |
view plain | print | ? |
JavaScript:
1 | YAHOO.example.ACFlickr = function() { |
2 | // Set up a local proxy to the Flickr webservice |
3 | var myDS = new YAHOO.util.XHRDataSource("assets/php/flickr_proxy.php"); |
4 | myDS.responseSchema = { |
5 | resultNode: "photo", |
6 | fields: ["title", "id", "owner", "secret", "server"] |
7 | }; |
8 | myDS.responseType = YAHOO.util.XHRDataSource.TYPE_XML; |
9 | myDS.maxCacheEntries = 100; |
10 | |
11 | // Instantiate AutoComplete |
12 | var myAC = new YAHOO.widget.AutoComplete("flickrinput","flickrcontainer", myDS); |
13 | myAC.resultTypeList = false; |
14 | myAC.suppressInputUpdate = true; |
15 | myAC.generateRequest = function(sQuery) { |
16 | return "?method=flickr.photos.search&tags="+sQuery; |
17 | }; |
18 | var getImgUrl = function(oPhoto, sSize) { |
19 | var sId = oPhoto.id; |
20 | var sSecret = oPhoto.secret; |
21 | var sServer = oPhoto.server; |
22 | var sUrl = "http://static.flickr.com/" + |
23 | sServer + |
24 | "/" + |
25 | sId + |
26 | "_" + |
27 | sSecret + |
28 | "_"+ (sSize || "s") +".jpg"; |
29 | return "<img src='" + sUrl + "' class='flickrImg'>"; |
30 | } |
31 | |
32 | myAC.formatResult = function(oResultItem, sQuery) { |
33 | // This was defined by the schema array of the data source |
34 | var sTitle = oResultItem.title; |
35 | var sMarkup = getImgUrl(oResultItem) + " " + sTitle; |
36 | return (sMarkup); |
37 | }; |
38 | myAC.itemSelectEvent.subscribe(function(sType, aArgs){ |
39 | var oPhoto = aArgs[2]; |
40 | YAHOO.util.Dom.get("photos").innerHTML = |
41 | "<p>"+getImgUrl(oPhoto, "m")+"</p>"+YAHOO.util.Dom.get("photos").innerHTML |
42 | }); |
43 | |
44 | return { |
45 | oDS: myDS, |
46 | oAC: myAC |
47 | }; |
48 | }(); |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 195ms (+69) 3:19:42 PM:
LogReader instance0
LogReader initialized
INFO 126ms (+2) 3:19:42 PM:
AutoComplete instance0 flickrinput
AutoComplete initialized
INFO 124ms (+124) 3:19:42 PM:
DataSource instance0
DataSource initialized
INFO 0ms (+0) 3:19:42 PM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings