chore(): optimize CordovaProperty and InstanceProperty decorators (#635)

* chore(): optimize InstanceProperty wrapper

* chore(): optimize CordovaProperty wrapper

* tslint'

* fix decorators
This commit is contained in:
Ibrahim Hadeed 2016-12-04 11:42:30 -05:00 committed by GitHub
parent 717c1438c4
commit abc90f2e6a
10 changed files with 87 additions and 88 deletions

View File

@ -57,7 +57,7 @@ export class AppRate {
* See table below for options * See table below for options
*/ */
@CordovaProperty @CordovaProperty
static get preferences() { return window.AppRate.preferences; } static preferences;
/** /**
* Prompts the user for rating * Prompts the user for rating

View File

@ -42,20 +42,20 @@ export interface IContactProperties {
*/ */
export class Contact implements IContactProperties { export class Contact implements IContactProperties {
private _objectInstance: any; private _objectInstance: any;
@InstanceProperty get id(): string { return; } @InstanceProperty id: string;
@InstanceProperty get displayName(): string { return; } @InstanceProperty displayName: string;
@InstanceProperty get name(): IContactName {return; } @InstanceProperty name: IContactName;
@InstanceProperty get nickname(): string { return; } @InstanceProperty nickname: string;
@InstanceProperty get phoneNumbers(): IContactField[] { return; } @InstanceProperty phoneNumbers: IContactField[];
@InstanceProperty get emails(): IContactField[] { return; } @InstanceProperty emails: IContactField[];
@InstanceProperty get addresses(): IContactAddress[] { return; } @InstanceProperty addresses: IContactAddress[];
@InstanceProperty get ims(): IContactField[] { return; } @InstanceProperty ims: IContactField[];
@InstanceProperty get organizations(): IContactOrganization[] { return; } @InstanceProperty organizations: IContactOrganization[];
@InstanceProperty get birthday(): Date { return; } @InstanceProperty birthday: Date;
@InstanceProperty get note(): string { return; } @InstanceProperty note: string;
@InstanceProperty get photos(): IContactField[] { return; } @InstanceProperty photos: IContactField[];
@InstanceProperty get categories(): IContactField[] { return; } @InstanceProperty categories: IContactField[];
@InstanceProperty get urls(): IContactField[] { return; } @InstanceProperty urls: IContactField[];
constructor() { constructor() {
this._objectInstance = navigator.contacts.create(); this._objectInstance = navigator.contacts.create();
@ -126,12 +126,12 @@ export class ContactName implements IContactName {
this._objectInstance = new window.ContactName(formatted, familyName, givenName, middleName, honorificPrefix, honorificSuffix); this._objectInstance = new window.ContactName(formatted, familyName, givenName, middleName, honorificPrefix, honorificSuffix);
} }
@InstanceProperty get formatted(): string { return; } @InstanceProperty formatted: string;
@InstanceProperty get familyName(): string { return; } @InstanceProperty familyName: string;
@InstanceProperty get givenName(): string { return; } @InstanceProperty givenName: string;
@InstanceProperty get middleName(): string { return; } @InstanceProperty middleName: string;
@InstanceProperty get honorificPrefix(): string { return; } @InstanceProperty honorificPrefix: string;
@InstanceProperty get honorificSuffix(): string { return; } @InstanceProperty honorificSuffix: string;
} }
export interface IContactField { export interface IContactField {
@ -153,9 +153,9 @@ export class ContactField implements IContactField {
this._objectInstance = new window.ContactField(type, value, pref); this._objectInstance = new window.ContactField(type, value, pref);
} }
@InstanceProperty get type(): string { return; } @InstanceProperty type: string;
@InstanceProperty get value(): string { return; } @InstanceProperty value: string;
@InstanceProperty get pref(): boolean { return; } @InstanceProperty pref: boolean;
} }
export interface IContactAddress { export interface IContactAddress {
@ -195,21 +195,21 @@ export class ContactAddress implements IContactAddress {
} }
/** Set to true if this ContactAddress contains the user's preferred value. */ /** Set to true if this ContactAddress contains the user's preferred value. */
@InstanceProperty get pref(): boolean { return; } @InstanceProperty pref: boolean;
/** A string indicating what type of field this is, home for example. */ /** A string indicating what type of field this is, home for example. */
@InstanceProperty get type(): string { return; } @InstanceProperty type: string;
/** The full address formatted for display. */ /** The full address formatted for display. */
@InstanceProperty get formatted(): string { return; } @InstanceProperty formatted: string;
/** The full street address. */ /** The full street address. */
@InstanceProperty get streetAddress(): string { return; } @InstanceProperty streetAddress: string;
/** The city or locality. */ /** The city or locality. */
@InstanceProperty get locality(): string { return; } @InstanceProperty locality: string;
/** The state or region. */ /** The state or region. */
@InstanceProperty get region(): string { return; } @InstanceProperty region: string;
/** The zip code or postal code. */ /** The zip code or postal code. */
@InstanceProperty get postalCode(): string { return; } @InstanceProperty postalCode: string;
/** The country name. */ /** The country name. */
@InstanceProperty get country(): string { return; } @InstanceProperty country: string;
} }
export interface IContactOrganization { export interface IContactOrganization {
@ -234,15 +234,15 @@ export class ContactOrganization implements IContactOrganization {
this._objectInstance = new window.ContactOrganization(); this._objectInstance = new window.ContactOrganization();
} }
/** Set to true if this ContactOrganization contains the user's preferred value. */ /** Set to true if this ContactOrganization contains the user's preferred value. */
@InstanceProperty get pref(): boolean { return; } @InstanceProperty pref: boolean;
/** 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. */
@InstanceProperty get type(): string { return; } @InstanceProperty type: string;
/** The name of the organization. */ /** The name of the organization. */
@InstanceProperty get name(): string { return; } @InstanceProperty name: string;
/** The department the contract works for. */ /** The department the contract works for. */
@InstanceProperty get department(): string { return; } @InstanceProperty department: string;
/** The contact's title at the organization. */ /** The contact's title at the organization. */
@InstanceProperty get title(): string { return; } @InstanceProperty title: string;
} }
/** Search options to filter navigator.contacts. */ /** Search options to filter navigator.contacts. */
@ -272,22 +272,22 @@ export class ContactFindOptions implements IContactFindOptions {
/** /**
* The search string used to find navigator.contacts. (Default: "") * The search string used to find navigator.contacts. (Default: "")
*/ */
@InstanceProperty get filter(): string { return; } @InstanceProperty filter: string;
/** /**
* Determines if the find operation returns multiple navigator.contacts. (Default: false) * Determines if the find operation returns multiple navigator.contacts. (Default: false)
*/ */
@InstanceProperty get multiple(): boolean { return; } @InstanceProperty multiple: boolean;
/** /**
* Contact fields to be returned back. If specified, the resulting Contact object only features values for these fields. * Contact fields to be returned back. If specified, the resulting Contact object only features values for these fields.
*/ */
@InstanceProperty get desiredFields(): any { return; } @InstanceProperty desiredFields: any;
/** /**
* (Android only): Filters the search to only return contacts with a phone number informed. * (Android only): Filters the search to only return contacts with a phone number informed.
*/ */
@InstanceProperty get hasPhoneNumber(): boolean { return; } @InstanceProperty hasPhoneNumber: boolean;
} }
/** /**

View File

@ -1,11 +1,12 @@
import { CordovaProperty, Plugin } from './plugin'; import { CordovaProperty, Plugin } from './plugin';
declare var window: { declare var window: any;
device: Device
};
export interface Device { /**
* @private
*/
export interface IDevice {
/** Get the version of Cordova running on the device. */ /** Get the version of Cordova running on the device. */
cordova: string; cordova: string;
/** /**
@ -54,6 +55,6 @@ export class Device {
* @returns {Device} The device object. * @returns {Device} The device object.
*/ */
@CordovaProperty @CordovaProperty
static get device(): Device { return window.device; } static device: IDevice;
} }

