[ContactsExchanger]update ContactsExchanger(tizen_2.1)
[samples/web/ContactsExchanger.git] / js / app.ui.js
1 /*jslint devel: true*/
2 /*global $, tizen, App, app, localStorage: true, TemplateManager, document, window, setTimeout */
3
4 App.Ui = null;
5
6 (function () { // strict mode wrapper
7         'use strict';
8
9         App.Ui = function App_Ui(app) {
10                 this.app = app;
11                 this.templateManager = new TemplateManager(app.config);
12         };
13
14         App.Ui.prototype = {
15                 popup: null,
16
17                 prepareCallerName: function ui_prepareCallerName(contact) {
18                         var callerName, firstName, lastName;
19
20                         callerName = '';
21                         firstName = contact.name.firstName;
22                         lastName = contact.name.lastName;
23
24                         if (firstName !== '' && firstName !== null) {
25                                 callerName = firstName;
26                         }
27                         if (lastName !== '' && lastName !== null) {
28                                 if (callerName !== '') {
29                                         callerName += ' ';
30                                 }
31                                 callerName += lastName;
32                         }
33                         if (callerName === '') {
34                                 callerName = 'No Name';
35                         }
36                         return callerName;
37                 },
38
39                 /**
40                  *
41                  * @param {string} message
42                  * @param {string} onclick
43                  * @returns {string}
44                  */
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>'
52                                 + '</div>'
53                                 + '</div>';
54                         return html;
55                 },
56
57                 /**
58                  * Show a popup
59                  * @param {string} message
60                  * @param {jQuery} page
61                  */
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');
68                 },
69
70                 closePopup: function ui_closePopup() {
71                         var page = this.popup.data('page');
72                         page.data('monit', '');
73                         this.popup.popupwindow('close');
74                         this.popup.remove();
75                         this.popup = null;
76                 },
77
78                 createListRecord: function ui_createListRecord() {
79                         $.mobile.changePage('#choose');
80                 },
81
82                 /**
83                  *
84                  * @param {string} text
85                  * @returns {string}
86                  */
87                 getWaitingContentHtml: function getWaitingContentHtml(text) {
88                         var html;
89                         html += '<p class="defaultVeryBigText">';
90                         html += text;
91                         html += '</p>';
92
93                         html += '<p class="defaultCounterText" id="counter"></p>';
94
95                         return html;
96                 },
97
98                 /**
99                  *
100                  * @param {string} title
101                  * @param {string} text
102                  */
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
114                         this.app.countDown(10, $('#counter'));
115                 },
116
117                 /**
118                  * @returns {string}
119                  */
120                 getTemporaryBoxHtml: function ui_getTemporaryBoxHtml() {
121                         return '<div class="box" id="temporaryBox"></div>';
122                 },
123
124                 /**
125                  * @returns {string}
126                  */
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?'
131                                 + '</p>';
132                 },
133
134                 loadTemporaryContent: function ui_loadTemporaryContent() {
135                         var temporaryBox, temporaryContent, temporaryButton;
136
137                         temporaryBox = $(this.getTemporaryBoxHtml());
138                         temporaryContent = $(this.getTemporaryBoxContentHtml());
139                         temporaryButton = $(this.getButtonHtml('Create default card'));
140
141                         temporaryButton.on('tap', function (event) {
142                                 event.preventDefault();
143                                 $.mobile.changePage('#choose');
144                         });
145
146                         temporaryBox.append(temporaryContent).append(temporaryButton);
147
148                         $('#content-start').empty().append(temporaryBox).trigger('create');
149                 },
150                 /**
151                  *
152                  * @param {string} text button text
153                  * @returns {string}
154                  */
155                 getButtonHtml: function ui_getButtonHtml(text) {
156                         return '<div data-role="button" class="ui-btn-create">'
157                                 + text
158                                 + '</div>';
159                 },
160
161                 /**
162                  *
163                  * @param {string} firstName
164                  * @param {string} lastName
165                  * @returns {string}
166                  */
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 || '')
172                                 + '</p>'
173                                 + '</div>';
174                         return html;
175                 },
176
177                 /**
178                  * Change Contact button action
179                  * @event
180                  * @param {Event} event
181                  */
182                 changeContact: function ui_changeContact(event) {
183                         event.preventDefault();
184                         $.mobile.changePage('#choose');
185                 },
186
187                 /**
188                  * @returns {jQuery}
189                  */
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;
195                 },
196
197                 /**
198                  * Read From Card button action
199                  * @event
200                  * @param {Event} event
201                  */
202                 readFromCard: function ui_readFromCard(event) {
203                         event.preventDefault();
204                         $('#transfer').data('option', 'read');
205                         $.mobile.changePage('#transfer');
206                 },
207
208                 /**
209                  * @returns {jQuery}
210                  */
211                 getReadFromCardButton: function ui_getReadFromCardButton() {
212                         var readFromCardButton;
213                         readFromCardButton = $(this.getButtonHtml('Read from card'));
214                         readFromCardButton.on('tap', this.readFromCard);
215                         return readFromCardButton;
216                 },
217
218                 /**
219                  * Write To Card button action
220                  * @param {Event} event
221                  */
222                 writeToCard: function ui_writeToCard(event) {
223                         event.preventDefault();
224                         $('#transfer').data('option', 'write');
225                         $.mobile.changePage('#transfer');
226                 },
227
228                 /**
229                  * @returns {jQuery}
230                  */
231                 getWriteToCardButton: function ui_getWriteToCardButton() {
232                         var writeToCardButton;
233                         writeToCardButton = $(this.getButtonHtml('Write to card'));
234                         writeToCardButton.on('tap', this.writeToCard);
235                         return writeToCardButton;
236                 },
237
238                 /**
239                  * Communicate With Other Device button action
240                  * @param {type} event
241                  */
242                 communicateWithOtherDevice: function ui_communicateWithOtherDevice(event) {
243                         event.preventDefault();
244                         $('#transfer').data('option', 'communicate');
245                         $.mobile.changePage('#transfer');
246                 },
247
248                 /**
249                  * @returns {jQuery}
250                  */
251                 getCommunicateWithOtherDeviceButton: function ui_getCommunicateWithOtherDeviceButton() {
252                         var communicateWithOtherDeviceButton;
253                         communicateWithOtherDeviceButton = $(this.getButtonHtml('Communicate with another device'));
254                         communicateWithOtherDeviceButton.on('tap', this.communicateWithOtherDevice);
255                         return communicateWithOtherDeviceButton;
256                 },
257
258                 loadStartContent: function ui_loadStartContent() {
259                         var startBox, contentStart, gap, comment;
260                         contentStart = $('#content-start');
261                         startBox = $('<div class="box" id="startBox"></div>');
262                         gap = $('<div class="gap"></div>');
263                         comment = $(this.getCommentHtml(localStorage.firstName, localStorage.lastName));
264
265                         contentStart.empty();
266                         startBox
267                                 .append(this.getChangeContactButton())
268                                 .append(gap.clone())
269                                 .append(this.getReadFromCardButton())
270                                 .append(gap.clone())
271                                 .append(this.getWriteToCardButton())
272                                 .append(gap.clone())
273                                 .append(this.getCommunicateWithOtherDeviceButton())
274                                 .prepend(comment);
275
276                         contentStart.append(startBox);
277                         contentStart.trigger('create');
278                 },
279
280                 loadStartPage: function ui_loadStartPage() {
281                         if (localStorage.started === undefined) {
282                                 this.loadTemporaryContent();
283                         } else {
284                                 this.loadStartContent();
285                         }
286                 },
287
288                 /**
289                  *
290                  * @param {string} value
291                  * @param {string} label
292                  * @returns {string}
293                  */
294                 getLiHtml: function ui_getLiHtml(value, label) {
295                         var html;
296                         html = '<li class="ui-li-multiline">'
297                                 + '<a href="#">'
298                                 + ((value === '' || value === 'null') ? '...' : value)
299                                 + '<span class="ui-li-text-sub">' + label + '</span>'
300                                 + '</a>'
301                                 + '</li>';
302                         return html;
303                 },
304
305                 /**
306                  *
307                  * @param {string} phone
308                  * @param {string} first
309                  * @param {string} last
310                  * @returns {string}
311                  */
312                 getContactsUlHtml: function ui_getContactsUlHtml(phone, first, last) {
313                         var html;
314                         html = '<ul data-role="listview" id="contacts-data">';
315                         html += this.getLiHtml(first, 'First Name');
316                         html += this.getLiHtml(last, 'Last Name');
317                         html += this.getLiHtml(phone, 'Phone');
318                         html += '</ul>';
319                         return html;
320                 },
321
322                 /**
323                  * @param {object} contact
324                  * @returns {string}
325                  */
326                 getContactsListElement: function ui_getContactsListElement(contact) {
327                         var html =
328                                         '<li class="ui-li-multiline" firstName="' + contact.firstName
329                                         + '" lastName="' + contact.lastName +
330                                         '" phoneNumber="' + contact.phoneNumber +
331                                         '" id="' + contact.id +
332                                         '" vCard="' + contact.vCard + '">'
333                                         + '<a href="#">' + contact.caller
334                                         + '<span class="ui-li-text-sub">' +
335                                         contact.phoneNumber
336                                         + '</span>'
337                                         + '</a>'
338                                         + '</li>';
339                         return html;
340                 },
341
342                 prepareContactsTemplate: function ui_prepareContactsTemplate(phone, first, last) {
343                         $('#content-contact > .ui-scrollview-view')
344                                 .empty()
345                                 .append(this.getContactsUlHtml(phone, first, last));
346                         $('#content-contact').trigger('create');
347                 },
348
349                 contactsCompare: function ui_contactsCompare(a, b) {
350                         if (a.caller < b.caller) {
351                                 return -1;
352                         }
353                         if (a.caller > b.caller) {
354                                 return 1;
355                         }
356                         return 0;
357                 },
358
359                 createSortedContactArray: function ui_createSortedContactArray(contacts) {
360                         var i, len, sortedContactList = [], contact, phoneNumber;
361
362                         for (i = 0, len = contacts.length; i < len; i += 1) {
363                                 contact = contacts[i];
364                                 if (contact.phoneNumbers.length === 0) {
365                                         phoneNumber = '';
366                                 } else {
367                                         phoneNumber = contact.phoneNumbers[0].number;
368                                 }
369                                 sortedContactList.push({
370                                         caller: this.prepareCallerName(contact),
371                                         firstName: contact.name.firstName || '',
372                                         lastName: contact.name.lastName || '',
373                                         phoneNumber: phoneNumber,
374                                         id: contact.id,
375                                         vCard: contact.convertToString('VCARD_30'),
376                                         contact: contact
377                                 });
378                         }
379                         sortedContactList.sort(this.contactsSort);
380
381                         return sortedContactList;
382                 },
383
384                 createSortedContactList: function ui_createSortedContactList(contacts) {
385                         var sortedContactList = this.createSortedContactArray(contacts),
386                                 ul = $('<ul data-role="listview" id="list-choose"></ul>'),
387                                 i,
388                                 len,
389                                 listElement,
390                                 listElementTap,
391                                 self = this;
392
393                         listElementTap = function (event) {
394                                 event.preventDefault();
395                                 $(this).addClass('selected').siblings().removeClass('selected');
396                                 self.app.saveDefaultCard();
397                         };
398
399                         for (i = 0, len = sortedContactList.length; i < len; i += 1) {
400                                 listElement = $(this.getContactsListElement(sortedContactList[i]));
401                                 if (localStorage.id === listElement.attr('id')) {
402                                         listElement.addClass('selected');
403                                 }
404                                 ul.append(listElement);
405                         }
406                         ul.on('tap taphold', 'li', listElementTap);
407                         return ul;
408                 },
409
410                 showContactsList: function ui_showContactsList(contacts) {
411                         var ul = this.createSortedContactList(contacts);
412                         $('#content-choose > .ui-scrollview-view').empty().append(ul);
413                         $('#content-choose').trigger('create');
414                 },
415
416                 moveToStartPage: function ui_moveToStartPage(monit) {
417                         $('#start').data('monit', monit || '');
418                         $.mobile.changePage('#start');
419                 },
420
421                 moveToContactPage: function ui_moveToContactPage(obj) {
422                         $('#start').data('monit', '');
423                         $('#contact').data('contactsData', obj);
424                         $.mobile.changePage('#contact');
425                 },
426
427                 disableSelections: function ui_disableSelections() {
428                         $.mobile.tizen.disableSelection(document);
429                 },
430
431                 defineEvents: function ui_defineEvents() {
432                         var self = this;
433
434                         $('#header-start .ui-btn-back').on('tap', function (event) {
435                                 event.preventDefault();
436                                 self.app.nfc.stopNFC();
437                         });
438
439                         $('#footer-contact').on('tap', '.ui-btn-back', function (event) {
440                                 event.preventDefault();
441                                 $.mobile.changePage('#start');
442                         });
443
444                         $('#footer-choose').on('tap', '.ui-btn-back', function (event) {
445                                 event.preventDefault();
446                                 $.mobile.changePage('#start');
447                         });
448
449                         $('#choose').on('pageshow', function (event) {
450                                 self.app.loadContacts(self.showContactsList.bind(self), function (e) {
451                                         alert('Cannot load the contacts list: ' + e.message);
452                                         console.error(e.message, e);
453                                 });
454                         });
455
456                         $('#contact').on('pageshow', function (event) {
457                                 var data = $(this).data('contactsData');
458                                 self.prepareContactsTemplate(data.phone, data.first, data.last);
459                         });
460
461                         $('#save-contact').on('tap', function (event) {
462                                 event.preventDefault();
463                                 self.app.saveContact();
464                         });
465
466                         $('#start').on('pagebeforeshow', function () {
467                                 if (self.app.started) {
468                                         self.loadStartPage();
469                                 }
470                         });
471
472                         $('#start').on('pageshow', function () {
473                                 var monit, obj, contentStart, contentStartHeight;
474                                 obj = $(this);
475                                 monit = obj.data('monit');
476                                 if (monit !== '' && monit !== undefined) {
477                                         self.showPopup(obj.data('monit'), obj);
478                                 }
479                                 contentStart = $('#content-start');
480
481                                 if (contentStart.height() > $(window).height()) {
482                                         contentStartHeight = $(window).height() - $('#header-start').height()
483                                                 - parseInt(contentStart.css('padding-top'), 10) - parseInt(contentStart.css('padding-bottom'), 10);
484                                 } else {
485                                         contentStartHeight = contentStart.height();
486                                 }
487                                 setTimeout(function () { // workaround (setTimeout with 0 delay)
488                                         contentStart
489                                                 .css('height', contentStartHeight  + 'px')
490                                                 .css('min-height', 'auto')
491                                         obj.css('min-height', 'auto');
492                                 }, 0);
493                         });
494
495                         $('#transfer').on('pageshow', function () {
496                                 var contentTransfer = $('#content-transfer');
497                                 contentTransfer.css('width', contentTransfer.width() + 'px');
498                                 setTimeout(function () { // workaround (setTimeout with 0 delay)
499                                         contentTransfer.css('height', contentTransfer.css('min-height'));
500                                 }, 0);
501                         });
502
503                         $('#transfer').on('pageshow', function () {
504                                 try {
505                                         var option = $(this).data('option');
506                                         if (option === 'read') {
507                                                 self.prepareWaitingPage('Card to Device', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
508                                                 self.app.nfc.card.setTagDetectRead();
509                                         } else if (option === 'write') {
510                                                 self.prepareWaitingPage('Device to Card', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
511                                                 self.app.nfc.card.setTagDetectWrite();
512                                         } else {
513                                                 self.prepareWaitingPage('Device to Device', 'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
514                                                 self.app.nfc.peer.setTargetDetect();
515                                         }
516                                 } catch (e) {
517                                         console.error(e.message);
518                                 }
519                         });
520                 }
521
522         };
523
524 }());