')
- .text(contact.caller).html(),
- phoneNumber = contact.phoneNumber,
- html = ''
- + '' + caller + ''
- + phoneNumber + '' + '' + '';
- return html;
- },
-
- prepareContactsTemplate: function ui_prepareContactsTemplate(
- phone,
- phone_type,
- first,
- middle,
- last,
- vCard
- ) {
- $('#content-contact > .ui-scrollview-view')
- .empty()
- .append(
- this.getContactsUlHtml(
- phone,
- phone_type,
- first,
- middle,
- last,
- vCard
- )
- );
- $('#content-contact').trigger('create');
- },
-
- contactsCompare: function ui_contactsCompare(a, b) {
- if (a.caller < b.caller) {
- return -1;
- }
- if (a.caller > b.caller) {
- return 1;
- }
- return 0;
- },
-
- createSortedContactArray: function ui_createSortedContactArray(
- contacts
- ) {
- var i, len, sortedContactList = [],
- contact, phoneNumber;
-
- for (i = 0, len = contacts.length; i < len; i += 1) {
- contact = contacts[i];
- if (contact.phoneNumbers.length === 0) {
- phoneNumber = '';
- } else {
- phoneNumber = contact.phoneNumbers[0].number;
- }
- sortedContactList.push({
- caller: this.prepareCallerName(contact),
- firstName: contact.name.firstName || '',
- middleName: contact.name.middleName || '',
- lastName: contact.name.lastName || '',
- phoneNumber: phoneNumber,
- id: contact.id,
- vCard: contact.convertToString('VCARD_30'),
- contact: contact
- });
- }
- sortedContactList.sort(this.contactsSort);
-
- return sortedContactList;
- },
-
- createSortedContactList: function ui_createSortedContactList(
- contacts
- ) {
- var sortedContactList = this.createSortedContactArray(contacts),
- ul = $(''),
- i,
- len,
- listElement,
- listElementTap,
- self = this,
- contact;
-
- listElementTap = function (event) {
- event.preventDefault();
- $(this).addClass('selected').siblings().removeClass('selected');
- self.app.saveDefaultCard();
- };
-
- for (i = 0, len = sortedContactList.length; i < len; i += 1) {
- contact = sortedContactList[i];
- if (contact.phoneNumber !== '') {
- listElement = $(this.getContactsListElement(contact));
- listElement
- .data('caller', contact.caller)
- .data('firstName', contact.firstName)
- .data('middleName', contact.middleName)
- .data('lastName', contact.lastName)
- .data('phoneNumber', contact.phoneNumber)
- .data('id', contact.id)
- .data('vCard', contact.vCard);
- if (localStorage.id === listElement.data('id')) {
- listElement.addClass('selected');
- }
- ul.append(listElement);
- }
- }
- ul.on('tap taphold click', 'li', listElementTap);
- return ul;
- },
-
- showContactsList: function ui_showContactsList(contacts) {
- var ul = this.createSortedContactList(contacts);
- $('#content-choose > .ui-scrollview-view').empty().append(ul);
- $('#content-choose').trigger('create');
- },
-
- moveToStartPage: function ui_moveToStartPage(monit) {
- $('#start').data('monit', monit || '');
- $.mobile.changePage('#start');
- },
-
- isActivePage: function ui_isActivePage(id) {
- return (id === $.mobile.activePage.attr('id'));
- },
-
- refreshIfActivePage: function ui_refreshIfActivePage(id) {
- if (this.isActivePage(id)) {
- $.mobile.activePage
- .trigger('pagebeforeshow')
- .trigger('pageshow');
- }
- },
-
- moveToContactPage: function ui_moveToContactPage(obj) {
- $('#start').data('monit', '');
- $('#contact').data('contactsData', obj);
- $.mobile.changePage('#contact');
- },
-
- defineEvents: function ui_defineEvents() {
- var self = this;
-
- $('#header-start .ui-btn-back').on('click', function (event) {
- event.preventDefault();
- self.app.nfc.stopNFC();
- });
-
- $('#header-start')
- .on('click', '.ui-btn-back.ui-focus', function () {
- return false;
- });
-
- $('#footer-contact').on('click', '.ui-btn-back', function (event) {
- event.preventDefault();
- $.mobile.changePage('#start');
- });
-
- $('#footer-choose').on('click', '.ui-btn-back', function (event) {
- event.preventDefault();
- $.mobile.changePage('#start');
- });
-
- $('#choose').on('pageshow', function (event) {
- self.app.loadContacts(
- self.showContactsList.bind(self),
- function (e) {
- alert('Cannot load the contacts list: ' + e.message);
- console.error(e.message, e);
- }
- );
- });
-
- $('#contact').on('pageshow', function (event) {
- var data = $(this).data('contactsData');
- self.prepareContactsTemplate(
- data.phone,
- data.phone_type,
- data.first,
- data.middle,
- data.last,
- data.vCard
- );
- });
-
- $('#save-contact').on('click', function (event) {
- event.preventDefault();
- self.app.saveContact();
- });
-
- $('#start').on('pagebeforeshow', function () {
- if (self.app.started) {
- self.loadStartPage();
- }
- });
-
- $('#start').on('pageshow', function () {
- var obj = $(this),
- monit = obj.data('monit');
- if (monit !== '' && monit !== undefined) {
- self.showPopup(obj.data('monit'));
- obj.data('monit', '');
- }
- });
-
- $('#contact-nfc-error').bind({
- popupafterclose: function () {
- tizen.application.getCurrentApplication().exit();
- }
- });
-
- window.addEventListener('tizenhwkey', function (e) {
- if (e.keyName === 'back') {
- if ($.mobile.popup.active) {
- $.mobile.popup.active.close();
- } else if ($.mobile.activePage.attr('id') === 'start') {
- tizen.application.getCurrentApplication().exit();
- } else {
- self.app.nfc.timeExpired();
- }
- }
- });
-
- document.addEventListener('webkitvisibilitychange', function () {
- if (document.webkitVisibilityState === 'visible') {
- if ($.mobile.activePage.attr('id') === 'choose') {
- $.mobile.activePage.trigger('pageshow');
- }
- app.counterState = true;
- } else {
- app.counterState = false;
- }
- });
-
- $('#transfer').on('pageshow', function () {
- if (tizen.nfc.getDefaultAdapter().powered) {
- try {
- var option = $(this).data('option');
- if (option === 'read') {
- self.prepareWaitingPage(
- 'Card to device',
- 'PUT WIRELESS TAG
CLOSE TO
YOUR DEVICE'
- );
- self.app.nfc.card.setTagDetectRead();
- } else if (option === 'write') {
- self.prepareWaitingPage(
- 'Device to card',
- 'PUT WIRELESS TAG
CLOSE TO
YOUR DEVICE'
- );
- self.app.nfc.card.setTagDetectWrite();
- } else {
- self.prepareWaitingPage('Device to device',
- 'PUT YOUR DEVICE
CLOSE TO
OTHER DEVICE');
- self.app.nfc.peer.setTargetDetect();
- }
- } catch (e) {
- console.error(e.message);
- }
- } else {
- $.mobile.changePage('#start');
- alert('Please turn on NFC adapter');
- }
- });
- }
-
- };
-
-}());
\ No newline at end of file
+ 'use strict';
+
+ App.Ui = function App_Ui(app) {
+ this.app = app;
+ this.templateManager = new TemplateManager(app.config);
+ };
+
+ App.Ui.prototype = {
+ prepareCallerName: function ui_prepareCallerName(contact) {
+ var callerName, firstName, lastName;
+
+ callerName = '';
+ firstName = contact.name.firstName;
+ lastName = contact.name.lastName;
+
+ if (firstName !== '' && firstName !== null) {
+ callerName = firstName;
+ }
+ if (lastName !== '' && lastName !== null) {
+ if (callerName !== '') {
+ callerName += ' ';
+ }
+ callerName += lastName;
+ }
+ if (callerName === '') {
+ callerName = 'No Name';
+ }
+ return callerName;
+ },
+
+ /**
+ * Show a popup
+ * @param {string} message
+ */
+ showPopup: function ui_showPopup(message) {
+ var popup = $("#alertPopup");
+ if(!popup.hasClass('ui-popup')) {
+ popup.popup().trigger('create');
+ }
+ $(".ui-popup-text", popup).text(message);
+ popup.popup("open", {positionTo: 'window'});
+ },
+
+ closePopup: function ui_closePopup() {
+ var activePopup = $.mobile.popup.active;
+ if (activePopup) {
+ if (activePopup.attr('id') === 'alertPopup') {
+ activePopup.close();
+ }
+ }
+ },
+
+ createListRecord: function ui_createListRecord() {
+ $.mobile.changePage('#choose');
+ },
+
+ /**
+ *
+ * @param {string} text
+ * @returns {string}
+ */
+ getWaitingContentHtml: function getWaitingContentHtml(text) {
+ var html;
+ html += '';
+ html += text;
+ html += '
';
+
+ html += '';
+
+ return html;
+ },
+
+ /**
+ *
+ * @param {string} title
+ * @param {string} text
+ */
+ prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
+ var waitingBox, waitingContent,
+ contentTransfer = $('#content-transfer');
+ waitingBox = $('');
+ waitingContent = $(this.getWaitingContentHtml(text));
+ $('#header-transfer H1').text(title);
+ contentTransfer.empty();
+ waitingBox.append(waitingContent);
+ contentTransfer.append(waitingBox);
+ $('#content-start').trigger('create');
+ this.app.countDown(10, $('#counter'));
+ },
+
+ /**
+ * @returns {string}
+ */
+ getTemporaryBoxHtml: function ui_getTemporaryBoxHtml() {
+ return '';
+ },
+
+ /**
+ * @returns {string}
+ */
+ showPopupWarning: function ui_showPopupWarning(){
+ setTimeout(function(){
+ $("#contact-nfc-error").popup("open",{"positionTo":"window"});
+ },500);
+ },
+
+ getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
+ return ''
+ + 'Default card is not defined yet!
'
+ + 'Do you want to define it now?'
+ + '
';
+ },
+
+ loadTemporaryContent: function ui_loadTemporaryContent() {
+ var temporaryBox, temporaryContent, temporaryButton;
+
+ temporaryBox = $(this.getTemporaryBoxHtml());
+ temporaryContent = $(this.getTemporaryBoxContentHtml());
+ temporaryButton = $(this.getButtonHtml('Create default card'));
+
+ temporaryButton.on('click', function (event) {
+ event.preventDefault();
+ $.mobile.changePage('#choose');
+ });
+
+ temporaryBox.append(temporaryContent).append(temporaryButton);
+
+ $('#content-start').empty().append(temporaryBox).trigger('create');
+ },
+ /**
+ *
+ * @param {string} text button text
+ * @returns {string}
+ */
+ getButtonHtml: function ui_getButtonHtml(text) {
+ return ''
+ + text
+ + '
';
+ },
+
+ /**
+ *
+ * @param {string} firstName
+ * @param {string} lastName
+ * @returns {string}
+ */
+ getCommentHtml: function ui_getCommentHtml(data) {
+ var def_name = $('';
+ },
+
+
+ /**
+ * Change Contact button action
+ * @event
+ * @param {Event} event
+ */
+ changeContact: function ui_changeContact(event) {
+ event.preventDefault();
+ $.mobile.changePage('#choose');
+ },
+
+ /**
+ * @returns {jQuery}
+ */
+ getChangeContactButton: function ui_getChangeContactButton() {
+ var changeContactButton;
+ changeContactButton = $(this.getButtonHtml('Change your default contact'));
+ changeContactButton.on('click', this.changeContact);
+ return changeContactButton;
+ },
+
+ /**
+ * Read From Card button action
+ * @event
+ * @param {Event} event
+ */
+ readFromCard: function ui_readFromCard(event) {
+ event.preventDefault();
+ if (tizen.nfc.getDefaultAdapter().powered) {
+ try {
+ $('#transfer').data('option', 'read');
+ $.mobile.changePage('#transfer');
+ } catch (e) {
+ console.error(e.message);
+ }
+ } else {
+ $.mobile.changePage('#start');
+ alert('Please turn on NFC adapter');
+ }
+ },
+
+ /**
+ * @returns {jQuery}
+ */
+ getReadFromCardButton: function ui_getReadFromCardButton() {
+ var readFromCardButton;
+ readFromCardButton = $(this.getButtonHtml('Read from card'));
+ readFromCardButton.on('click', this.readFromCard);
+ return readFromCardButton;
+ },
+
+ /**
+ * Write To Card button action
+ * @param {Event} event
+ */
+ writeToCard: function ui_writeToCard(event) {
+ event.preventDefault();
+ if (tizen.nfc.getDefaultAdapter().powered) {
+ try {
+ $('#transfer').data('option', 'write');
+ $.mobile.changePage('#transfer');
+ } catch (e) {
+ console.error(e.message);
+ }
+ } else {
+ alert('Please turn on NFC adapter');
+ }
+ },
+
+ /**
+ * @returns {jQuery}
+ */
+ getWriteToCardButton: function ui_getWriteToCardButton() {
+ var writeToCardButton;
+ writeToCardButton = $(this.getButtonHtml('Write to card'));
+ writeToCardButton.on('click', this.writeToCard);
+ return writeToCardButton;
+ },
+
+ /**
+ * Communicate With Other Device button action
+ * @param {type} event
+ */
+ communicateWithOtherDevice: function ui_communicateWithOtherDevice(event) {
+ event.preventDefault();
+ if (tizen.nfc.getDefaultAdapter().powered) {
+ try {
+ $('#transfer').data('option', 'communicate');
+ $.mobile.changePage('#transfer');
+ } catch (e) {
+ console.error(e.message);
+ }
+ } else {
+ alert('Please turn on NFC adapter');
+ }
+ },
+
+ /**
+ * @returns {jQuery}
+ */
+ getCommunicateWithOtherDeviceButton: function ui_getCommunicateWithOtherDeviceButton() {
+ var communicateWithOtherDeviceButton;
+ communicateWithOtherDeviceButton = $(this.getButtonHtml('Communicate with another device'));
+ communicateWithOtherDeviceButton.on('click', this.communicateWithOtherDevice);
+ return communicateWithOtherDeviceButton;
+ },
+
+ loadStartContent: function ui_loadStartContent() {
+ var startBox, contentStart, gap, comment;
+ contentStart = $('#content-start');
+ startBox = $('');
+ gap = $('');
+ comment = $(this.getCommentHtml(localStorage));
+
+ contentStart.empty();
+ startBox
+ .append(this.getChangeContactButton())
+ .append(gap.clone())
+ .append(this.getReadFromCardButton())
+ .append(gap.clone())
+ .append(this.getWriteToCardButton())
+ .append(gap.clone())
+ .append(this.getCommunicateWithOtherDeviceButton())
+ .prepend(comment);
+
+ contentStart.append(startBox);
+ contentStart.trigger('create');
+ },
+
+ loadStartPage: function ui_loadStartPage() {
+ if (localStorage.started === undefined) {
+ this.loadTemporaryContent();
+ } else {
+ this.loadStartContent();
+ }
+ },
+
+ /**
+ *
+ * @param {string} value
+ * @param {string} label
+ * @returns {string}
+ */
+ getLiHtml: function ui_getLiHtml(value, label) {
+ var html;
+ html = ''
+ + ''
+ + '' + label + ''
+ + ''
+ + ((!value) ? '...' : value)
+ + ''
+ + ''
+ + '';
+ return html;
+ },
+
+ /**
+ *
+ * @param {string} phone
+ * @param {string} first
+ * @param {string} last
+ * @returns {string}
+ */
+ getContactsUlHtml: function ui_getContactsUlHtml(phone, first, last) {
+ var html;
+ html = '';
+ return html;
+ },
+
+ /**
+ * @param {object} contact
+ * @returns {string}
+ */
+ getContactsListElement: function ui_getContactsListElement(contact) {
+ var caller = $('')
+ .text(contact.caller).html(),
+ phoneNumber = contact.phoneNumber,
+ html =
+ ''
+ + '' + caller
+ + '' + phoneNumber + ''
+ + ''
+ + '';
+ return html;
+ },
+
+ prepareContactsTemplate: function ui_prepareContactsTemplate(phone, first, last) {
+ $('#content-contact > .ui-scrollview-view')
+ .empty()
+ .append(this.getContactsUlHtml(phone, first, last));
+ $('#content-contact').trigger('create');
+ },
+
+ contactsCompare: function ui_contactsCompare(a, b) {
+ if (a.caller < b.caller) {
+ return -1;
+ }
+ if (a.caller > b.caller) {
+ return 1;
+ }
+ return 0;
+ },
+
+ createSortedContactArray: function ui_createSortedContactArray(contacts) {
+ var i, len, sortedContactList = [], contact, phoneNumber;
+
+ for (i = 0, len = contacts.length; i < len; i += 1) {
+ contact = contacts[i];
+ if (contact.phoneNumbers.length === 0) {
+ phoneNumber = '';
+ } else {
+ phoneNumber = contact.phoneNumbers[0].number;
+ }
+ sortedContactList.push({
+ caller: this.prepareCallerName(contact),
+ firstName: contact.name.firstName || '',
+ lastName: contact.name.lastName || '',
+ phoneNumber: phoneNumber,
+ id: contact.id,
+ vCard: contact.convertToString('VCARD_30'),
+ contact: contact
+ });
+ }
+ sortedContactList.sort(this.contactsSort);
+
+ return sortedContactList;
+ },
+
+ createSortedContactList: function ui_createSortedContactList(contacts) {
+ var sortedContactList = this.createSortedContactArray(contacts),
+ ul = $(''),
+ i,
+ len,
+ listElement,
+ listElementTap,
+ self = this,
+ contact;
+
+ listElementTap = function (event) {
+ event.preventDefault();
+ $(this).addClass('selected').siblings().removeClass('selected');
+ self.app.saveDefaultCard();
+ };
+
+ for (i = 0, len = sortedContactList.length; i < len; i += 1) {
+ contact = sortedContactList[i];
+ if (contact.phoneNumber !== '') {
+ listElement = $(this.getContactsListElement(contact));
+ listElement
+ .data('caller', contact.caller)
+ .data('firstName', contact.firstName)
+ .data('lastName', contact.lastName)
+ .data('phoneNumber', contact.phoneNumber)
+ .data('id', contact.id)
+ .data('vCard', contact.vCard);
+ if (localStorage.id === listElement.data('id')) {
+ listElement.addClass('selected');
+ }
+ ul.append(listElement);
+ }
+ }
+ ul.on('tap taphold click', 'li', listElementTap);
+ return ul;
+ },
+
+ showContactsList: function ui_showContactsList(contacts) {
+ var ul = this.createSortedContactList(contacts);
+ $('#content-choose > .ui-scrollview-view').empty().append(ul);
+ $('#content-choose').trigger('create');
+ },
+
+ moveToStartPage: function ui_moveToStartPage(monit) {
+ $('#start').data('monit', monit || '');
+ $.mobile.changePage('#start');
+ },
+
+ isActivePage: function ui_isActivePage(id) {
+ return (id === $.mobile.activePage.attr("id"));
+ },
+
+ refreshIfActivePage: function ui_refreshIfActivePage(id) {
+ if (this.isActivePage(id)) {
+ $.mobile.activePage
+ .trigger("pagebeforeshow")
+ .trigger("pageshow");
+ }
+ },
+
+ moveToContactPage: function ui_moveToContactPage(obj) {
+ $('#start').data('monit', '');
+ $('#contact').data('contactsData', obj);
+ $.mobile.changePage('#contact');
+ },
+
+ defineEvents: function ui_defineEvents() {
+ var self = this;
+
+ $('#header-start .ui-btn-back').on('click', function (event) {
+ event.preventDefault();
+ self.app.nfc.stopNFC();
+ });
+
+ $('#header-start').on('click', '.ui-btn-back.ui-focus', function () {
+ return false;
+ });
+
+ $('#footer-contact').on('click', '.ui-btn-back', function (event) {
+ event.preventDefault();
+ $.mobile.changePage('#start');
+ });
+
+ $('#footer-choose').on('click', '.ui-btn-back', function (event) {
+ event.preventDefault();
+ $.mobile.changePage('#start');
+ });
+
+ $('#choose').on('pageshow', function (event) {
+ self.app.loadContacts(self.showContactsList.bind(self), function (e) {
+ alert('Cannot load the contacts list: ' + e.message);
+ console.error(e.message, e);
+ });
+ });
+
+ $('#contact').on('pageshow', function (event) {
+ var data = $(this).data('contactsData');
+ self.prepareContactsTemplate(data.phone, data.first, data.last);
+ });
+
+ $('#save-contact').on('click', function (event) {
+ event.preventDefault();
+ self.app.saveContact();
+ });
+
+ $('#start').on('pagebeforeshow', function () {
+ if (self.app.started) {
+ self.loadStartPage();
+ }
+ });
+
+ $('#start').on('pageshow', function () {
+ var obj = $(this), monit = obj.data('monit');
+ if (monit !== '' && monit !== undefined) {
+ self.showPopup(obj.data('monit'));
+ obj.data('monit', '');
+ }
+ });
+
+ $( "#contact-nfc-error" ).bind({
+ popupafterclose: function(){
+ tizen.application.getCurrentApplication().exit();
+ }
+ });
+
+ window.addEventListener('tizenhwkey', function(e) {
+ if (e.keyName == "back") {
+ if ($.mobile.popup.active) {
+ $.mobile.popup.active.close();
+ } else if ($.mobile.activePage.attr('id') === 'start') {
+ tizen.application.getCurrentApplication().exit();
+ } else {
+ self.app.nfc.timeExpired();
+ }
+ }
+ });
+
+ document.addEventListener('webkitvisibilitychange', function () {
+ if(document.webkitVisibilityState === "visible") {
+ if ($.mobile.activePage.attr('id') === "choose") {
+ $.mobile.activePage.trigger('pageshow');
+ }
+ app.counterState = true;
+ } else {
+ app.counterState = false;
+ }
+ });
+
+ $('#transfer').on('pageshow', function () {
+ if (tizen.nfc.getDefaultAdapter().powered) {
+ try {
+ var option = $(this).data('option');
+ if (option === 'read') {
+ self.prepareWaitingPage('Card to device', 'PUT WIRELESS TAG
CLOSE TO
YOUR DEVICE');
+ self.app.nfc.card.setTagDetectRead();
+ } else if (option === 'write') {
+ self.prepareWaitingPage('Device to card', 'PUT WIRELESS TAG
CLOSE TO
YOUR DEVICE');
+ self.app.nfc.card.setTagDetectWrite();
+ } else {
+ self.prepareWaitingPage('Device to device', 'PUT YOUR DEVICE
CLOSE TO
OTHER DEVICE');
+ self.app.nfc.peer.setTargetDetect();
+ }
+ } catch (e) {
+ console.error(e.message);
+ }
+ } else {
+ $.mobile.changePage('#start');
+ alert('Please turn on NFC adapter');
+ }
+ });
+ }
+
+ };
+
+}());
diff --git a/project/js/app.ui.templateManager.js b/project/js/app.ui.templateManager.js
index c880c5e..68c6678 100644
--- a/project/js/app.ui.templateManager.js
+++ b/project/js/app.ui.templateManager.js
@@ -1,187 +1,111 @@
-/*
-* 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 */
-
+/*global tizen, $, app */
/**
-* @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;
- },
-
- /**
- * 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;
- }
- };
-
-}());
+ 'use strict';
+ TemplateManager.prototype = {
+
+ /**
+ * Template cache
+ */
+ cache: {},
+
+ /**
+ * UI module initialisation
+ */
+ init: function init() {
+ },
+
+ /**
+ * 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, replaceRegExp;
+
+ for (tplParam in tplParams) {
+ if (tplParams.hasOwnProperty(tplParam)) {
+ replaceRegExp = new RegExp(['%', tplParam, '%'].join(''), 'g');
+ tplHtml = tplHtml.replace(replaceRegExp, tplParams[tplParam]);
+ }
+ }
+
+ return tplHtml;
+ }
+ };
+
+}());
\ No newline at end of file