[ContactsExchanger] updated ContactsExchanger sources
[samples/web/ContactsExchanger.git] / js / app.ui.js
index 1582e9a..971268c 100644 (file)
@@ -110,8 +110,8 @@ App.Ui = null;
                        waitingBox.append(waitingContent);
                        contentTransfer.append(waitingBox);
                        $('#content-start').trigger('create');
-                       $('#waitingBox').css("display", "table-cell");
                        this.app.countDown(10, $('#counter'));
+                       this.verticalCenter($('#waitingBox'));
                },
 
                /**
@@ -124,6 +124,12 @@ App.Ui = null;
                /**
                 * @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/>'
@@ -164,14 +170,14 @@ App.Ui = null;
                 * @param {string} lastName
                 * @returns {string}
                 */
-               getCommentHtml: function ui_getCommentHtml(firstName, lastName) {
-                       var html = '<div id="comment">'
+               getCommentHtml: function ui_getCommentHtml(data) {
+                       return '<div id="comment">'
                                + '<p class="comment">Your default contact</p>'
                                + '<p class="comment" id="comment-name">'
-                               + (firstName || '') + ' ' + (lastName || '')
-                               + '</p>'
+                               + data.caller + '</p>'
+                               + '<p class="comment" id="comment-phone">'
+                               + data.phoneNumber + '</p>'
                                + '</div>';
-                       return html;
                },
 
                /**
@@ -201,17 +207,17 @@ App.Ui = null;
                 */
                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');
+                       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');
+                       }
                },
 
                /**
@@ -258,8 +264,16 @@ App.Ui = null;
                 */
                communicateWithOtherDevice: function ui_communicateWithOtherDevice(event) {
                        event.preventDefault();
-                       $('#transfer').data('option', 'communicate');
-                       $.mobile.changePage('#transfer');
+                       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');
+                       }
                },
 
                /**
@@ -277,7 +291,7 @@ App.Ui = null;
                        contentStart = $('#content-start');
                        startBox = $('<div class="box" id="startBox"></div>');
                        gap = $('<div class="gap"></div>');
-                       comment = $(this.getCommentHtml(localStorage.firstName, localStorage.lastName));
+                       comment = $(this.getCommentHtml(localStorage));
 
                        contentStart.empty();
                        startBox
@@ -300,6 +314,8 @@ App.Ui = null;
                        } else {
                                this.loadStartContent();
                        }
+                       $.mobile.activePage.page('refresh');
+                       $('#start, #content-start').css("min-height", 0);
                },
 
                /**
@@ -342,11 +358,7 @@ App.Ui = null;
                 */
                getContactsListElement: function ui_getContactsListElement(contact) {
                        var html =
-                                       '<li class="ui-li-multiline" firstName="' + contact.firstName
-                                       + '" lastName="' + contact.lastName +
-                                       '" phoneNumber="' + contact.phoneNumber +
-                                       '" id="' + contact.id +
-                                       '" vCard="' + contact.vCard + '">'
+                                       '<li class="ui-li-multiline">'
                                        + '<a href="#">' + contact.caller
                                        + '<span class="ui-li-text-sub">' +
                                        contact.phoneNumber
@@ -405,7 +417,8 @@ App.Ui = null;
                                len,
                                listElement,
                                listElementTap,
-                               self = this;
+                               self = this,
+                               contact;
 
                        listElementTap = function (event) {
                                event.preventDefault();
@@ -414,11 +427,21 @@ App.Ui = null;
                        };
 
                        for (i = 0, len = sortedContactList.length; i < len; i += 1) {
-                               listElement = $(this.getContactsListElement(sortedContactList[i]));
-                               if (localStorage.id === listElement.attr('id')) {
-                                       listElement.addClass('selected');
+                               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.append(listElement);
                        }
                        ul.on('tap taphold', 'li', listElementTap);
                        return ul;
@@ -435,6 +458,16 @@ App.Ui = null;
                        $.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("pageshow");
+                       }
+               },
+
                moveToContactPage: function ui_moveToContactPage(obj) {
                        $('#start').data('monit', '');
                        $('#contact').data('contactsData', obj);
@@ -445,11 +478,14 @@ App.Ui = null;
                        $.mobile.tizen.disableSelection(document);
                },
 
-               setContentHeight: function ui_setContentHeight(page, content, header) {
-                       content
-                               .css('height', ($(window).height() - header.height()) + 'px')
-                               .css('min-height', 'auto');
-                       page.css('min-height', 'auto');
+               verticalCenter: function (obj) {
+                       var marginTop = ($(window).height()
+                               - $('[data-role=header]:visible').height()
+                               - $('[data-role=footer]:visible').height()
+                               - obj.outerHeight())/2;
+                       if (parseInt(obj.css('margin-top'), 10) !== marginTop) {
+                               obj.hide().css('margin-top', marginTop).show();
+                       }
                },
 
                defineEvents: function ui_defineEvents() {
@@ -460,6 +496,10 @@ App.Ui = null;
                                self.app.nfc.stopNFC();
                        });
 
+                       $('#header-start').on('click', '.ui-btn-back.ui-focus', function () {
+                               return false;
+                       });
+
                        $('#footer-contact').on('tap', '.ui-btn-back', function (event) {
                                event.preventDefault();
                                $.mobile.changePage('#start');
@@ -491,6 +531,7 @@ App.Ui = null;
                                if (self.app.started) {
                                        self.loadStartPage();
                                }
+                               self.verticalCenter($('#startBox'));
                        });
 
                        $('#start').on('pageshow', function () {
@@ -498,11 +539,35 @@ App.Ui = null;
                                if (monit !== '' && monit !== undefined) {
                                        self.showPopup(obj.data('monit'), obj);
                                }
-                               self.setContentHeight(obj, $('#content-start'), $('#header-start'));
                        });
 
-                       $('#transfer').on('pagebeforeshow', function () {
-                               self.setContentHeight($(this), $('#content-transfer'), $('#header-transfer'));
+                       $('#start').one('pageshow', function () {
+                               setTimeout(function () {
+                                       self.verticalCenter($('#startBox'));
+                               }, 20);
+                       });
+                       $( "#contact-nfc-error" ).bind({
+                                       popupafterclose: function(){
+                                       tizen.application.getCurrentApplication().exit();
+                               }
+                       });
+
+                       document.addEventListener('tizenhwkey', function(e) {
+                               if (e.keyName == "back") {
+                                       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');
+                                       }
+                               }
                        });
 
                        $('#transfer').on('pageshow', function () {
@@ -510,13 +575,13 @@ App.Ui = null;
                                        try {
                                                var option = $(this).data('option');
                                                if (option === 'read') {
-                                                       self.prepareWaitingPage('Card to Device', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
+                                                       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.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.prepareWaitingPage('Device to device', 'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
                                                        self.app.nfc.peer.setTargetDetect();
                                                }
                                        } catch (e) {