]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
tagThisPerson plugin with simple tests [#247 state:peer-review-requested]
authorAnna Sobiepanek <annaopensource@gmail.com>
Fri, 7 Jan 2011 20:43:05 +0000 (15:43 -0500)
committerAnna Sobiepanek <annaopensource@gmail.com>
Fri, 7 Jan 2011 20:43:05 +0000 (15:43 -0500)
plugins/tagThisPerson/popcorn.tagThisPerson.html [new file with mode: 0644]
plugins/tagThisPerson/popcorn.tagThisPerson.js [new file with mode: 0644]
plugins/tagThisPerson/popcorn.tagThisPerson.unit.html [new file with mode: 0644]
plugins/tagThisPerson/popcorn.tagThisPerson.unit.js [new file with mode: 0644]

diff --git a/plugins/tagThisPerson/popcorn.tagThisPerson.html b/plugins/tagThisPerson/popcorn.tagThisPerson.html
new file mode 100644 (file)
index 0000000..b746292
--- /dev/null
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn tagThisPerson Plug-in Demo</title>
+  
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.tagThisPerson.js"></script>
+  <script>
+    document.addEventListener('DOMContentLoaded', function () {
+      var p = Popcorn('#video')
+      .play() 
+      .volume(0)
+      .tagThisPerson({
+        start: 0, // seconds
+        end: 10, // seconds
+        person: 'Anna Sob',
+        target: 'tagdiv'
+      } )
+      .tagThisPerson({
+        start: 5, // seconds
+        end: 15, // seconds
+        person: 'Scott',
+        target: 'tagdiv'
+      } )
+      .tagThisPerson({
+        start: 0, // seconds
+        end: 15, // seconds
+        person: 'Scott',
+        target: 'tagdiv2'
+      } )
+    }, false);
+
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn tagThisPerson Plug-in Demo</h1>
+  <p> A tag displaying Scott will appear at 0 seconds and disappear at 15 seconds.</p>
+  <p> A separate tag displaying Anna Sob will appear at 0 seconds and disappear at 10 seconds. At 5 seconds Scott will be appended to this tag.</p>
+  <div>
+    <video id='video' 
+      controls preload='none' 
+      width= '250px'
+      poster="../../test/poster.png"> 
+
+      <source id='mp4'
+        src="../../test/trailer.mp4"
+        type='video/mp4; codecs="avc1, mp4a"'> 
+
+      <source id='ogv'
+        src="../../test/trailer.ogv"
+        type='video/ogg; codecs="theora, vorbis"'> 
+
+      <p>Your user agent does not support the HTML5 Video element.</p> 
+
+    </video> 
+  </div>
+  <div id="tagdiv" width="50%" height="50%">  </div>
+
+  <div id="tagdiv2" width="50%" height="50%">  </div>
+
+</body>
+</html>
diff --git a/plugins/tagThisPerson/popcorn.tagThisPerson.js b/plugins/tagThisPerson/popcorn.tagThisPerson.js
new file mode 100644 (file)
index 0000000..de08976
--- /dev/null
@@ -0,0 +1,99 @@
+// PLUGIN: tagThisPerson
+
+(function (Popcorn) {
+  
+  /**
+   * tagThisPerson popcorn plug-in 
+   * Adds people's names to an element on the page.
+   * Options parameter will need a start, end, target and person.
+   * 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 
+   * Person is the name of the person who you want to tag
+   * Target is the id of the document element that the text needs to be 
+   * attached to, this target element must exist on the DOM
+   * 
+   * @param {Object} options
+   * 
+   * Example:
+     var p = Popcorn('#video')
+        .tagThisPerson({
+          start: 5, // seconds
+          end: 15, // seconds
+          person: '@annasob',
+          target: 'tagdiv'
+        } )
+   *
+   */
+  Popcorn.plugin( "tagThisPerson" , (function(){
+    
+    var peopleArray = [];
+    var People = function(){
+      this.name = "";
+      this.contains = {};
+      this.toString = function(target) {
+        var r = [];
+        for ( var j in this.contains) {
+          if (this.contains.hasOwnProperty(j)) {
+            r.push(" " + this.contains[j]);
+          }
+        }
+        return r.toString();
+      };
+    };
+    
+    return {
+      manifest: {
+        about:{
+          name: "Popcorn tagThisPerson Plugin",
+          version: "0.1",
+          author: "@annasob",
+          website: "annasob.wordpress.com"
+        },
+        options:{
+          start    : {elem:'input', type:'text', label:'In'},
+          end      : {elem:'input', type:'text', label:'Out'},
+          target   : 'tag-container',
+          text     : {elem:'input', type:'text', label:'Text'}
+        }
+      },
+      _setup: function(options) {
+        var exists = false;
+        // loop through the existing objects to ensure no duplicates
+        // the idea here is to have one object per unique options.target
+        for (var i = 0; i< peopleArray.length; i++) {
+          if (peopleArray[i].name === options.target ) {
+            options._p = peopleArray[i];  
+            exists = true;
+            break;
+          }
+        }
+        if (!exists) {
+          options._p = new People();
+          options._p.name = options.target;
+          peopleArray.push(options._p );
+        }
+      },
+      /**
+       * @member tagThisPerson 
+       * 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._p.contains[options.person] = options.person;
+        document.getElementById(options.target).innerHTML  = options._p.toString();
+      },
+      /**
+       * @member tagThisPerson 
+       * 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){
+        delete options._p.contains[options.person];
+        document.getElementById(options.target).innerHTML  = options._p.toString();
+      }
+   };
+  })());
+
+})( Popcorn );
\ No newline at end of file
diff --git a/plugins/tagThisPerson/popcorn.tagThisPerson.unit.html b/plugins/tagThisPerson/popcorn.tagThisPerson.unit.html
new file mode 100644 (file)
index 0000000..43e90b7
--- /dev/null
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn tagThisPerson test</title>
+  <link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
+  <script src="../../test/qunit/qunit.js"></script>
+  <!-- 
+    do not move - this must be called immediately prior to 
+    popcorn-api-draft.js
+  -->
+  
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.tagThisPerson.js"></script>
+
+  <script src="popcorn.tagThisPerson.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn tagThisPerson Plugin</h1>
+  <h2 id="qunit-banner"></h2>
+  <div id="qunit-testrunner-toolbar"></div>
+  <h2 id="qunit-userAgent"></h2>
+  <ol id="qunit-tests"></ol>
+  <div id="qunit-fixture">  </div>
+  
+  <video id='video' 
+    controls preload='none' 
+    width= '250px'
+    poster="../../test/poster.png"> 
+
+    <source id='mp4'
+      src="../../test/trailer.mp4"
+      type='video/mp4; codecs="avc1, mp4a"'> 
+
+    <source id='ogv'
+      src="../../test/trailer.ogv"
+      type='video/ogg; codecs="theora, vorbis"'> 
+
+    <p>Your user agent does not support the HTML5 Video element.</p> 
+
+  </video> 
+  <div id="tagdiv"></div>  
+</body>
+</html>
diff --git a/plugins/tagThisPerson/popcorn.tagThisPerson.unit.js b/plugins/tagThisPerson/popcorn.tagThisPerson.unit.js
new file mode 100644 (file)
index 0000000..fc6ae2c
--- /dev/null
@@ -0,0 +1,73 @@
+test("Popcorn tagThisPerson Plugin", function () {
+  
+  var popped = Popcorn("#video"),
+      expects = 6, 
+      count = 0,
+      interval,
+      interval2,
+      interval3,
+      tagdiv = document.getElementById('tagdiv');
+  
+  expect(expects);
+  
+  function plus() {
+    if ( ++count===expects) {
+      start();
+    }
+  }
+  
+  stop();
+   
+  ok ('tagThisPerson' in popped, "tagThisPerson is a method of the popped instance");
+  plus();
+  
+  equals ( tagdiv.innerHTML, "", "initially, there is nothing inside the tagdiv" );
+  plus();
+  
+  popped.tagThisPerson({
+      start: 0, // seconds
+      end: 5, // seconds
+      person: 'Anna Sob',
+      target: 'tagdiv'
+    } )
+    .tagThisPerson({
+      start: 3, // seconds
+      end: 10, // seconds
+      person: 'Scott',
+      target: 'tagdiv'
+    } )
+    .volume(0)
+    .play();
+  
+  
+  interval = setInterval( function() {
+    if( popped.currentTime() > 0 && popped.currentTime() <= 5 ) {
+      equals ( tagdiv.innerHTML.trim() , "Anna Sob" ,"tagdiv shows the first tag" );
+      plus();
+      clearInterval( interval );
+    }
+  }, 2000);
+  
+  interval2 = setInterval( function() {
+    if( popped.currentTime() > 3 && popped.currentTime() < 5  ) {
+      equals ( tagdiv.innerHTML.trim() , "Anna Sob, Scott", "tagdiv shows the first & second tag" );
+      plus();
+      clearInterval( interval2 );
+    }
+  }, 2000);
+  
+  interval3 = setInterval( function() {
+    if( popped.currentTime() > 5) {
+      equals ( tagdiv.innerHTML.trim() , "Scott" ,"tagdiv shows the second tag only" );
+      plus();
+      clearInterval( interval3 );
+    }
+  }, 5000);
+  interval4 = setInterval( function() {
+    if( popped.currentTime() > 10) {
+      equals ( tagdiv.innerHTML , "" ,"tagdiv is now cleared" );
+      plus();
+      clearInterval( interval4 );
+    }
+  }, 4000);
+});