35ebeddab2404cc14ad0fad4960a9932468df9a0
[profile/ivi/Modello_Phone.git] / js / phone.js
1 /* global ko, formatPhoneNumber, moment */
2
3 /**
4  * This class provides methods to operate with contacts and call history from [tizen.phone](./native/group__phoned.html).
5  * Sample contact object looks like:
6  *
7  *     {
8  *          uid: "JeremyMartinson15417543010",
9  *          personId: "15417543010",
10  *                      name: {
11  *                              firstName: "Jeremy",
12  *                              lastName: "Martinson"
13  *                              displayName :"Jeremy Martinson"
14  *                      },
15  *                      phoneNumbers: {
16  *                              number:"1-541-754-3010"
17  *                      },
18  *                      emails: {
19  *                              email: "Jeremy.Martinson@gmail.com"
20  *                      },
21  *                      photoURI: {
22  *                              photoURI: "url://"
23  *                      },
24  *                      addresses: {
25  *                              streetAddress: "455 Larkspur Dr.",
26  *                              city: "San Jose",
27  *                              country: "California",
28  *                              postalCode: " "
29  *                      }
30  *     }
31  *
32  * @class Phone
33  * @module PhoneApplication
34  * @constructor
35  */
36 var Phone = (function() {
37         "use strict";
38
39         function Phone() {
40                 var self = this;
41                 if (typeof(tizen.phone) !== 'undefined') {
42                         this.phone = tizen.phone;
43                 } else {
44                         this.phone = null;
45                 }
46                 this.contacts = ko.observableArray([]);
47                 this.callHistory = ko.observableArray([]);
48
49                 this.contactsAlphabetFilter = ko.observable("");
50
51                 this.contactsComputed = ko.computed(function() {
52                         if (self.contactsAlphabetFilter() !== "") {
53                                 return ko.utils.arrayFilter(self.contacts(), function(contact) {
54                                         if ( !! contact.name && !! contact.name.leftLastName) {
55                                                 return contact.name.lastName.toString().toLowerCase().trim().indexOf(
56                                                         self.contactsAlphabetFilter().toString().toLowerCase().trim()) === 0;
57                                         }
58                                         return false;
59                                 });
60                         }
61                         return self.contacts();
62                 });
63         }
64
65 /**
66  * This method download contacts from tizen.phone. Using  API method tizen.phone.getContacts.
67  * @method loadContacts
68  * @param callback {function(error)} Callback function called after method is finished. Parameter `error` will contain any error that was intercepted.
69  */
70
71         Phone.prototype.loadContacts = function(callback) {
72                 var self = this;
73                 var i, conntactsArrayLength;
74                 if ( !! self.phone) {
75
76                         self.phone.getContacts(0, function(contactsArray) {
77
78                                 contactsArray = self.formatContacts(contactsArray);
79                                 self.contacts(contactsArray);
80                                 if ( !! callback) {
81                                         callback(null);
82                                 }
83                         }, function(err) {
84                                 console.log("Error(" + err.code + "): " + err.message);
85                                 if ( !! callback) {
86                                         callback(err);
87                                 }
88                         });
89                 } else {
90                         if ( !! callback) {
91                                 callback("Phone API is not available.");
92                         }
93                 }
94         };
95
96         /**
97          * This method provides compare contact by last name, it is use in observable array from [knockout library](http://knockoutjs.com/documentation/observableArrays.html).
98          * @method compareByLastName
99          * @param left {Object} left compare element
100          * @param right {Object} right compare element
101          */
102
103         Phone.prototype.compareByLastName = function(left, right) {
104
105                 var leftLastName = "Unknown";
106                 if ( !!left.name && !!left.name.lastName && left.name.lastName !== "") {
107                         leftLastName = left.name.lastName;
108                 }
109                 leftLastName = leftLastName.toString().trim().toLowerCase();
110                 var rightLastName = "Unknown";
111                 if ( !!right.name && !!right.name.lastName && right.name.lastName !== "") {
112                         rightLastName = right.name.lastName;
113                 }
114                 rightLastName = rightLastName.toString().trim().toLowerCase();
115                 return leftLastName === rightLastName ? 0 : (leftLastName < rightLastName) ? -1 : 1;
116         };
117         /**
118          * This method compares call history items by date, it is used in observable array from [knockout library](http://knockoutjs.com/documentation/observableArrays.html).
119          * @method compareHistoryByDate
120          * @param left {Object} left compare element
121          * @param right {Object} right compare element
122          */
123         Phone.prototype.compareHistoryByDate = function(left, right) {
124
125                 var monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
126
127                 var tmpLeftDateTime = left.startTime.toUpperCase().split(" ");
128                 var tmpLeftDate = tmpLeftDateTime[0].split("/");
129                 tmpLeftDate[0] = tmpLeftDate[0].indexOf(tmpLeftDate[0]) + 1;
130                 var tmpLeftTime = tmpLeftDateTime[1].split(":");
131                 var leftDate = new Date(parseInt(tmpLeftDate[0], 10), parseInt(tmpLeftDate[1], 10), parseInt(tmpLeftDate[2], 10), parseInt(tmpLeftTime[0], 10), parseInt(tmpLeftTime[1], 10));
132
133                 var tmpRightDateTime = right.startTime.toUpperCase().split(" ");
134                 var tmpRightDate = tmpRightDateTime[0].split("/");
135                 tmpRightDate[0] = tmpRightDate[0].indexOf(tmpRightDate[0]) + 1;
136                 var tmpRightTime = tmpRightDateTime[1].split(":");
137                 var rightDate = new Date(parseInt(tmpRightDate[0], 10), parseInt(tmpRightDate[1], 10), parseInt(tmpRightDate[2], 10), parseInt(tmpRightTime[0], 10), parseInt(tmpRightTime[1], 10));
138
139                 return leftDate === rightDate ? 0 : (leftDate > rightDate) ? -1 : 1;
140
141         };
142         /**
143          * This method clear contacts array.
144          * @method clearContacts
145          */
146         Phone.prototype.clearContacts = function() {
147
148                 var self = this;
149                 self.contacts.removeAll();
150                 self.contacts([]);
151         };
152         /**
153          * This method searches contact in contacts list by id .
154          * @method getContactById
155          * @param id {String} search contact id
156          * @return {Object} if contact is found, return contact. Else return first element from contacts list.
157          */
158         Phone.prototype.getContactById = function(id) {
159
160                 var self = this;
161                 var contact = ko.utils.arrayFirst(self.contacts(), function(contact) {
162                         return contact.uid === id;
163                 });
164                 return contact;
165         };
166         /**
167          * This method searches contact in contacts list by personId.
168          * @method getContactByPersonId
169          * @param id {String} search contact personId
170          * @return {Object} if contact is found, return contact. Else return first element from contacts list.
171          */
172         Phone.prototype.getContactByPersonId = function(personId) {
173
174                 var self = this;
175                 var contact = ko.utils.arrayFirst(self.contacts(), function(contact) {
176                         if (contact.personId === personId) {
177                                 return true;
178                         }
179
180                         if ( !! contact.phoneNumbers && contact.phoneNumbers.length) {
181                                 for (var i = 0; i < contact.phoneNumbers.length; ++i) {
182                                         if ( !! contact.phoneNumbers[i].number && contact.phoneNumbers[i].number === personId) {
183                                                 return true;
184                                         }
185                                 }
186                         }
187
188                         return false;
189                 });
190                 return contact;
191         };
192         /**
193          * This method searches contact in contacts list by phoneNumber.
194          * @method getContactByPhoneNumber
195          * @param id {String} search contact phoneNumber
196          * @return {Object} if contact is found, return contact. Else return first element from contacts list.
197          */
198         Phone.prototype.getContactByPhoneNumber = function(phoneNumber) {
199
200                 var self = this;
201                 if ( !! phoneNumber && phoneNumber !== "") {
202                         phoneNumber = formatPhoneNumber(phoneNumber);
203                         var foundContact = ko.utils.arrayFirst(self.contacts(), function(contact) {
204                                 if ( !! contact.phoneNumbers && contact.phoneNumbers.length) {
205
206
207                                         for (var i = 0; i < contact.phoneNumbers.length; ++i) {
208                                                 if ( !! contact.phoneNumbers[i].number && contact.phoneNumbers[i].number === phoneNumber) {
209                                                         return true;
210                                                 }
211                                         }
212                                 }
213                                 return false;
214                         });
215                         return foundContact;
216                 }
217                 return null;
218         };
219         /**
220          * This method composes display name from contact names.
221          * @method getDisplayNameStr
222          * @param contact {Object} contact object
223          * @return {String} contact name for display
224          */
225         Phone.prototype.getDisplayNameStr = function(contact) {
226
227                 var self = this;
228                 if ( !! contact && !! contact.name) {
229                         var name = [];
230                         if ( !! contact.name.lastName && contact.name.lastName !== "") {
231                                 name.push(contact.name.lastName);
232                         }
233                         if ( !! contact.name.firstName && contact.name.firstName !== "") {
234                                 name.push(contact.name.firstName);
235                         }
236                         if (name.length === 0 && !! contact.name.displayName && contact.name.displayName !== "") {
237                                 name.push(contact.name.displayName);
238                         }
239                         return name.join(" ");
240                 }
241                 return "Unknown";
242         };
243         /**
244  * This method downloads call history from tizen.phone. Using  API method tizen.phone.getCallHistory.
245
246  * @method loadCallHistory
247  * @param callback {function(error)} Callback function called after method is finished. Parameter `error` will contain any error that was intercepted.
248  */
249         Phone.prototype.loadCallHistory = function(callback) {
250
251                 var self = this;
252                 var i;
253                 var callHistoryArrayLength;
254                 if ( !! self.phone) {
255                         self.phone.getCallHistory(25, function(callHistoryArray) {
256                                 callHistoryArray = self.formatCallHistory(callHistoryArray);
257
258                                 self.callHistory(callHistoryArray);
259                                 if ( !! callback) {
260                                         callback(null);
261                                 }
262                         }, function(err) {
263                                 console.log("Error(" + err.code + "): " + err.message);
264                                 if ( !! callback) {
265                                         callback(err);
266                                 }
267                         });
268                 } else {
269                         if ( !! callback) {
270                                 callback("Phone API is not available.");
271                         }
272                 }
273         };
274         /**
275  * This method prepares contact data to be displayed in html.
276
277  * @method formatContacts
278  * @param contactsList {Array} list of contacts
279  * @return {Array} list of contacts
280  */
281         Phone.prototype.formatContacts = function(contactsList) {
282                 var i, j;
283                 var contactsListLength;
284                 var phoneNumbersLength;
285                 contactsListLength = contactsList.length;
286                 for (i = 0; i < contactsListLength; i++) {
287                         if (!contactsList[i].photoURI) {
288                                 contactsList[i].photoURI = null;
289                         }
290                         if (!contactsList[i].addresses) {
291                                 contactsList[i].addresses = null;
292                         }
293
294                         if (!contactsList[i].emails) {
295                                 contactsList[i].emails = null;
296                         }
297                         if ( !! contactsList[i].phoneNumbers) {
298                                 phoneNumbersLength = contactsList[i].phoneNumbers.length;
299                                 for (j = 0; j < phoneNumbersLength; j++) {
300                                         contactsList[i].phoneNumbers[j].number = formatPhoneNumber(contactsList[i].phoneNumbers[j].number);
301                                 }
302                         }
303                 }
304                 return contactsList;
305         };
306         /**
307  * This method prepares call history data to be displayed in html.
308
309  * @method formatCallHistory
310  * @param callHistoryList {Array} list of call history
311  * @return {Array} call history array
312  */
313         Phone.prototype.formatCallHistory = function(callHistoryList) {
314
315                 var callHistoryListLength;
316                 var i;
317                 callHistoryListLength = callHistoryList.length;
318                 for (i = 0; i < callHistoryListLength; i++) {
319                         if ( !! callHistoryList[i].startTime) {
320                                 var date = callHistoryList[i].startTime;
321                                 callHistoryList[i].startTime = moment(date).format("MMM/DD/YYYY HH:mm");
322                         } else {
323                                 callHistoryList[i].startTime = null;
324                         }
325                 }
326                 return callHistoryList;
327         };
328         /**
329          * This method clears call history.
330          * @method clearCallHistory
331          */
332         Phone.prototype.clearCallHistory = function() {
333
334                 var self = this;
335                 self.callHistory.removeAll();
336                 self.callHistory([]);
337         };
338         /**
339          * This method searches call history by contact personID.
340          * @method getCallHistoryByPersonId
341          * @param personId {string} search contact personId
342          * @return {Array} call history array
343          */
344         Phone.prototype.getCallHistoryByPersonId = function(personId) {
345
346                 var self = this;
347                 var callHistory = ko.utils.arrayFilter(self.callHistory(), function(callHistoryEntry) {
348                         if ( !! callHistoryEntry.remoteParties) {
349                                 for (var i = 0; i < callHistoryEntry.remoteParties.length; ++i) {
350                                         if (callHistoryEntry.remoteParties[i].personId === personId) {
351                                                 return true;
352                                         }
353                                 }
354                         }
355                         return false;
356                 });
357                 return callHistory;
358         };
359         window.__phone = undefined === window.__phone ? new Phone() : window.__phone;
360         return window.__phone;
361 })();