application sources from tizen_2.2
[apps/web/sample/CallLog.git] / project / js / app.ui.templateManager.js
1 /*global tizen, $, app, ModifierManager */
2 /**
3  * @class TemplateManager
4  */
5 function TemplateManager() {
6         'use strict';
7         this.init();
8 }
9
10 (function () { // strict mode wrapper
11         'use strict';
12         TemplateManager.prototype = {
13
14                 /**
15                  * Template cache
16                  */
17                 cache: {},
18
19                 /**
20                  * UI module initialisation
21                  */
22                 init: function init() {
23                         this.modifiers = new ModifierManager().getAll();
24                 },
25
26                 /**
27                  * Returns template html (from cache)
28                  * @param {string} tplName
29                  * @param {string} tplParams
30                  */
31                 get: function TemplateManager_get(tplName, tplParams) {
32                         if (this.cache[tplName] !== undefined) {
33                                 return this.getCompleted(this.cache[tplName], tplParams);
34                         }
35                         return '';
36                 },
37
38                 /**
39                  * Load templates to cache
40                  * @param {string} tplNames
41                  * @param {function} onSuccess
42                  */
43                 loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
44                         var self = this,
45                                 cachedTemplates = 0,
46                                 tplName,
47                                 tplPath;
48
49                         if ($.isArray(tplNames)) {
50
51                                 // for each template
52                                 $.each(tplNames, function (index, fileName) {
53
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('/');
58
59                                                 $.ajax({
60                                                         url: tplPath,
61                                                         cache: true,
62                                                         dataType: 'html',
63                                                         async: true,
64                                                         success: function (data) {
65                                                                 // increase counter
66                                                                 cachedTemplates += 1;
67
68                                                                 // save to cache
69                                                                 self.cache[fileName] = data;
70
71                                                                 // if all templates are cached launch callback
72                                                                 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
73                                                                         onSuccess();
74                                                                 }
75                                                         },
76                                                         error: function (jqXHR, textStatus, errorThrown) {
77                                                                 console.error('templateManagerError: ' + errorThrown);
78                                                         }
79                                                 });
80                                         } else {
81                                                 // template is already cached
82                                                 cachedTemplates += 1;
83                                                 // if all templates are cached launch callback
84                                                 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
85                                                         onSuccess();
86                                                 }
87                                         }
88                                 });
89
90                         }
91                 },
92
93                 /**
94                  * Returns template completed by specified params
95                 * @param {string} tplHtml
96                 * @param {string} tplParams
97                  */
98                 getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
99                         var tplParam;
100
101                         for (tplParam in tplParams) {
102                                 if (tplParams.hasOwnProperty(tplParam)) {
103                                         tplHtml = this.passThruModifiers(tplHtml, tplParam, tplParams[tplParam]);
104                                 }
105                         }
106
107                         return tplHtml;
108                 },
109
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'),
116                                 modifiers, i;
117
118                         if (content && (typeof content === 'string')) {
119                                 content = content.replace(specRegExp, '$$$$');
120                         }
121
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);
127                                         } else {
128                                                 console.error('unknown modifier: ' + modifiers[i]);
129                                         }
130                                 }
131                                 tplHtml = tplHtml.replace(regModPut, content);
132                         }
133                         tplHtml = tplHtml.replace(regModOff, content);
134
135                         return tplHtml;
136                 }
137         };
138
139 }());