forked from CapMousse/T-Lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlite.js
131 lines (105 loc) · 3.63 KB
/
tlite.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
// Version : 1.0.1
// (c) 2011 Jérémy Barbe.
// May be freely distributed under the MIT license.
window['Tlite'] = new function() {
var _this = this,
topContext,
curContext,
ifCache = {};
/**
* Find the value asked in the template in the current context
* @param {String} elem the element to search
*/
function findValue(elem){
var delimiter = '.',
path = elem.split(delimiter),
value = curContext[path.shift()],
call;
//don't loop if value is undefined or is a function
while (value != undefined && !(call = value.call) && path.length) {
value = value[path.shift()];
}
//if the current value is function, call it and pass the current context and the left path
if(call){
path = [curContext, findValue(path.join(delimiter))];
return value.apply(this, path)
}
return value;
}
/**
* Parse condition
* @param {String} condition
* @param {String} result
*/
function parseIf(condition, result) {
var type;
condition = condition.split(' ');
type = ""+condition[1];
if (condition[1] && !ifCache[type]) {
ifCache[type] = new Function("x", "y", "return x " + condition[1] + " y");
}
if(condition[1]){
type = ifCache[type](findValue(condition[0]), findValue(condition[2]))
}else{
type = condition[0][0] != "!";
condition[0] = type ? condition[0] : condition[0].substr(1);
type = !!findValue(condition[0]) == type; //convert findValue to boolean
}
return type ? parse(result) : '';
}
/**
* Do a for loop
* @param {Array|Object} forVar the var used for the loop
* @param {String} content content of the loop
*/
function parseFor(forVar, content) {
var filterString, forReturn = '', i, result;
filterString = forVar.match(/(\w+)(?:(.*))/);
forVar = findValue(filterString[1]);
filterString = (filterString[2] || '') + '|value|key|top';
for (i in forVar) {
if (forVar.hasOwnProperty(i) && filterString.search(i) < 0) {
result = typeof forVar[i] == "object" ? forVar[i] : {};
result.value = forVar[i];
result.key = i;
result.top = topContext;
curContext = result;
forReturn += parse(content);
}
}
return forReturn;
}
/**
* Parse the current template to find var/condition/for
* @param {String} tpl
*/
function parse(tpl){
var context = curContext;
tpl = tpl.replace(/<tpl id:(.*) (.*?):(.*?)>(.*?)<\/tpl id:\1>/g, function(string, id, type, value, content){
return type == 'if' ? parseIf(value, content): parseFor(value, content);
});
//reset current context
curContext = context;
return _this.find(tpl);
}
/**
* replace template var to their value
* @param {String} tpl
* @param {Object} context
*/
_this.find = function(tpl, context) {
context&&(curContext = context)
return tpl.replace(/\{(.+?)\}/g, function(foundVar, varContent) {
return findValue(varContent) || foundVar;
});
}
/**
* Exposed parse, run template parsing
* @param {String} tpl
* @param {Object} context
*/
_this.parse = function(tpl, context) {
curContext = topContext = context;
return parse(tpl);
}
};