fix(contacts): fix few bugs in Contacts (#846)
This commit is contained in:
parent
abc90f2e6a
commit
b19f6d1ccd
@ -1,8 +1,10 @@
|
|||||||
import { Cordova, CordovaInstance, InstanceProperty, Plugin } from './plugin';
|
import { CordovaInstance, InstanceProperty, Plugin, getPromise } from './plugin';
|
||||||
|
|
||||||
declare var window: any,
|
declare var window: any,
|
||||||
navigator: any;
|
navigator: any;
|
||||||
|
|
||||||
|
export type ContactFieldType = 'addresses' | 'birthday' | 'categories' | 'country' | 'department' | 'displayName' | 'emails' | 'familyName' | 'formatted' | 'givenName' | 'honorificPrefix' | 'honorificSuffix' | 'id' | 'ims' | 'locality' | 'middleName' | 'name' | 'nickname' | 'note' | 'organizations' | 'phoneNumbers' | 'photos' | 'postalCode' | 'region' | 'streetAddress' | 'title' | 'urls';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
@ -73,8 +75,14 @@ export class Contact implements IContactProperties {
|
|||||||
@CordovaInstance()
|
@CordovaInstance()
|
||||||
remove(): Promise<any> { return; }
|
remove(): Promise<any> { return; }
|
||||||
|
|
||||||
@CordovaInstance()
|
save(): Promise<any> {
|
||||||
save(): Promise<any> { return; }
|
return getPromise((resolve, reject) => {
|
||||||
|
this._objectInstance.save((contact) => {
|
||||||
|
this._objectInstance = contact;
|
||||||
|
resolve(this);
|
||||||
|
}, reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -120,42 +128,30 @@ export interface IContactName {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
export class ContactName implements IContactName {
|
export class ContactName implements IContactName {
|
||||||
private _objectInstance: any;
|
constructor(public formatted?: string,
|
||||||
|
public familyName?: string,
|
||||||
constructor(formatted?: string, familyName?: string, givenName?: string, middleName?: string, honorificPrefix?: string, honorificSuffix?: string) {
|
public givenName?: string,
|
||||||
this._objectInstance = new window.ContactName(formatted, familyName, givenName, middleName, honorificPrefix, honorificSuffix);
|
public middleName?: string,
|
||||||
}
|
public honorificPrefix?: string,
|
||||||
|
public honorificSuffix?: string) {}
|
||||||
@InstanceProperty formatted: string;
|
|
||||||
@InstanceProperty familyName: string;
|
|
||||||
@InstanceProperty givenName: string;
|
|
||||||
@InstanceProperty middleName: string;
|
|
||||||
@InstanceProperty honorificPrefix: string;
|
|
||||||
@InstanceProperty honorificSuffix: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IContactField {
|
export interface IContactField {
|
||||||
/** A string that indicates what type of field this is, home for example. */
|
/** A string that indicates what type of field this is, home for example. */
|
||||||
type: string;
|
type?: string;
|
||||||
/** The value of the field, such as a phone number or email address. */
|
/** The value of the field, such as a phone number or email address. */
|
||||||
value: string;
|
value?: string;
|
||||||
/** Set to true if this ContactField contains the user's preferred value. */
|
/** Set to true if this ContactField contains the user's preferred value. */
|
||||||
pref: boolean;
|
pref?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
export class ContactField implements IContactField {
|
export class ContactField implements IContactField {
|
||||||
private _objectInstance: any;
|
constructor(public type?: string,
|
||||||
|
public value?: string,
|
||||||
constructor(type?: string, value?: string, pref?: boolean) {
|
public pref?: boolean) {}
|
||||||
this._objectInstance = new window.ContactField(type, value, pref);
|
|
||||||
}
|
|
||||||
|
|
||||||
@InstanceProperty type: string;
|
|
||||||
@InstanceProperty value: string;
|
|
||||||
@InstanceProperty pref: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IContactAddress {
|
export interface IContactAddress {
|
||||||
@ -181,35 +177,14 @@ export interface IContactAddress {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
export class ContactAddress implements IContactAddress {
|
export class ContactAddress implements IContactAddress {
|
||||||
private _objectInstance: any;
|
constructor(public pref?: boolean,
|
||||||
|
public type?: string,
|
||||||
constructor(pref?: boolean,
|
public formatted?: string,
|
||||||
type?: string,
|
public streetAddress?: string,
|
||||||
formatted?: string,
|
public locality?: string,
|
||||||
streetAddress?: string,
|
public region?: string,
|
||||||
locality?: string,
|
public postalCode?: string,
|
||||||
region?: string,
|
public country?: string) {}
|
||||||
postalCode?: string,
|
|
||||||
country?: string) {
|
|
||||||
this._objectInstance = new window.ContactAddress(pref, type, formatted, streetAddress, locality, region, postalCode, country);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Set to true if this ContactAddress contains the user's preferred value. */
|
|
||||||
@InstanceProperty pref: boolean;
|
|
||||||
/** A string indicating what type of field this is, home for example. */
|
|
||||||
@InstanceProperty type: string;
|
|
||||||
/** The full address formatted for display. */
|
|
||||||
@InstanceProperty formatted: string;
|
|
||||||
/** The full street address. */
|
|
||||||
@InstanceProperty streetAddress: string;
|
|
||||||
/** The city or locality. */
|
|
||||||
@InstanceProperty locality: string;
|
|
||||||
/** The state or region. */
|
|
||||||
@InstanceProperty region: string;
|
|
||||||
/** The zip code or postal code. */
|
|
||||||
@InstanceProperty postalCode: string;
|
|
||||||
/** The country name. */
|
|
||||||
@InstanceProperty country: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IContactOrganization {
|
export interface IContactOrganization {
|
||||||
@ -229,20 +204,13 @@ export interface IContactOrganization {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
export class ContactOrganization implements IContactOrganization {
|
export class ContactOrganization implements IContactOrganization {
|
||||||
private _objectInstance: any;
|
constructor(
|
||||||
constructor() {
|
public type?: string,
|
||||||
this._objectInstance = new window.ContactOrganization();
|
public name?: string,
|
||||||
}
|
public department?: string,
|
||||||
/** Set to true if this ContactOrganization contains the user's preferred value. */
|
public title?: string,
|
||||||
@InstanceProperty pref: boolean;
|
public pref?: boolean
|
||||||
/** A string that indicates what type of field this is, home for example. */
|
) {}
|
||||||
@InstanceProperty type: string;
|
|
||||||
/** The name of the organization. */
|
|
||||||
@InstanceProperty name: string;
|
|
||||||
/** The department the contract works for. */
|
|
||||||
@InstanceProperty department: string;
|
|
||||||
/** The contact's title at the organization. */
|
|
||||||
@InstanceProperty title: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Search options to filter navigator.contacts. */
|
/** Search options to filter navigator.contacts. */
|
||||||
@ -263,31 +231,10 @@ export interface IContactFindOptions {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
export class ContactFindOptions implements IContactFindOptions {
|
export class ContactFindOptions implements IContactFindOptions {
|
||||||
private _objectInstance: any;
|
constructor(public filter?: string,
|
||||||
|
public multiple?: boolean,
|
||||||
constructor() {
|
public desiredFields?: string[],
|
||||||
this._objectInstance = new window.ContactFindOptions();
|
public hasPhoneNumber?: boolean) {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The search string used to find navigator.contacts. (Default: "")
|
|
||||||
*/
|
|
||||||
@InstanceProperty filter: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines if the find operation returns multiple navigator.contacts. (Default: false)
|
|
||||||
*/
|
|
||||||
@InstanceProperty multiple: boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Contact fields to be returned back. If specified, the resulting Contact object only features values for these fields.
|
|
||||||
*/
|
|
||||||
@InstanceProperty desiredFields: any;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* (Android only): Filters the search to only return contacts with a phone number informed.
|
|
||||||
*/
|
|
||||||
@InstanceProperty hasPhoneNumber: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -302,11 +249,14 @@ export class ContactFindOptions implements IContactFindOptions {
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* let contact = new Contact();
|
* let contact = new Contact();
|
||||||
* contact.displayName = 'Mr. Ionitron';
|
*
|
||||||
|
* contact.name = new ContactName(null, 'Smith', 'John');
|
||||||
|
* contact.phoneNumbers = [new ContactField('mobile', '6471234567')];
|
||||||
* contact.save().then(
|
* contact.save().then(
|
||||||
* () => console.log('Contact saved!', contact),
|
* () => console.log('Contact saved!', contact),
|
||||||
* (error: any) => console.error('Error saving contact.', error)
|
* (error: any) => console.error('Error saving contact.', error)
|
||||||
* );
|
* );
|
||||||
|
*
|
||||||
* ```
|
* ```
|
||||||
* @interfaces
|
* @interfaces
|
||||||
* IContactProperties
|
* IContactProperties
|
||||||
@ -332,29 +282,46 @@ export class Contacts {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for contacts in the Contacts list.
|
* Search for contacts in the Contacts list.
|
||||||
* @param fields {string[]} Contact fields to be used as a search qualifier.
|
* @param fields {ContactFieldType[]} Contact fields to be used as a search qualifier.
|
||||||
* A zero-length contactFields parameter is invalid and results in ContactError.INVALID_ARGUMENT_ERROR.
|
* A zero-length contactFields parameter is invalid and results in ContactError.INVALID_ARGUMENT_ERROR.
|
||||||
* A contactFields value of "*" searches all contact fields.
|
* A contactFields value of "*" searches all contact fields.
|
||||||
*
|
*
|
||||||
* @param options {Object} the options to query with:
|
* @param options {IContactFindOptions} the options to query with:
|
||||||
* filter: The search string used to find navigator.contacts. (string) (Default: "")
|
* filter: The search string used to find navigator.contacts. (string) (Default: "")
|
||||||
* multiple: Determines if the find operation returns multiple navigator.contacts. (Boolean) (Default: false)
|
* multiple: Determines if the find operation returns multiple navigator.contacts. (Boolean) (Default: false)
|
||||||
* desiredFields: Contact fields to be returned back. If specified, the resulting Contact object only features values for these fields. (DOMString[]) [Optional]
|
* desiredFields: Contact fields to be returned back. If specified, the resulting Contact object only features values for these fields. (DOMString[]) [Optional]
|
||||||
* hasPhoneNumber(Android only): Filters the search to only return contacts with a phone number informed. (Boolean) (Default: false)
|
* hasPhoneNumber(Android only): Filters the search to only return contacts with a phone number informed. (Boolean) (Default: false)
|
||||||
*
|
*
|
||||||
* @returns {Promise<any>} Returns a Promise that resolves with the search results (an array of Contact objects)
|
* @returns {Promise<Contact[]>} Returns a Promise that resolves with the search results (an array of Contact objects)
|
||||||
*/
|
*/
|
||||||
@Cordova({
|
static find(fields: ContactFieldType[], options?: IContactFindOptions): Promise<Contact[]> {
|
||||||
successIndex: 1,
|
return getPromise((resolve, reject) => {
|
||||||
errorIndex: 2
|
navigator.contacts.find(fields, (contacts) => {
|
||||||
})
|
resolve(contacts.map(processContact));
|
||||||
static find(fields: string[], options?: any): Promise<any> { return; }
|
}, reject, options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select a single Contact.
|
* Select a single Contact.
|
||||||
* @returns {Promise<any>} Returns a Promise that resolves with the selected Contact
|
* @returns {Promise<Contact>} Returns a Promise that resolves with the selected Contact
|
||||||
*/
|
*/
|
||||||
@Cordova()
|
static pickContact(): Promise<Contact> {
|
||||||
static pickContact(): Promise<any> { return; }
|
return getPromise((resolve, reject) => {
|
||||||
|
navigator.contacts.pickContact((contact) => resolve(processContact(contact)), reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function processContact(contact) {
|
||||||
|
let newContact = new Contact();
|
||||||
|
for (let prop in contact) {
|
||||||
|
if (typeof contact[prop] === 'function') continue;
|
||||||
|
newContact[prop] = contact[prop];
|
||||||
|
}
|
||||||
|
return newContact;
|
||||||
|
}
|
||||||
|
@ -127,7 +127,10 @@ function callCordovaPlugin(pluginObj: any, methodName: string, args: any[], opts
|
|||||||
return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
|
return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPromise(cb) {
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
export function getPromise(cb) {
|
||||||
|
|
||||||
const tryNativePromise = () => {
|
const tryNativePromise = () => {
|
||||||
if (window.Promise) {
|
if (window.Promise) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user