]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#340] plugins now using getScript
authorScottDowne <sdowne@learn.senecac.on.ca>
Tue, 1 Mar 2011 16:24:52 +0000 (11:24 -0500)
committerScottDowne <sdowne@learn.senecac.on.ca>
Tue, 1 Mar 2011 16:24:52 +0000 (11:24 -0500)
plugins/flickr/popcorn.flickr.js
plugins/googlemap/popcorn.googlemap.js
plugins/googlenews/popcorn.googlenews.html
plugins/googlenews/popcorn.googlenews.js
plugins/lastfm/popcorn.lastfm.js
plugins/subtitle/popcorn.subtitle.html
plugins/subtitle/popcorn.subtitle.js
plugins/twitter/popcorn.twitter.js
plugins/wikipedia/popcorn.wikipedia.js
plugins/wikipedia/popcorn.wikipedia.unit.js

index 6a731f5a6707fbf89b6e739461aa60b27a9bc81e..e7ccf894efedac3c59179f70f62a2709901c482a 100644 (file)
@@ -87,7 +87,6 @@
         uri += "lang=en-us&format=json&jsoncallback=flickr";
         
         Popcorn.xhr.getJSONP( uri, function( data ) {
-        //$.getJSON( uri, function( data ) {
           options.container.innerHTML = "<p style='padding:" + padding + ";'>" + data.title + "<p/>";
           
           Popcorn.forEach( data.items, function ( item, i ) {
index 3c765230a35f67b27c40dfddf6fabb773eb317cf..1a45eaf549a3ce33e0b32612be15dd4a150a4849 100644 (file)
@@ -32,7 +32,10 @@ var googleCallback;
   *
   */
 
-  var newdiv, i = 1, _mapFired = false, _mapLoaded = false;
+  var newdiv,
+      i = 1,
+      _mapFired = false,
+      _mapLoaded = false;
 
   // callback function fires when the script is run
   googleCallback = function() {
@@ -41,23 +44,19 @@ var googleCallback;
   // insert google api script once
   if (!_mapFired) {
     _mapFired = true;
-    var loadScriptTime = (new Date).getTime();
-    var head = document.getElementsByTagName("head")[0];
-    var script = document.createElement("script");
-
-    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=googleCallback";
-    script.type = "text/javascript";
-    head.insertBefore( script, head.firstChild );
+    Popcorn.getScript("http://maps.google.com/maps/api/js?sensor=false&callback=googleCallback");
   }
 
 
   Popcorn.plugin( "googlemap" , function( options ) {
-    var newdiv, map, location;
+    var newdiv,
+        map,
+        location;
 
     // create a new div this way anything in the target div
     // this is later passed on to the maps api
     newdiv = document.createElement("div");
-    newdiv.id = "actualmap"+i;
+    newdiv.id = "actualmap" + i;
     newdiv.style.width = "100%";
     newdiv.style.height = "100%";
     i++;
index 908eaf5c2279b07c7575393630739bc2104db899..a35a344b415ce80eed13c4a3164959322bb0ae9e 100644 (file)
@@ -5,9 +5,6 @@
   
   <script src="../../popcorn.js"></script>
 
-  <script src="http://google.com/jsapi"></script>
-  <script>google.load("elements", "1", {packages : ["newsshow"]});</script>
-
   <script src="popcorn.googlenews.js"></script>
   <script>
     document.addEventListener('DOMContentLoaded', function () {
index 69cfaf957916929ff533a921e958296279cd5919..acc5e8a42a0636e7a953619e30e861092b76c0c3 100644 (file)
@@ -1,7 +1,12 @@
 // PLUGIN: Google News
 
 (function (Popcorn) {
-  
+  var scriptLoaded = false;
+
+  Popcorn.getScript( "http://www.google.com/jsapi", function() {
+    google.load("elements", "1", {packages : ["newsshow"], callback: function() {scriptLoaded = true;}});
+  });
+
   /**
    */
   Popcorn.plugin( "googlenews" , {
         if ( document.getElementById( options.target ) ) {
           document.getElementById( options.target ).appendChild( options.container );
         }
-        var newsShow = new google.elements.NewsShow( options.container, {
-          format : "300x250",
-          queryList : [
-            { q: options.topic || "Top Stories" }
-          ]
-        } );
+
+        var readyCheck = setInterval(function() {
+          if ( !scriptLoaded ) {
+            return;
+          }
+          clearInterval(readyCheck);
+
+          var newsShow = new google.elements.NewsShow( options.container, {
+            format : "300x250",
+            queryList : [
+              { q: options.topic || "Top Stories" }
+            ]
+          } );
+
+        }, 5);
+
         options.container.style.display = "none";
 
       },
index 880d7a0faa24b8889a4fe717ff6e8b621bde8f84..c156a6cdc0cb3245c01f1d8611ee2efe6e510a0e 100644 (file)
@@ -1,5 +1,28 @@
 // PLUGIN: LASTFM
-var lastFMcallback;
+    var _artists = [],
+        lastFMcallback = function(data){
+          if (data.artist) {
+            var htmlString = '<h3>'+data.artist.name+'</h3>';
+            htmlString += '<a href="'+data.artist.url+'" target="_blank" style="float:left;margin:0 10px 0 0;"><img src="'+ data.artist.image[2]['#text'] +'" alt=""></a>';
+            htmlString += '<p>'+ data.artist.bio.summary +'</p>';
+            htmlString += '<hr /><p><h4>Tags</h4><ul>';
+            
+            Popcorn.forEach( data.artist.tags.tag, function( val, i) {
+              htmlString += '<li><a href="'+ val.url +'">'+ val.name +'</a></li>';
+            });
+            
+            htmlString += '</ul></p>';
+            htmlString += '<hr /><p><h4>Similar</h4><ul>';
+            
+            Popcorn.forEach( data.artist.similar.artist, function( val, i) {
+              htmlString += '<li><a href="'+ val.url +'">'+ val.name +'</a></li>';
+            });
+            
+            htmlString += '</ul></p>';
+            
+            _artists[data.artist.name.toLowerCase()] = htmlString;
+          }
+        };
 
 (function (Popcorn) {
   
@@ -29,7 +52,6 @@ var lastFMcallback;
    */
   Popcorn.plugin( "lastfm" , (function(){
       
-    var _artists = [];
     
     return {
       manifest: {
@@ -59,40 +81,12 @@ var lastFMcallback;
         }
         
         if(!_artists[options.artist]) {
-          var loadScriptTime = (new Date).getTime();
-          var head = document.getElementsByTagName('head')[0];
-          var script = document.createElement('script');
-         
-          script.src = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+ options.artist +"&api_key="+options.apikey+"&format=json&callback=lastFMcallback";
-          script.type = "text/javascript";
-          head.insertBefore( script, head.firstChild );
-          
+
           _artists[options.artist] = "Unknown Artist";
+          Popcorn.getScript("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+ options.artist +"&api_key="+options.apikey+"&format=json&callback=lastFMcallback");
+          
         }
         
-        lastFMcallback = function(data){
-          if (data.artist) {
-            var htmlString = '<h3>'+data.artist.name+'</h3>';
-            htmlString += '<a href="'+data.artist.url+'" target="_blank" style="float:left;margin:0 10px 0 0;"><img src="'+ data.artist.image[2]['#text'] +'" alt=""></a>';
-            htmlString += '<p>'+ data.artist.bio.summary +'</p>';
-            htmlString += '<hr /><p><h4>Tags</h4><ul>';
-            
-            Popcorn.forEach( data.artist.tags.tag, function( val, i) {
-              htmlString += '<li><a href="'+ val.url +'">'+ val.name +'</a></li>';
-            });
-            
-            htmlString += '</ul></p>';
-            htmlString += '<hr /><p><h4>Similar</h4><ul>';
-            
-            Popcorn.forEach( data.artist.similar.artist, function( val, i) {
-              htmlString += '<li><a href="'+ val.url +'">'+ val.name +'</a></li>';
-            });
-            
-            htmlString += '</ul></p>';
-            
-            _artists[data.artist.name.toLowerCase()] = htmlString;
-          }
-        };
       },
       /**
        * @member LastFM 
index 96a7207f2ac851fb0fe2533c650faeca5f9b8880..c01a405d52389af15698433cfaa87ef5371f3fcb 100644 (file)
@@ -4,8 +4,6 @@
   <title>Popcorn Subtitle Plug-in Demo</title>
   
   <script src="../../popcorn.js"></script>
-  <script src="http://google.com/jsapi"></script>
-  <script>google.load("language", "1");</script>
 
   <script src="popcorn.subtitle.js"></script>
   <script>
index 27ad26b188a4cd1009674cb4bc6bc995b7d623e3..5755331abe1df15cf3dd4c798d18762b712e7f09 100644 (file)
@@ -1,7 +1,13 @@
 // PLUGIN: Subtitle
 
 (function (Popcorn) {
-  
+
+  var scriptLoaded = false;
+
+  Popcorn.getScript( "http://www.google.com/jsapi", function() {
+    google.load("language", "1", {callback: function() {scriptLoaded = true;}});
+  });
+
   /**
    */
 
     } );
   };
 
-
-/*
-      // Find element offset
-      if (element.offsetParent) {
-        do {
-          offsetX += element.offsetLeft;
-          offsetY += element.offsetTop;
-        } while ((element = element.offsetParent));
-      }
-*/
-
   Popcorn.plugin( "subtitle" , {
     
       manifest: {
         options.toggleSubtitles = function() {};
         options.that = this;
         
-
-        if ( options.languagesrc ) {
-          options.showSubtitle = translate;
-          options.languageSrc = document.getElementById( options.languagesrc );
-          options.selectedLanguage = options.languageSrc.options[ options.languageSrc.selectedIndex ].value;
-
-          if ( !this.languageSources ) {
-            this.languageSources = {};
+        var readyCheck = setInterval(function() {
+          if ( !scriptLoaded ) {
+            return;
           }
+          clearInterval(readyCheck);
 
-          
+          if ( options.languagesrc ) {
+            options.showSubtitle = translate;
+            options.languageSrc = document.getElementById( options.languagesrc );
+            options.selectedLanguage = options.languageSrc.options[ options.languageSrc.selectedIndex ].value;
+
+            if ( !this.languageSources ) {
+              this.languageSources = {};
+            }
 
-          if ( !this.languageSources[ options.languagesrc ] ) {
-            this.languageSources[ options.languagesrc ] = {};
+            if ( !this.languageSources[ options.languagesrc ] ) {
+              this.languageSources[ options.languagesrc ] = {};
             
-          }
+            }
 
-          if ( !this.languageSources[ options.languagesrc ][ options.target ] ) {
-            this.languageSources[ options.languagesrc ][ options.target ] = true;
+            if ( !this.languageSources[ options.languagesrc ][ options.target ] ) {
+              this.languageSources[ options.languagesrc ][ options.target ] = true;
 
-            options.languageSrc.addEventListener( "change", function() {
+              options.languageSrc.addEventListener( "change", function() {
 
+                options.toggleSubtitles();
+                options.showSubtitle( options, options.container.innerHTML );
 
-              options.toggleSubtitles();
-              options.showSubtitle( options, options.container.innerHTML );
+              }, false );
 
-            }, false );
+            }
 
           }
+          if ( accessibility ) {
+            options.accessibility = accessibility;
 
-        }
+            options.toggleSubtitles = function() {
+              options.selectedLanguage = options.languageSrc.options[ options.languageSrc.selectedIndex ].value;
+              if ( options.accessibility.checked || options.selectedLanguage !== ( options.language || "") ) {
+                options.display = "inline";
+                options.container.style.display = options.display;
+              } else if ( options.selectedLanguage === ( options.language || "") ) {
+                options.display = "none";
+                options.container.style.display = options.display;
+              }
+
+            };
+
+            options.accessibility.addEventListener( "change", options.toggleSubtitles, false );
+
+            // initiate it once to set starting state
+            options.toggleSubtitles();
+          }
+        }, 5);
 
-        if ( accessibility ) {
-          options.accessibility = accessibility;
 
-          options.toggleSubtitles = function() {
-            options.selectedLanguage = options.languageSrc.options[ options.languageSrc.selectedIndex ].value;
-            if ( options.accessibility.checked || options.selectedLanguage !== ( options.language || "") ) {
-              options.display = "inline";
-              options.container.style.display = options.display;
-            } else if ( options.selectedLanguage === ( options.language || "") ) {
-              options.display = "none";
-              options.container.style.display = options.display;
-            }
 
-          };
 
-          options.accessibility.addEventListener( "change", options.toggleSubtitles, false );
 
-          // initiate it once to set starting state
-          options.toggleSubtitles();
-        }
 
       },
       /**
index 107558c8723c4e7dc5ffe3ec4ec7e642ee78d89c..cb0e2c8a78e0977af2b704839d97443586c0d190 100644 (file)
    */
 
   if ( !window.TWTR ) {
-    var head = document.getElementsByTagName("head")[0] || document.documentElement,
-        script = document.createElement("script");
-
-    script.src = "http://widgets.twimg.com/j/2/widget.js";
-
-    head.insertBefore( script, head.firstChild );
+    Popcorn.getScript("http://widgets.twimg.com/j/2/widget.js");
   }
 
   Popcorn.plugin( "twitter" , {
index f81ee30ae665585d77a7f1653fc45a4f36beca0c..ce143803eea595848c4301c086d2cf769ca1bdaa 100644 (file)
@@ -87,11 +87,7 @@ var wikiCallback;
         options._fired = true;
       };
       
-      var head   = document.getElementsByTagName( "head" )[0];
-      var script = document.createElement( "script" );
-      script.src = "http://" + options.lang + ".wikipedia.org/w/api.php?action=parse&props=text&page=" + ( options.title || options.src.slice( options.src.lastIndexOf("/")+1) ) + "&format=json&callback=wikiCallback" + _guid;
-
-      head.insertBefore( script, head.firstChild );        
+      Popcorn.getScript("http://" + options.lang + ".wikipedia.org/w/api.php?action=parse&props=text&page=" + ( options.title || options.src.slice( options.src.lastIndexOf("/")+1) ) + "&format=json&callback=wikiCallback" + _guid);
     },
     /**
      * @member wikipedia 
index 89ff683a63eb90e0866956813cca03f67ec8af00..bf103f504cd13aae647595527c58fb9521a362f9 100644 (file)
@@ -1,7 +1,7 @@
 test("Popcorn wikipedia Plugin", function () {
   
   var popped        = Popcorn("#video"),
-      expects       = 9, 
+      expects       = 10, 
       count         = 0,
       theArticle    = document.getElementById('wikidiv'),
       wikiInterval,
@@ -49,9 +49,9 @@ test("Popcorn wikipedia Plugin", function () {
       plus();
       equals (theArticle.childElementCount, 2, "wikidiv now contains two child elements" );
       plus();
-      equals (theArticle.childElement[0].innerHTML, "this is an article", "wikidiv has the right title" );
+      equals (theArticle.childNodes[0].innerHTML, "this is an article", "wikidiv has the right title" );
       plus();
-      equals (theArticle.childElement[1].innerHTML, "Cape Town metropolitan municipality. It is the provincial capital and primate city ...", "wikidiv has the right content" );
+      equals (theArticle.childNodes[1].innerHTML, "Cape Town is the second-most populous city in South Africa, and the largest in land area, forming part of the City of Cape Town metropolitan municipality. It is the provincial capital and primate city ...", "wikidiv has the right content" );
       plus();
       clearInterval( wikiInterval );
     }
@@ -71,7 +71,7 @@ test("Popcorn wikipedia Plugin", function () {
       plus();
       equals (theArticle.childElementCount, 2, "wikidiv now contains two child elements" );
       plus();
-      equals (theArticle.childElement[1].innerHTML, "São Paulo is the largest city in Brazil, the largest city in the southern hemisphere, and the world's 7th largest metropolitan area. The city is the capital of the state of São Paulo, the most populou ...", "wikidiv has the right content" );
+      equals (theArticle.childNodes[1].innerHTML, "São Paulo is the largest city in Brazil, the largest city in the southern hemisphere, and the world's 7th largest metropolitan area. The city is the capital of the state of São Paulo, the most populou ...", "wikidiv has the right content" );
       plus();
       clearInterval( wikiInterval3 );
     }