+/*
+* Copyright 2013 Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
/*global tizen, $, app, ModifierManager */
+
/**
- * @class TemplateManager
- */
+* @class TemplateManager
+*/
function TemplateManager() {
- 'use strict';
- this.init();
+ 'use strict';
+ this.init();
}
(function () { // strict mode wrapper
- 'use strict';
- TemplateManager.prototype = {
-
- /**
- * Template cache
- */
- cache: {},
-
- /**
- * UI module initialisation
- */
- init: function init() {
- this.modifiers = new ModifierManager().getAll();
- },
-
- /**
- * Returns template html (from cache)
- * @param {string} tplName
- * @param {string} tplParams
- */
- get: function TemplateManager_get(tplName, tplParams) {
- if (this.cache[tplName] !== undefined) {
- return this.getCompleted(this.cache[tplName], tplParams);
- }
- return '';
- },
-
- /**
- * Load templates to cache
- * @param {string} tplNames
- * @param {function} onSuccess
- */
- loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
- var self = this,
- cachedTemplates = 0,
- tplName,
- tplPath;
-
- if ($.isArray(tplNames)) {
-
- // for each template
- $.each(tplNames, function (index, fileName) {
-
- // cache template html
- if (self.cache[fileName] === undefined) {
- tplName = [fileName, app.config.get('templateExtension')].join('');
- tplPath = [app.config.get('templateDir'), tplName].join('/');
-
- $.ajax({
- url: tplPath,
- cache: true,
- dataType: 'html',
- async: true,
- success: function (data) {
- // increase counter
- cachedTemplates += 1;
-
- // save to cache
- self.cache[fileName] = data;
-
- // if all templates are cached launch callback
- if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
- onSuccess();
- }
- },
- error: function (jqXHR, textStatus, errorThrown) {
- console.error('templateManagerError: ' + errorThrown);
- }
- });
- } else {
- // template is already cached
- cachedTemplates += 1;
- // if all templates are cached launch callback
- if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
- onSuccess();
- }
- }
- });
-
- }
- },
-
- /**
- * Returns template completed by specified params
- * @param {string} tplHtml
- * @param {string} tplParams
- */
- getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
- var tplParam;
-
- for (tplParam in tplParams) {
- if (tplParams.hasOwnProperty(tplParam)) {
- tplHtml = this.passThruModifiers(tplHtml, tplParam, tplParams[tplParam]);
- }
- }
-
- return tplHtml;
- },
-
- passThruModifiers: function (tplHtml, tplParam, content) {
- var regModOn = new RegExp('%' + tplParam + '\\|([a-zA-Z]){1,}%', 'g'),
- regModOff = new RegExp(['%', tplParam, '%'].join(''), 'g'),
- regModGet = new RegExp('%' + tplParam + '\\|(.+?)%'),
- specRegExp = new RegExp('\\$','g'),
- modifier;
-
- if (content && (typeof content === 'string')) {
- content = content.replace(specRegExp, '$$$$');
- }
-
- if (regModOn.test(tplHtml)) {
- modifier = tplHtml.match(regModGet)[1];
- try {
- content = this.modifiers[modifier](content);
- } catch (error) {
- console.error('unknown modifier: ' + modifier);
- }
- tplHtml = tplHtml.replace(regModOn, content);
- } else {
- tplHtml = tplHtml.replace(regModOff, content);
- }
- return tplHtml;
- }
- };
-
-}());
\ No newline at end of file
+ 'use strict';
+
+ TemplateManager.prototype = {
+
+ /**
+ * Template cache
+ */
+ cache: {},
+
+ /**
+ * UI module initialisation
+ */
+ init: function init() {
+ this.modifiers = new ModifierManager().getAll();
+ },
+
+ /**
+ * Returns template html (from cache)
+ * @param {string} tplName
+ * @param {string} tplParams
+ */
+ get: function TemplateManager_get(tplName, tplParams) {
+ if (this.cache[tplName] !== undefined) {
+ return this.getCompleted(this.cache[tplName], tplParams);
+ }
+ return '';
+ },
+
+ /**
+ * Load templates to cache
+ * @param {string} tplNames
+ * @param {function} onSuccess
+ */
+ loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
+ var self = this,
+ cachedTemplates = 0,
+ tplName,
+ tplPath;
+
+ if ($.isArray(tplNames)) {
+
+ // for each template
+ $.each(tplNames, function (index, fileName) {
+
+ // cache template html
+ if (self.cache[fileName] === undefined) {
+ tplName = [
+ fileName,
+ app.config.get('templateExtension')
+ ].join('');
+ tplPath = [
+ app.config.get('templateDir'),
+ tplName
+ ].join('/');
+
+ $.ajax({
+ url: tplPath,
+ cache: true,
+ dataType: 'html',
+ async: true,
+ success: function (data) {
+ // increase counter
+ cachedTemplates += 1;
+
+ // save to cache
+ self.cache[fileName] = data;
+
+ // if all templates are cached launch callback
+ if (
+ cachedTemplates >= tplNames.length &&
+ typeof onSuccess === 'function'
+ ) {
+ onSuccess();
+ }
+ },
+ error: function (jqXHR, textStatus, errorThrown) {
+ console.error(
+ 'templateManagerError: ' +
+ errorThrown
+ );
+ }
+ });
+ } else {
+ // template is already cached
+ cachedTemplates += 1;
+ // if all templates are cached launch callback
+ if (
+ cachedTemplates >= tplNames.length &&
+ typeof onSuccess === 'function'
+ ) {
+ onSuccess();
+ }
+ }
+ });
+
+ }
+ },
+
+ /**
+ * Returns template completed by specified params
+ * @param {string} tplHtml
+ * @param {string} tplParams
+ */
+ getCompleted: function TemplateManager_getCompleted(
+ tplHtml,
+ tplParams
+ ) {
+ var tplParam;
+
+ for (tplParam in tplParams) {
+ if (tplParams.hasOwnProperty(tplParam)) {
+ tplHtml = this.passThruModifiers(
+ tplHtml,
+ tplParam,
+ tplParams[tplParam]
+ );
+ }
+ }
+
+ return tplHtml;
+ },
+
+ /**
+ * Returns template completed by specified params
+ * including modifiers
+ * @param {string} tplHtml
+ * @param {string} tplParams
+ * @param {string} content
+ */
+ passThruModifiers: function (tplHtml, tplParam, content) {
+ var regModOn = new RegExp('%' + tplParam + '(\\|(.+?)){1,}%', 'g'),
+ regModOff = new RegExp(['%', tplParam, '%'].join(''), 'g'),
+ regModGet = new RegExp('%' + tplParam + '\\|(.+?)%'),
+ regModPut = new RegExp('%' + tplParam + '\\|(.+?)%', 'g'),
+ specRegExp = new RegExp('\\$', 'g'),
+ modifiers,
+ i;
+
+ if (content && (typeof content === 'string')) {
+ content = content.replace(specRegExp, '$$$$');
+ }
+
+ if (regModOn.test(tplHtml)) {
+ modifiers = tplHtml.match(regModGet)[1].split('|');
+ for (i in modifiers) {
+ if (this.modifiers[modifiers[i]] instanceof Function) {
+ content = this.modifiers[modifiers[i]](content);
+ } else {
+ console.error('unknown modifier: ' + modifiers[i]);
+ }
+ }
+ tplHtml = tplHtml.replace(regModPut, content);
+ }
+ tplHtml = tplHtml.replace(regModOff, content);
+
+ return tplHtml;
+ }
+ };
+
+}());