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) {
54 if ( !! contact.name && !! contact.name.leftLastName) {
55 return contact.name.lastName.toString().toLowerCase().trim().indexOf(
56 self.contactsAlphabetFilter().toString().toLowerCase().trim()) === 0;
61 return self.contacts();
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.
71 Phone.prototype.loadContacts = function(callback) {
73 var i, conntactsArrayLength;
76 self.phone.getContacts(0, function(contactsArray) {
78 contactsArray = self.formatContacts(contactsArray);
79 self.contacts(contactsArray);
84 console.log("Error(" + err.code + "): " + err.message);
91 callback("Phone API is not available.");
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
103 Phone.prototype.compareByLastName = function(left, right) {
105 var leftLastName = "Unknown";
106 if ( !!left.name && !!left.name.lastName && left.name.lastName !== "") {
107 leftLastName = left.name.lastName;
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;
114 rightLastName = rightLastName.toString().trim().toLowerCase();
115 return leftLastName === rightLastName ? 0 : (leftLastName < rightLastName) ? -1 : 1;
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
123 Phone.prototype.compareHistoryByDate = function(left, right) {
125 var monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
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));
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));
139 return leftDate === rightDate ? 0 : (leftDate > rightDate) ? -1 : 1;
143 * This method clear contacts array.
144 * @method clearContacts
146 Phone.prototype.clearContacts = function() {
149 self.contacts.removeAll();
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.
158 Phone.prototype.getContactById = function(id) {
161 var contact = ko.utils.arrayFirst(self.contacts(), function(contact) {
162 return contact.uid === id;
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.
172 Phone.prototype.getContactByPersonId = function(personId) {
175 var contact = ko.utils.arrayFirst(self.contacts(), function(contact) {
176 if (contact.personId === personId) {
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) {
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.
198 Phone.prototype.getContactByPhoneNumber = function(phoneNumber) {
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) {
207 for (var i = 0; i < contact.phoneNumbers.length; ++i) {
208 if ( !! contact.phoneNumbers[i].number && contact.phoneNumbers[i].number === phoneNumber) {
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
225 Phone.prototype.getDisplayNameStr = function(contact) {
228 if ( !! contact && !! contact.name) {
230 if ( !! contact.name.lastName && contact.name.lastName !== "") {
231 name.push(contact.name.lastName);
233 if ( !! contact.name.firstName && contact.name.firstName !== "") {
234 name.push(contact.name.firstName);
236 if (name.length === 0 && !! contact.name.displayName && contact.name.displayName !== "") {
237 name.push(contact.name.displayName);
239 return name.join(" ");
244 * This method downloads call history from tizen.phone. Using API method tizen.phone.getCallHistory.
246 * @method loadCallHistory
247 * @param callback {function(error)} Callback function called after method is finished. Parameter `error` will contain any error that was intercepted.
249 Phone.prototype.loadCallHistory = function(callback) {
253 var callHistoryArrayLength;
254 if ( !! self.phone) {
255 self.phone.getCallHistory(25, function(callHistoryArray) {
256 callHistoryArray = self.formatCallHistory(callHistoryArray);
258 self.callHistory(callHistoryArray);
263 console.log("Error(" + err.code + "): " + err.message);
270 callback("Phone API is not available.");
275 * This method prepares contact data to be displayed in html.
277 * @method formatContacts
278 * @param contactsList {Array} list of contacts
279 * @return {Array} list of contacts
281 Phone.prototype.formatContacts = function(contactsList) {
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;
290 if (!contactsList[i].addresses) {
291 contactsList[i].addresses = null;
294 if (!contactsList[i].emails) {
295 contactsList[i].emails = null;
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);
307 * This method prepares call history data to be displayed in html.
309 * @method formatCallHistory
310 * @param callHistoryList {Array} list of call history
311 * @return {Array} call history array
313 Phone.prototype.formatCallHistory = function(callHistoryList) {
315 var callHistoryListLength;
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");
323 callHistoryList[i].startTime = null;
326 return callHistoryList;
329 * This method clears call history.
330 * @method clearCallHistory
332 Phone.prototype.clearCallHistory = function() {
335 self.callHistory.removeAll();
336 self.callHistory([]);
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
344 Phone.prototype.getCallHistoryByPersonId = function(personId) {
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) {
359 window.__phone = undefined === window.__phone ? new Phone() : window.__phone;
360 return window.__phone;