]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Mustache plugin initial work, sans tests.
authorDavid Humphrey <david.humphrey@senecac.on.ca>
Sat, 12 Feb 2011 21:03:04 +0000 (16:03 -0500)
committerDavid Humphrey <david.humphrey@senecac.on.ca>
Sat, 12 Feb 2011 21:03:04 +0000 (16:03 -0500)
plugins/mustache/popcorn.mustache.html [new file with mode: 0644]
plugins/mustache/popcorn.mustache.js [new file with mode: 0644]

diff --git a/plugins/mustache/popcorn.mustache.html b/plugins/mustache/popcorn.mustache.html
new file mode 100644 (file)
index 0000000..50e96b7
--- /dev/null
@@ -0,0 +1,141 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn Mustache Plugin Demo</title>
+
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.mustache.js"></script>
+  <script>
+    document.addEventListener('DOMContentLoaded', function () {
+      var p = Popcorn('#video')
+
+        // Example using template and JSON strings.
+        .mustache({
+          start: 5,  // seconds
+          end:  10,  // seconds
+          target: 'mustache-div',
+          template: "<h1>{{header}}</h1>"                         +
+                    "{{#items}}"                                  +
+                    "  {{#first}}"                                +
+                    "    <li><strong>{{name}}</strong></li>"      +
+                    "  {{/first}}"                                +
+                    "  {{#link}}"                                 +
+                    '    <li><a href="{{url}}">{{name}}</a></li>' +
+                    "  {{/link}}"                                 +
+                    "{{/items}}"                                  +
+                    ""                                            +
+                    "{{#empty}}"                                  +
+                    "  <p>The list is empty.</p>"                 +
+                    "{{/empty}}"                                  ,
+
+          data:     '{'                                                        +
+                    '  "header": "Colors", '                                   +
+                    '  "items": [ '                                            +
+                    '      {"name": "red", "first": true, "url": "#Red"}, '    +
+                    '      {"name": "green", "link": true, "url": "#Green"}, ' +
+                    '      {"name": "blue", "link": true, "url": "#Blue"} '    +
+                    '  ],'                                                     +
+                    '  "empty": false'                                         +
+                    '}',
+          static: true /* The json is not going to change, load it early. */
+        } )
+
+        // Example mixing functions and literals.
+        .mustache({
+          start: 15,  // seconds
+          end: 20,
+          target: 'mustache-div',
+          template: function(plugin, options) {
+            return  "<h1>{{header}}</h1>"                         +
+                    "{{#items}}"                                  +
+                    "  {{#first}}"                                +
+                    "    <li><strong>{{name}}</strong></li>"      +
+                    "  {{/first}}"                                +
+                    "  {{#link}}"                                 +
+                    '    <li><a href="{{url}}">{{name}}</a></li>' +
+                    "  {{/link}}"                                 +
+                    "{{/items}}"                                  +
+                    ""                                            +
+                    "{{#empty}}"                                  +
+                    "  <p>The list is empty.</p>"                 +
+                    "{{/empty}}"                                  ;
+          },
+          data: {
+                  header: "Colors",
+                  items: [
+                    { name: "red", first: true, url: "#Red" },
+                    { name: "green", link: true, url: "#Green" },
+                    { name: "blue", link: true, url: "#Blue"}
+                  ],
+                  empty: false
+                }
+        } )
+
+        // Example using functions, not static.
+        .mustache({
+          start: 25,  // seconds
+          end: 30,
+          target: 'mustache-div',
+          template: function(plugin, options) {
+            return  "<h1>{{header}}</h1>"                         +
+                    "{{#items}}"                                  +
+                    "  {{#first}}"                                +
+                    "    <li><strong>{{name}}</strong></li>"      +
+                    "  {{/first}}"                                +
+                    "  {{#link}}"                                 +
+                    '    <li><a href="{{url}}">{{name}}</a></li>' +
+                    "  {{/link}}"                                 +
+                    "{{/items}}"                                  +
+                    ""                                            +
+                    "{{#empty}}"                                  +
+                    "  <p>The list is empty.</p>"                 +
+                    "{{/empty}}"                                  ;
+          },
+          data: function(plugin, options) {
+            return  JSON.parse('' +
+                    '{'                                                        +
+                    '  "header": "Colors", '                                   +
+                    '  "items": [ '                                            +
+                    '      {"name": "red", "first": true, "url": "#Red"}, '    +
+                    '      {"name": "green", "link": true, "url": "#Green"}, ' +
+                    '      {"name": "blue", "link": true, "url": "#Blue"} '    +
+                    '  ],'                                                     +
+                    '  "empty": false'                                         +
+                    '}');
+          }
+        } )
+      .volume(0)
+      .play();
+    }, false);
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn Mustache Plugin Demo</h1>
+  <p>Mustache - <a href="http://mustache.github.com/">http://mustache.github.com/</a>
+  <p>Mustache generated HTML templates, created using various data methods, are shown at 5, 15, and 25 seconds.</p>
+  <div>
+    <video id='video'
+      autobuffer
+      preload="auto"
+      controls
+      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 style="width:700px">
+    <div>Mustache Template</div>
+    <div id="mustache-div" style="position:relative;float:left;width:300px;height:400px"></div>
+  </div>
+</body>
+</html>
diff --git a/plugins/mustache/popcorn.mustache.js b/plugins/mustache/popcorn.mustache.js
new file mode 100644 (file)
index 0000000..d48f540
--- /dev/null
@@ -0,0 +1,183 @@
+// PLUGIN: Mustache
+
+(function (Popcorn) {
+
+  (function() {
+    // TODO: what's the right approach for referencing remote scripts vs. including?
+    var mustacheUrl = "https://github.com/janl/mustache.js/raw/master/mustache.js";
+
+    var head = document.getElementsByTagName('head')[0];
+    var script = document.createElement('script');
+
+    script.type = "text/javascript";
+    script.src = mustacheUrl;
+    head.insertBefore( script, head.firstChild );
+  })();
+
+  /**
+   * Mustache Popcorn Plug-in
+   *
+   * Adds the ability to render JSON using templates via the Mustache templating library.
+   *
+   * @param {Object} options
+   *
+   * Required parameters: start, end, template, data, and target.
+   * Optional parameter: static.
+   *
+   *   start: the time in seconds when the mustache template should be rendered
+   *          in the target div.
+   *
+   *   end: the time in seconds when the rendered mustache template should be
+   *        removed from the target div.
+   *
+   *   target: a String -- the target div's id.
+   *
+   *   template: the mustache template for the plugin to use when rendering.  This can be
+   *             a String containing the template, or a Function that returns the template's
+   *             String.
+   *
+   *   data: the data to be rendered using the mustache template.  This can be a JSON String,
+   *         a JavaScript Object literal, or a Function returning a String or Literal.
+   *
+   *   static: an optional argument indicating that the template and json data are static
+   *           and can be safely preloaded and cached.  Defaults to False (i.e., json data
+   *           will be loaded only when needed, and every time needed vs. at setup).
+   *
+   * Example:
+     var p = Popcorn('#video')
+
+        // Example using template and JSON strings.
+        .mustache({
+          start: 5, // seconds
+          end:  15,  // seconds
+          target: 'mustache',
+          template: "<h1>{{header}}</h1>"                         +
+                    "{{#bug}}"                                    +
+                    "{{/bug}}"                                    +
+                    ""                                            +
+                    "{{#items}}"                                  +
+                    "  {{#first}}"                                +
+                    "    <li><strong>{{name}}</strong></li>"      +
+                    "  {{/first}}"                                +
+                    "  {{#link}}"                                 +
+                    '    <li><a href="{{url}}">{{name}}</a></li>' +
+                    "  {{/link}}"                                 +
+                    "{{/items}}"                                  +
+                    ""                                            +
+                    "{{#empty}}"                                  +
+                    "  <p>The list is empty.</p>"                 +
+                    "{{/empty}}"                                  ,
+
+          data:     '{'                                                        +
+                    '  "header": "Colors", '                                   +
+                    '  "items": [ '                                            +
+                    '      {"name": "red", "first": true, "url": "#Red"}, '    +
+                    '      {"name": "green", "link": true, "url": "#Green"}, ' +
+                    '      {"name": "blue", "link": true, "url": "#Blue"} '    +
+                    '  ],'                                                     +
+                    '  "empty": false'                                         +
+                    '}',
+          static: true // The json is not going to change, load it early.
+        } )
+
+        // Example showing Functions instead of Strings.
+        .mustache({
+          start: 20,  // seconds
+          end:   25,  // seconds
+          target: 'mustache',
+          template: function(instance, options) {
+                      var template = // load your template file here...
+                      return template;
+                    },
+          data:     function(instance, options) {
+                      var json = // load your json here...
+                      return json;
+                    }
+        } );
+   *
+   */
+
+  Popcorn.plugin( "mustache" , function() {
+
+      var getData,
+          data,
+          getTemplate,
+          template,
+          self;
+
+      return {
+        manifest: {
+          about:{
+            name: "Popcorn Mustache Plugin",
+            version: "0.1",
+            author: "David Humphrey (@humphd)",
+            website: "http://vocamus.net/dave"
+          },
+          options:{
+            start    : {elem:'input', type:'text', label:'In'},
+            end      : {elem:'input', type:'text', label:'Out'},
+            target   : 'mustache-container',
+            template : {elem:'input', type:'text', label:'Template'},
+            data     : {elem:'input', type:'text', label:'Data'},
+            static   : {elem:'input', type:'checkbox', label:'Static'} /* TODO: how to show a checkbox/boolean? */
+          }
+        },
+
+        _setup : function( options ) {
+          this.start = options.start;
+          this.end = options.end;
+
+          if (typeof options.template === 'function') {
+            if (options.static) {
+              template = options.template(this, options);
+            } else {
+              getTemplate = options.template;
+            }
+          } else if (typeof options.template === 'string') {
+            template = options.template;
+          } else {
+            throw "Mustache Plugin Error: options.template must be a String or a Function.";
+          }
+
+          if (typeof options.data === 'function') {
+            if (options.static) {
+              data = options.data(this, options);
+            } else {
+              getData = options.data;
+            }
+          } else if (typeof options.data === 'string') {
+            data = JSON.parse(options.data);
+          } else if (typeof options.data === 'object') {
+            data = options.data;
+          } else {
+            throw "Mustache Plugin Error: options.data must be a String, Object, or Function.";
+          }
+
+          self = this;
+
+          console.log(options.start, data, getData, template, getTemplate);
+        },
+
+        start: function( event, options ) {
+          // if not static=true, freshen json data on every call to start, just in case.
+          if (getData) {
+            data = getData(this, options);
+          }
+
+          if (getTemplate) {
+            template = getTemplate(this, options);
+          }
+
+          var html = Mustache.to_html(template, data).replace(/^\s*/mg, '');
+          document.getElementById(options.target).innerHTML = html;
+        },
+
+        end: function( event, options ) {
+          document.getElementById(options.target).innerHTML = '';
+        }
+
+      };
+
+    });
+
+})( Popcorn );