1 /* global ko, formatPhoneNumber, moment */
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:
8 * uid: "JeremyMartinson15417543010",
9 * personId: "15417543010",
11 * firstName: "Jeremy",
12 * lastName: "Martinson"
13 * displayName :"Jeremy Martinson"
16 * number:"1-541-754-3010"
19 * email: "Jeremy.Martinson@gmail.com"
25 * streetAddress: "455 Larkspur Dr.",
27 * country: "California",
33 * @module PhoneApplication
36 var Phone = (function() {
41 if (typeof(tizen.phone) !== 'undefined') {
42 this.phone = tizen.phone;
46 this.contacts = ko.observableArray([]);
47 this.callHistory = ko.observableArray([]);
49 this.contactsAlphabetFilter = ko.observable("");
51 this.contactsComputed = ko.computed(function() {
52 if (self.contactsAlphabetFilter() !== "") {
53 return ko.utils.arrayFilter(self.contacts(), function(contact) {
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;
67 return name.toString().toLowerCase().trim().indexOf(
68 self.contactsAlphabetFilter().toString().toLowerCase().trim()) === 0;
73 return self.contacts();
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.
83 Phone.prototype.loadContacts = function(callback) {
85 var i, conntactsArrayLength;
88 self.phone.getContacts(0, function(contactsArray) {
90 contactsArray = self.formatContacts(contactsArray);
91 self.contacts(contactsArray);
96 console.log("Error(" + err.code + "): " + err.message);
103 callback("Phone API is not available.");
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
115 Phone.prototype.compareByLastName = function(left, right) {
117 var leftLastName = "Unknown";
118 if ( !!left.name && !!left.name.lastName && left.name.lastName !== "") {
119 leftLastName = left.name.lastName;
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;
126 rightLastName = rightLastName.toString().trim().toLowerCase();
127 return leftLastName === rightLastName ? 0 : (leftLastName < rightLastName) ? -1 : 1;
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
135 Phone.prototype.compareHistoryByDate = function(left, right) {
137 var monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
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));
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));
151 return leftDate === rightDate ? 0 : (leftDate > rightDate) ? -1 : 1;
155 * This method clear contacts array.
156 * @method clearContacts
158 Phone.prototype.clearContacts = function() {
161 self.contacts.removeAll();
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.
170 Phone.prototype.getContactById = function(id) {
173 var contact = ko.utils.arrayFirst(self.contacts(), function(contact) {
174 return contact.uid === id;
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.
184 Phone.prototype.getContactByPersonId = function(personId) {
187 var contact = ko.utils.arrayFirst(self.contacts(), function(contact) {
188 if (contact.personId === personId) {
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) {
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.
210 Phone.prototype.getContactByPhoneNumber = function(phoneNumber) {
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) {
219 for (var i = 0; i < contact.phoneNumbers.length; ++i) {
220 if ( !! contact.phoneNumbers[i].number && contact.phoneNumbers[i].number === phoneNumber) {
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
237 Phone.prototype.getDisplayNameStr = function(contact) {
240 if ( !! contact && !! contact.name) {
242 if ( !! contact.name.lastName && contact.name.lastName !== "") {
243 name.push(contact.name.lastName);
245 if ( !! contact.name.firstName && contact.name.firstName !== "") {
246 name.push(contact.name.firstName);
248 if (name.length === 0 && !! contact.name.displayName && contact.name.displayName !== "") {
249 name.push(contact.name.displayName);
251 return name.join(" ");
256 * This method downloads call history from tizen.phone. Using API method tizen.phone.getCallHistory.
258 * @method loadCallHistory
259 * @param callback {function(error)} Callback function called after method is finished. Parameter `error` will contain any error that was intercepted.
261 Phone.prototype.loadCallHistory = function(callback) {
265 var callHistoryArrayLength;
266 if ( !! self.phone) {
267 self.phone.getCallHistory(25, function(callHistoryArray) {
268 callHistoryArray = self.formatCallHistory(callHistoryArray);
270 self.callHistory(callHistoryArray);
275 console.log("Error(" + err.code + "): " + err.message);
282 callback("Phone API is not available.");
287 * This method prepares contact data to be displayed in html.
289 * @method formatContacts
290 * @param contactsList {Array} list of contacts
291 * @return {Array} list of contacts
293 Phone.prototype.formatContacts = function(contactsList) {
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;
302 if (!contactsList[i].addresses) {
303 contactsList[i].addresses = null;
306 if (!contactsList[i].emails) {
307 contactsList[i].emails = null;
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);
319 * This method prepares call history data to be displayed in html.
321 * @method formatCallHistory
322 * @param callHistoryList {Array} list of call history
323 * @return {Array} call history array
325 Phone.prototype.formatCallHistory = function(callHistoryList) {
327 var callHistoryListLength;
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");
335 callHistoryList[i].startTime = null;
338 return callHistoryList;
341 * This method clears call history.
342 * @method clearCallHistory
344 Phone.prototype.clearCallHistory = function() {
347 self.callHistory.removeAll();
348 self.callHistory([]);
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
356 Phone.prototype.getCallHistoryByPersonId = function(personId) {
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) {
371 window.__phone = undefined === window.__phone ? new Phone() : window.__phone;
372 return window.__phone;