| var exec = require('cordova/exec'), |
| ContactError = require('cordova/plugin/ContactError'), |
| utils = require('cordova/utils'); |
| |
| /** |
| * Converts primitives into Complex Object |
| * Currently only used for Date fields |
| */ |
| function convertIn(contact) { |
| var value = contact.birthday; |
| try { |
| contact.birthday = new Date(parseFloat(value)); |
| } catch (exception){ |
| console.log("Cordova Contact convertIn error: exception creating date."); |
| } |
| return contact; |
| } |
| |
| /** |
| * Converts Complex objects into primitives |
| * Only conversion at present is for Dates. |
| **/ |
| |
| function convertOut(contact) { |
| var value = contact.birthday; |
| if (value !== null) { |
| // try to make it a Date object if it is not already |
| if (!utils.isDate(value)){ |
| try { |
| value = new Date(value); |
| } catch(exception){ |
| value = null; |
| } |
| } |
| if (utils.isDate(value)){ |
| value = value.valueOf(); // convert to milliseconds |
| } |
| contact.birthday = value; |
| } |
| return contact; |
| } |
| |
| /** |
| * Contains information about a single contact. |
| * @constructor |
| * @param {DOMString} id unique identifier |
| * @param {DOMString} displayName |
| * @param {ContactName} name |
| * @param {DOMString} nickname |
| * @param {Array.<ContactField>} phoneNumbers array of phone numbers |
| * @param {Array.<ContactField>} emails array of email addresses |
| * @param {Array.<ContactAddress>} addresses array of addresses |
| * @param {Array.<ContactField>} ims instant messaging user ids |
| * @param {Array.<ContactOrganization>} organizations |
| * @param {DOMString} birthday contact's birthday |
| * @param {DOMString} note user notes about contact |
| * @param {Array.<ContactField>} photos |
| * @param {Array.<ContactField>} categories |
| * @param {Array.<ContactField>} urls contact's web sites |
| */ |
| var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, addresses, |
| ims, organizations, birthday, note, photos, categories, urls) { |
| this.id = id || null; |
| this.rawId = null; |
| this.displayName = displayName || null; |
| this.name = name || null; // ContactName |
| this.nickname = nickname || null; |
| this.phoneNumbers = phoneNumbers || null; // ContactField[] |
| this.emails = emails || null; // ContactField[] |
| this.addresses = addresses || null; // ContactAddress[] |
| this.ims = ims || null; // ContactField[] |
| this.organizations = organizations || null; // ContactOrganization[] |
| this.birthday = birthday || null; |
| this.note = note || null; |
| this.photos = photos || null; // ContactField[] |
| this.categories = categories || null; // ContactField[] |
| this.urls = urls || null; // ContactField[] |
| }; |
| |
| /** |
| * Removes contact from device storage. |
| * @param successCB success callback |
| * @param errorCB error callback |
| */ |
| Contact.prototype.remove = function(successCB, errorCB) { |
| var fail = function(code) { |
| errorCB(new ContactError(code)); |
| }; |
| if (this.id === null) { |
| fail(ContactError.UNKNOWN_ERROR); |
| } |
| else { |
| exec(successCB, fail, "Contacts", "remove", [this.id]); |
| } |
| }; |
| |
| /** |
| * Creates a deep copy of this Contact. |
| * With the contact ID set to null. |
| * @return copy of this Contact |
| */ |
| Contact.prototype.clone = function() { |
| var clonedContact = utils.clone(this); |
| var i; |
| clonedContact.id = null; |
| clonedContact.rawId = null; |
| // Loop through and clear out any id's in phones, emails, etc. |
| if (clonedContact.phoneNumbers) { |
| for (i = 0; i < clonedContact.phoneNumbers.length; i++) { |
| clonedContact.phoneNumbers[i].id = null; |
| } |
| } |
| if (clonedContact.emails) { |
| for (i = 0; i < clonedContact.emails.length; i++) { |
| clonedContact.emails[i].id = null; |
| } |
| } |
| if (clonedContact.addresses) { |
| for (i = 0; i < clonedContact.addresses.length; i++) { |
| clonedContact.addresses[i].id = null; |
| } |
| } |
| if (clonedContact.ims) { |
| for (i = 0; i < clonedContact.ims.length; i++) { |
| clonedContact.ims[i].id = null; |
| } |
| } |
| if (clonedContact.organizations) { |
| for (i = 0; i < clonedContact.organizations.length; i++) { |
| clonedContact.organizations[i].id = null; |
| } |
| } |
| if (clonedContact.categories) { |
| for (i = 0; i < clonedContact.categories.length; i++) { |
| clonedContact.categories[i].id = null; |
| } |
| } |
| if (clonedContact.photos) { |
| for (i = 0; i < clonedContact.photos.length; i++) { |
| clonedContact.photos[i].id = null; |
| } |
| } |
| if (clonedContact.urls) { |
| for (i = 0; i < clonedContact.urls.length; i++) { |
| clonedContact.urls[i].id = null; |
| } |
| } |
| return clonedContact; |
| }; |
| |
| /** |
| * Persists contact to device storage. |
| * @param successCB success callback |
| * @param errorCB error callback |
| */ |
| Contact.prototype.save = function(successCB, errorCB) { |
| var fail = function(code) { |
| errorCB(new ContactError(code)); |
| }; |
| var success = function(result) { |
| if (result) { |
| if (typeof successCB === 'function') { |
| var fullContact = require('cordova/plugin/contacts').create(result); |
| successCB(convertIn(fullContact)); |
| } |
| } |
| else { |
| // no Entry object returned |
| fail(ContactError.UNKNOWN_ERROR); |
| } |
| }; |
| var dupContact = convertOut(utils.clone(this)); |
| exec(success, fail, "Contacts", "save", [dupContact]); |
| }; |
| |
| |
| module.exports = Contact; |