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);
17 prepareCallerName: function ui_prepareCallerName(contact) {
18 var callerName, firstName, lastName;
21 firstName = contact.name.firstName;
22 lastName = contact.name.lastName;
24 if (firstName !== '' && firstName !== null) {
25 callerName = firstName;
27 if (lastName !== '' && lastName !== null) {
28 if (callerName !== '') {
31 callerName += lastName;
33 if (callerName === '') {
34 callerName = 'No Name';
41 * @param {string} message
42 * @param {string} onclick
45 getPopupHtml: function ui_getPopupHtml(message, onclick) {
46 var html = '<div id="popup" data-role="popupwindow"'
47 + ' data-style="center_basic_1btn">'
48 + '<p data-role="text">' + message + '</p>'
49 + '<div data-role="button-bg">'
50 + '<a href="#" data-role="button" data-inline="true"'
51 + ' onclick="' + onclick + '">OK</a>'
59 * @param {string} message
60 * @param {jQuery} page
62 showPopup: function ui_showPopup(message, page) {
63 this.popup = $(this.getPopupHtml(message, "app.ui.closePopup();"));
64 page.append(this.popup);
65 page.trigger('create');
66 this.popup.data('page', page);
67 this.popup.popupwindow('open');
70 closePopup: function ui_closePopup() {
71 var page = this.popup.data('page');
72 page.data('monit', '');
73 this.popup.popupwindow('close');
78 createListRecord: function ui_createListRecord() {
79 $.mobile.changePage('#choose');
84 * @param {string} text
87 getWaitingContentHtml: function getWaitingContentHtml(text) {
89 html += '<p class="defaultVeryBigText">';
93 html += '<p class="defaultCounterText" id="counter"></p>';
100 * @param {string} title
101 * @param {string} text
103 prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
104 var waitingBox, waitingContent,
105 contentTransfer = $('#content-transfer');
106 waitingBox = $('<div class="box" id="waitingBox"></div>');
107 waitingContent = $(this.getWaitingContentHtml(text));
108 $('#header-transfer H1').text(title);
109 contentTransfer.empty();
110 waitingBox.append(waitingContent);
111 contentTransfer.append(waitingBox);
112 $('#content-start').trigger('create');
113 $('#waitingBox').css("display", "table-cell");
114 this.app.countDown(10, $('#counter'));
120 getTemporaryBoxHtml: function ui_getTemporaryBoxHtml() {
121 return '<div class="box" id="temporaryBox"></div>';
127 getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
128 return '<p class="defaultText">'
129 + 'Default card is not defined yet!<br/>'
130 + 'Do you want to define it now?'
134 loadTemporaryContent: function ui_loadTemporaryContent() {
135 var temporaryBox, temporaryContent, temporaryButton;
137 temporaryBox = $(this.getTemporaryBoxHtml());
138 temporaryContent = $(this.getTemporaryBoxContentHtml());
139 temporaryButton = $(this.getButtonHtml('Create default card'));
141 temporaryButton.on('tap', function (event) {
142 event.preventDefault();
143 $.mobile.changePage('#choose');
146 temporaryBox.append(temporaryContent).append(temporaryButton);
148 $('#content-start').empty().append(temporaryBox).trigger('create');
152 * @param {string} text button text
155 getButtonHtml: function ui_getButtonHtml(text) {
156 return '<div data-role="button" class="ui-btn-create">'
163 * @param {string} firstName
164 * @param {string} lastName
167 getCommentHtml: function ui_getCommentHtml(firstName, lastName) {
168 var html = '<div id="comment">'
169 + '<p class="comment">Your default contact</p>'
170 + '<p class="comment" id="comment-name">'
171 + (firstName || '') + ' ' + (lastName || '')
178 * Change Contact button action
180 * @param {Event} event
182 changeContact: function ui_changeContact(event) {
183 event.preventDefault();
184 $.mobile.changePage('#choose');
190 getChangeContactButton: function ui_getChangeContactButton() {
191 var changeContactButton;
192 changeContactButton = $(this.getButtonHtml('Change your default contact'));
193 changeContactButton.on('tap', this.changeContact);
194 return changeContactButton;
198 * Read From Card button action
200 * @param {Event} event
202 readFromCard: function ui_readFromCard(event) {
203 event.preventDefault();
204 if (tizen.nfc.getDefaultAdapter().powered) {
206 $('#transfer').data('option', 'read');
207 $.mobile.changePage('#transfer');
209 console.error(e.message);
212 $.mobile.changePage('#start');
213 alert('Please turn on NFC adapter');
220 getReadFromCardButton: function ui_getReadFromCardButton() {
221 var readFromCardButton;
222 readFromCardButton = $(this.getButtonHtml('Read from card'));
223 readFromCardButton.on('tap', this.readFromCard);
224 return readFromCardButton;
228 * Write To Card button action
229 * @param {Event} event
231 writeToCard: function ui_writeToCard(event) {
232 event.preventDefault();
233 if (tizen.nfc.getDefaultAdapter().powered) {
235 $('#transfer').data('option', 'write');
236 $.mobile.changePage('#transfer');
238 console.error(e.message);
241 alert('Please turn on NFC adapter');
248 getWriteToCardButton: function ui_getWriteToCardButton() {
249 var writeToCardButton;
250 writeToCardButton = $(this.getButtonHtml('Write to card'));
251 writeToCardButton.on('tap', this.writeToCard);
252 return writeToCardButton;
256 * Communicate With Other Device button action
257 * @param {type} event
259 communicateWithOtherDevice: function ui_communicateWithOtherDevice(event) {
260 event.preventDefault();
261 $('#transfer').data('option', 'communicate');
262 $.mobile.changePage('#transfer');
268 getCommunicateWithOtherDeviceButton: function ui_getCommunicateWithOtherDeviceButton() {
269 var communicateWithOtherDeviceButton;
270 communicateWithOtherDeviceButton = $(this.getButtonHtml('Communicate with another device'));
271 communicateWithOtherDeviceButton.on('tap', this.communicateWithOtherDevice);
272 return communicateWithOtherDeviceButton;
275 loadStartContent: function ui_loadStartContent() {
276 var startBox, contentStart, gap, comment;
277 contentStart = $('#content-start');
278 startBox = $('<div class="box" id="startBox"></div>');
279 gap = $('<div class="gap"></div>');
280 comment = $(this.getCommentHtml(localStorage.firstName, localStorage.lastName));
282 contentStart.empty();
284 .append(this.getChangeContactButton())
286 .append(this.getReadFromCardButton())
288 .append(this.getWriteToCardButton())
290 .append(this.getCommunicateWithOtherDeviceButton())
293 contentStart.append(startBox);
294 contentStart.trigger('create');
297 loadStartPage: function ui_loadStartPage() {
298 if (localStorage.started === undefined) {
299 this.loadTemporaryContent();
301 this.loadStartContent();
307 * @param {string} value
308 * @param {string} label
311 getLiHtml: function ui_getLiHtml(value, label) {
313 html = '<li class="ui-li-multiline">'
315 + ((value === '' || value === 'null') ? '...' : value)
316 + '<span class="ui-li-text-sub">' + label + '</span>'
324 * @param {string} phone
325 * @param {string} first
326 * @param {string} last
329 getContactsUlHtml: function ui_getContactsUlHtml(phone, first, last) {
331 html = '<ul data-role="listview" id="contacts-data">';
332 html += this.getLiHtml(first, 'First Name');
333 html += this.getLiHtml(last, 'Last Name');
334 html += this.getLiHtml(phone, 'Phone');
340 * @param {object} contact
343 getContactsListElement: function ui_getContactsListElement(contact) {
345 '<li class="ui-li-multiline" firstName="' + contact.firstName
346 + '" lastName="' + contact.lastName +
347 '" phoneNumber="' + contact.phoneNumber +
348 '" id="' + contact.id +
349 '" vCard="' + contact.vCard + '">'
350 + '<a href="#">' + contact.caller
351 + '<span class="ui-li-text-sub">' +
359 prepareContactsTemplate: function ui_prepareContactsTemplate(phone, first, last) {
360 $('#content-contact > .ui-scrollview-view')
362 .append(this.getContactsUlHtml(phone, first, last));
363 $('#content-contact').trigger('create');
366 contactsCompare: function ui_contactsCompare(a, b) {
367 if (a.caller < b.caller) {
370 if (a.caller > b.caller) {
376 createSortedContactArray: function ui_createSortedContactArray(contacts) {
377 var i, len, sortedContactList = [], contact, phoneNumber;
379 for (i = 0, len = contacts.length; i < len; i += 1) {
380 contact = contacts[i];
381 if (contact.phoneNumbers.length === 0) {
384 phoneNumber = contact.phoneNumbers[0].number;
386 sortedContactList.push({
387 caller: this.prepareCallerName(contact),
388 firstName: contact.name.firstName || '',
389 lastName: contact.name.lastName || '',
390 phoneNumber: phoneNumber,
392 vCard: contact.convertToString('VCARD_30'),
396 sortedContactList.sort(this.contactsSort);
398 return sortedContactList;
401 createSortedContactList: function ui_createSortedContactList(contacts) {
402 var sortedContactList = this.createSortedContactArray(contacts),
403 ul = $('<ul data-role="listview" id="list-choose"></ul>'),
410 listElementTap = function (event) {
411 event.preventDefault();
412 $(this).addClass('selected').siblings().removeClass('selected');
413 self.app.saveDefaultCard();
416 for (i = 0, len = sortedContactList.length; i < len; i += 1) {
417 listElement = $(this.getContactsListElement(sortedContactList[i]));
418 if (localStorage.id === listElement.attr('id')) {
419 listElement.addClass('selected');
421 ul.append(listElement);
423 ul.on('tap taphold', 'li', listElementTap);
427 showContactsList: function ui_showContactsList(contacts) {
428 var ul = this.createSortedContactList(contacts);
429 $('#content-choose > .ui-scrollview-view').empty().append(ul);
430 $('#content-choose').trigger('create');
433 moveToStartPage: function ui_moveToStartPage(monit) {
434 $('#start').data('monit', monit || '');
435 $.mobile.changePage('#start');
438 moveToContactPage: function ui_moveToContactPage(obj) {
439 $('#start').data('monit', '');
440 $('#contact').data('contactsData', obj);
441 $.mobile.changePage('#contact');
444 disableSelections: function ui_disableSelections() {
445 $.mobile.tizen.disableSelection(document);
448 setContentHeight: function ui_setContentHeight(page, content, header) {
450 .css('height', ($(window).height() - header.height()) + 'px')
451 .css('min-height', 'auto');
452 page.css('min-height', 'auto');
455 defineEvents: function ui_defineEvents() {
458 $('#header-start .ui-btn-back').on('tap', function (event) {
459 event.preventDefault();
460 self.app.nfc.stopNFC();
463 $('#footer-contact').on('tap', '.ui-btn-back', function (event) {
464 event.preventDefault();
465 $.mobile.changePage('#start');
468 $('#footer-choose').on('tap', '.ui-btn-back', function (event) {
469 event.preventDefault();
470 $.mobile.changePage('#start');
473 $('#choose').on('pageshow', function (event) {
474 self.app.loadContacts(self.showContactsList.bind(self), function (e) {
475 alert('Cannot load the contacts list: ' + e.message);
476 console.error(e.message, e);
480 $('#contact').on('pageshow', function (event) {
481 var data = $(this).data('contactsData');
482 self.prepareContactsTemplate(data.phone, data.first, data.last);
485 $('#save-contact').on('tap', function (event) {
486 event.preventDefault();
487 self.app.saveContact();
490 $('#start').on('pagebeforeshow', function () {
491 if (self.app.started) {
492 self.loadStartPage();
496 $('#start').on('pageshow', function () {
497 var obj = $(this), monit = obj.data('monit');
498 if (monit !== '' && monit !== undefined) {
499 self.showPopup(obj.data('monit'), obj);
501 self.setContentHeight(obj, $('#content-start'), $('#header-start'));
504 $('#transfer').on('pagebeforeshow', function () {
505 self.setContentHeight($(this), $('#content-transfer'), $('#header-transfer'));
508 $('#transfer').on('pageshow', function () {
509 if (tizen.nfc.getDefaultAdapter().powered) {
511 var option = $(this).data('option');
512 if (option === 'read') {
513 self.prepareWaitingPage('Card to Device', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
514 self.app.nfc.card.setTagDetectRead();
515 } else if (option === 'write') {
516 self.prepareWaitingPage('Device to Card', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
517 self.app.nfc.card.setTagDetectWrite();
519 self.prepareWaitingPage('Device to Device', 'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
520 self.app.nfc.peer.setTargetDetect();
523 console.error(e.message);
526 $.mobile.changePage('#start');
527 alert('Please turn on NFC adapter');