From 737ba5849755717baae6f8f3cf8e2c0c55001758 Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Sat, 12 Feb 2011 16:03:04 -0500 Subject: [PATCH] Mustache plugin initial work, sans tests. --- plugins/mustache/popcorn.mustache.html | 141 +++++++++++++++++++ plugins/mustache/popcorn.mustache.js | 183 +++++++++++++++++++++++++ 2 files changed, 324 insertions(+) create mode 100644 plugins/mustache/popcorn.mustache.html create mode 100644 plugins/mustache/popcorn.mustache.js diff --git a/plugins/mustache/popcorn.mustache.html b/plugins/mustache/popcorn.mustache.html new file mode 100644 index 0000000..50e96b7 --- /dev/null +++ b/plugins/mustache/popcorn.mustache.html @@ -0,0 +1,141 @@ + + + + Popcorn Mustache Plugin Demo + + + + + + +

Popcorn Mustache Plugin Demo

+

Mustache - http://mustache.github.com/ +

Mustache generated HTML templates, created using various data methods, are shown at 5, 15, and 25 seconds.

+
+ +
+
+
Mustache Template
+
+
+ + diff --git a/plugins/mustache/popcorn.mustache.js b/plugins/mustache/popcorn.mustache.js new file mode 100644 index 0000000..d48f540 --- /dev/null +++ b/plugins/mustache/popcorn.mustache.js @@ -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: "

{{header}}

" + + "{{#bug}}" + + "{{/bug}}" + + "" + + "{{#items}}" + + " {{#first}}" + + "
  • {{name}}
  • " + + " {{/first}}" + + " {{#link}}" + + '
  • {{name}}
  • ' + + " {{/link}}" + + "{{/items}}" + + "" + + "{{#empty}}" + + "

    The list is empty.

    " + + "{{/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 ); -- 2.43.0