Tizen 2.0 Release
[samples/web/ContactsExchanger.git] / js / app.js
1 /*
2  *      Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.0 (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
7  *
8  *              http://floralicense.org/license/
9  *
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.
15  */
16
17 /*jslint devel: true*/
18 /*global $, tizen */
19
20 var App = null;
21
22 // App instance
23 var app = null;
24
25 (function () { // strict mode wrapper
26         'use strict';
27
28         function checkCharsSize(string) {
29                 var i, result = '', length = string.length;
30                 for (i = 0; i < length; i += 1) {
31                         if (string.charCodeAt(i) < 256) {
32                                 result += string[i];
33                         }
34                 }
35                 return result;
36         }
37
38         /**
39          * Creates a new application object
40          *
41          * @class Application
42          */
43         App = function App() {};
44
45         App.prototype = {
46                 nfcAdapter: null,
47                 addressBook: null,
48                 started: false,
49                 timeOutHandler: null,
50                 nfc: null,
51
52                 init: function appInit() {
53                         this.ui = new App.Ui(this);
54                         this.nfc = new App.NFCControl(this);
55
56                         this.ui.disableSelections();
57                         this.ui.defineEvents();
58                         this.initAddressBook();
59                         this.nfc.startNFC();
60                 },
61
62                 saveDefaultCard: function saveDefaultCard() {
63                         var elementSelected = $('#list-choose li.selected');
64                         localStorage.started = true;
65                         localStorage.id = elementSelected.attr('id');
66                         localStorage.firstName = elementSelected.attr('firstName');
67                         localStorage.lastName = elementSelected.attr('lastName');
68                         localStorage.phoneNumber = elementSelected.attr('phoneNumber');
69                         localStorage.vCard = elementSelected.attr('vCard');
70                         $.mobile.changePage('#start');
71                 },
72
73                 initAddressBook: function initAddressBook() {
74                         var addressBooksCB = function (addressbooks) {
75                                 if (addressbooks.length > 0) {
76                                         this.addressBook =  addressbooks[0];
77                                 } else {
78                                         console.error('initAddressBook failed');
79                                 }
80                         },
81                                 errorCB = function (e) {
82                                         console.log('problem with getAddressBooks() method: ' + e.message);
83                                 };
84
85                         try {
86                                 tizen.contact.getAddressBooks(addressBooksCB.bind(this), errorCB);
87                         } catch (e) {
88                                 console.error('problem with getAddressBooks() method: ' + e.message);
89                         }
90                 },
91
92                 countDown: function countDown(time, obj) {
93                         obj.text(time);
94                         if (time > 0) {
95                                 time -= 1;
96                                 this.timeOutHandler = setTimeout(function () { this.countDown(time, obj); }.bind(this), 1000);
97                         } else {
98                                 this.nfc.timeExpired();
99                         }
100                 },
101
102                 saveContact: function saveContact() {
103                         var phone, first, last, contactPage = $('#contact'), contact = null;
104                         phone = contactPage.data('contactsData').phone;
105                         first = contactPage.data('contactsData').first;
106                         last = contactPage.data('contactsData').last;
107
108                         try {
109                                 contact = new tizen.Contact({name: new tizen.ContactName({firstName: first, lastName: last}), phoneNumbers: [new tizen.ContactPhoneNumber(phone)]});
110                                 this.addressBook.add(contact);
111                                 this.app.ui.moveToStartPage('New contact added');
112                         } catch (err) {
113                                 this.app.ui.moveToStartPage('Problem with new contact adding');
114                                 console.error('The following error occurred while adding: ' + err.name);
115                         }
116                 },
117
118                 loadContacts: function loadContacts() {
119                         var self = this,
120                                 errorCB = function (e) {
121                                         console.error('loadContacts() problem with find() method: ' + e.message);
122                                 };
123                         this.addressBook.find(function (contacts) {
124                                 self.ui.showContactsList(contacts);
125                         }, errorCB);
126                 }
127
128         };
129
130 }());
131
132 app = new App();
133
134 $(document).ready(app.init.bind(app));