c880c5e79214f6ddfe8bdd96f3f93ed7399a4c9d
[apps/web/sample/CallLog.git] / project / js / app.ui.templateManager.js
1 /*
2 *      Copyright 2013  Samsung Electronics Co., Ltd
3 *
4 *      Licensed under the Flora License, Version 1.1 (the "License");
5 *      you may not use this file except in compliance with the License.
6 *      You may obtain a copy of the License at
7 *
8 *              http://floralicense.org/license/
9 *
10 *      Unless required by applicable law or agreed to in writing, software
11 *      distributed under the License is distributed on an "AS IS" BASIS,
12 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *      See the License for the specific language governing permissions and
14 *      limitations under the License.
15 */
16
17 /*global tizen, $, app, ModifierManager */
18
19 /**
20 * @class TemplateManager
21 */
22 function TemplateManager() {
23     'use strict';
24     this.init();
25 }
26
27 (function () { // strict mode wrapper
28     'use strict';
29
30     TemplateManager.prototype = {
31
32         /**
33         * Template cache
34         */
35         cache: {},
36
37         /**
38         * UI module initialisation
39         */
40         init: function init() {
41             this.modifiers = new ModifierManager().getAll();
42         },
43
44         /**
45         * Returns template html (from cache)
46         * @param {string} tplName
47         * @param {string} tplParams
48         */
49         get: function TemplateManager_get(tplName, tplParams) {
50             if (this.cache[tplName] !== undefined) {
51                 return this.getCompleted(this.cache[tplName], tplParams);
52             }
53             return '';
54         },
55
56         /**
57         * Load templates to cache
58         * @param {string} tplNames
59         * @param {function} onSuccess
60         */
61         loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
62             var self = this,
63                 cachedTemplates = 0,
64                 tplName,
65                 tplPath;
66
67             if ($.isArray(tplNames)) {
68
69                 // for each template
70                 $.each(tplNames, function (index, fileName) {
71
72                     // cache template html
73                     if (self.cache[fileName] === undefined) {
74                         tplName = [
75                             fileName,
76                             app.config.get('templateExtension')
77                         ].join('');
78                         tplPath = [
79                             app.config.get('templateDir'),
80                             tplName
81                         ].join('/');
82
83                         $.ajax({
84                             url: tplPath,
85                             cache: true,
86                             dataType: 'html',
87                             async: true,
88                             success: function (data) {
89                                 // increase counter
90                                 cachedTemplates += 1;
91
92                                 // save to cache
93                                 self.cache[fileName] = data;
94
95                                 // if all templates are cached launch callback
96                                 if (
97                                     cachedTemplates >= tplNames.length &&
98                                         typeof onSuccess === 'function'
99                                 ) {
100                                     onSuccess();
101                                 }
102                             },
103                             error: function (jqXHR, textStatus, errorThrown) {
104                                 console.error(
105                                     'templateManagerError: ' +
106                                         errorThrown
107                                 );
108                             }
109                         });
110                     } else {
111                         // template is already cached
112                         cachedTemplates += 1;
113                         // if all templates are cached launch callback
114                         if (
115                             cachedTemplates >= tplNames.length &&
116                                 typeof onSuccess === 'function'
117                         ) {
118                             onSuccess();
119                         }
120                     }
121                 });
122
123             }
124         },
125
126         /**
127         * Returns template completed by specified params
128         * @param {string} tplHtml
129         * @param {string} tplParams
130         */
131         getCompleted: function TemplateManager_getCompleted(
132             tplHtml,
133             tplParams
134         ) {
135             var tplParam;
136
137             for (tplParam in tplParams) {
138                 if (tplParams.hasOwnProperty(tplParam)) {
139                     tplHtml = this.passThruModifiers(
140                         tplHtml,
141                         tplParam,
142                         tplParams[tplParam]
143                     );
144                 }
145             }
146
147             return tplHtml;
148         },
149
150         /**
151          * Returns template completed by specified params
152          * including modifiers
153          * @param {string} tplHtml
154          * @param {string} tplParams
155          * @param {string} content
156          */
157         passThruModifiers: function (tplHtml, tplParam, content) {
158             var regModOn = new RegExp('%' + tplParam + '(\\|(.+?)){1,}%', 'g'),
159                 regModOff = new RegExp(['%', tplParam, '%'].join(''), 'g'),
160                 regModGet = new RegExp('%' + tplParam + '\\|(.+?)%'),
161                 regModPut = new RegExp('%' + tplParam + '\\|(.+?)%', 'g'),
162                 specRegExp = new RegExp('\\$', 'g'),
163                 modifiers,
164                 i;
165
166             if (content && (typeof content === 'string')) {
167                 content = content.replace(specRegExp, '$$$$');
168             }
169
170             if (regModOn.test(tplHtml)) {
171                 modifiers = tplHtml.match(regModGet)[1].split('|');
172                 for (i in modifiers) {
173                     if (this.modifiers[modifiers[i]] instanceof Function) {
174                         content = this.modifiers[modifiers[i]](content);
175                     } else {
176                         console.error('unknown modifier: ' + modifiers[i]);
177                     }
178                 }
179                 tplHtml = tplHtml.replace(regModPut, content);
180             }
181             tplHtml = tplHtml.replace(regModOff, content);
182
183             return tplHtml;
184         }
185     };
186
187 }());