2 /*global $, tizen, App, app, localStorage: true, TemplateManager, document, window, setTimeout */
6 (function () { // strict mode wrapper
9 App.Ui = function App_Ui(app) {
11 this.templateManager = new TemplateManager(app.config);
15 prepareCallerName: function ui_prepareCallerName(contact) {
16 var callerName, firstName, lastName;
19 firstName = contact.name.firstName;
20 lastName = contact.name.lastName;
22 if (firstName !== '' && firstName !== null) {
23 callerName = firstName;
25 if (lastName !== '' && lastName !== null) {
26 if (callerName !== '') {
29 callerName += lastName;
31 if (callerName === '') {
32 callerName = 'No Name';
39 * @param {string} message
41 showPopup: function ui_showPopup(message) {
42 var popup = $("#alertPopup");
43 if(!popup.hasClass('ui-popup')) {
44 popup.popup().trigger('create');
46 $(".ui-popup-text", popup).text(message);
47 popup.popup("open", {positionTo: 'window'});
50 closePopup: function ui_closePopup() {
51 var activePopup = $.mobile.popup.active;
53 if (activePopup.attr('id') === 'alertPopup') {
59 createListRecord: function ui_createListRecord() {
60 $.mobile.changePage('#choose');
65 * @param {string} text
68 getWaitingContentHtml: function getWaitingContentHtml(text) {
70 html += '<p class="defaultVeryBigText">';
74 html += '<p class="defaultCounterText" id="counter"></p>';
81 * @param {string} title
82 * @param {string} text
84 prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
85 var waitingBox, waitingContent,
86 contentTransfer = $('#content-transfer');
87 waitingBox = $('<div class="box" id="waitingBox"></div>');
88 waitingContent = $(this.getWaitingContentHtml(text));
89 $('#header-transfer H1').text(title);
90 contentTransfer.empty();
91 waitingBox.append(waitingContent);
92 contentTransfer.append(waitingBox);
93 $('#content-start').trigger('create');
94 this.app.countDown(10, $('#counter'));
100 getTemporaryBoxHtml: function ui_getTemporaryBoxHtml() {
101 return '<div class="box" id="temporaryBox"></div>';
107 showPopupWarning: function ui_showPopupWarning(){
108 setTimeout(function(){
109 $("#contact-nfc-error").popup("open",{"positionTo":"window"});
113 getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
114 return '<p class="defaultText">'
115 + 'Default card is not defined yet!<br/>'
116 + 'Do you want to define it now?'
120 loadTemporaryContent: function ui_loadTemporaryContent() {
121 var temporaryBox, temporaryContent, temporaryButton;
123 temporaryBox = $(this.getTemporaryBoxHtml());
124 temporaryContent = $(this.getTemporaryBoxContentHtml());
125 temporaryButton = $(this.getButtonHtml('Create default card'));
127 temporaryButton.on('click', function (event) {
128 event.preventDefault();
129 $.mobile.changePage('#choose');
132 temporaryBox.append(temporaryContent).append(temporaryButton);
134 $('#content-start').empty().append(temporaryBox).trigger('create');
138 * @param {string} text button text
141 getButtonHtml: function ui_getButtonHtml(text) {
142 return '<div data-role="button" class="ui-btn-create">'
149 * @param {string} firstName
150 * @param {string} lastName
153 getCommentHtml: function ui_getCommentHtml(data) {
154 var def_name = $('<p class="comment" id="comment-name">')
155 .text(data.caller).html();
156 return '<div id="comment">'
157 + '<p class="comment">Your default contact</p>'
158 + '<p class="comment" id="comment-userName">'
160 + '<p class="comment" id="comment-phone">'
161 + data.phoneNumber + '</p>'
167 * Change Contact button action
169 * @param {Event} event
171 changeContact: function ui_changeContact(event) {
172 event.preventDefault();
173 $.mobile.changePage('#choose');
179 getChangeContactButton: function ui_getChangeContactButton() {
180 var changeContactButton;
181 changeContactButton = $(this.getButtonHtml('Change your default contact'));
182 changeContactButton.on('click', this.changeContact);
183 return changeContactButton;
187 * Read From Card button action
189 * @param {Event} event
191 readFromCard: function ui_readFromCard(event) {
192 event.preventDefault();
193 if (tizen.nfc.getDefaultAdapter().powered) {
195 $('#transfer').data('option', 'read');
196 $.mobile.changePage('#transfer');
198 console.error(e.message);
201 $.mobile.changePage('#start');
202 alert('Please turn on NFC adapter');
209 getReadFromCardButton: function ui_getReadFromCardButton() {
210 var readFromCardButton;
211 readFromCardButton = $(this.getButtonHtml('Read from card'));
212 readFromCardButton.on('click', this.readFromCard);
213 return readFromCardButton;
217 * Write To Card button action
218 * @param {Event} event
220 writeToCard: function ui_writeToCard(event) {
221 event.preventDefault();
222 if (tizen.nfc.getDefaultAdapter().powered) {
224 $('#transfer').data('option', 'write');
225 $.mobile.changePage('#transfer');
227 console.error(e.message);
230 alert('Please turn on NFC adapter');
237 getWriteToCardButton: function ui_getWriteToCardButton() {
238 var writeToCardButton;
239 writeToCardButton = $(this.getButtonHtml('Write to card'));
240 writeToCardButton.on('click', this.writeToCard);
241 return writeToCardButton;
245 * Communicate With Other Device button action
246 * @param {type} event
248 communicateWithOtherDevice: function ui_communicateWithOtherDevice(event) {
249 event.preventDefault();
250 if (tizen.nfc.getDefaultAdapter().powered) {
252 $('#transfer').data('option', 'communicate');
253 $.mobile.changePage('#transfer');
255 console.error(e.message);
258 alert('Please turn on NFC adapter');
265 getCommunicateWithOtherDeviceButton: function ui_getCommunicateWithOtherDeviceButton() {
266 var communicateWithOtherDeviceButton;
267 communicateWithOtherDeviceButton = $(this.getButtonHtml('Communicate with another device'));
268 communicateWithOtherDeviceButton.on('click', this.communicateWithOtherDevice);
269 return communicateWithOtherDeviceButton;
272 loadStartContent: function ui_loadStartContent() {
273 var startBox, contentStart, gap, comment;
274 contentStart = $('#content-start');
275 startBox = $('<div class="box" id="startBox"></div>');
276 gap = $('<div class="gap"></div>');
277 comment = $(this.getCommentHtml(localStorage));
279 contentStart.empty();
281 .append(this.getChangeContactButton())
283 .append(this.getReadFromCardButton())
285 .append(this.getWriteToCardButton())
287 .append(this.getCommunicateWithOtherDeviceButton())
290 contentStart.append(startBox);
291 contentStart.trigger('create');
294 loadStartPage: function ui_loadStartPage() {
295 if (localStorage.started === undefined) {
296 this.loadTemporaryContent();
298 this.loadStartContent();
304 * @param {string} value
305 * @param {string} label
308 getLiHtml: function ui_getLiHtml(value, label) {
310 html = '<li class="ui-li-multiline">'
312 + '<span>' + label + '</span>'
313 + '<span class="ui-li-text-sub">'
314 + ((!value) ? '...' : value)
323 * @param {string} phone
324 * @param {string} first
325 * @param {string} last
328 getContactsUlHtml: function ui_getContactsUlHtml(phone, first, last) {
330 html = '<ul data-role="listview" id="contacts-data">';
331 html += this.getLiHtml(first, 'First Name');
332 html += this.getLiHtml(last, 'Last Name');
333 html += this.getLiHtml(phone, 'Phone');
339 * @param {object} contact
342 getContactsListElement: function ui_getContactsListElement(contact) {
343 var caller = $('<span class="ui-li-text-sub">')
344 .text(contact.caller).html(),
345 phoneNumber = contact.phoneNumber,
347 '<li class="ui-li-multiline">'
348 + '<a href="#">' + caller
349 + '<span class="ui-li-text-sub">' + phoneNumber + '</span>'
355 prepareContactsTemplate: function ui_prepareContactsTemplate(phone, first, last) {
356 $('#content-contact > .ui-scrollview-view')
358 .append(this.getContactsUlHtml(phone, first, last));
359 $('#content-contact').trigger('create');
362 contactsCompare: function ui_contactsCompare(a, b) {
363 if (a.caller < b.caller) {
366 if (a.caller > b.caller) {
372 createSortedContactArray: function ui_createSortedContactArray(contacts) {
373 var i, len, sortedContactList = [], contact, phoneNumber;
375 for (i = 0, len = contacts.length; i < len; i += 1) {
376 contact = contacts[i];
377 if (contact.phoneNumbers.length === 0) {
380 phoneNumber = contact.phoneNumbers[0].number;
382 sortedContactList.push({
383 caller: this.prepareCallerName(contact),
384 firstName: contact.name.firstName || '',
385 lastName: contact.name.lastName || '',
386 phoneNumber: phoneNumber,
388 vCard: contact.convertToString('VCARD_30'),
392 sortedContactList.sort(this.contactsSort);
394 return sortedContactList;
397 createSortedContactList: function ui_createSortedContactList(contacts) {
398 var sortedContactList = this.createSortedContactArray(contacts),
399 ul = $('<ul data-role="listview" id="list-choose"></ul>'),
407 listElementTap = function (event) {
408 event.preventDefault();
409 $(this).addClass('selected').siblings().removeClass('selected');
410 self.app.saveDefaultCard();
413 for (i = 0, len = sortedContactList.length; i < len; i += 1) {
414 contact = sortedContactList[i];
415 if (contact.phoneNumber !== '') {
416 listElement = $(this.getContactsListElement(contact));
418 .data('caller', contact.caller)
419 .data('firstName', contact.firstName)
420 .data('lastName', contact.lastName)
421 .data('phoneNumber', contact.phoneNumber)
422 .data('id', contact.id)
423 .data('vCard', contact.vCard);
424 if (localStorage.id === listElement.data('id')) {
425 listElement.addClass('selected');
427 ul.append(listElement);
430 ul.on('tap taphold click', 'li', listElementTap);
434 showContactsList: function ui_showContactsList(contacts) {
435 var ul = this.createSortedContactList(contacts);
436 $('#content-choose > .ui-scrollview-view').empty().append(ul);
437 $('#content-choose').trigger('create');
440 moveToStartPage: function ui_moveToStartPage(monit) {
441 $('#start').data('monit', monit || '');
442 $.mobile.changePage('#start');
445 isActivePage: function ui_isActivePage(id) {
446 return (id === $.mobile.activePage.attr("id"));
449 refreshIfActivePage: function ui_refreshIfActivePage(id) {
450 if (this.isActivePage(id)) {
452 .trigger("pagebeforeshow")
453 .trigger("pageshow");
457 moveToContactPage: function ui_moveToContactPage(obj) {
458 $('#start').data('monit', '');
459 $('#contact').data('contactsData', obj);
460 $.mobile.changePage('#contact');
463 defineEvents: function ui_defineEvents() {
466 $('#header-start .ui-btn-back').on('click', function (event) {
467 event.preventDefault();
468 self.app.nfc.stopNFC();
471 $('#header-start').on('click', '.ui-btn-back.ui-focus', function () {
475 $('#footer-contact').on('click', '.ui-btn-back', function (event) {
476 event.preventDefault();
477 $.mobile.changePage('#start');
480 $('#footer-choose').on('click', '.ui-btn-back', function (event) {
481 event.preventDefault();
482 $.mobile.changePage('#start');
485 $('#choose').on('pageshow', function (event) {
486 self.app.loadContacts(self.showContactsList.bind(self), function (e) {
487 alert('Cannot load the contacts list: ' + e.message);
488 console.error(e.message, e);
492 $('#contact').on('pageshow', function (event) {
493 var data = $(this).data('contactsData');
494 self.prepareContactsTemplate(data.phone, data.first, data.last);
497 $('#save-contact').on('click', function (event) {
498 event.preventDefault();
499 self.app.saveContact();
502 $('#start').on('pagebeforeshow', function () {
503 if (self.app.started) {
504 self.loadStartPage();
508 $('#start').on('pageshow', function () {
509 var obj = $(this), monit = obj.data('monit');
510 if (monit !== '' && monit !== undefined) {
511 self.showPopup(obj.data('monit'));
512 obj.data('monit', '');
516 $( "#contact-nfc-error" ).bind({
517 popupafterclose: function(){
518 tizen.application.getCurrentApplication().exit();
522 window.addEventListener('tizenhwkey', function(e) {
523 if (e.keyName == "back") {
524 if ($.mobile.popup.active) {
525 $.mobile.popup.active.close();
526 } else if ($.mobile.activePage.attr('id') === 'start') {
527 tizen.application.getCurrentApplication().exit();
529 self.app.nfc.timeExpired();
534 document.addEventListener('webkitvisibilitychange', function () {
535 if(document.webkitVisibilityState === "visible") {
536 if ($.mobile.activePage.attr('id') === "choose") {
537 $.mobile.activePage.trigger('pageshow');
539 app.counterState = true;
541 app.counterState = false;
545 $('#transfer').on('pageshow', function () {
546 if (tizen.nfc.getDefaultAdapter().powered) {
548 var option = $(this).data('option');
549 if (option === 'read') {
550 self.prepareWaitingPage('Card to device', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
551 self.app.nfc.card.setTagDetectRead();
552 } else if (option === 'write') {
553 self.prepareWaitingPage('Device to card', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
554 self.app.nfc.card.setTagDetectWrite();
556 self.prepareWaitingPage('Device to device', 'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
557 self.app.nfc.peer.setTargetDetect();
560 console.error(e.message);
563 $.mobile.changePage('#start');
564 alert('Please turn on NFC adapter');