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
|
/*global tizen, $, app, ModifierManager */
/**
* @class TemplateManager
*/
function TemplateManager() {
'use strict';
this.init();
}
(function () { // strict mode wrapper
'use strict';
TemplateManager.prototype = {
/**
* Template cache
*/
cache: {},
/**
* UI module initialisation
*/
init: function init() {
this.modifiers = new ModifierManager().getAll();
},
/**
* Returns template html (from cache)
* @param {string} tplName
* @param {string} tplParams
*/
get: function TemplateManager_get(tplName, tplParams) {
if (this.cache[tplName] !== undefined) {
return this.getCompleted(this.cache[tplName], tplParams);
}
return '';
},
/**
* Load templates to cache
* @param {string} tplNames
* @param {function} onSuccess
*/
loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
var self = this,
cachedTemplates = 0,
tplName,
tplPath;
if ($.isArray(tplNames)) {
// for each template
$.each(tplNames, function (index, fileName) {
// cache template html
if (self.cache[fileName] === undefined) {
tplName = [fileName, app.config.get('templateExtension')].join('');
tplPath = [app.config.get('templateDir'), tplName].join('/');
$.ajax({
url: tplPath,
cache: true,
dataType: 'html',
async: true,
success: function (data) {
// increase counter
cachedTemplates += 1;
// save to cache
self.cache[fileName] = data;
// if all templates are cached launch callback
if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
onSuccess();
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.error('templateManagerError: ' + errorThrown);
}
});
} else {
// template is already cached
cachedTemplates += 1;
// if all templates are cached launch callback
if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
onSuccess();
}
}
});
}
},
/**
* Returns template completed by specified params
* @param {string} tplHtml
* @param {string} tplParams
*/
getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
var tplParam;
for (tplParam in tplParams) {
if (tplParams.hasOwnProperty(tplParam)) {
tplHtml = this.passThruModifiers(tplHtml, tplParam, tplParams[tplParam]);
}
}
return tplHtml;
},
passThruModifiers: function (tplHtml, tplParam, content) {
var regModOn = new RegExp('%' + tplParam + '\\|([a-zA-Z]){1,}%', 'g'),
regModOff = new RegExp(['%', tplParam, '%'].join(''), 'g'),
regModGet = new RegExp('%' + tplParam + '\\|(.+?)%'),
modifier;
if (regModOn.test(tplHtml)) {
modifier = tplHtml.match(regModGet)[1];
try {
content = this.modifiers[modifier](content);
} catch (error) {
console.error('unknown modifier: ' + modifier);
}
tplHtml = tplHtml.replace(regModOn, content);
} else {
tplHtml = tplHtml.replace(regModOff, content);
}
return tplHtml;
}
};
}());
|