JavaScript DHTML/YUI Library/Flickr

Материал из Web эксперт
Перейти к: навигация, поиск

Use AutoComplete to find images by tag from the Flickr webservice

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>

   <meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>Find Photos on Flickr</title> <style type="text/css"> /*margin and padding on body element

 can introduce errors in determining
 element position and are not recommended;
 we turn them off as a foundation for YUI
 CSS treatments. */

body {

 margin:0;
 padding:0;

} </style> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/autocomplete/assets/skins/sam/autocomplete.css" /> <script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/animation/animation-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/connection/connection-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script> <script type="text/javascript" src="yui_2.7.0b-lib/autocomplete/autocomplete-min.js"></script>


<style type="text/css">

  1. flickrselections {
   float:right;
   width:240px;
   padding:10px;
   background-color:#FFA928;

}

  1. flickrselections h5 {
   color:#009;
   margin:0;

} /* custom styles for scrolling container */

  1. flickrautocomplete {
   width:15em; /* set width of widget here */
   padding-bottom:2em;

}

  1. flickrautocomplete .yui-ac-content {
   max-height:30em;overflow:auto;overflow-x:hidden; /* set scrolling */
   _height:30em; /* ie6 */

}

  1. flickrautocomplete .flickrImg {
   width:6em;height:6em;padding:.1em;vertical-align:middle;

} </style>


</head> <body class=" yui-skin-sam">

Find Photos on Flickr

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. </div>

Find photos by tag and collect your selections:

Selections
 <input id="flickrinput" type="text">

<script type="text/javascript"> YAHOO.example.ACFlickr = function() {

   // Set up a local proxy to the Flickr webservice
   var myDS = new YAHOO.util.XHRDataSource("yui_2.7.0b-assets/autocomplete-assets/php/flickr_proxy.php");
   myDS.responseSchema = {
       resultNode: "photo",
       fields: ["title", "id", "owner", "secret", "server"]
   };
   myDS.responseType = YAHOO.util.XHRDataSource.TYPE_XML;
   myDS.maxCacheEntries = 100;
   
   // Instantiate AutoComplete
   var myAC = new YAHOO.widget.AutoComplete("flickrinput","flickrcontainer", myDS);
   myAC.resultTypeList = false;
   myAC.suppressInputUpdate = true;
   myAC.generateRequest = function(sQuery) {
       return "?method=flickr.photos.search&tags="+sQuery;
   };
   var getImgUrl = function(oPhoto, sSize) {
       var sId = oPhoto.id;
       var sSecret = oPhoto.secret;
       var sServer = oPhoto.server;
       var sUrl = "http://static.flickr.ru/" +
           sServer +
           "/" +
           sId +
           "_" +
           sSecret +
           "_"+ (sSize || "s") +".jpg";
       return "<img src="" + sUrl + "" class="flickrImg">";
   }
   
   myAC.formatResult = function(oResultItem, sQuery) {
       // This was defined by the schema array of the data source
       var sTitle = oResultItem.title;
       var sMarkup = getImgUrl(oResultItem) + " " + sTitle;
       return (sMarkup);
   };
   myAC.itemSelectEvent.subscribe(function(sType, aArgs){
       var oPhoto = aArgs[2];
       YAHOO.util.Dom.get("photos").innerHTML = 
"<p>"+getImgUrl(oPhoto, "m")+"

"+YAHOO.util.Dom.get("photos").innerHTML
   });
   
   return {
       oDS: myDS,
       oAC: myAC
   };

}(); </script>

</body> </html>


 </source>
   
  
<A href="http://www.wbex.ru/Code/JavaScriptDownload/yui_2.7.0b.zip">yui_2.7.0b.zip( 4,431 k)</a>