[EventManager]update EventManager(tizen_2.1)
[samples/web/EventManager.git] / js / app.ui.templateManager.js
1 /*global tizen, $, app */
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                 },
24
25                 /**
26                  * Returns template html (from cache)
27                  * @param {string} tplName
28                  * @param {string} tplParams
29                  */
30                 get: function TemplateManager_get(tplName, tplParams) {
31                         if (this.cache[tplName] !== undefined) {
32                                 return this.getCompleted(this.cache[tplName], tplParams);
33                         }
34                         return '';
35                 },
36
37                 /**
38                  * Load templates to cache
39                  * @param {string} tplNames
40                  * @param {function} onSuccess
41                  */
42                 loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
43                         var self = this,
44                                 cachedTemplates = 0,
45                                 tplName,
46                                 tplPath;
47
48                         if ($.isArray(tplNames)) {
49
50                                 // for each template
51                                 $.each(tplNames, function (index, fileName) {
52
53                                         // cache template html
54                                         if (self.cache[fileName] === undefined) {
55                                                 tplName = [fileName, app.config.get('templateExtension')].join('');
56                                                 tplPath = [app.config.get('templateDir'), tplName].join('/');
57
58                                                 $.ajax({
59                                                         url: tplPath,
60                                                         cache: true,
61                                                         dataType: 'html',
62                                                         async: true,
63                                                         success: function (data) {
64                                                                 // increase counter
65                                                                 cachedTemplates += 1;
66
67                                                                 // save to cache
68                                                                 self.cache[fileName] = data;
69
70                                                                 // if all templates are cached launch callback
71                                                                 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
72                                                                         onSuccess();
73                                                                 }
74                                                         },
75                                                         error: function (jqXHR, textStatus, errorThrown) {
76                                                                 console.error('templateManagerError: ' + errorThrown);
77                                                         }
78                                                 });
79                                         } else {
80                                                 // template is already cached
81                                                 cachedTemplates += 1;
82                                                 // if all templates are cached launch callback
83                                                 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
84                                                         onSuccess();
85                                                 }
86                                         }
87                                 });
88
89                         }
90                 },
91
92                 /**
93                  * Returns template completed by specified params
94                 * @param {string} tplHtml
95                 * @param {string} tplParams
96                  */
97                 getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
98                         var tplParam, replaceRegExp;
99
100                         for (tplParam in tplParams) {
101                                 if (tplParams.hasOwnProperty(tplParam)) {
102                                         replaceRegExp = new RegExp(['%', tplParam, '%'].join(''), 'g');
103                                         tplHtml = tplHtml.replace(replaceRegExp, tplParams[tplParam]);
104                                 }
105                         }
106
107                         return tplHtml;
108                 }
109         };
110
111 }());