1 /*global tizen, $, app */
3 * @class TemplateManager
5 function TemplateManager() {
10 (function () { // strict mode wrapper
12 TemplateManager.prototype = {
20 * UI module initialisation
22 init: function init() {
26 * Returns template html (from cache)
27 * @param {string} tplName
28 * @param {string} tplParams
30 get: function TemplateManager_get(tplName, tplParams) {
31 if (this.cache[tplName] !== undefined) {
32 return this.getCompleted(this.cache[tplName], tplParams);
38 * Load templates to cache
39 * @param {string} tplNames
40 * @param {function} onSuccess
42 loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
48 if ($.isArray(tplNames)) {
51 $.each(tplNames, function (index, fileName) {
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('/');
63 success: function (data) {
68 self.cache[fileName] = data;
70 // if all templates are cached launch callback
71 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
75 error: function (jqXHR, textStatus, errorThrown) {
76 console.error('templateManagerError: ' + errorThrown);
80 // template is already cached
82 // if all templates are cached launch callback
83 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
93 * Returns template completed by specified params
94 * @param {string} tplHtml
95 * @param {string} tplParams
97 getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
98 var tplParam, replaceRegExp;
100 for (tplParam in tplParams) {
101 if (tplParams.hasOwnProperty(tplParam)) {
102 replaceRegExp = new RegExp(['%', tplParam, '%'].join(''), 'g');
103 tplHtml = tplHtml.replace(replaceRegExp, tplParams[tplParam]);