View File

@ -33,27 +33,21 @@ export class MediaCapture {
* @returns {ConfigurationData[]} * @returns {ConfigurationData[]}
*/ */
@CordovaProperty @CordovaProperty
static get supportedImageModes(): ConfigurationData[] { static supportedImageModes: ConfigurationData[];
return <ConfigurationData[]>navigator.device.capture.supportedImageModes;
}
/** /**
* The audio recording formats supported by the device. * The audio recording formats supported by the device.
* @returns {ConfigurationData[]} * @returns {ConfigurationData[]}
*/ */
@CordovaProperty @CordovaProperty
static get supportedAudioModes(): ConfigurationData[] { static supportedAudioModes: ConfigurationData[];
return <ConfigurationData[]>navigator.device.capture.supportedAudioModes;
}
/** /**
* The recording video resolutions and formats supported by the device. * The recording video resolutions and formats supported by the device.
* @returns {ConfigurationData[]} * @returns {ConfigurationData[]}
*/ */
@CordovaProperty @CordovaProperty
static get supportedVideoModes(): ConfigurationData[] { static supportedVideoModes: ConfigurationData[];
return <ConfigurationData[]>navigator.device.capture.supportedVideoModes;
}
/** /**
* Start the audio recorder application and return information about captured audio clip files. * Start the audio recorder application and return information about captured audio clip files.

View File

@ -16,6 +16,8 @@ declare var mixpanel: any;
* .catch(onError); * .catch(onError);
* *
* ``` * ```
* @classes
* MixpanelPeople
*/ */
@Plugin({ @Plugin({
pluginName: 'Mixpanel', pluginName: 'Mixpanel',

View File

@ -55,7 +55,7 @@ export class Network {
* Return the network connection type * Return the network connection type
*/ */
@CordovaProperty @CordovaProperty
static get connection(): string { return navigator.connection.type; } static connection: string;
/** /**
* Get notified when the device goes offline * Get notified when the device goes offline

View File

@ -404,43 +404,51 @@ export function CordovaInstance(opts: any = {}) {
* *
* Before calling the original method, ensure Cordova and the plugin are installed. * Before calling the original method, ensure Cordova and the plugin are installed.
*/ */
export function CordovaProperty(target: Function, key: string, descriptor: TypedPropertyDescriptor<any>) { export function CordovaProperty(target: Function, key: string) {
let originalMethod = descriptor.get; let exists: Function = function(): boolean {
descriptor.get = function(...args: any[]) {
if (!window.cordova) { if (!window.cordova) {
cordovaWarn(this.name, null); cordovaWarn(this.name, null);
return {}; return false;
} }
let pluginObj: any = this; let pluginInstance = getPlugin(this.pluginRef);
let pluginInstance = getPlugin(pluginObj.pluginRef);
if (!pluginInstance) { if (!pluginInstance) {
pluginWarn(this, key); pluginWarn(this, key);
return {}; return false;
} }
return originalMethod.apply(this, args); return true;
}; };
return descriptor; Object.defineProperty(target, key, {
get: function() {
if (exists) {
return this.pluginRef[key];
} else {
return {};
}
},
set: function(value) {
if (exists) {
this.pluginRef[key] = value;
}
}
});
} }
/** /**
* @private * @private
* @param target * @param target
* @param key * @param key
* @param descriptor
* @constructor * @constructor
*/ */
export function InstanceProperty(target: any, key: string, descriptor: TypedPropertyDescriptor<any>) { export function InstanceProperty(target: any, key: string) {
descriptor.get = function() { Object.defineProperty(target, key, {
get: function(){
return this._objectInstance[key]; return this._objectInstance[key];
}; },
set: function(value){
descriptor.set = function(...args: any[]) { this._objectInstance[key] = value;
this._objectInstance[key] = args[0]; }
}; });
return descriptor;
} }
/** /**

View File

@ -59,11 +59,9 @@ export class ScreenOrientation {
@Cordova({ sync: true }) @Cordova({ sync: true })
static unlockOrientation(): void { } static unlockOrientation(): void { }
/* /**
* Get the current orientation of the device. * Get the current orientation of the device.
*/ */
@CordovaProperty @CordovaProperty
static get orientation() { static orientation: string;
return window.screen.orientation;
}
} }

View File

@ -117,6 +117,6 @@ export class StatusBar {
* Whether the StatusBar is currently visible or not. * Whether the StatusBar is currently visible or not.
*/ */
@CordovaProperty @CordovaProperty
static get isVisible() { return window.StatusBar.isVisible; } static isVisible: boolean;
} }

View File

@ -26,14 +26,10 @@ declare var window;
export class WebIntent { export class WebIntent {
@CordovaProperty @CordovaProperty
static get ACTION_VIEW() { static ACTION_VIEW;
return window.plugins.webintent.ACTION_VIEW;
}
@CordovaProperty @CordovaProperty
static get EXTRA_TEXT() { static EXTRA_TEXT;
return window.plugins.webintent.EXTRA_TEXT;
}
/** /**
* @param options {Object} { action: any, url: string, type?: string } * @param options {Object} { action: any, url: string, type?: string }