]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
updated flickr [#248]
authorAnna Sobiepanek <annaopensource@gmail.com>
Tue, 5 Apr 2011 19:19:52 +0000 (15:19 -0400)
committerAnna Sobiepanek <annaopensource@gmail.com>
Tue, 5 Apr 2011 19:19:52 +0000 (15:19 -0400)
plugins/flickr/popcorn.flickr.html
plugins/flickr/popcorn.flickr.js
plugins/flickr/popcorn.flickr.unit.html
plugins/flickr/popcorn.flickr.unit.js

index 6988b7acf11dda9b1f52b582d9b0c0ee84b17030..8765e95ea9340417324c03225ae7f61edd17d131 100644 (file)
@@ -4,11 +4,17 @@
   <title>Popcorn Flickr Plug-in Demo</title>
   
   <script src="../../popcorn.js"></script>
-  <script src="../../test/jquery.js"></script>
   <script src="popcorn.flickr.js"></script>
   <script>
     document.addEventListener('DOMContentLoaded', function () {
       var p = Popcorn('#video')
+      .flickr({
+        start: 0, // seconds
+        end: 5, // seconds
+        username: 'AniaSob',
+        api_key: 'd1d249260dd1673ec8810c8ce5150ae1',
+        target: 'flickrdiv'
+      } )
       .flickr({
         start: 5, // seconds
         end: 15, // seconds
@@ -37,8 +43,8 @@
 </head>
 <body>
   <h1 id="qunit-header">Popcorn Flickr Plug-in Demo</h1>
-  <p>This plugin requires jquery</p>
-  <p>Flickr images by '35034346917@N01' will appear at 5 seconds and disappear at 15 seconds.
+  <p>Flickr images by 'AniaSob' will appear at 0 seconds and disappear at 5 seconds.
+  <br />Flickr images by '35034346917@N01' will appear at 5 seconds and disappear at 15 seconds.
   <br />Flickr images tagged 'georgia' will appear at 20 seconds and disappear at 30 seconds.
   <br />Flickr images by '35034346917@N01' and tagged 'georgia' will appear at 35 seconds and disappear at 45 seconds.</p>
   <div>
index e7ccf894efedac3c59179f70f62a2709901c482a..8c1583e3ea5c0ffcb3583c7ecedd6950668a9a56 100644 (file)
@@ -1,20 +1,24 @@
-// PLUGIN: FLICKR
-
+// PLUGIN: Flickr
 (function (Popcorn) {
+  
   /**
    * Flickr popcorn plug-in 
    * Appends a users Flickr images to an element on the page.
-   * Options parameter will need a start, end, target and userid.
+   * Options parameter will need a start, end, target and userid or username and api_key.
    * Optional parameters are numberofimages, height, width, padding, and border
    * Start is the time that you want this plug-in to execute
    * End is the time that you want this plug-in to stop executing
    * Userid is the id of who's Flickr images you wish to show
    * Tags is a mutually exclusive list of image descriptor tags
+   * Username is the username of who's Flickr images you wish to show 
+   *  using both userid and username is redundant
+   *  an api_key is required when using username
+   * Api_key is your own api key provided by Flickr 
    * Target is the id of the document element that the images are
    *  appended to, this target element must exist on the DOM
-   * Numberofimages specify the number of images to retreive from flickr, defaults to 8
-   * Height the height of the component, defaults to '50px'
-   * Width the width of the component, defaults to '50px'
+   * Numberofimages specify the number of images to retreive from flickr, defaults to 4
+   * Height the height of the image, defaults to '50px'
+   * Width the width of the image, defaults to '50px'
    * Padding number of pixels between images, defaults to '5px'
    * Border border size in pixels around images, defaults to '0px'
    * 
         } )
    *
    */
-  Popcorn.plugin( "flickr" , {
-
-      manifest: {
-        about:{
-          name:    "Popcorn Flickr Plugin",
-          version: "0.1.1",
-          author:  "Scott Downe, Steven Weerdenburg",
-          website: "http://scottdowne.wordpress.com/"
-        },
-        options:{
-          start   : {elem:'input', type:'number', label:'In'},
-          end     : {elem:'input', type:'number', label:'Out'},
-          userid  : {elem:'input', type:'text',   label:'Source'},
-          tags    : {elem:'input', type:'text',   label:'Tags'},
-          target  :  'Flickr-container',
-          height  : {elem:'input', type:'text', label:'Height'},
-          width   : {elem:'input', type:'text', label:'Width'},
-          padding : {elem:'input', type:'text', label:'Padding'},
-          border  : {elem:'input', type:'text', label:'Border'},
-          numberofimages : {elem:'input', type:'text', label:'Number of Images'}
-        }
-      },
+  Popcorn.plugin( "flickr" , function( options ) {
+    var containerDiv,
+        _userid,
+        _uri,
+        _link,
+        _image,
+        _count   = options.numberofimages || 4 ,
+        _height  = options.height || "50px",
+        _width   = options.width || "50px",
+        _padding = options.padding || "5px",
+        _border  = options.border || "0px",
+        i;
 
-      _setup: function( options ) {
-        options.container = document.createElement( 'div' );
-        options.container.style.display = "none";
-        
-        if ( document.getElementById( options.target ) ) {
-          document.getElementById( options.target ).appendChild( options.container );
-        }
-        
-        var height  = options.height || "50px",
-            width   = options.width || "50px",
-            count   = options.numberofimages || 4,
-            padding = options.padding || "5px",
-            tags    = options.tags || "",
-            userid  = options.userid || "",
-            border  = options.border || "0px",
-            uri = "http://api.flickr.com/services/feeds/photos_public.gne?";
-        
-        if ( userid ) {
-          uri += "id="+userid+"&";
-        }
-        
-        if ( tags ) {
-          uri += "tags="+tags+"&";
-        }
-
-        uri += "lang=en-us&format=json&jsoncallback=flickr";
+    // create a new div this way anything in the target div is left intact
+    // this is later populated with Flickr images
+    containerDiv               = document.createElement( "div" );
+    containerDiv.id            = "flickr"+ i;
+    containerDiv.style.width   = "100%";
+    containerDiv.style.height  = "100%";
+    containerDiv.style.display = "none";
+    i++;
+    
+    // ensure the target container the user chose exists
+    if ( document.getElementById( options.target ) ) {
+      document.getElementById( options.target ).appendChild( containerDiv );
+    } else { 
+      throw ( "flickr target container doesn't exist" ); 
+    }
+    
+    // get the userid from Flickr API by using the username and api_key
+    var isUserIDReady = function() {
+      if ( !_userid ) {
+        _uri  = "http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&";        
+        _uri += "username=" + options.username + "&api_key=" + options.api_key + "&format=json&jsoncallback=flickr";
+        Popcorn.xhr.getJSONP( _uri, function(data) {
+          _userid = data.user.nsid;
+          getFlickrData();
+        });
+      } else {
+        setTimeout(function () {
+          isUserIDReady();
+        }, 5);
+      }
+    };
+    // get the photos from Flickr API by using the user_id and/or tags
+    var getFlickrData = function() { 
+      _uri  = "http://api.flickr.com/services/feeds/photos_public.gne?";        
+      _uri += "id=" + _userid + "&";
+      if ( options.tags ) {
+        _uri += "tags=" + options.tags + "&";
+      }
+      _uri += "lang=en-us&format=json&jsoncallback=flickr";
+      
+      Popcorn.xhr.getJSONP( _uri, function( data ) {
+        containerDiv.innerHTML = "<p style='padding:" + _padding + ";'>" + data.title + "<p/>";
         
-        Popcorn.xhr.getJSONP( uri, function( data ) {
-          options.container.innerHTML = "<p style='padding:" + padding + ";'>" + data.title + "<p/>";
-          
-          Popcorn.forEach( data.items, function ( item, i ) {
-            if ( i < count ) {
-              var link = document.createElement('a');
-              link.setAttribute( 'href', item.link );
-              link.setAttribute( "target", "_blank" );
-              var image = document.createElement( 'img' );
-              image.setAttribute( 'src', item.media.m );
-              image.setAttribute( 'height', height );
-              image.setAttribute( 'width', width );
-              image.setAttribute( 'style', 'border:' + border + ';padding:' + padding );
-              link.appendChild( image );
-              
-              options.container.appendChild( link );
-            } else {
-              return false;
-            }
-          });
+        Popcorn.forEach( data.items, function ( item, i ) {
+          if ( i < _count ) {
+            _link = document.createElement( 'a' );
+            _link.setAttribute( 'href', item.link );
+            _link.setAttribute( "target", "_blank" );
+            _image = document.createElement( 'img' );
+            _image.setAttribute( 'src', item.media.m );
+            _image.setAttribute( 'height',_height );
+            _image.setAttribute( 'width', _width );
+            _image.setAttribute( 'style', 'border:' + _border + ';padding:' + _padding );
+            _link.appendChild( _image );         
+            containerDiv.appendChild( _link );
+          } else {
+            return false;
+          }
         });
-      },
+      });
+    };
+    if ( options.userid ) {
+      _userid = options.userid;
+      getFlickrData();
+      
+    } else if ( options.username && options.api_key ) {
+      isUserIDReady();
+    }
+    return {
       /**
-       * @member Flickr 
-       * The start function will be executed when the currentTime 
-       * of the video  reaches the start time provided by the 
+       * @member flickr
+       * The start function will be executed when the currentTime
+       * of the video reaches the start time provided by the
        * options variable
        */
-      start: function( event, options ) {
-        options.container.style.display = "inline";
+      start: function( event, options ){
+        containerDiv.style.display = "inline";
       },
       /**
-       * @member Flickr 
-       * The end function will be executed when the currentTime 
-       * of the video  reaches the end time provided by the 
+       * @member flickr
+       * The end function will be executed when the currentTime
+       * of the video reaches the end time provided by the
        * options variable
        */
-      end: function( event, options ) {
-        options.container.style.display = "none";
+      end: function( event, options ){      
+          containerDiv.style.display = "none";       
       }
-    });
-
+    };
+  },
+  {
+    about:{
+      name:    "Popcorn Flickr Plugin",
+      version: "0.1.1",
+      author:  "Scott Downe, Steven Weerdenburg, Annasob",
+      website: "http://scottdowne.wordpress.com/"
+    },
+    options:{
+      start          : {elem:'input', type:'number', label:'In'},
+      end            : {elem:'input', type:'number', label:'Out'},
+      userid         : {elem:'input', type:'text',   label:'UserID'},
+      tags           : {elem:'input', type:'text',   label:'Tags'},
+      username       : {elem:'input', type:'text',   label:'Username'},
+      api_key        : {elem:'input', type:'text',   label:'Api_key'},
+      target         :  'Flickr-container',
+      height         : {elem:'input', type:'text', label:'Height'},
+      width          : {elem:'input', type:'text', label:'Width'},
+      padding        : {elem:'input', type:'text', label:'Padding'},
+      border         : {elem:'input', type:'text', label:'Border'},
+      numberofimages : {elem:'input', type:'text', label:'Number of Images'}
+    }
+  });
 })( Popcorn );
index aa40c5395681eeb23bdad1841381df739dcb9ee5..356b11d2b0b8d4bbe10311894b063c2b18aa9194 100644 (file)
@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html>
 <head>
-  <title>Popcorn API</title>
+  <title>Popcorn Flickr</title>
   <link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
   <script src="../../test/qunit/qunit.js"></script>
   <!-- 
index d41d8b597b057e0032b573945b24d985484ad082..76d08a6183cd75027a006680061b1fad942bc551 100644 (file)
@@ -1,7 +1,7 @@
 test("Popcorn Flickr Plugin", function () {
   
   var popped = Popcorn("#video"),
-      expects = 5, 
+      expects = 6, 
       count = 0,
       flickrdiv = document.getElementById('flickrdiv');
   
@@ -27,7 +27,15 @@ test("Popcorn Flickr Plugin", function () {
     userid: '35034346917@N01',
     numberofimages: '1',
     target: 'flickrdiv'
-  } );
+  } )
+  .flickr({
+    start: 4, // seconds
+    end: 7,   // seconds
+    username: 'AniaSob',
+    api_key: 'd1d249260dd1673ec8810c8ce5150ae1',
+    numberofimages: '1',
+    target: 'flickrdiv'
+  } );;
 
   popped.exec( 2, function() {
     ok( /display: inline;/.test( flickrdiv.innerHTML ), "Div contents are displayed" );
@@ -35,8 +43,15 @@ test("Popcorn Flickr Plugin", function () {
     ok( /img/.test( flickrdiv.innerHTML ), "An image exists" );
     plus();
   });
+  
+  popped.exec( 5, function() {
+    ok( /display: inline;/.test( flickrdiv.innerHTML ), "Div contents are displayed" );
+    plus();
+    ok( /img/.test( flickrdiv.innerHTML ), "An image exists" );
+    plus();
+  });
 
-  popped.exec( 4, function() {
+  popped.exec( 7, function() {
     ok( /display: none;/.test( flickrdiv.innerHTML ), "Div contents are hidden again" );
     plus();
   });