-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.js
60 lines (58 loc) · 2.31 KB
/
view.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
/*
example: var view = new S.view('<div>{{hello}}</div>', {hello:'world'});
view.render();
*/
S.viewTags = { startChar: '{{', endChar: '}}' }; //overridable property
S.view = function (html, vars, tagStartChar, tagEndChar) {
//tagStartChar & tagEndChar is optional, defines the symbols (#)
//to use when searching for view variable placeholders
this.html = html;
this.vars = vars;
if (tagStartChar) {
this.tagStartChar = tagStartChar;
} else { this.tagStartChar = S.viewTags.startChar; }
if (tagEndChar) {
this.tagEndChar = tagEndChar;
} else { this.tagEndChar = S.viewTags.endChar; }
}
S.view.prototype.render = function () {
var a = 0, b = 0, c = 0, d = 0;
var tagslen = this.tagStartChar.length + this.tagEndChar.length;
var endlen = this.tagEndChar.length;
var htm = this.html;
var ischanged = true;
for (var key in this.vars) {
ischanged = true;
while (ischanged) {
ischanged = false;
//check for view closing first
a = htm.indexOf(this.tagStartChar + '/' + key + this.tagEndChar);
if (a >= 0) {
//found a group of html to show or hide based on view element boolean value
b = a + tagslen + key.length + 1;
c = htm.indexOf(this.tagStartChar + key);
d = htm.indexOf(this.tagEndChar, c + 1);
if (c >= 0 && d > c) {
if (this.vars[key] === false) {
//hide group of html
htm = htm.substr(0, c) + htm.substr(b);
ischanged = true;
} else if (this.vars[key] === true) {
//show group of html
htm = htm.substr(0, c) + htm.substr(d + endlen, a - (d + endlen)) + htm.substr(b);
ischanged = true;
}
continue;
}
}
//check for view element to replace with a value
if (ischanged == false) {
if (htm.indexOf(this.tagStartChar + key + this.tagEndChar) >= 0) {
htm = htm.replace(this.tagStartChar + key + this.tagEndChar, this.vars[key]);
ischanged = true;
}
}
}
}
return htm;
}