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 | 17x
83x
83x
83x
83x
81x
83x
81x
145x
2x
2x
143x
8x
8x
8x
135x
135x
92x
145x
81x
83x
83x
16x
16x
16x
28x
14x
67x
1x
1x
1x
66x
65x
65x
65x
1x
1x
1x
2x
83x
189x
189x
189x
235x
4x
4x
231x
16x
16x
215x
215x
235x
189x
83x
1479x
1470x
1884x
25x
25x
25x
5x
1859x
20x
1839x
1205x
1479x
1470x
1470x
240x
1230x
5x
1225x
20x
20x
20x
1205x
1205x
921x
1230x
1479x
897x
1470x
1470x
83x
3915x
3314x
601x
1x
600x
600x
600x
600x
600x
83x
10x
9x
83x
2247x
2247x
83x
730x
2x
83x
614x
434x
2108x
83x
17x
83x
| var Templater = function(list) {
var itemSource,
templater = this;
var init = function() {
itemSource = templater.getItemSource(list.item);
if (itemSource) {
itemSource = templater.clearSourceItem(itemSource, list.valueNames);
}
};
this.clearSourceItem = function(el, valueNames) {
for(var i = 0, il = valueNames.length; i < il; i++) {
var elm;
if (valueNames[i].data) {
for (var j = 0, jl = valueNames[i].data.length; j < jl; j++) {
el.setAttribute('data-'+valueNames[i].data[j], '');
}
} else if (valueNames[i].attr && valueNames[i].name) {
elm = list.utils.getByClass(el, valueNames[i].name, true);
Eif (elm) {
elm.setAttribute(valueNames[i].attr, "");
}
} else {
elm = list.utils.getByClass(el, valueNames[i], true);
if (elm) {
elm.innerHTML = "";
}
}
elm = undefined;
}
return el;
};
this.getItemSource = function(item) {
if (item === undefined) {
var nodes = list.list.childNodes,
items = [];
for (var i = 0, il = nodes.length; i < il; i++) {
// Only textnodes have a data attribute
if (nodes[i].data === undefined) {
return nodes[i].cloneNode(true);
}
}
} else if (/<tr[\s>]/g.exec(item)) {
var tbody = document.createElement('tbody');
tbody.innerHTML = item;
return tbody.firstChild;
} else if (item.indexOf("<") !== -1) {
var div = document.createElement('div');
div.innerHTML = item;
return div.firstChild;
} else {
var source = document.getElementById(list.item);
Eif (source) {
return source;
}
}
return undefined;
};
this.get = function(item, valueNames) {
templater.create(item);
var values = {};
for(var i = 0, il = valueNames.length; i < il; i++) {
var elm;
if (valueNames[i].data) {
for (var j = 0, jl = valueNames[i].data.length; j < jl; j++) {
values[valueNames[i].data[j]] = list.utils.getAttribute(item.elm, 'data-'+valueNames[i].data[j]);
}
} else if (valueNames[i].attr && valueNames[i].name) {
elm = list.utils.getByClass(item.elm, valueNames[i].name, true);
values[valueNames[i].name] = elm ? list.utils.getAttribute(elm, valueNames[i].attr) : "";
} else {
elm = list.utils.getByClass(item.elm, valueNames[i], true);
values[valueNames[i]] = elm ? elm.innerHTML : "";
}
elm = undefined;
}
return values;
};
this.set = function(item, values) {
var getValueName = function(name) {
for (var i = 0, il = list.valueNames.length; i < il; i++) {
if (list.valueNames[i].data) {
var data = list.valueNames[i].data;
for (var j = 0, jl = data.length; j < jl; j++) {
if (data[j] === name) {
return { data: name };
}
}
} else if (list.valueNames[i].attr && list.valueNames[i].name && list.valueNames[i].name == name) {
return list.valueNames[i];
} else if (list.valueNames[i] === name) {
return name;
}
}
};
var setValue = function(name, value) {
var elm;
var valueName = getValueName(name);
if (!valueName)
return;
if (valueName.data) {
item.elm.setAttribute('data-'+valueName.data, value);
} else if (valueName.attr && valueName.name) {
elm = list.utils.getByClass(item.elm, valueName.name, true);
Eif (elm) {
elm.setAttribute(valueName.attr, value);
}
} else {
elm = list.utils.getByClass(item.elm, valueName, true);
if (elm) {
elm.innerHTML = value;
}
}
elm = undefined;
};
if (!templater.create(item)) {
for(var v in values) {
Eif (values.hasOwnProperty(v)) {
setValue(v, values[v]);
}
}
}
};
this.create = function(item) {
if (item.elm !== undefined) {
return false;
}
if (itemSource === undefined) {
throw new Error("The list need to have at list one item on init otherwise you'll have to add a template.");
}
/* If item source does not exists, use the first item in list as
source for new items */
var newItem = itemSource.cloneNode(true);
newItem.removeAttribute('id');
item.elm = newItem;
templater.set(item, item.values());
return true;
};
this.remove = function(item) {
if (item.elm.parentNode === list.list) {
list.list.removeChild(item.elm);
}
};
this.show = function(item) {
templater.create(item);
list.list.appendChild(item.elm);
};
this.hide = function(item) {
if (item.elm !== undefined && item.elm.parentNode === list.list) {
list.list.removeChild(item.elm);
}
};
this.clear = function() {
/* .innerHTML = ''; fucks up IE */
if (list.list.hasChildNodes()) {
while (list.list.childNodes.length >= 1)
{
list.list.removeChild(list.list.firstChild);
}
}
};
init();
};
module.exports = function(list) {
return new Templater(list);
};
|