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