Tizen 2.0 Release
[samples/web/CallLog.git] / js / app.ui.templateManager.js
1 /*jslint devel: true*/
2 /*global tizen, $, app */
3 /**
4  * @class TemplateManager
5  */
6 function TemplateManager() {
7         'use strict';
8         this.init();
9 }
10
11 (function () { // strict mode wrapper
12         'use strict';
13         TemplateManager.prototype = {
14
15                 /**
16                  * Template cache
17                  */
18                 cache: {},
19
20                 /**
21                  * UI module initialisation
22                  */
23                 init: function init() {
24
25                 },
26
27                 /**
28                  * Returns template html (from cache)
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                  */
40                 loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
41                         var self = this,
42                                 cachedTemplates = 0,
43                                 tplName,
44                                 tplPath;
45
46                         if ($.isArray(tplNames)) {
47
48                                 // for each template
49                                 $.each(tplNames, function (index, fileName) {
50
51                                         // cache template html
52                                         if (self.cache[fileName] === undefined) {
53                                                 tplName = [fileName, app.config.get('templateExtension')].join('');
54                                                 tplPath = [app.config.get('templateDir'), tplName].join('/');
55
56                                                 $.ajax({
57                                                         url: tplPath,
58                                                         cache: true,
59                                                         dataType: 'html',
60                                                         async: true,
61                                                         success: function (data) {
62                                                                 // increase counter
63                                                                 cachedTemplates += 1;
64
65                                                                 // save to cache
66                                                                 self.cache[fileName] = data;
67
68                                                                 // if all templates are cached launch callback
69                                                                 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
70                                                                         onSuccess();
71                                                                 }
72                                                         },
73                                                         error: function (jqXHR, textStatus, errorThrown) {
74                                                                 console.error('templateManagerError: ' + errorThrown);
75                                                         }
76                                                 });
77                                         } else {
78                                                 // template is already cached
79                                                 cachedTemplates += 1;
80                                                 // if all templates are cached launch callback
81                                                 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
82                                                         onSuccess();
83                                                 }
84                                         }
85                                 });
86
87                         }
88                 },
89
90                 /**
91                  * Returns template completed by specified params
92                  */
93                 getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
94                         var tplParam, replaceRegExp;
95
96                         for (tplParam in tplParams) {
97                                 if (tplParams.hasOwnProperty(tplParam)) {
98                                         replaceRegExp = new RegExp(['%', tplParam, '%'].join(''), 'g');
99                                         tplHtml = tplHtml.replace(replaceRegExp, tplParams[tplParam]);
100                                 }
101                         }
102
103                         return tplHtml;
104                 }
105         };
106
107 }());