-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.fs.template.js
More file actions
374 lines (351 loc) · 15.1 KB
/
jquery.fs.template.js
File metadata and controls
374 lines (351 loc) · 15.1 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*!
* Fantasite Template JavaScript Library v0.2.1
* https://github.com/MasGaNo/jqueryTemplate
*
* Dependency with jquery.js
* http://jquery.com/
*
* Date: 2013-10-09
*/
(function($)
{
// Default config of fs.template
var defaultConfig = {
// Prefix of class for DOM Template mode
prefix: 'fs_',
// Template render mode. Default: dom
template: 'dom'
};
/**
* Merge object to another object
* @param Object obj1 Object to merge to
* @param Object obj2 Object to merge from
* @return Object Return obj1
*/
var mergeObject = function(obj1, obj2)
{
for (var i in obj2) {
obj1[i] = obj2[i];
}
return obj1;
};
/************************\
** DOM Template mode **
\************************/
/**
* Get class name of template element
* @param DOMElement element DOMElement to check
* @param Object options Options of template
* @return string Class name of FS Element template. null if not found.
*/
var getClassName = function(element, options) {
var lClass = $(element).attr('class');
if (!lClass) {
return null;
}
var classList = lClass.split(/\s+/);//Check each className
var returnClass = null;
$.each(classList, function(index, className) {
var lClassName = className.substr(0, options.prefix.length);
if (lClassName === options.prefix) {
returnClass = className;
return false;// "Break jQuery each
}
});
return returnClass;
};
/**
* Parse template DOM
* @param DOMElement element Template to parse
* @param Object options Options Options of template
* @return Object Collection of template object
*/
var getTpl2 = function(element, options, list) {
if (list === undefined) {
list = {};
var className = getClassName(element, options);
if (className) {
var shortName = className.substr(options.prefix.length);
list[shortName] = {tpl:$(element), child:{}};
return getTpl2($(element), options, list[shortName].child);
}
}
var child = $(element).children();
child.each(function(index, item) {
var className = getClassName(item, options);
if (className) {
var shortName = className.substr(options.prefix.length);
list[shortName] = {tpl:$(item), child:{}};
getTpl2($(item), options, list[shortName].child);
} else {
getTpl2($(item), options, list);
}
});
return list;
}
/**
* Apply values to template to generate HTML
* @param Object tplList Collection of template
* @param Object args Values to apply to template
* @param Object options Options Options of template
*/
var applyTpl2 = function(tplList, args, options) {
for (var i in tplList) {
if (args[i] === undefined) {
tplList[i].tpl.remove();//Remove this child if unused
} else if (args[i] instanceof Array) {
var lArgs = args[i];
for (var ind = 0; ind < lArgs.length; ++ind) {
var lArg = lArgs[ind];
var lClone = tplList[i].tpl.clone();
tplList[i].tpl.after(lClone);//Insert after this element ?
applyTpl2(getTpl2(lClone, options), lArg, options);
}
tplList[i].tpl.remove();
} else if (args[i] instanceof Object) {//converter if is object) {
var lArg = args[i];
if (lArg['value'] === undefined) {
continue;
}
if (lArg['converter'] !== undefined) {
tplList[i].tpl.html(converters[lArg['converter']](lArg['value']));
} else {
tplList[i].tpl.html(lArg['value']);
}
}else {
tplList[i].tpl.html(args[i]);
}
}
};
/****************************\
** End DOM Template mode **
\****************************/
/************************************\
** Variabke Replace Template mode **
\************************************/
/**
* Decode all variables of template and keep hierarchy between variable
* @param Array matches List of variables
* @return Object Hierarchy of variable
*/
var recureParseVariablesVariableReplace = function(matches) {
var variables = {array:[], variables:{}};
var currVariables = {};
for (var i = 0; i < matches.length; ++i) {
var match = matches[i];
var variable = match.substr(1, match.length - 2);
if (variable.charAt(0) === '$') {
var name = variable.substr(1);
if (variables.variables[name] === undefined) {
currVariables[name] = variables.variables[name] = {value:match, type:'variable'};
} else {
currVariables[name] = variables.variables[name] = mergeObject(variables.variables[name], {value:match, type:'variable'});
}
} else {
var parts = variable.split(':');
if (parts.length !== 2 || parts[1].charAt(0) !== '$') {
continue;
}
/** TODO: Refactoring with external "plugin" function add new Variable type **/
if (parts[0] === 'array') {
var searchEnd = '{/' + variable + '}';
var countRecursive = 0;//Check if pattern element is not in it-self
for (var j = i + 1; j < matches.length; ++j) {
if (matches[j] === match) {
++countRecursive;
} else if (matches[j] === searchEnd) {
if (countRecursive > 0) {
--countRecursive;
} else {
var child = {name:parts[1].substr(1), variable:{type:'array', value:match, valueEnd:searchEnd, child:recureParseVariablesVariableReplace(matches.splice(i + 1, j - i - 1))}};
if (currVariables[name] !== undefined) {
child = mergeObject(child, currVariables[name]);
delete variables.variables[name];
}
variables.array.push(child);
currVariables[name] = child;
matches.splice(i, 1);
break;
}
}
}
} else if (parts[0] === 'isset') {//TODO: {isset:$var}{/isset:$var}. if args[$var] doesn't exit, remove block, if exist keep variables context.
var name = parts[1].substr(1);
if (currVariables[name] === undefined) {
currVariables[name] = variables.variables[name] = {isset:{value:match, valueEnd:'{/' + variable + '}'}};
} else {
currVariables[name]['isset'] = {value:match, valueEnd:'{/' + variable + '}'};
}
//Remove {/isset:$var} from matches;
} else if (parts[0] === 'empty') {//TODO: {empty:$var}{/empty:$var}. if args[$var] doesn't exit, use this block and keep variables context.
var name = parts[1].substr(1);
if (currVariables[name] === undefined) {
currVariables[name] = variables.variables[name] = {empty:{value:match, valueEnd:'{/' + variable + '}'}};
} else {
currVariables[name]['empty'] = {value:match, valueEnd:'{/' + variable + '}'};
}
//Remove {/empty:$var} from matches;
} else if (parts[0] === 'count'){//TODO: {count:$var}. if args[$var] doesn't exit, replace by 0. Count array.
var name = parts[1].substr(1);
if (currVariables[name] === undefined) {
currVariables[name] = variables.variables[name] = {count:{value:match}};
} else {
currVariables[name]['count'] = {value:match};
}
}
}
}
return variables;
};
/**
* Parse template on VariableReplace mode
* @param String template Template to parse
* @return Object List of variables in the template
*/
var parseTemplateVariableReplace = function(template) {
var matches = template.match(/\{(.+?)\}/g);
return recureParseVariablesVariableReplace(matches);
};
/**
* Apply values to template to generate HTML
* @param String template Template code
* @param Object args Values to apply to template
* @param Object variables List of template's variables
* @param Object options Options Options of template
* @return String HTML generated
*/
var applyTplVariableReplace = function(template, args, variables, options) {
for (var i = 0; i < variables.array.length; ++i) {
var variable = variables.array[i];
var start = template.indexOf(variable.variable.value);
var end = template.indexOf(variable.variable.valueEnd, end) + variable.variable.valueEnd.length;
var isNull = false;
var count = 0;
//Add empty/isset check
if (args[variable.name] === undefined) {
template = template.substr(start, end);
isNull = true;
} else {
var tpl1 = template.substr(0, start);
var tpl = template.substring(start + variable.variable.value.length, end - variable.variable.valueEnd.length);
var tpl2 = template.substr(end);
var lArg = args[variable.name];
template = '';
var variablesChild = variable.variable.child;
count = lArg.length;
for (var ind = 0; ind < lArg.length; ++ind) {
template += applyTplVariableReplace(tpl, lArg[ind], variablesChild, options);
}
isNull = (template === '');
template = tpl1 + template + tpl2;
}
if (isNull) {
if (variable['empty'] !== undefined) {
var lEmpty = variable['empty'];
template = template.split(lEmpty.value).join('').split(lEmpty.valueEnd).join('');
}
if (variable['isset'] !== undefined) {
var lIsset = variable['isset'];
template = template.substr(0, template.indexOf(lIsset.value)) + template.substr(template.indexOf(lIsset.valueEnd) + lIsset.valueEnd.length);
}
} else {
if (variable['empty'] !== undefined) {
var lEmpty = variable['empty'];
template = template.substr(0, template.indexOf(lEmpty.value)) + template.substr(template.indexOf(lEmpty.valueEnd) + lEmpty.valueEnd.length);
}
if (variable['isset'] !== undefined) {
var lIsset = variable['isset'];
template = template.split(lIsset.value).join('').split(lIsset.valueEnd).join('');
}
}
if (variable['count'] !== undefined) {
template = template.split(variable.count.value).join(count);
}
}
for (var i in variables.variables) {
var variable = variables.variables[i];
var value;
var lArg = args[i];
//Add empty/isset check
if (lArg === undefined) {
value = null;
} else {
if (lArg instanceof Object) {
if (lArg['value'] === undefined) {
value = null;
} else if (lArg['converter'] !== undefined) {
value = converters[lArg['converter']](lArg['value']);
} else {
value = lArg['value'];
}
} else {
value = lArg;
}
}
if (value === null) {
if (variable['empty'] !== undefined) {
var lEmpty = variable['empty'];
template = template.split(lEmpty.value).join('').split(lEmpty.valueEnd).join('');
}
if (variable['isset'] !== undefined) {
var lIsset = variable['isset'];
template = template.substr(0, template.indexOf(lIsset.value)) + template.substr(template.indexOf(lIsset.valueEnd) + lIsset.valueEnd.length);
continue;
}
value = '';
} else {
if (variable['isset'] !== undefined) {
var lIsset = variable['isset'];
template = template.split(lIsset.value).join('').split(lIsset.valueEnd).join('');
}
if (variable['empty'] !== undefined) {
var lEmpty = variable['empty'];
template = template.substr(0, template.indexOf(lEmpty.value)) + template.substr(template.indexOf(lEmpty.valueEnd) + lEmpty.valueEnd.length);
}
}
template = template.split(variable.value).join(value);
}
return template;
};
/****************************************\
** End Variable Replace Template mode **
\****************************************/
/**
* Entry point of method's list:
* - dom: Template based on DOM and Class variable (fast but only content replace)
* - variableReplace: Template based on {$variable} (heavy but flexible, full HTML replace)
*/
var templateMode = {
dom: function(template, args, options) {
var tplList = getTpl2(template, options);
applyTpl2(tplList, args, options);
return template;
},
variableReplace: function(template, args, options) {
if (template instanceof Object) {
template = template[0].outerHTML;
}
var variables = parseTemplateVariableReplace(template);
return applyTplVariableReplace(template, args, variables, options);
}
};
$.fn.fsTpl = function(args, config)
{
return $.fsTpl($(this), args, config);
};
$.fsTpl = function(template, args, config)
{
if (config === undefined) {
config = {};
}
var options = mergeObject(mergeObject({}, defaultConfig), config);
var clone;
if (template instanceof Object) {
clone = template.clone();
} else {
clone = template.slice();
}
return templateMode[options.template](clone, args, options);
};
var converters = $.fsTpl.converters = $.fn.fsTpl.cvtr = $.fn.fsTpl.converters = $.fn.fsTpl.cvtr = {};
})(jQuery);