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.defineEvents();
57                         this.initAddressBook();
58                         this.nfc.startNFC();
59                 },
60
61                 saveDefaultCard: function saveDefaultCard() {
62                         var elementSelected = $('#list-choose li.selected');
63                         console.log(elementSelected);
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                                 console.log('addressBooksCB success');
76                                 if (addressbooks.length > 0) {
77                                         this.addressBook =  addressbooks[0];
78                                 } else {
79                                         console.log('addressBook: failed');
80                                 }
81                         },
82                                 errorCB = function (e) {
83                                         console.log('problem with getAddressBooks() method: ' + e.message);
84                                 };
85
86                         try {
87                                 tizen.contact.getAddressBooks(addressBooksCB.bind(this), errorCB);
88                         } catch (e) {
89                                 console.log('problem with getAddressBooks() method: ' + e.message);
90                         }
91                 },
92
93                 countDown: function countDown(time, obj) {
94                         obj.text(time);
95                         if (time > 0) {
96                                 time -= 1;
97                                 this.timeOutHandler = setTimeout(function () { this.countDown(time, obj); }.bind(this), 1000);
98                         } else {
99                                 this.nfc.timeExpired();
100                         }
101                 },
102
103                 saveContact: function saveContact() {
104                         var phone, first, last, contactPage = $('#contact'), contact = null;
105                         phone = contactPage.data('contactsData').phone;
106                         first = contactPage.data('contactsData').first;
107                         last = contactPage.data('contactsData').last;
108
109                         try {
110                                 contact = new tizen.Contact({name: new tizen.ContactName({firstName: first, lastName: last}), phoneNumbers: [new tizen.ContactPhoneNumber(phone)]});
111                                 this.addressBook.add(contact);
112                                 this.app.ui.moveToStartPage('New contact added');
113                         } catch (err) {
114                                 this.app.ui.moveToStartPage('Problem with new contact adding');
115                                 console.log('The following error occurred while adding: ' + err.name);
116                         }
117                 },
118
119                 loadContacts: function loadContacts() {
120                         var self = this,
121                                 errorCB = function (e) {
122                                         console.log('problem with find() method: ' + e.message);
123                                 };
124                         this.addressBook.find(function (contacts) {
125                                 self.ui.showContactsList(contacts);
126                         }, errorCB);
127                 }
128
129         };
130
131 }());
132
133 app = new App();
134
135 $(document).ready(app.init.bind(app));