]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
still need tests
authorAnna Sobiepanek <annaopensource@gmail.com>
Fri, 17 Dec 2010 17:11:20 +0000 (12:11 -0500)
committerAnna Sobiepanek <annaopensource@gmail.com>
Fri, 17 Dec 2010 17:11:20 +0000 (12:11 -0500)
plugins/wikipedia/popcorn.wikipedia.html
plugins/wikipedia/popcorn.wikipedia.js

index 8d93137413f0ea4e8d8af6a98a7d301a0c0a23a1..6d45ed745b54bb9936438522e3cfc9c39cf3c3ab 100644 (file)
@@ -4,24 +4,17 @@
   <title>Popcorn WebPage Plug-in Demo</title>
   
   <script src="../../popcorn.js"></script>
-  <script src="popcorn.webpage.js"></script>
+  <script src="popcorn.wikipedia.js"></script>
   <script>
     document.addEventListener('DOMContentLoaded', function () {
       var p = Popcorn('#video')
+      .volume(0)
       .play() 
-      .webpage({
-        id: "webpages-a", 
-        start: 5, // seconds
-        end: 15, // seconds
-        src: 'http://www.webmademovies.org',
-        target: 'webpagediv'
-      } )
-      .webpage({
-        id: "webpages-b", 
-        start: 20, // seconds
-        end: 45, // seconds
-        src: 'http://zenit.senecac.on.ca/wiki/index.php/Processing.js',
-        target: 'webpagediv2'
+      .wikipedia({
+        start: 10, // seconds
+        end: 50, // seconds
+        src: 'http://en.wikipedia.org/wiki/Cape_Town',
+        target: 'wikidiv'
       } );
 
     }, false);
@@ -50,8 +43,7 @@
 
     </video> 
   </div>
-  <div id="webpagediv" width="50%" height="50%">
-  <div id="webpagediv2">
+  <div id="wikidiv" width="50%" height="50%">
   </div>
 </body>
 </html>
index 136970dd57b41d6a414a35a4e5fc30338267a202..722c6f2a89f4d66dba9f5945092aa415ed332eef 100644 (file)
@@ -1,4 +1,12 @@
-// PLUGIN: WEBPAGE
+// PLUGIN: WIKIPEDIA
+
+
+// wikipedia callback functions executes when data is retrieved from the wiki
+// stores the data in a global variable
+var wikidatastring;
+var wikiCallback = function (data) { 
+  wikidatastring = data; 
+};
 
 (function (Popcorn) {
   
    * Target is the id of the document element that the iframe needs to be attached to, 
    * this target element must exist on the DOM
    * Lang (optional, defaults to english)is the language in which the article is in.
-   * Src is the url of the article
-   * NumOfWords (optional, defaults to 200) is  the number of words you want displaid. 
+   * Src is the url of the article 
+   * Title (optional) is the title of the article
+   * NumOfWords (optional, defaults to 200) is  the number of words you want displaid.  
    *
    * @param {Object} options
    * 
    * Example:
      var p = Popcorn('#video')
         .wikipedia({
-          id: "webpages-a", 
           start: 5, // seconds
           end: 15, // seconds
-          src: 'http://www.webmademovies.org',
+          src: 'http://en.wikipedia.org/wiki/Cape_Town',
           target: 'webpagediv'
         } )
    *
    */
+   
+  
   Popcorn.plugin( "wikipedia" , (function(){
       
-    var temp, length;
+    var temp, length, link, p, desc, text;
     
     
     return {
+      /**
+       * @member wikipedia 
+       * The setup function will get all of the needed 
+       * items in place before the start function is called
+       */
+      _setup : function( options ) {
+        // if no language was specified default to english
+        if (typeof options.lang === 'undefined') { options.lang ="en"; }
+        temp    = document.getElementById( options.target );
+        length  = options.numOfWords || 200;
+        
+        //get the wiki article on a separate thread
+        setTimeout(function() {
+          getJson("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");
+        }, 1000);
+        var getJson  = function(url) {
+          var head   = document.getElementsByTagName("head")[0];
+          var script = document.createElement("script");
+          // once you get the data store it
+          script.onload = function () {
+            if (wikidatastring) {
+              // create a link that the user can use to view the whole article
+              link = document.createElement('a');
+              link.setAttribute('href', options.src);
+              link.setAttribute('target', '_blank');
+              // create a paragraph that the title of the article can go into
+              p = document.createElement('p');
+              p.innerHTML = wikidatastring.parse.displaytitle;
+              link.appendChild(p);
+              // get the first 140 characters of the wiki content
+              desc = document.createElement('p');
+              text = wikidatastring.parse.text["*"].substr(wikidatastring.parse.text["*"].indexOf('<p>'));
+              text = text.replace(/((<(.|\n)+?>)|(\((.*?)\) )|(\[(.*?)\]))/g, "");
+              desc.innerHTML = text.substr(0,  length ) + " ...";
+            }
+          };  
+          script.src = url;
+          head.insertBefore( script, head.firstChild );
+        };
+        
+      },
       /**
        * @member wikipedia 
        * The start function will be executed when the currentTime 
        * options variable
        */
       start: function(event, options){
-        if (typeof options.lang === 'undefined') {options.lang="en";}
-        temp    = document.getElementById( options.target );
-        length = options.numOfWords || 200;
-        
+        temp.appendChild(link);
+        temp.appendChild(desc);
       },
       /**
        * @member wikipedia 
        * options variable
        */
       end: function(event, options){
-        temp.removeChild(iframe);
+        temp.removeChild(link);
+        temp.removeChild(desc);
       }
       
     };