cordova-android/framework/assets/js/contact.js

323 lines
10 KiB
JavaScript
Raw Normal View History

/*
2010-10-19 04:05:36 +08:00
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2005-2010, Nitobi Software Inc.
2010-10-19 04:05:36 +08:00
* Copyright (c) 2010, IBM Corporation
*/
/**
* Contains information about a single contact.
* @param {DOMString} id unique identifier
* @param {DOMString} displayName
* @param {ContactName} name
* @param {DOMString} nickname
* @param {ContactField[]} phoneNumbers array of phone numbers
* @param {ContactField[]} emails array of email addresses
* @param {ContactAddress[]} addresses array of addresses
* @param {ContactField[]} ims instant messaging user ids
* @param {ContactOrganization[]} organizations
* @param {DOMString} published date contact was first created
* @param {DOMString} updated date contact was last updated
* @param {DOMString} birthday contact's birthday
* @param (DOMString} anniversary contact's anniversary
* @param {DOMString} gender contact's gender
* @param {DOMString} note user notes about contact
* @param {DOMString} preferredUsername
* @param {ContactField[]} photos
* @param {ContactField[]} tags
* @param {ContactField[]} relationships
* @param {ContactField[]} urls contact's web sites
* @param {ContactAccounts[]} accounts contact's online accounts
* @param {DOMString} utcOffset UTC time zone offset
* @param {DOMString} connected
*/
var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, addresses,
ims, organizations, published, updated, birthday, anniversary, gender, note,
preferredUsername, photos, tags, relationships, urls, accounts, utcOffset, connected) {
2010-09-24 23:43:10 +08:00
this.id = id || null;
this.rawId = null;
2010-09-24 23:43:10 +08:00
this.displayName = displayName || null;
this.name = name || null; // ContactName
2010-09-24 23:43:10 +08:00
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[]
2010-09-24 23:43:10 +08:00
this.published = published || null;
this.updated = updated || null;
this.birthday = birthday || null;
this.anniversary = anniversary || null;
this.gender = gender || null;
this.note = note || null;
this.preferredUsername = preferredUsername || null;
this.photos = photos || null; // ContactField[]
this.tags = tags || null; // ContactField[]
this.relationships = relationships || null; // ContactField[]
this.urls = urls || null; // ContactField[]
this.accounts = accounts || null; // ContactAccount[]
2010-09-24 23:43:10 +08:00
this.utcOffset = utcOffset || null;
this.connected = connected || null;
};
2010-10-19 04:05:36 +08:00
/**
* Removes contact from device storage.
* @param successCB success callback
* @param errorCB error callback
*/
2010-10-01 11:09:59 +08:00
Contact.prototype.remove = function(successCB, errorCB) {
if (this.id == null) {
var errorObj = new ContactError();
errorObj.code = ContactError.NOT_FOUND_ERROR;
errorCB(errorObj);
}
PhoneGap.exec(successCB, errorCB, "Contacts", "remove", [this.id]);
};
2010-10-19 04:05:36 +08:00
/**
* 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 = PhoneGap.clone(this);
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.tags) {
for (i=0; i<clonedContact.tags.length; i++) {
clonedContact.tags[i].id = null;
}
}
if (clonedContact.relationships) {
for (i=0; i<clonedContact.relationships.length; i++) {
clonedContact.relationships[i].id = null;
}
}
if (clonedContact.urls) {
for (i=0; i<clonedContact.urls.length; i++) {
clonedContact.urls[i].id = null;
}
}
return clonedContact;
};
2010-10-19 04:05:36 +08:00
/**
* Persists contact to device storage.
* @param successCB success callback
* @param errorCB error callback
*/
Contact.prototype.save = function(successCB, errorCB) {
PhoneGap.exec(successCB, errorCB, "Contacts", "save", [this]);
};
2010-10-19 04:05:36 +08:00
/**
* Contact name.
* @param formatted
* @param familyName
* @param givenName
* @param middle
* @param prefix
* @param suffix
*/
var ContactName = function(formatted, familyName, givenName, middle, prefix, suffix) {
2010-09-24 23:43:10 +08:00
this.formatted = formatted || null;
this.familyName = familyName || null;
this.givenName = givenName || null;
this.middleName = middle || null;
this.honorificPrefix = prefix || null;
this.honorificSuffix = suffix || null;
};
2009-12-02 03:48:10 +08:00
2010-10-19 04:05:36 +08:00
/**
* Generic contact field.
* @param {DOMString} id unique identifier, should only be set by native code
2010-10-19 04:05:36 +08:00
* @param type
* @param value
* @param primary
*/
var ContactField = function(type, value, primary) {
this.id = null;
2010-09-24 23:43:10 +08:00
this.type = type || null;
this.value = value || null;
this.primary = primary || null;
};
2010-10-19 04:05:36 +08:00
/**
* Contact address.
* @param {DOMString} id unique identifier, should only be set by native code
2010-10-19 04:05:36 +08:00
* @param formatted
* @param streetAddress
* @param locality
* @param region
* @param postalCode
* @param country
*/
var ContactAddress = function(formatted, streetAddress, locality, region, postalCode, country) {
this.id = null;
2010-09-24 23:43:10 +08:00
this.formatted = formatted || null;
this.streetAddress = streetAddress || null;
this.locality = locality || null;
this.region = region || null;
this.postalCode = postalCode || null;
this.country = country || null;
};
2010-10-19 04:05:36 +08:00
/**
* Contact organization.
* @param {DOMString} id unique identifier, should only be set by native code
2010-10-19 04:05:36 +08:00
* @param name
* @param dept
* @param title
* @param startDate
* @param endDate
* @param location
* @param desc
*/
var ContactOrganization = function(name, dept, title, startDate, endDate, location, desc) {
this.id = null;
2010-09-24 23:43:10 +08:00
this.name = name || null;
this.department = dept || null;
this.title = title || null;
this.startDate = startDate || null;
this.endDate = endDate || null;
this.location = location || null;
this.description = desc || null;
};
2010-10-19 04:05:36 +08:00
/**
* Contact account.
* @param {DOMString} id unique identifier, should only be set by native code
2010-10-19 04:05:36 +08:00
* @param domain
* @param username
* @param userid
*/
var ContactAccount = function(domain, username, userid) {
this.id = null;
2010-09-24 23:43:10 +08:00
this.domain = domain || null;
this.username = username || null;
this.userid = userid || null;
}
2010-10-19 04:05:36 +08:00
/**
* Represents a group of Contacts.
*/
var Contacts = function() {
this.inProgress = false;
2010-09-17 22:24:22 +08:00
this.records = new Array();
}
2010-10-19 04:05:36 +08:00
/**
* Returns an array of Contacts matching the search criteria.
* @param fields that should be searched
2010-10-19 04:05:36 +08:00
* @param successCB success callback
* @param errorCB error callback
* @param {ContactFindOptions} options that can be applied to contact searching
* @return array of Contacts matching search criteria
*/
Contacts.prototype.find = function(fields, successCB, errorCB, options) {
2010-11-19 00:45:36 +08:00
PhoneGap.exec(successCB, errorCB, "Contacts", "search", [fields, options]);
};
2010-10-19 04:05:36 +08:00
/**
* This function creates a new contact, but it does not persist the contact
* to device storage. To persist the contact to device storage, invoke
* contact.save().
* @param properties an object who's properties will be examined to create a new Contact
* @returns new Contact object
*/
2010-10-02 05:10:24 +08:00
Contacts.prototype.create = function(properties) {
var contact = new Contact();
for (i in properties) {
if (contact[i]!='undefined') {
contact[i]=properties[i];
}
}
return contact;
2010-10-02 04:55:20 +08:00
};
/**
* This function returns and array of contacts. It is required as we need to convert raw
* JSON objects into concrete Contact objects. Currently this method is called after
* navigator.service.contacts.find but before the find methods success call back.
*
* @param jsonArray an array of JSON Objects that need to be converted to Contact objects.
* @returns an array of Contact objects
*/
2010-11-19 00:45:36 +08:00
Contacts.prototype.cast = function(pluginResult) {
var contacts = new Array();
2010-11-19 00:45:36 +08:00
for (var i=0; i<pluginResult.message.length; i++) {
contacts.push(navigator.service.contacts.create(pluginResult.message[i]));
}
2010-11-19 00:45:36 +08:00
pluginResult.message = contacts;
return pluginResult;
}
2010-10-19 04:05:36 +08:00
/**
* ContactFindOptions.
* @param filter used to match contacts against
* @param multiple boolean used to determine if more than one contact should be returned
* @param limit maximum number of results to return from the contacts search
* @param updatedSince return only contact records that have been updated on or after the given time
*/
var ContactFindOptions = function(filter, multiple, limit, updatedSince) {
this.filter = filter || '';
this.multiple = multiple || false;
this.limit = limit || 1;
this.updatedSince = updatedSince || '';
};
2010-10-19 04:05:36 +08:00
/**
* ContactError.
* An error code assigned by an implementation when an error has occurred
*/
var ContactError = function() {
this.code=null;
};
/**
2010-10-19 04:05:36 +08:00
* Error codes
*/
2010-10-01 11:09:59 +08:00
ContactError.UNKNOWN_ERROR = 0;
ContactError.INVALID_ARGUMENT_ERROR = 1;
ContactError.NOT_FOUND_ERROR = 2;
2010-10-01 11:09:59 +08:00
ContactError.TIMEOUT_ERROR = 3;
ContactError.PENDING_OPERATION_ERROR = 4;
2010-10-01 11:09:59 +08:00
ContactError.IO_ERROR = 5;
ContactError.NOT_SUPPORTED_ERROR = 6;
ContactError.PERMISSION_DENIED_ERROR = 20;
2010-10-19 04:05:36 +08:00
/**
* Add the contact interface into the browser.
*/
PhoneGap.addConstructor(function() {
2010-09-15 02:51:31 +08:00
if(typeof navigator.service == "undefined") navigator.service = new Object();
if(typeof navigator.service.contacts == "undefined") navigator.service.contacts = new Contacts();
});