]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Fixed issue in popcorndocs plugin, added unit tests t627
authorChristopher De Cairos <chris.decairos@gmail.com>
Tue, 27 Sep 2011 14:52:18 +0000 (10:52 -0400)
committerChristopher De Cairos <chris.decairos@gmail.com>
Tue, 27 Sep 2011 14:52:18 +0000 (10:52 -0400)
plugins/popcorndoc/popcorn.popcorndoc.html [new file with mode: 0644]
plugins/popcorndoc/popcorn.popcorndoc.js [new file with mode: 0644]
plugins/popcorndoc/popcorn.popcorndoc.unit.html [new file with mode: 0644]
plugins/popcorndoc/popcorn.popcorndoc.unit.js [new file with mode: 0644]

diff --git a/plugins/popcorndoc/popcorn.popcorndoc.html b/plugins/popcorndoc/popcorn.popcorndoc.html
new file mode 100644 (file)
index 0000000..9b23885
--- /dev/null
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn popcorndoc Plug-in Demo</title>
+
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.popcorndoc.js"></script>
+  <script>
+    document.addEventListener('DOMContentLoaded', function () {
+      var p = Popcorn('#video')
+      .play()
+      .popcorndoc({
+        start: 1, // seconds
+        end: 5, // seconds
+        lines: '25', // line(s) to highlight and show
+        target: 'gistdiv', // DIV in which to load gist
+        gistUrl: 'https://gist.github.com/289467' // URL of gist
+      })
+      .popcorndoc({
+        start: 5, // seconds
+        end: 10, // seconds
+        lines: '2,4,6,12-17', // line(s) to highlight and show
+        target: 'gistdiv', // DIV in which to load gist
+        gistUrl: 'https://gist.github.com/289467' // URL of gist
+      });
+
+    }, false);
+
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn popcorndoc Plug-in Demo</h1>
+  <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"'>
+
+      <source id='webm'
+        src="../../test/trailer.webm"
+        type='video/webm; codecs="vp8, vorbis"'>
+
+      <p>Your user agent does not support the HTML5 Video element.</p>
+
+    </video>
+  </div>
+  <div id="gistdiv" width="50%" height="50%">
+  </div>
+</body>
+</html>
diff --git a/plugins/popcorndoc/popcorn.popcorndoc.js b/plugins/popcorndoc/popcorn.popcorndoc.js
new file mode 100644 (file)
index 0000000..7791d38
--- /dev/null
@@ -0,0 +1,247 @@
+// PLUGIN: popcorndoc
+
+(function (Popcorn) {
+
+  /**
+   * Github Gist popcorn plug-in
+   * Loads and allows lines of code in a gist to be highlighted, and examples run.
+   *
+   * @param {Object} options
+   *
+   * Example:
+   *  var p = Popcorn('#video')
+   *     .popcorndoc({
+   *       start: 5, // seconds
+   *       end: 15, // seconds
+   *       lines: '25', // line(s) to highlight and show
+   *       target: 'gist-container', // DIV in which to load gist
+   *       gistUrl: 'https://gist.github.com/289467' // URL of gist
+   *     })
+   *     // The specified "line" can also be a list
+   *     .popcorndoc({
+   *       start: 20, // seconds
+   *       end: 25, // seconds
+   *       lines: '26,29-32', // line(s) to highlight and show
+   *       target: 'gist-container', // DIV in which to load gist
+   *       gistUrl: 'https://gist.github.com/289467' // URL of gist
+   *     })
+   *     // Highlighting can be removed
+   *     .popcorndoc({
+   *       start: 20, // seconds
+   *       end: 25, // seconds
+   *       lines: null // line(s) to highlight and show
+   *       target: 'gist-container', // DIV in which to load gist
+   *       gistUrl: 'https://gist.github.com/289467' // URL of gist
+   *     });
+   *     // Run the example.  NOTE: should be last gist track.
+   *     .popcorndoc({
+   *       start: 29, // seconds
+   *       target: 'gist-container', // DIV in which to load gist
+   *       gistUrl: 'https://gist.github.com/289467', // URL of gist
+   *       runIn: 'iframe-container-div' // DIV in which to load the example in an iframe
+   *     });
+   */
+
+   /**
+    * script.js - https://github.com/kares/script.js
+    *
+    * http://www.apache.org/licenses/LICENSE-2.0.html
+    * @version 0.7-SNAPSHOT
+    */
+var script=function(){function n(e,b){var d=e.loaded,a=e.complete;if(e.defer){var c=document.createElement("script");c.src=e.src;if(e.type)c.type=e.type;e.charset&&c.setAttribute("charset",e.charset);var g=function(){if(!(d&&d.call(c,e.writes?h||void 0:void 0)===!1)){var b=document.getElementById(e.id);if(e.writes&&h){var g=function(a){c.nextSibling?c.parentNode.insertBefore(a,c.nextSibling):c.parentNode.appendChild(a)};b||(b=document.createElement("div"),g(b));b.style.display="none";b.innerHTML=
+"<br/>"+h.join("");b.removeChild(b.childNodes[0]);for(var f=b.childNodes[0];f;)b.removeChild(f),g(f),f=b.childNodes[0]}b&&b.parentNode.removeChild(b);a&&a.call(c)}},f=!1;c.onload=c.onreadystatechange=function(){if(!f&&(!this.readyState||this.readyState==="loaded"||this.readyState==="complete")){f=!0;c.onload=c.onreadystatechange=null;try{g()}finally{b&&b()}}};e.append(c)}else{c=document.getElementById(e.id);try{var i;d&&(i=d.call(c));i!==!1&&a&&a.call(c)}finally{if(e.idGenerated)c.id=null;b&&b()}}}
+function i(){if(f&&!f.loading)f.loading=!0,function b(){if(f.length){var d=f.shift();if(d)if(d.writes){if(!j)j=document.write;if(!k)k=document.writeln;document.write=o;document.writeln=p;h=null;n(d,function(){if(j)document.write=j;if(k)document.writeln=k;h=k=j=null;b()})}else n(d),b();else b()}else delete f.loading}()}var h=null,o=function(){var e=arguments;e&&e[0]&&(h||(h=[]),h.push(e[0]))},p=function(e){o(e+"\n")},j,k,f=[],g,l=function(){g&&(g=null,i())};if(document.addEventListener)g=function(){document.removeEventListener("DOMContentLoaded",
+g,!1);g=null;i()},document.addEventListener("DOMContentLoaded",g,!1),window.addEventListener("load",l,!1);else if(document.attachEvent){g=function(){document.readyState==="complete"&&(document.detachEvent("onreadystatechange",g),g=null,i())};document.attachEvent("onreadystatechange",g);window.attachEvent("onload",l);l=!1;try{l=window.frameElement==null}catch(r){}document.documentElement.doScroll&&l&&function b(){if(g){try{document.documentElement.doScroll("left")}catch(d){setTimeout(b,1);return}i()}}()}var m=
+function(b){if(!b)throw"script : no arguments given";if(typeof b==="string")b={src:b};else if(!b.src)throw"script : 'src' is required";var d,a={},c=m.defaults;if(c)for(d in c)a[d]=c[d];for(d in b)a[d]=b[d];if(a.base&&a.src.substring(0,4)!=="http")d=a.base,c=a.base.length-1,d=d[c]=="/"?d.substring(0,c):d,a.src=d+"/"+a.src;if(a.onload!=null)a.onLoad=a.onLoad!=null?b.onload!=null?b.onload:b.onLoad!=null?b.onLoad:a.onload:a.onload,delete a.onload;if(typeof a.defer==="undefined")a.defer=a.onLoad;if(!a.id)a.id=
+m._generateId(),a.idGenerated=!0;var h=a.append;if(typeof h==="string")a.append=function(a){document.getElementById(h).appendChild(a)};else if(h&&h.appendChild)a.append=function(a){h.appendChild(a)};if(a.writes==null)a.writes=!0;if(g){if(a.defer){if(b='<div id="'+a.id+'"',b+=a.loadingHTML?">"+a.loadingHTML:' style="diplay: none;">',b+="</div>",!a.append)a.append=function(b){var c=document.getElementById(a.id);c.parentNode.insertBefore(b,c)}}else b='<script id="'+a.id+'" src="'+a.src+'"><\/script>';
+document.write(b);a.order==null?f.push(a):f.splice(a.order,0,a)}else{a.defer=!0;if(!a.append)a.append=function(a){document.getElementsByTagName("body")[0].appendChild(a)};a.order==null?f.push(a):f.splice(a.order,0,a);i()}},q=0;m._generateId=function(){return"_script-"+q++};m.defaults={type:"text/javascript"};return m}();
+
+  /**
+   * Inject CSS class into DOM, from http://paulirish.com/2008/bookmarklet-inject-new-css-rules/
+   */
+  var gistCssClassName;
+  function setupCss(className) {
+    if (className) {
+      gistCssClassName = className;
+      return;
+    }
+
+    gistCssClassName = "popcorn-gist-highlighted";
+    var defaultHighlightedCSS = ".popcorn-gist-highlighted { background-color: #FFC; }";
+
+    if ("\v" == "v") {
+      document.createStyleSheet().cssText = defaultHighlightedCSS;
+    } else {
+      var tag = document.createElement("style");
+      tag.type = "text/css";
+      document.getElementsByTagName("head")[0].appendChild(tag);
+      tag[(typeof document.body.style.WebkitAppearance == "string") ? "innerText" : "innerHTML"] = defaultHighlightedCSS;
+    }
+  }
+
+  function applyStyle(linesString, func) {
+    var lines = [];
+    Popcorn.forEach( linesString.split( ',' ), function ( item, i ) {
+      var range = item.split('-'),
+        lineStart = range[0] | 0,
+        lineEnd = range[1] | 0;
+
+      if (lineEnd) {
+        // Make sure the line range is valid
+        lineEnd = lineEnd > lineStart ? lineEnd : lineStart + 1;
+
+        while (lineStart <= lineEnd ) {
+          lines.push( lineStart++ );
+        }
+      } else {
+        lines.push( lineStart );
+      }
+    } );
+
+    var i = lines.length;
+    while ( i-- ) {
+      func( lines[i] );
+    }
+  }
+
+  var highlighted = {};
+
+  function getLine(num) {
+    return document.getElementById('LC' + num);
+  }
+
+  function unhighlight(line) {
+    line = getLine(line);
+    if (!line) {
+      return;
+    }
+
+    line.classList.remove('popcorn-gist-highlighted');
+  }
+
+  function highlight(line) {
+    line = getLine(line);
+    if (!line) {
+      return;
+    }
+
+    line.classList.add('popcorn-gist-highlighted');
+  }
+
+  function scrollTo(line) {
+    // todo...
+  }
+
+  var gists = {};
+
+  function loadGist(url, target) {
+    var gistId = url.match(/gist\.github\.com\/(\d+)/)[1];
+    script({
+      src: 'https://gist.github.com/' + gistId + '.js',
+      append: target
+    });
+    gists[url] = true;
+  }
+
+  var iframe;
+  function cleanUp(elem) {
+    var container = elem.parentNode;
+    if (!container) {
+      throw new Error(ERR_MSG);
+    }
+    container.removeChild(iframe);
+    iframe = null;
+  }
+
+  var ERR_MSG = "popcorndoc: runIn property must be a vaild ID of a DIV in the document.";
+
+  Popcorn.plugin( "popcorndoc" , {
+
+    manifest: {
+      about:{
+        name: "Popcorn Documentation Gist Plugin",
+        version: "0.1",
+        author: "@humphd, @rwaldron, @remixmanifesto",
+        website: "http://popcornjs.org"
+      },
+      options:{
+        start    : {elem:'input', type:'text', label:'In'},
+        end      : {elem:'input', type:'text', label:'Out'},
+        target   : 'gist-container',
+        lines    : {elem:'input', type:'text', label:'Line'},
+        gistUrl  : {elem:'input', type:'text', label:'Gist URL'},
+        runIn    : {elem:'input', type:'text', label:'Div to Run Example in'}
+      }
+    },
+
+    _setup: function(options) {
+      if (!gistCssClassName) {
+        setupCss(options.className);
+      }
+
+      if (!gists[options.gistUrl]) {
+        loadGist(options.gistUrl, options.target);
+      }
+    },
+
+    start: function(event, options) {
+      if (options.lines) {
+        applyStyle( options.lines, highlight );
+        return;
+      }
+
+      unhighlight();
+
+      if (options.runIn) {
+        console.log('runIn start');
+        if (iframe) {
+          cleanUp(iframe);
+        }
+
+        iframe = document.createElement( "iframe" );
+
+        var contents = document.querySelectorAll( "div.line" ),
+          container = document.getElementById(options.runIn),
+          iframeDoc;
+
+        if (!container) {
+          throw new Error(ERR_MSG);
+        }
+
+        container.style.display = 'block';
+
+        iframe.setAttribute( 'width', '100%' );
+        iframe.setAttribute( 'height', '100%' );
+
+        container.appendChild( iframe );
+        iframeDoc = ( iframe.contentWindow || iframe.contentDocument ).document;
+        iframeDoc.open();
+
+        Popcorn.forEach( contents, function( div ) {
+          var text = div.textContent;
+          if ( text ) {
+            // Remove single-line comments, being careful not to ditch URLs (http://....)
+            text = text.replace( /[^:]\/\/.+$/, '' );
+            iframeDoc.write( text + '\n' );
+          }
+        } );
+
+        iframeDoc.close();
+      }
+    },
+
+    end: function( event, options ){
+      if (options.lines) {
+        applyStyle( options.lines, unhighlight );
+      }
+
+      if (options.runIn) {
+        console.log('runIn end');
+        if (iframe) {
+          cleanUp(iframe);
+        }
+      }
+    }
+  });
+
+})( Popcorn );
diff --git a/plugins/popcorndoc/popcorn.popcorndoc.unit.html b/plugins/popcorndoc/popcorn.popcorndoc.unit.html
new file mode 100644 (file)
index 0000000..5b1c5b1
--- /dev/null
@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorndocs unit tests</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.popcorndoc.js"></script>
+
+  <script src="popcorn.popcorndoc.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn popcorndocs 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"'>
+
+    <source id='webm'
+      src="../../test/trailer.webm"
+      type='video/webm; codecs="vp8, vorbis"'>
+
+    <p>Your user agent does not support the HTML5 Video element.</p>
+
+  </video>
+  <div id="gistdiv"></div>
+</body>
+</html>
diff --git a/plugins/popcorndoc/popcorn.popcorndoc.unit.js b/plugins/popcorndoc/popcorn.popcorndoc.unit.js
new file mode 100644 (file)
index 0000000..11222a2
--- /dev/null
@@ -0,0 +1,157 @@
+test( "popcorndoc existence", function(){
+
+  var popped = Popcorn( "#video" ).pause();
+
+  var expects = 2,
+    count = 0,
+
+  plus = function() {
+    if ( ++count === expects ) {
+      start();
+      Popcorn.destroy( popped );
+    }
+  };
+
+  expect( expects );
+  stop();
+
+  ok( popped.popcorndoc, "popcorndoc exists:" );
+  plus();
+  equals( typeof popped.popcorndoc, "function" , "Popcorn.popcorndoc is a function" );
+  plus();
+
+});
+
+test( "popcorndoc functional tests", function() {
+
+  var popped = Popcorn( "#video" );
+  popped.pause().volume( 0 );
+
+  var expects = 20,
+    count = 0,
+
+  plus = function() {
+    if ( ++count === expects ) {
+      Popcorn.destroy( popped );
+      start();
+    }
+  };
+
+  stop();
+
+  popped.popcorndoc({
+    start: 1,
+    end: 2,
+    lines: '1',
+    target: 'gistdiv',
+    gistUrl: 'https://gist.github.com/289467'
+  })
+  .popcorndoc({
+    start: 1,
+    end: 2,
+    lines: '3-5',
+    target: 'gistdiv',
+    gistUrl: 'https://gist.github.com/289467'
+  })
+  .popcorndoc({
+    start: 2,
+    end: 3,
+    lines: '2,6-8',
+    target: 'gistdiv',
+    gistUrl: 'https://gist.github.com/289467'
+  })
+  .popcorndoc({
+    start: 3,
+    end: 4,
+    lines: '15-10',
+    target: 'gistdiv',
+    gistUrl: 'https://gist.github.com/289467'
+  });
+
+  var line,
+      lines = [],
+      i = 0;
+
+  while( line = document.getElementById( "LC" + ( ++i ) ) ) {
+    lines.push( line );
+  }
+
+  popped.exec( 1.5, function() {
+    var line1 = document.getElementById( "LC1" ),
+        line3 = document.getElementById( "LC3" ),
+        line4 = document.getElementById( "LC4" ),
+        line5 = document.getElementById( "LC5" );
+
+    ok( line1.className.match( /popcorn-gist-highlighted/ ), "line 1 is highlighted" );
+    plus();
+    ok( line3.className.match( /popcorn-gist-highlighted/ ), "line 3 is highlighted" );
+    plus();
+    ok( line4.className.match( /popcorn-gist-highlighted/ ), "line 4 is highlighted" );
+    plus();
+    ok( line5.className.match( /popcorn-gist-highlighted/ ), "line 5 is highlighted" );
+    plus();
+  })
+  .exec( 2.5, function() {
+    var line2 = document.getElementById( "LC2" ),
+        line6 = document.getElementById( "LC6" ),
+        line7 = document.getElementById( "LC7" ),
+        line8 = document.getElementById( "LC8" ),
+        line1 = document.getElementById( "LC1" ),
+        line3 = document.getElementById( "LC3" ),
+        line4 = document.getElementById( "LC4" ),
+        line5 = document.getElementById( "LC5" );
+
+    ok( !line1.className.match( /popcorn-gist-highlighted/ ), "line 1 is not highlighted" );
+    plus();
+    ok( !line3.className.match( /popcorn-gist-highlighted/ ), "line 3 is not highlighted" );
+    plus();
+    ok( !line4.className.match( /popcorn-gist-highlighted/ ), "line 4 is not highlighted" );
+    plus();
+    ok( !line5.className.match( /popcorn-gist-highlighted/ ), "line 5 is not highlighted" );
+    plus();
+
+    ok( line2.className.match( /popcorn-gist-highlighted/ ), "line 2 is highlighted" );
+    plus();
+    ok( line6.className.match( /popcorn-gist-highlighted/ ), "line 6 is highlighted" );
+    plus();
+    ok( line7.className.match( /popcorn-gist-highlighted/ ), "line 7 is highlighted" );
+    plus();
+    ok( line8.className.match( /popcorn-gist-highlighted/ ), "line 8 is highlighted" );
+    plus();
+  })
+  .exec( 3.5, function() {
+    var line15 = document.getElementById( "LC15" ),
+        line16 = document.getElementById( "LC16" ),
+        line2 = document.getElementById( "LC2" ),
+        line6 = document.getElementById( "LC6" ),
+        line7 = document.getElementById( "LC7" ),
+        line8 = document.getElementById( "LC8" );
+
+    ok( !line2.className.match( /popcorn-gist-highlighted/ ), "line 2 is not highlighted" );
+    plus();
+    ok( !line6.className.match( /popcorn-gist-highlighted/ ), "line 6 is not highlighted" );
+    plus();
+    ok( !line7.className.match( /popcorn-gist-highlighted/ ), "line 7 is not highlighted" );
+    plus();
+    ok( !line8.className.match( /popcorn-gist-highlighted/ ), "line 8 is not highlighted" );
+    plus();
+
+    ok( line15.className.match( /popcorn-gist-highlighted/ ), "line 15 is highlighted, with bad range input" );
+    plus();
+    ok( line16.className.match( /popcorn-gist-highlighted/ ), "line 16 is highlighted as the fallback for an incorrect range" );
+    plus();
+    ok( !document.getElementById( "LC10" ).className.match( /popcorn-gist-highlighted/ ), "line 10 is NOT highlighted" );
+    plus();
+  })
+  .exec( 4.5, function() {
+    var highlighted = 0 ;
+    for( var i = 0, len = lines.len; i < len; i++ ) {
+      lines[ i ].className.match( /popcorn.gist.highlighted/ ) && highlighted++;
+    }
+    equals( highlighted, 0, "0 lines are highlighted" );
+    plus();
+
+  });
+
+  popped.play();
+});
\ No newline at end of file