Repository navigation
Expand file tree
/
Copy pathtemplate.js
More file actions
115 lines (88 loc) · 2.36 KB
/
Copy pathtemplate.js
File metadata and controls
115 lines (88 loc) · 2.36 KB
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
(function(window) {
var POSSIBLE_HTML = /[&<>"'`]/;
var span = document.createElement('span');
function create(templates) {
var key, result = {};
for (key in templates) {
if (templates.hasOwnProperty(key)) {
result[key] = new Template(templates[key]);
}
}
return result;
}
function Template(fn) {
this.template = fn;
}
Template.handlers = {
arg: function(key) {
if (typeof(this.data) === 'undefined') {
return '';
} else if (typeof(this.data) !== 'object') {
return this.data;
}
return this.data[key];
},
'h': function(a) {
var arg = this.arg(a);
//only escape bad looking stuff saves
//a ton of time
if (POSSIBLE_HTML.test(arg)) {
span.textContent = arg;
return span.innerHTML.replace(/"/g, '"').replace(/'/g, ''');
} else {
// we don't want to call String in the case of "".
// we emit a string version rather then an empty string.
return arg || '';
}
},
's': function(a) {
var arg = this.arg(a);
return String((arg || ''));
},
bool: function(key, onTrue) {
if (this.data[key]) {
return onTrue;
} else {
return '';
}
},
'l10n': function(key, prefix) {
var value = this.arg(key);
if (prefix) {
value = prefix + value;
}
return navigator.mozL10n.get(value);
}
};
Template.prototype = {
/**
* Renders template with given slots.
*
* @param {Object} object key, value pairs for template.
*/
render: function(data) {
Template.handlers.data = data;
return this.template.apply(Template.handlers);
},
/**
* Renders template multiple times
*
* @param {Array} objects object details to render.
* @param {String} [join] optional join argument will join the array.
* @return {String|Array} String if join argument is given array otherwise.
*/
renderEach: function(objects, join) {
var i = 0, len = objects.length,
result = [];
for (; i < len; i++) {
result.push(this.render(objects[i]));
}
if (typeof(join) !== 'undefined') {
return result.join(join);
}
return result;
}
};
Template.create = create;
Calendar.Template = Template;
}(this));