- '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 name = contact.name.displayName;
- return (name && name.length > 0) ? name : 'No Name';
- },
-
- /**
- * 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 += '<p class="defaultVeryBigText">';
- html += text;
- html += '</p>';
-
- html += '<p class="defaultCounterText" id="counter"></p>';
-
- return html;
- },
-
- /**
- *
- * @param {string} title
- * @param {string} text
- */
- prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
- var waitingBox, waitingContent,
- contentTransfer = $('#content-transfer');
- waitingBox = $('<div class="box" id="waitingBox"></div>');
- 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 '<div class="box" id="temporaryBox"></div>';
- },
-
- /**
- * @returns {string}
- */
- showPopupWarning: function ui_showPopupWarning() {
- setTimeout(function () {
- $('#contact-nfc-error').popup('open', {
- 'positionTo': 'window'
- });
- }, 500);
- },
-
- getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
- return '<p class="defaultText">'
- + 'Default card is not defined yet!<br/>'
- + 'Do you want to define it now?' + '</p>';
- },
-
- 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 '<div data-role="button" class="ui-btn-create">'
- + text + '</div>';
- },
-
- /**
- *
- * @param {string} firstName
- * @param {string} lastName
- * @returns {string}
- */
- getCommentHtml: function ui_getCommentHtml(data) {
- var def_name = $('<p class="comment" id="comment-name">')
- .text(data.caller).html();
- return '<div id="comment">'
- + '<p class="comment">Your default contact</p>'
- + '<p class="comment" id="comment-userName">'
- + def_name + '</p>' + '<p class="comment" id="comment-phone">'
- + data.phoneNumber + '</p>' + '</div>';
- },
-
-
- /**
- * 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
- */
- commWithOtherDevice: function ui_commWithOtherDevice(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}
- */
- getCommWithOtherDeviceBtnn: function ui_getCommWithOtherDeviceBtnn() {
- var communicateWithOtherDeviceButton;
- communicateWithOtherDeviceButton =
- $(this.getButtonHtml('Communicate with another device'));
- communicateWithOtherDeviceButton
- .on('click', this.commWithOtherDevice);
- return communicateWithOtherDeviceButton;
- },
-
- loadStartContent: function ui_loadStartContent() {
- var startBox, contentStart, gap, comment;
- contentStart = $('#content-start');
- startBox = $('<div class="box" id="startBox"></div>');
- gap = $('<div class="gap"></div>');
- 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.getCommWithOtherDeviceBtnn())
- .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 = '<li class="ui-li-multiline" id="' + label + '">'
- + '<a href="#">' + '<span>' + label + '</span>'
- + '<span class="ui-li-text-sub">' + ((!value) ? '...' : value)
- + '</span>' + '</a>' + '</li>';
- return html;
- },
-
- /**
- *
- * @param {string} phone
- * @param {string} first
- * @param {string} last
- * @returns {string}
- */
- getContactsUlHtml: function ui_getContactsUlHtml(
- phone,
- phone_type,
- first,
- middle,
- last,
- vCard
- ) {
- var html;
- html = '<ul data-role="listview" id="contacts-data">';
- html += this.getLiHtml(first, 'First Name');
- html += this.getLiHtml(middle, 'Middle Name');
- html += this.getLiHtml(last, 'Last Name');
- html += this.getLiHtml(phone, 'Phone');
- html += this.getLiHtml(phone_type, 'Type');
- html += this.getLiHtml(vCard, 'vCard');
- html += '</ul>';
- return html;
- },
-
- /**
- * @param {object} contact
- * @returns {string}
- */
- getContactsListElement: function ui_getContactsListElement(contact) {
- var caller = $('<span class="ui-li-text-sub">')
- .text(contact.caller).html(),
- phoneNumber = contact.phoneNumber,
- html = '<li class="ui-li-multiline">'
- + '<a href="#">' + caller + '<span class="ui-li-text-sub">'
- + phoneNumber + '</span>' + '</a>' + '</li>';
- 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 = $('<ul data-role="listview" id="list-choose"></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<br>CLOSE TO<br>YOUR DEVICE'
- );
- self.app.nfc.card.setTagDetectRead();
- } else if (option === 'write') {
- self.prepareWaitingPage(
- 'Device to card',
- 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE'
- );
- self.app.nfc.card.setTagDetectWrite();
- } else {
- self.prepareWaitingPage('Device to device',
- 'PUT YOUR DEVICE<br>CLOSE TO<br>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 += '<p class="defaultVeryBigText">';
+ html += text;
+ html += '</p>';
+
+ html += '<p class="defaultCounterText" id="counter"></p>';
+
+ return html;
+ },
+
+ /**
+ *
+ * @param {string} title
+ * @param {string} text
+ */
+ prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
+ var waitingBox, waitingContent,
+ contentTransfer = $('#content-transfer');
+ waitingBox = $('<div class="box" id="waitingBox"></div>');
+ 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 '<div class="box" id="temporaryBox"></div>';
+ },
+
+ /**
+ * @returns {string}
+ */
+ showPopupWarning: function ui_showPopupWarning(){
+ setTimeout(function(){
+ $("#contact-nfc-error").popup("open",{"positionTo":"window"});
+ },500);
+ },
+
+ getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
+ return '<p class="defaultText">'
+ + 'Default card is not defined yet!<br/>'
+ + 'Do you want to define it now?'
+ + '</p>';
+ },
+
+ 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 '<div data-role="button" class="ui-btn-create">'
+ + text
+ + '</div>';
+ },
+
+ /**
+ *
+ * @param {string} firstName
+ * @param {string} lastName
+ * @returns {string}
+ */
+ getCommentHtml: function ui_getCommentHtml(data) {
+ var def_name = $('<p class="comment" id="comment-name">')
+ .text(data.caller).html();
+ return '<div id="comment">'
+ + '<p class="comment">Your default contact</p>'
+ + '<p class="comment" id="comment-userName">'
+ + def_name + '</p>'
+ + '<p class="comment" id="comment-phone">'
+ + data.phoneNumber + '</p>'
+ + '</div>';
+ },
+
+
+ /**
+ * 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 = $('<div class="box" id="startBox"></div>');
+ gap = $('<div class="gap"></div>');
+ 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 = '<li class="ui-li-multiline">'
+ + '<a href="#">'
+ + '<span>' + label + '</span>'
+ + '<span class="ui-li-text-sub">'
+ + ((!value) ? '...' : value)
+ + '</span>'
+ + '</a>'
+ + '</li>';
+ return html;
+ },
+
+ /**
+ *
+ * @param {string} phone
+ * @param {string} first
+ * @param {string} last
+ * @returns {string}
+ */
+ getContactsUlHtml: function ui_getContactsUlHtml(phone, first, last) {
+ var html;
+ html = '<ul data-role="listview" id="contacts-data">';
+ html += this.getLiHtml(first, 'First Name');
+ html += this.getLiHtml(last, 'Last Name');
+ html += this.getLiHtml(phone, 'Phone');
+ html += '</ul>';
+ return html;
+ },
+
+ /**
+ * @param {object} contact
+ * @returns {string}
+ */
+ getContactsListElement: function ui_getContactsListElement(contact) {
+ var caller = $('<span class="ui-li-text-sub">')
+ .text(contact.caller).html(),
+ phoneNumber = contact.phoneNumber,
+ html =
+ '<li class="ui-li-multiline">'
+ + '<a href="#">' + caller
+ + '<span class="ui-li-text-sub">' + phoneNumber + '</span>'
+ + '</a>'
+ + '</li>';
+ 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 = $('<ul data-role="listview" id="list-choose"></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<br>CLOSE TO<br>YOUR DEVICE');
+ self.app.nfc.card.setTagDetectRead();
+ } else if (option === 'write') {
+ self.prepareWaitingPage('Device to card', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
+ self.app.nfc.card.setTagDetectWrite();
+ } else {
+ self.prepareWaitingPage('Device to device', 'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
+ self.app.nfc.peer.setTargetDetect();
+ }
+ } catch (e) {
+ console.error(e.message);
+ }
+ } else {
+ $.mobile.changePage('#start');
+ alert('Please turn on NFC adapter');
+ }
+ });
+ }
+
+ };
+
+}());