2014-01-21 19:12:46 +00:00
|
|
|
define([
|
2014-06-30 01:16:50 +00:00
|
|
|
"util/i18n",
|
2014-01-21 19:12:46 +00:00
|
|
|
"util/dom2"
|
2014-06-30 01:16:50 +00:00
|
|
|
], function(i18n) {
|
2014-01-21 19:12:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
A simple, Mustache-compatible templating library that doesn't use eval, so
|
|
|
|
it's safe for Chrome apps. It's not fast, but templating is rarely our
|
|
|
|
bottleneck.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var cache = {};
|
|
|
|
var fragment = document.createDocumentFragment();
|
|
|
|
var fragBody = document.createElement("body");
|
|
|
|
fragment.append(fragBody);
|
|
|
|
|
|
|
|
var parse = function(html) {
|
|
|
|
fragBody.innerHTML = html;
|
|
|
|
return fragBody.children[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
var process = function(template, data) {
|
|
|
|
var searcher = /\{\{([^\}]+)\}\}/;
|
|
|
|
var found, before, after, replacement;
|
|
|
|
while (found = searcher.exec(template)) {
|
|
|
|
var tag = found[1];
|
|
|
|
before = template.substr(0, found.index);
|
2014-01-22 06:47:02 +00:00
|
|
|
|
|
|
|
if (tag[0] == "#") { //section tags
|
2014-01-21 19:12:46 +00:00
|
|
|
var key = tag.substr(1);
|
|
|
|
//it's a section, let's replace it
|
|
|
|
var findEnding = new RegExp("\\{\\{/" + key + "\\}\\}");
|
|
|
|
var ending = findEnding.exec(template);
|
|
|
|
after = template.substr(ending.index + ending[0].length);
|
|
|
|
var contents = template.substr(found.index + found[0].length, ending.index - (found.index + found[0].length));
|
|
|
|
|
|
|
|
var value = data[key];
|
2014-01-22 06:47:02 +00:00
|
|
|
replacement = "";
|
2014-01-21 19:12:46 +00:00
|
|
|
//switch based on the type
|
2014-01-22 16:37:28 +00:00
|
|
|
if (value && value.map) {
|
2014-01-21 19:12:46 +00:00
|
|
|
//arrays get a loop
|
|
|
|
var boundProcess = process.bind(null, contents);
|
|
|
|
replacement = value.map(boundProcess).join("");
|
|
|
|
} else if (typeof value == "object") {
|
2014-01-22 06:47:02 +00:00
|
|
|
//objects get re-templated, should probably merge with parent
|
2014-01-21 19:12:46 +00:00
|
|
|
replacement = process(contents, value);
|
|
|
|
} else if (value) {
|
|
|
|
//otherwise we evaluate for truthiness in the current scope
|
|
|
|
replacement = process(contents, data);
|
|
|
|
}
|
2014-01-22 06:47:02 +00:00
|
|
|
} else if (tag[0] == "^") { //negation tags
|
|
|
|
var findEnding = new RegExp("\\{\\{/" + key + "\\}\\}");
|
|
|
|
var ending = findEnding.exec(template);
|
|
|
|
after = template.substr(ending.index + ending[0].length);
|
|
|
|
var value = data[key];
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
replacement = template.substr(found.index + found[0].length, ending.index - (found.index + found[0].length));
|
|
|
|
} else {
|
|
|
|
replacement = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (tag[0] == ">") { //partial, load and recurse
|
|
|
|
var id = tag.substr(1).trim();
|
|
|
|
after = template.substr(found.index + found[0].length);
|
|
|
|
if (cache[id]) {
|
|
|
|
replacement = process(cache[id], data);
|
|
|
|
} else {
|
|
|
|
replacement = "";
|
|
|
|
}
|
2014-03-08 07:30:47 +00:00
|
|
|
} else if (tag == ".") { //straight value substitution
|
|
|
|
after = template.substr(found.index + found[0].length);
|
|
|
|
replacement = data;
|
2014-01-22 06:47:02 +00:00
|
|
|
} else { //regular substitution
|
2014-01-21 19:12:46 +00:00
|
|
|
after = template.substr(found.index + found[0].length);
|
2014-01-22 03:01:00 +00:00
|
|
|
replacement = typeof data[tag] != "undefined" ? data[tag] : "";
|
2014-01-21 19:12:46 +00:00
|
|
|
}
|
|
|
|
template = before + replacement + after;
|
|
|
|
}
|
|
|
|
return template;
|
|
|
|
};
|
|
|
|
|
|
|
|
var inflateHTML = function(id, data) {
|
|
|
|
var template = cache[id];
|
|
|
|
if (!template) return null;
|
|
|
|
return process(template, data);
|
|
|
|
};
|
|
|
|
|
|
|
|
var inflate = function(id, data) {
|
|
|
|
var html = inflateHTML(id, data);
|
2014-01-22 03:01:00 +00:00
|
|
|
if (!html) return null;
|
2014-01-21 19:12:46 +00:00
|
|
|
return parse(html);
|
|
|
|
};
|
|
|
|
|
|
|
|
//load existing templates from DOM
|
|
|
|
document.findAll("template").forEach(function(template) {
|
|
|
|
var id = template.getAttribute("id");
|
|
|
|
cache[id] = template.innerHTML;
|
|
|
|
});
|
|
|
|
|
|
|
|
var load = function(path) {
|
|
|
|
return new Promise(function(ok) {
|
|
|
|
require(["util/text!" + path], function(text) {
|
2014-06-30 01:16:50 +00:00
|
|
|
cache[path] = i18n.process(text);
|
2014-01-21 19:12:46 +00:00
|
|
|
ok();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
get: inflate,
|
|
|
|
getHTML: inflateHTML,
|
2014-01-22 03:01:00 +00:00
|
|
|
load: load,
|
2014-01-22 16:42:08 +00:00
|
|
|
loadHTML: function(id, template) {
|
|
|
|
cache[id] = template;
|
|
|
|
},
|
2014-01-21 19:12:46 +00:00
|
|
|
getAsync: function(id, data) {
|
|
|
|
if (cache[id]) {
|
|
|
|
return Promise.resolve(inflate(id, data));
|
|
|
|
}
|
|
|
|
return load(id).then(function() {
|
|
|
|
var rendered = inflate(id, data);
|
|
|
|
return Promise.resolve(rendered);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|