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