1 /*global tizen, $, app, ModifierManager */
3 * @class TemplateManager
5 function TemplateManager() {
10 (function () { // strict mode wrapper
12 TemplateManager.prototype = {
20 * UI module initialisation
22 init: function init() {
23 this.modifiers = new ModifierManager().getAll();
27 * Returns template html (from cache)
28 * @param {string} tplName
29 * @param {string} tplParams
31 get: function TemplateManager_get(tplName, tplParams) {
32 if (this.cache[tplName] !== undefined) {
33 return this.getCompleted(this.cache[tplName], tplParams);
39 * Load templates to cache
40 * @param {string} tplNames
41 * @param {function} onSuccess
43 loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
49 if ($.isArray(tplNames)) {
52 $.each(tplNames, function (index, fileName) {
54 // cache template html
55 if (self.cache[fileName] === undefined) {
56 tplName = [fileName, app.config.get('templateExtension')].join('');
57 tplPath = [app.config.get('templateDir'), tplName].join('/');
64 success: function (data) {
69 self.cache[fileName] = data;
71 // if all templates are cached launch callback
72 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
76 error: function (jqXHR, textStatus, errorThrown) {
77 console.error('templateManagerError: ' + errorThrown);
81 // template is already cached
83 // if all templates are cached launch callback
84 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
94 * Returns template completed by specified params
95 * @param {string} tplHtml
96 * @param {string} tplParams
98 getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
101 for (tplParam in tplParams) {
102 if (tplParams.hasOwnProperty(tplParam)) {
103 tplHtml = this.passThruModifiers(tplHtml, tplParam, tplParams[tplParam]);
110 passThruModifiers: function (tplHtml, tplParam, content) {
111 var regModOn = new RegExp('%' + tplParam + '(\\|(.+?)){1,}%', 'g'),
112 regModOff = new RegExp(['%', tplParam, '%'].join(''), 'g'),
113 regModGet = new RegExp('%' + tplParam + '\\|(.+?)%'),
114 regModPut = new RegExp('%' + tplParam + '\\|(.+?)%', 'g'),
115 specRegExp = new RegExp('\\$','g'),
118 if (content && (typeof content === 'string')) {
119 content = content.replace(specRegExp, '$$$$');
122 if (regModOn.test(tplHtml)) {
123 modifiers = tplHtml.match(regModGet)[1].split('|');
124 for (i in modifiers) {
125 if (this.modifiers[modifiers[i]] instanceof Function){
126 content = this.modifiers[modifiers[i]](content);
128 console.error('unknown modifier: ' + modifiers[i]);
131 tplHtml = tplHtml.replace(regModPut, content);
133 tplHtml = tplHtml.replace(regModOff, content);