-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.temple.js
211 lines (173 loc) · 6.54 KB
/
jquery.temple.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
(function() {
//exit where there is no $ (jQuery or Zepto)
if (typeof $ === 'undefined') {
return;
}
//used for actual templating when the node is a leaf node
$.fn.templeLeaf = function(propValue, propName, currentStash) {
var typeOfPropValue = typeof propValue,
leafNode = this;
switch(typeOfPropValue) {
//when value is a primitive (string and number are assumed to be easily
//convertible to text) just put the value in the node with the default
//way (depending on the node type)
case 'string':
case 'number':
var htmlContainer = leafNode,
textContainer = leafNode;
//because of simple templating sometimes we want to find a node
//that doesn't have the attribute data-templ, but in that case
//it should only be used by text templating
//example: $(node).templeLeaf('some text')
//html templating must always require data-templ-html on the node
if (propName) {
htmlContainer = htmlContainer.filter('[data-templ-html~="'+propName+'"]');
}
else {
htmlContainer = htmlContainer.filter('[data-templ-html]');
}
//if we need to use HTML
if (htmlContainer.length) {
htmlContainer.empty().html( propValue );
//newly created stash might complain data-templ
//attribs, then they might want to attempt
//to be templated
currentStash && htmlContainer.children().temple(currentStash);
//short circuit to avoid doing both html and text
return leafNode;
}
//defaults to text
//uses value for input types
if (propName) {
textContainer = textContainer.filter('[data-templ~="'+propName+'"]');
}
textContainer.each(function(i, node) {
switch ( node.nodeName ) {
case 'INPUT':
case 'TEXTAREA':
$(node).val( propValue );
break;
case 'OPTGROUP':
$(node).attr( 'label', propValue );
break;
default:
$(node).text( propValue )
break;
}
});
break;
case 'object':
case 'function':
if (propValue===null) {
leafNode.filter('[data-templ~="'+propName+'"]').remove();
}
else {
var jqPropValue = propValue.value,
jqPropAttr = propValue.attr;
//if attr name is preceded with a + we need to
//append a new value, rather than swap
//this is typical when adding classes
//but we will make it generic
if ( jqPropAttr.charAt(0)=='+' ) {
jqPropAttr = jqPropAttr.substr(1);
jqPropValue = function(i, oldValue) {
//jQuery is supposed to pass an oldValue,
//but it sometimes fails (in case of a class)
if (typeof oldValue=='undefined') {
oldValue = leafNode.attr(jqPropAttr)||'';
}
//problem here is when appending attributes
//sometimes you want to add them after a space
//and sometimes you just want to append
//for now we will just add a space bu default
//because there doesn't seem to be any obvious use case
//for other scenario
return oldValue + ' ' + propValue.value;
}
}
if ( jqPropAttr.charAt(0)=='-' ) {
jqPropAttr = jqPropAttr.substr(1);
jqPropValue = function(i, oldValue) {
//jQuery is supposed to pass an oldValue,
//but it sometimes fails (in case of a class)
if (typeof oldValue=='undefined') {
oldValue = leafNode.attr(jqPropAttr)||'';
}
return oldValue.replace(propValue.value, '');
}
}
leafNode
.filter('[data-templ~="'+propName+'"]')
.attr(jqPropAttr, jqPropValue);
}
break;
}
return leafNode;
};
//used on a root node, traverses the tree looking for nodes to template
$.fn.temple = function(stash, stencil) {
var rootNode = this,
dataSelector = '[data-templ], [data-templ-html]',
stencil,
dataNodes = rootNode
.filter(dataSelector)
.add( rootNode.find(dataSelector) );
for (var propName in stash) {
var stashProp = stash[propName];
if ( $.isArray(stashProp) ) {
var $node = dataNodes
.filter([
'[data-templ~="'+propName+'"]',
'[data-templ-html~="'+propName+'"]'
].join(','));
if ($node.length) {
//look for stencil
//1. The stencil can be passed as a jQuery compatible argument
//make sure we have a jQuery compatible argument to work with
if (typeof stencil !== 'undefined') {
if (typeof stencil.jquery == 'undefined') {
stencil = $(stencil);
}
}
else {
//2. look for previously cached stencil
var cachedStencil = $node.data('temple-stencil');
if (cachedStencil && cachedStencil.length) {
stencil = cachedStencil
}
//3. treat 1st child of the element as a stencil
else {
stencil = $node.children().eq(0);
}
}
if (stencil.length) {
stencil = stencil.clone();
$node.empty();
//cache stencil on the node
//this might be incompatible with zepto as it doesn't save
//object in data
$node.data('temple-stencil', stencil.clone());
$.each(stashProp, function(index, stashArrayElem) {
var $subNode = stencil.clone().show();
if (typeof stashArrayElem === 'string') {
//we will ignore the key here and just template it
//problem is it will not template the node without
//the key
$subNode.templeLeaf(stashArrayElem)
}
$subNode.temple(stashArrayElem)
$node.append($subNode);
});
}
}
}
else {
var stashCopy = $.extend({}, stash);
delete stashCopy[propName];
dataNodes
.templeLeaf(stashProp, propName, stashCopy);
}
}
return rootNode;
};
})();