forked from twigkit/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempo.js
693 lines (593 loc) · 25.6 KB
/
tempo.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*!
* Tempo Template Engine 1.6
*
* http://tempojs.com/
*/
function TempoEvent(type, item, element) {
this.type = type;
this.item = item;
this.element = element;
return this;
}
TempoEvent.Types = {
RENDER_STARTING : 'render_starting',
ITEM_RENDER_STARTING : 'item_render_starting',
ITEM_RENDER_COMPLETE : 'item_render_complete',
RENDER_COMPLETE : 'render_complete'
};
var Tempo = (function (tempo) {
/*!
* Constants
*/
var NUMBER_FORMAT_REGEX = /(\d+)(\d{3})/;
/*!
* Helpers
*/
var utils = {
memberRegex : function (obj) {
var member_regex = '';
for (var member in obj) {
if (obj.hasOwnProperty(member)) {
if (member_regex.length > 0) {
member_regex += '|';
}
member_regex += member;
}
}
return member_regex;
},
pad : function (val, pad, size) {
while (val.length < size) {
val = pad + val;
}
return val;
},
trim : function (str) {
return str.replace(/^\s*([\S\s]*?)\s*$/, '$1');
},
startsWith : function (str, prefix) {
return (str.indexOf(prefix) === 0);
},
clearContainer : function (el) {
if (el !== undefined && el.childNodes !== undefined) {
for (var i = el.childNodes.length; i >= 0; i--) {
if (el.childNodes[i] !== undefined && el.childNodes[i].getAttribute !== undefined && el.childNodes[i].getAttribute('data-template') !== null) {
el.childNodes[i].parentNode.removeChild(el.childNodes[i]);
}
}
}
},
isNested : function (el) {
var p = el.parentNode;
while (p) {
if (p.getAttribute !== undefined && p.getAttribute('data-template') !== null) {
return true;
}
p = p.parentNode;
}
return false;
},
equalsIgnoreCase : function (str1, str2) {
return str1.toLowerCase() === str2.toLowerCase();
},
getElement : function (template, html) {
if (utils.equalsIgnoreCase(template.tagName, 'tr')) {
// Wrapping to get around read-only innerHTML
var el = document.createElement('div');
el.innerHTML = '<table><tbody>' + html + '</tbody></table>';
var depth = 3;
while (depth--) {
el = el.lastChild;
}
return el;
} else {
// No need to wrap
template.innerHTML = html;
return template;
}
},
typeOf : function (obj) {
if (typeof(obj) === "object") {
if (obj === null) {
return "null";
}
if (obj.constructor === ([]).constructor) {
return "array";
}
if (obj.constructor === (new Date()).constructor) {
return "date";
}
if (obj.constructor === (new RegExp()).constructor) {
return "regex";
}
return "object";
}
return typeof(obj);
},
notify : function (listener, event) {
if (listener !== undefined) {
listener(event);
}
}
};
function Templates(params, nestedItem) {
this.params = params;
this.defaultTemplate = null;
this.namedTemplates = {};
this.container = null;
this.nestedItem = nestedItem !== undefined ? nestedItem : null;
this.var_brace_left = '\\{\\{';
this.var_brace_right = '\\}\\}';
this.tag_brace_left = '\\{%';
this.tag_brace_right = '%\\}';
if (typeof params !== 'undefined') {
for (var prop in params) {
if (prop === 'var_braces') {
this.var_brace_left = params[prop].substring(0, params[prop].length / 2);
this.var_brace_right = params[prop].substring(params[prop].length / 2);
} else if (prop === 'tag_braces') {
this.tag_brace_left = params[prop].substring(0, params[prop].length / 2);
this.tag_brace_right = params[prop].substring(params[prop].length / 2);
} else if (typeof this[prop] !== 'undefined') {
this[prop] = params[prop];
}
}
}
return this;
}
Templates.prototype = {
parse: function (container) {
this.container = container;
var children = container.getElementsByTagName('*');
for (var i = 0; i < children.length; i++) {
if (children[i].getAttribute !== undefined && children[i].getAttribute('data-template') !== null && (this.nestedItem === children[i].getAttribute('data-template') || children[i].getAttribute('data-template') === '' || children[i].getAttribute('data-template') === 'data-template' && !utils.isNested(children[i]))) {
this.createTemplate(children[i]);
} else if (children[i].getAttribute !== undefined && children[i].getAttribute('data-template-fallback') !== null) {
// Hiding the fallback template
children[i].style.display = 'none';
}
}
// If there is no default template (data-template) then create one from container
if (this.defaultTemplate === null) {
// Creating a template inside the container
var el = document.createElement('div');
el.setAttribute('data-template', '');
el.innerHTML = this.container.innerHTML;
// Clearing container before adding the wrapped contents
this.container.innerHTML = '';
// There is now a default template present with a data-template attribute
this.container.appendChild(el);
this.createTemplate(el);
}
utils.clearContainer(this.container);
},
createTemplate : function (node) {
var element = node.cloneNode(true);
// Clear display: none;
if (element.style.removeAttribute) {
element.style.removeAttribute('display');
}
else {
element.style.removeProperty('display');
}
// Remapping container element in case template
// is deep in container
this.container = node.parentNode;
// Element is a template
var nonDefault = false;
for (var a = 0; a < element.attributes.length; a++) {
var attr = element.attributes[a];
// If attribute
if (utils.startsWith(attr.name, 'data-if-')) {
var val;
if (attr.value === '') {
val = true;
} else {
val = '\'' + attr.value + '\'';
}
this.namedTemplates[attr.name.substring(8, attr.name.length) + '==' + val] = element;
element.removeAttribute(attr.name);
nonDefault = true;
}
}
// Setting as default template, last one wins
if (!nonDefault) {
this.defaultTemplate = element;
}
},
templateFor: function (i) {
for (var templateName in this.namedTemplates) {
if (eval('i.' + templateName)) {
return this.namedTemplates[templateName].cloneNode(true);
}
}
if (this.defaultTemplate) {
return this.defaultTemplate.cloneNode(true);
}
}
};
/*!
* Renderer for populating containers with data using templates.
*/
function Renderer(templates) {
this.templates = templates;
this.listener = undefined;
this.started = false;
this.varRegex = new RegExp(this.templates.var_brace_left + '[ ]?([A-Za-z0-9$\\._\\[\\]]*?)([ ]?\\|[ ]?.*?)?[ ]?' + this.templates.var_brace_right, 'g');
this.tagRegex = new RegExp(this.templates.tag_brace_left + '[ ]?([\\s\\S]*?)( [\\s\\S]*?)?[ ]?' + this.templates.tag_brace_right + '(([\\s\\S]*?)(?=' + this.templates.tag_brace_left + '[ ]?end\\1[ ]?' + this.templates.tag_brace_right + '))?', 'g');
return this;
}
Renderer.prototype = {
notify : function (listener) {
this.listener = listener;
return this;
},
_replaceVariables : function (renderer, _tempo, i, str) {
return str.replace(this.varRegex, function (match, variable, args) {
try {
var val = null;
// Handling tempo_info variable
if (utils.startsWith(variable, '_tempo.')) {
return eval(variable);
}
if (utils.typeOf(i) === 'array') {
val = eval('i' + variable);
} else {
val = eval('i.' + variable);
}
// Handle filters
var filterSplitter = new RegExp('\\|[ ]?(?=' + utils.memberRegex(renderer.filters) + ')', 'g');
if (args !== undefined && args !== '') {
var filters = utils.trim(utils.trim(args).substring(1)).split(filterSplitter);
for (var p = 0; p < filters.length; p++) {
var filter = utils.trim(filters[p]);
var filter_args = [];
// If there is a space, there must be arguments
if (filter.indexOf(' ') > -1) {
var f = filter.substring(filter.indexOf(' ')).replace(/^[ ']*|[ ']*$/g, '');
filter_args = f.split(/(?:[\'"])[ ]?,[ ]?(?:[\'"])/);
filter = filter.substring(0, filter.indexOf(' '));
}
val = renderer.filters[filter](val, filter_args);
}
}
if (val !== undefined) {
return val;
}
} catch (err) {
}
return '';
});
},
_replaceObjects : function (renderer, _tempo, i, str) {
var regex = new RegExp('(?:__[\\.]?)((_tempo|\\[|' + utils.memberRegex(i) + ')([A-Za-z0-9$\\._\\[\\]]+)?)', 'g');
return str.replace(regex, function (match, variable, args) {
try {
var val = null;
// Handling tempo_info variable
if (utils.startsWith(variable, '_tempo.')) {
return eval(variable);
}
if (utils.typeOf(i) === 'array') {
val = eval('i' + variable);
} else {
val = eval('i.' + variable);
}
if (val !== undefined) {
if (utils.typeOf(val) === 'string') {
return '\'' + val + '\'';
} else {
return val;
}
}
} catch (err) {
}
return undefined;
});
},
_applyAttributeSetters : function (renderer, item, str) {
return str.replace(/([A-z0-9]+?)(?==).*?data-\1="(.*?)"/g, function (match, attr, data_value) {
if (data_value !== '') {
return attr + '="' + data_value + '"';
}
return match;
});
},
_applyTags : function (renderer, item, str) {
return str.replace(this.tagRegex, function (match, tag, args, body) {
if (renderer.tags.hasOwnProperty(tag)) {
args = args.substring(args.indexOf(' ')).replace(/^[ ]*|[ ]*$/g, '');
var filter_args = args.split(/(?:['"])[ ]?,[ ]?(?:['"])/);
return renderer.tags[tag](renderer, item, match, filter_args, body);
} else {
return '';
}
});
},
starting : function () {
// Use this to manually fire the RENDER_STARTING event e.g. just before you issue an AJAX request
// Useful if you're not calling prepare immediately before render
this.started = true;
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.RENDER_STARTING, undefined, undefined));
return this;
},
renderItem : function (renderer, tempo_info, i, fragment) {
var template = renderer.templates.templateFor(i);
if (template && i) {
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.ITEM_RENDER_STARTING, i, template));
var nestedDeclaration = template.innerHTML.match(/data-template="(.*?)"/g);
if (nestedDeclaration) {
for (var p = 0; p < nestedDeclaration.length; p++) {
var nested = nestedDeclaration[p].match(/"(.*?)"/)[1];
var t = new Templates(renderer.templates.params, nested);
t.parse(template);
var r = new Renderer(t);
r.render(eval('i.' + nested));
}
}
// Dealing with HTML as a String from now on (to be reviewed)
// Attribute values are escaped in FireFox so making sure there are no escaped tags
var html = template.innerHTML.replace(/%7B%7B/g, '{{').replace(/%7D%7D/g, '}}');
// Tags
html = this._applyTags(this, i, html);
// Content
html = this._replaceVariables(this, tempo_info, i, html);
// JavaScript objects
html = this._replaceObjects(this, tempo_info, i, html);
// Template class attribute
if (template.getAttribute('class')) {
template.className = this._replaceVariables(this, tempo_info, i, template.className);
}
// Template id
if (template.getAttribute('id')) {
template.id = this._replaceVariables(this, tempo_info, i, template.id);
}
html = this._applyAttributeSetters(this, i, html);
fragment.appendChild(utils.getElement(template, html));
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.ITEM_RENDER_COMPLETE, i, template));
}
},
_createFragment : function (data) {
if (data) {
var tempo_info = {};
var fragment = document.createDocumentFragment();
// If object then wrapping in an array
if (utils.typeOf(data) === 'object') {
data = [data];
}
for (var i = 0; i < data.length; i++) {
tempo_info.index = i;
this.renderItem(this, tempo_info, data[i], fragment);
}
return fragment;
}
return null;
},
render : function (data) {
// Check if starting event was manually fired
if (!this.started) {
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.RENDER_STARTING, undefined, undefined));
}
this.clear();
this.append(data);
return this;
},
append : function (data) {
// Check if starting event was manually fired
if (!this.started) {
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.RENDER_STARTING, undefined, undefined));
}
var fragment = this._createFragment(data);
if (fragment !== null) {
this.templates.container.appendChild(fragment);
}
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.RENDER_COMPLETE, undefined, undefined));
return this;
},
prepend : function (data) {
// Check if starting event was manually fired
if (!this.started) {
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.RENDER_STARTING, undefined, undefined));
}
var fragment = this._createFragment(data);
if (fragment !== null) {
this.templates.container.insertBefore(fragment, this.templates.container.firstChild);
}
utils.notify(this.listener, new TempoEvent(TempoEvent.Types.RENDER_COMPLETE, undefined, undefined));
return this;
},
clear : function (data) {
utils.clearContainer(this.templates.container);
},
tags : {
'if' : function (renderer, i, match, args, body) {
var member_regex = utils.memberRegex(i);
var expr = args[0].replace(/&/g, '&');
expr = expr.replace(new RegExp(member_regex, 'gi'), function (match) {
return 'i.' + match;
});
var blockRegex = new RegExp(renderer.templates.tag_brace_left + '[ ]?else[ ]?' + renderer.templates.tag_brace_right, 'g');
var blocks = body.split(blockRegex);
if (eval(expr)) {
return blocks[0];
} else if (blocks.length > 1) {
return blocks[1];
}
return '';
}
},
filters : {
'truncate' : function (value, args) {
if (value !== undefined) {
var len = 0;
var rep = '...';
if (args.length > 0) {
len = parseInt(args[0]);
}
if (args.length > 1) {
rep = args[1];
}
if (value.length > len - 3) {
return value.substr(0, len - 3) + rep;
}
return value;
}
},
'format' : function (value, args) {
if (value !== undefined) {
var x = (value + '').split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
while (NUMBER_FORMAT_REGEX.test(x1)) {
x1 = x1.replace(NUMBER_FORMAT_REGEX, '$1' + ',' + '$2');
}
return x1 + x2;
}
},
'upper' : function (value, args) {
return value.toUpperCase();
},
'lower' : function (value, args) {
return value.toLowerCase();
},
'trim' : function (value, args) {
return utils.trim(value);
},
'replace' : function (value, args) {
if (value !== undefined && args.length === 2) {
return value.replace(new RegExp(args[0], 'g'), args[1]);
}
return value;
},
'append' : function (value, args) {
if (value !== undefined && args.length === 1) {
return value + '' + args[0];
}
return value;
},
'prepend' : function (value, args) {
if (value !== undefined && args.length === 1) {
return args[0] + '' + value;
}
return value;
},
'default' : function (value, args) {
if (value !== undefined && value !== null) {
return value;
}
if (args.length === 1) {
return args[0];
}
return value;
},
'date' : function (value, args) {
if (value !== undefined && args.length === 1) {
var date = new Date(value);
var format = args[0];
if (format === 'localedate') {
return date.toLocaleDateString();
} else if (format === 'localetime') {
return date.toLocaleTimeString();
} else if (format === 'date') {
return date.toDateString();
} else if (format === 'time') {
return date.toTimeString();
} else {
var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var DATE_PATTERNS = {
'YYYY' : function (date) {
return date.getFullYear();
},
'YY' : function (date) {
return date.getFullYear().toFixed().substring(2);
},
'MMMM' : function (date) {
return MONTHS[date.getMonth()];
},
'MMM' : function (date) {
return MONTHS[date.getMonth()].substring(0, 3);
},
'MM' : function (date) {
return utils.pad((date.getMonth() + 1).toFixed(), '0', 2);
},
'M' : function (date) {
return date.getMonth() + 1;
},
'DD' : function (date) {
return utils.pad(date.getDate().toFixed(), '0', 2);
},
'D' : function (date) {
return date.getDate();
},
'EEEE' : function (date) {
return DAYS[date.getDay()];
},
'EEE' : function (date) {
return DAYS[date.getDay()].substring(0, 3);
},
'E' : function (date) {
return date.getDay();
},
'HH' : function (date) {
return utils.pad(date.getHours().toFixed(), '0', 2);
},
'H' : function (date) {
return date.getHours();
},
'mm' : function (date) {
return utils.pad(date.getMinutes().toFixed(), '0', 2);
},
'm' : function (date) {
return date.getMinutes();
},
'ss' : function (date) {
return utils.pad(date.getSeconds().toFixed(), '0', 2);
},
's' : function (date) {
return date.getSeconds();
},
'SSS' : function (date) {
return utils.pad(date.getMilliseconds().toFixed(), '0', 3);
},
'S' : function (date) {
return date.getMilliseconds();
},
'a' : function (date) {
return date.getHours() < 12 ? 'AM' : 'PM';
}
};
format = format.replace(/(\\)?(Y{2,4}|M{1,4}|D{1,2}|E{1,4}|H{1,2}|m{1,2}|s{1,2}|S{1,3}|a)/g, function (match, escape, pattern) {
if (!escape) {
if (DATE_PATTERNS.hasOwnProperty(pattern)) {
return DATE_PATTERNS[pattern](date);
}
}
return pattern;
});
return format;
}
}
return '';
}
}
};
/*!
* Prepare a container for rendering, gathering templates and
* clearing afterwards.
*/
tempo.prepare = function (container, params) {
if (typeof container === 'string') {
container = document.getElementById(container);
}
var templates = new Templates(params);
templates.parse(container);
return new Renderer(templates);
};
tempo.test = {
'utils' : utils,
'templates': new Templates({}),
'renderer' : new Renderer(new Templates({}))
};
return tempo;
})(Tempo || {});