2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /*jslint devel: true*/
18 /*global $, tizen, App, app, localStorage: true, TemplateManager,document */
19 /*global window, setTimeout */
23 (function () { // strict mode wrapper
26 App.Ui = function App_Ui(app) {
28 this.templateManager = new TemplateManager(app.config);
32 prepareCallerName: function ui_prepareCallerName(contact) {
33 var name = contact.name.displayName;
34 return (name && name.length > 0) ? name : '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);
52 closePopup: function ui_closePopup() {
53 var activePopup = $.mobile.popup.active;
55 if (activePopup.attr('id') === 'alertPopup') {
61 createListRecord: function ui_createListRecord() {
62 $.mobile.changePage('#choose');
67 * @param {string} text
70 getWaitingContentHtml: function getWaitingContentHtml(text) {
72 html += '<p class="defaultVeryBigText">';
76 html += '<p class="defaultCounterText" id="counter"></p>';
83 * @param {string} title
84 * @param {string} text
86 prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
87 var waitingBox, waitingContent,
88 contentTransfer = $('#content-transfer');
89 waitingBox = $('<div class="box" id="waitingBox"></div>');
90 waitingContent = $(this.getWaitingContentHtml(text));
91 $('#header-transfer H1').text(title);
92 contentTransfer.empty();
93 waitingBox.append(waitingContent);
94 contentTransfer.append(waitingBox);
95 $('#content-start').trigger('create');
96 this.app.countDown(10, $('#counter'));
102 getTemporaryBoxHtml: function ui_getTemporaryBoxHtml() {
103 return '<div class="box" id="temporaryBox"></div>';
109 showPopupWarning: function ui_showPopupWarning() {
110 setTimeout(function () {
111 $('#contact-nfc-error').popup('open', {
112 'positionTo': 'window'
117 getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
118 return '<p class="defaultText">'
119 + 'Default card is not defined yet!<br/>'
120 + 'Do you want to define it now?' + '</p>';
123 loadTemporaryContent: function ui_loadTemporaryContent() {
124 var temporaryBox, temporaryContent, temporaryButton;
126 temporaryBox = $(this.getTemporaryBoxHtml());
127 temporaryContent = $(this.getTemporaryBoxContentHtml());
128 temporaryButton = $(this.getButtonHtml('Create default card'));
130 temporaryButton.on('click', function (event) {
131 event.preventDefault();
132 $.mobile.changePage('#choose');
135 temporaryBox.append(temporaryContent).append(temporaryButton);
137 $('#content-start').empty().append(temporaryBox).trigger('create');
141 * @param {string} text button text
144 getButtonHtml: function ui_getButtonHtml(text) {
145 return '<div data-role="button" class="ui-btn-create">'
151 * @param {string} firstName
152 * @param {string} lastName
155 getCommentHtml: function ui_getCommentHtml(data) {
156 var def_name = $('<p class="comment" id="comment-name">')
157 .text(data.caller).html();
158 return '<div id="comment">'
159 + '<p class="comment">Your default contact</p>'
160 + '<p class="comment" id="comment-userName">'
161 + def_name + '</p>' + '<p class="comment" id="comment-phone">'
162 + data.phoneNumber + '</p>' + '</div>';
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 =
182 $(this.getButtonHtml('Change your default contact'));
183 changeContactButton.on('click', this.changeContact);
184 return changeContactButton;
188 * Read From Card button action
190 * @param {Event} event
192 readFromCard: function ui_readFromCard(event) {
193 event.preventDefault();
194 if (tizen.nfc.getDefaultAdapter().powered) {
196 $('#transfer').data('option', 'read');
197 $.mobile.changePage('#transfer');
199 console.error(e.message);
202 $.mobile.changePage('#start');
203 alert('Please turn on NFC adapter');
210 getReadFromCardButton: function ui_getReadFromCardButton() {
211 var readFromCardButton;
212 readFromCardButton = $(this.getButtonHtml('Read from card'));
213 readFromCardButton.on('click', this.readFromCard);
214 return readFromCardButton;
218 * Write To Card button action
219 * @param {Event} event
221 writeToCard: function ui_writeToCard(event) {
222 event.preventDefault();
223 if (tizen.nfc.getDefaultAdapter().powered) {
225 $('#transfer').data('option', 'write');
226 $.mobile.changePage('#transfer');
228 console.error(e.message);
231 alert('Please turn on NFC adapter');
238 getWriteToCardButton: function ui_getWriteToCardButton() {
239 var writeToCardButton;
240 writeToCardButton = $(this.getButtonHtml('Write to card'));
241 writeToCardButton.on('click', this.writeToCard);
242 return writeToCardButton;
246 * Communicate With Other Device button action
247 * @param {type} event
249 commWithOtherDevice: function ui_commWithOtherDevice(event) {
250 event.preventDefault();
251 if (tizen.nfc.getDefaultAdapter().powered) {
253 $('#transfer').data('option', 'communicate');
254 $.mobile.changePage('#transfer');
256 console.error(e.message);
259 alert('Please turn on NFC adapter');
266 getCommWithOtherDeviceBtnn: function ui_getCommWithOtherDeviceBtnn() {
267 var communicateWithOtherDeviceButton;
268 communicateWithOtherDeviceButton =
269 $(this.getButtonHtml('Communicate with another device'));
270 communicateWithOtherDeviceButton
271 .on('click', this.commWithOtherDevice);
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));
282 contentStart.empty();
284 .append(this.getChangeContactButton())
286 .append(this.getReadFromCardButton())
288 .append(this.getWriteToCardButton())
290 .append(this.getCommWithOtherDeviceBtnn())
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" id="' + label + '">'
314 + '<a href="#">' + '<span>' + label + '</span>'
315 + '<span class="ui-li-text-sub">' + ((!value) ? '...' : value)
316 + '</span>' + '</a>' + '</li>';
322 * @param {string} phone
323 * @param {string} first
324 * @param {string} last
327 getContactsUlHtml: function ui_getContactsUlHtml(
336 html = '<ul data-role="listview" id="contacts-data">';
337 html += this.getLiHtml(first, 'First Name');
338 html += this.getLiHtml(middle, 'Middle Name');
339 html += this.getLiHtml(last, 'Last Name');
340 html += this.getLiHtml(phone, 'Phone');
341 html += this.getLiHtml(phone_type, 'Type');
342 html += this.getLiHtml(vCard, 'vCard');
348 * @param {object} contact
351 getContactsListElement: function ui_getContactsListElement(contact) {
352 var caller = $('<span class="ui-li-text-sub">')
353 .text(contact.caller).html(),
354 phoneNumber = contact.phoneNumber,
355 html = '<li class="ui-li-multiline">'
356 + '<a href="#">' + caller + '<span class="ui-li-text-sub">'
357 + phoneNumber + '</span>' + '</a>' + '</li>';
361 prepareContactsTemplate: function ui_prepareContactsTemplate(
369 $('#content-contact > .ui-scrollview-view')
372 this.getContactsUlHtml(
381 $('#content-contact').trigger('create');
384 contactsCompare: function ui_contactsCompare(a, b) {
385 if (a.caller < b.caller) {
388 if (a.caller > b.caller) {
394 createSortedContactArray: function ui_createSortedContactArray(
397 var i, len, sortedContactList = [],
398 contact, phoneNumber;
400 for (i = 0, len = contacts.length; i < len; i += 1) {
401 contact = contacts[i];
402 if (contact.phoneNumbers.length === 0) {
405 phoneNumber = contact.phoneNumbers[0].number;
407 sortedContactList.push({
408 caller: this.prepareCallerName(contact),
409 firstName: contact.name.firstName || '',
410 middleName: contact.name.middleName || '',
411 lastName: contact.name.lastName || '',
412 phoneNumber: phoneNumber,
414 vCard: contact.convertToString('VCARD_30'),
418 sortedContactList.sort(this.contactsSort);
420 return sortedContactList;
423 createSortedContactList: function ui_createSortedContactList(
426 var sortedContactList = this.createSortedContactArray(contacts),
427 ul = $('<ul data-role="listview" id="list-choose"></ul>'),
435 listElementTap = function (event) {
436 event.preventDefault();
437 $(this).addClass('selected').siblings().removeClass('selected');
438 self.app.saveDefaultCard();
441 for (i = 0, len = sortedContactList.length; i < len; i += 1) {
442 contact = sortedContactList[i];
443 if (contact.phoneNumber !== '') {
444 listElement = $(this.getContactsListElement(contact));
446 .data('caller', contact.caller)
447 .data('firstName', contact.firstName)
448 .data('middleName', contact.middleName)
449 .data('lastName', contact.lastName)
450 .data('phoneNumber', contact.phoneNumber)
451 .data('id', contact.id)
452 .data('vCard', contact.vCard);
453 if (localStorage.id === listElement.data('id')) {
454 listElement.addClass('selected');
456 ul.append(listElement);
459 ul.on('tap taphold click', 'li', listElementTap);
463 showContactsList: function ui_showContactsList(contacts) {
464 var ul = this.createSortedContactList(contacts);
465 $('#content-choose > .ui-scrollview-view').empty().append(ul);
466 $('#content-choose').trigger('create');
469 moveToStartPage: function ui_moveToStartPage(monit) {
470 $('#start').data('monit', monit || '');
471 $.mobile.changePage('#start');
474 isActivePage: function ui_isActivePage(id) {
475 return (id === $.mobile.activePage.attr('id'));
478 refreshIfActivePage: function ui_refreshIfActivePage(id) {
479 if (this.isActivePage(id)) {
481 .trigger('pagebeforeshow')
482 .trigger('pageshow');
486 moveToContactPage: function ui_moveToContactPage(obj) {
487 $('#start').data('monit', '');
488 $('#contact').data('contactsData', obj);
489 $.mobile.changePage('#contact');
492 defineEvents: function ui_defineEvents() {
495 $('#header-start .ui-btn-back').on('click', function (event) {
496 event.preventDefault();
497 self.app.nfc.stopNFC();
501 .on('click', '.ui-btn-back.ui-focus', function () {
505 $('#footer-contact').on('click', '.ui-btn-back', function (event) {
506 event.preventDefault();
507 $.mobile.changePage('#start');
510 $('#footer-choose').on('click', '.ui-btn-back', function (event) {
511 event.preventDefault();
512 $.mobile.changePage('#start');
515 $('#choose').on('pageshow', function (event) {
516 self.app.loadContacts(
517 self.showContactsList.bind(self),
519 alert('Cannot load the contacts list: ' + e.message);
520 console.error(e.message, e);
525 $('#contact').on('pageshow', function (event) {
526 var data = $(this).data('contactsData');
527 self.prepareContactsTemplate(
537 $('#save-contact').on('click', function (event) {
538 event.preventDefault();
539 self.app.saveContact();
542 $('#start').on('pagebeforeshow', function () {
543 if (self.app.started) {
544 self.loadStartPage();
548 $('#start').on('pageshow', function () {
550 monit = obj.data('monit');
551 if (monit !== '' && monit !== undefined) {
552 self.showPopup(obj.data('monit'));
553 obj.data('monit', '');
557 $('#contact-nfc-error').bind({
558 popupafterclose: function () {
559 tizen.application.getCurrentApplication().exit();
563 window.addEventListener('tizenhwkey', function (e) {
564 if (e.keyName === 'back') {
565 if ($.mobile.popup.active) {
566 $.mobile.popup.active.close();
567 } else if ($.mobile.activePage.attr('id') === 'start') {
568 tizen.application.getCurrentApplication().exit();
570 self.app.nfc.timeExpired();
575 document.addEventListener('webkitvisibilitychange', function () {
576 if (document.webkitVisibilityState === 'visible') {
577 if ($.mobile.activePage.attr('id') === 'choose') {
578 $.mobile.activePage.trigger('pageshow');
580 app.counterState = true;
582 app.counterState = false;
586 $('#transfer').on('pageshow', function () {
587 if (tizen.nfc.getDefaultAdapter().powered) {
589 var option = $(this).data('option');
590 if (option === 'read') {
591 self.prepareWaitingPage(
593 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE'
595 self.app.nfc.card.setTagDetectRead();
596 } else if (option === 'write') {
597 self.prepareWaitingPage(
599 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE'
601 self.app.nfc.card.setTagDetectWrite();
603 self.prepareWaitingPage('Device to device',
604 'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
605 self.app.nfc.peer.setTargetDetect();
608 console.error(e.message);
611 $.mobile.changePage('#start');
612 alert('Please turn on NFC adapter');