2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /*global tizen, $, app, ModifierManager */
20 * @class TemplateManager
22 function TemplateManager() {
27 (function () { // strict mode wrapper
30 TemplateManager.prototype = {
38 * UI module initialisation
40 init: function init() {
41 this.modifiers = new ModifierManager().getAll();
45 * Returns template html (from cache)
46 * @param {string} tplName
47 * @param {string} tplParams
49 get: function TemplateManager_get(tplName, tplParams) {
50 if (this.cache[tplName] !== undefined) {
51 return this.getCompleted(this.cache[tplName], tplParams);
57 * Load templates to cache
58 * @param {string} tplNames
59 * @param {function} onSuccess
61 loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
67 if ($.isArray(tplNames)) {
70 $.each(tplNames, function (index, fileName) {
72 // cache template html
73 if (self.cache[fileName] === undefined) {
76 app.config.get('templateExtension')
79 app.config.get('templateDir'),
88 success: function (data) {
93 self.cache[fileName] = data;
95 // if all templates are cached launch callback
97 cachedTemplates >= tplNames.length &&
98 typeof onSuccess === 'function'
103 error: function (jqXHR, textStatus, errorThrown) {
105 'templateManagerError: ' +
111 // template is already cached
112 cachedTemplates += 1;
113 // if all templates are cached launch callback
115 cachedTemplates >= tplNames.length &&
116 typeof onSuccess === 'function'
127 * Returns template completed by specified params
128 * @param {string} tplHtml
129 * @param {string} tplParams
131 getCompleted: function TemplateManager_getCompleted(
137 for (tplParam in tplParams) {
138 if (tplParams.hasOwnProperty(tplParam)) {
139 tplHtml = this.passThruModifiers(
151 * Returns template completed by specified params
152 * including modifiers
153 * @param {string} tplHtml
154 * @param {string} tplParams
155 * @param {string} content
157 passThruModifiers: function (tplHtml, tplParam, content) {
158 var regModOn = new RegExp('%' + tplParam + '(\\|(.+?)){1,}%', 'g'),
159 regModOff = new RegExp(['%', tplParam, '%'].join(''), 'g'),
160 regModGet = new RegExp('%' + tplParam + '\\|(.+?)%'),
161 regModPut = new RegExp('%' + tplParam + '\\|(.+?)%', 'g'),
162 specRegExp = new RegExp('\\$', 'g'),
166 if (content && (typeof content === 'string')) {
167 content = content.replace(specRegExp, '$$$$');
170 if (regModOn.test(tplHtml)) {
171 modifiers = tplHtml.match(regModGet)[1].split('|');
172 for (i in modifiers) {
173 if (this.modifiers[modifiers[i]] instanceof Function) {
174 content = this.modifiers[modifiers[i]](content);
176 console.error('unknown modifier: ' + modifiers[i]);
179 tplHtml = tplHtml.replace(regModPut, content);
181 tplHtml = tplHtml.replace(regModOff, content);