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.
Implementers who are working with data from third-party sources, user input data, or otherwise untrustworthy sources should be sure to read the Security Considerations section of the AutoComplete user guide.
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 | <label for="flickrinput">Find photos by tag and collect your selections:</label> |
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 YQL |
3 | var myDS = new YAHOO.util.XHRDataSource("assets/php/yql_proxy.php?format=xml&q=select%20*%20from%20flickr.photos.search"); |
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 "%20where%20tags%3D%22"+sQuery+"%22"; |
17 | }; |
18 | |
19 | // Helper function to generate the img tag |
20 | var getImgMarkup = function(oPhoto, sSize) { |
21 | var sUrl = "http://static.flickr.com/" + |
22 | oPhoto.server + |
23 | "/" + |
24 | oPhoto.id + |
25 | "_" + |
26 | oPhoto.secret + |
27 | "_"+ (sSize || "s") +".jpg"; |
28 | return '<img src="' + sUrl + '" class="flickrImg" alt="' + oPhoto.title + '">'; |
29 | }; |
30 | |
31 | // Helper function to generate the link tag |
32 | var getImgLink = function(oPhoto) { |
33 | return '<a href="http://www.flickr.com/photos/' + oPhoto.owner + "/" + oPhoto.id + '">'; |
34 | }; |
35 | |
36 | // Custom formatting to display photos in the suggestions container |
37 | myAC.formatResult = function(oResultItem, sQuery) { |
38 | // This was defined by the schema array of the data source |
39 | var sTitle = oResultItem.title, |
40 | sMarkup = getImgMarkup(oResultItem) + " " + sTitle; |
41 | return (sMarkup); |
42 | }; |
43 | |
44 | // Selecting a photo adds it to the selections area |
45 | myAC.itemSelectEvent.subscribe(function(sType, aArgs){ |
46 | var oPhoto = aArgs[2]; |
47 | YAHOO.util.Dom.get("photos").innerHTML = |
48 | '<p>' + getImgLink(oPhoto) + |
49 | getImgMarkup(oPhoto, "m")+'</a></p>'+ |
50 | YAHOO.util.Dom.get("photos").innerHTML; |
51 | }); |
52 | |
53 | return { |
54 | oDS: myDS, |
55 | oAC: myAC |
56 | }; |
57 | }(); |
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.
Note: Logging and debugging is currently turned off for this example.
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings