Tizen 2.0 Release
[samples/web/FileManager.git] / js / app.ui.templateManager.js
1 /*jslint devel: true*/
2 /*global $, 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                         }
36                         return '';
37                 },
38
39                 /**
40                  * Load templates to cache
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                                                                 console.log('Cached template: ' + fileName);
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                                                                 alert(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                  */
96                 getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
97                         var tplParam, replaceRegExp;
98
99                         for (tplParam in tplParams) {
100                                 if (tplParams.hasOwnProperty(tplParam)) {
101                                         replaceRegExp = new RegExp(['%', tplParam, '%'].join(''), 'g');
102                                         tplHtml = tplHtml.replace(replaceRegExp, tplParams[tplParam]);
103                                 }
104                         }
105
106                         return tplHtml;
107                 }
108         };
109
110 }());