forked from mozilla/popcorn-js
/
popcorn.mustache.js
executable file
·210 lines (189 loc) · 6.86 KB
/
popcorn.mustache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// PLUGIN: Mustache
(function ( Popcorn ) {
/**
* 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.
*
* dynamic: an optional argument indicating that the template and json data are dynamic
* and need to be loaded dynamically on every use. Defaults to True.
*
* 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' +
'}',
dynamic: false // 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( options ){
var getData, data, getTemplate, template;
Popcorn.getScript( "http://mustache.github.com/extras/mustache.js" );
var shouldReload = !!options.dynamic,
typeOfTemplate = typeof options.template,
typeOfData = typeof options.data,
target = document.getElementById( options.target );
if ( !target && Popcorn.plugin.debug ) {
throw new Error( "target container doesn't exist" );
}
options.container = target || document.createElement( "div" );
if ( typeOfTemplate === "function" ) {
if ( !shouldReload ) {
template = options.template( options );
} else {
getTemplate = options.template;
}
} else if ( typeOfTemplate === "string" ) {
template = options.template;
} else if ( Popcorn.plugin.debug ) {
throw new Error( "Mustache Plugin Error: options.template must be a String or a Function." );
} else {
template = "";
}
if ( typeOfData === "function" ) {
if ( !shouldReload ) {
data = options.data( options );
} else {
getData = options.data;
}
} else if ( typeOfData === "string" ) {
data = JSON.parse( options.data );
} else if ( typeOfData === "object" ) {
data = options.data;
} else if ( Popcorn.plugin.debug ) {
throw new Error( "Mustache Plugin Error: options.data must be a String, Object, or Function." );
} else {
data = "";
}
return {
start: function( event, options ) {
var interval = function() {
if( !window.Mustache ) {
setTimeout( function() {
interval();
}, 10 );
} else {
// if dynamic, freshen json data on every call to start, just in case.
if ( getData ) {
data = getData( options );
}
if ( getTemplate ) {
template = getTemplate( options );
}
var html = Mustache.to_html( template,
data
).replace( /^\s*/mg, "" );
options.container.innerHTML = html;
}
};
interval();
},
end: function( event, options ) {
options.container.innerHTML = "";
},
_teardown: function( options ) {
getData = data = getTemplate = template = null;
}
};
},
{
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"
},
dynamic: {
elem: "input",
type: "checkbox",
label: "Dynamic",
default: true,
optional: true
}
}
});
})( Popcorn );