import { _throw } from 'rxjs/observable/throw' ; import { Observable } from 'rxjs/Observable' ; import { checkAvailability , instanceAvailability , overrideFunction , wrap , wrapInstance } from './plugin' ; import { getPlugin , getPromise } from './util' ; export interface PluginConfig { /**
     * Plugin name, this should match the class name
     */ pluginName : string ; /**
     * Plugin NPM package name
     */ plugin : string ; /**
     * Plugin object reference
     */ pluginRef ? : string ; /**
     * Github repository URL
     */ repo ? : string ; /**
     * Custom install command
     */ install ? : string ; /**
     * Available installation variables
     */ installVariables ? : string [ ] ; /**
     * Supported platforms
     */ platforms ? : string [ ] ; [ key ] : any ; } export interface CordovaOptions { destruct ? : boolean ; /**
     * Set to true if the wrapped method is a sync function
     */ sync ? : boolean ; /**
     * Callback order. Set to reverse if the success/error callbacks are the first 2 arguments that the wrapped method takes.
     */ callbackOrder ? : 'reverse' ; /**
     * Callback style
     */ callbackStyle ? : 'node' | 'object' ; /**
     * Set a custom index for the success callback function. This doesn't work if callbackOrder or callbackStyle are set.
     */ successIndex ? : number ; /**
     * Set a custom index for the error callback function. This doesn't work if callbackOrder or callbackStyle are set.
     */ errorIndex ? : number ; /**
     * Success function property name. This must be set if callbackStyle is set to object.
     */ successName ? : string ; /**
     * Error function property name. This must be set if callbackStyle is set to object.
     */ errorName ? : string ; /**
     * Set to true to return an observable
     */ observable ? : boolean ; /**
     * If observable is set to true, this can be set to a different function name that will cancel the observable.
     */ clearFunction ? : string ; /**
     * This can be used if clearFunction is set. Set this to true to call the clearFunction with the same arguments used in the initial function.
     */ clearWithArgs ? : boolean ; /**
     * Creates an observable that wraps a global event. Replaces document.addEventListener
     */ eventObservable ? : boolean ; /**
     * Event name, this must be set if eventObservable is set to true
     */ event ? : string ; /**
     * Element to attach the event listener to, this is optional, defaults to `window`
     */ element ? : any ; /**
     * Set to true if the wrapped method returns a promise
     */ otherPromise ? : boolean ; /**
     * Supported platforms
     */ platforms ? : string [ ] ; } export interface CordovaCheckOptions { sync ? : boolean ; observable ? : boolean ; } private export function InstanceCheck ( opts = { } ) { return ( pluginObj , methodName , descriptor ) : TypedPropertyDescriptor < any > => { return { value : function ( ... args ) : any { if ( instanceAvailability ( this ) ) { return descriptor . value . apply ( this , args ) ; } else { if ( opts . sync ) { return ; } else if ( opts . observable ) { return new Observable < any > ( ( ) => { } ) ; } return getPromise ( ( ) => { } ) ; } } , enumerable : true } ; } ; } private export function CordovaCheck ( opts = { } ) { return ( pluginObj , methodName , descriptor ) : TypedPropertyDescriptor < any > => { return { value : function ( ... args ) : any { const check = checkAvailability ( pluginObj ) ; if ( check === true ) { return descriptor . value . apply ( this , args ) ; } else { if ( opts . sync ) { return null ; } else if ( opts . observable ) { return _throw ( new Error ( check && check . error ) ) ; } return Promise . reject ( check && check . error ) ; } } , enumerable : true } ; } ; } private usage Plugin export function Plugin ( config ) { return function ( cls ) { for ( let prop in config ) { cls [ prop ] = config [ prop ] ; } cls [ 'installed' ] = function ( printWarning ? ) { return ! ! getPlugin ( config . pluginRef ) ; } ; cls [ 'getPlugin' ] = function ( ) { return getPlugin ( config . pluginRef ) ; } ; cls [ 'checkInstall' ] = function ( ) { return checkAvailability ( cls ) === true ; } ; cls [ 'getPluginName' ] = function ( ) { return config . pluginName ; } ; cls [ 'getPluginRef' ] = function ( ) { return config . pluginRef ; } ; cls [ 'getPluginInstallName' ] = function ( ) { return config . plugin ; } ; cls [ 'getPluginRepo' ] = function ( ) { return config . repo ; } ; cls [ 'getSupportedPlatforms' ] = function ( ) { return config . platforms ; } ; return cls ; } ; } private export function Cordova ( opts = { } ) { return ( target , methodName , descriptor ) => { return { value : function ( ... args ) { return wrap ( this , methodName , opts ) . apply ( this , args ) ; } , enumerable : true } ; } ; } private export function CordovaInstance ( opts = { } ) { return ( target , methodName ) => { return { value : function ( ... args ) { return wrapInstance ( this , methodName , opts ) . apply ( this , args ) ; } , enumerable : true } ; } ; } private export function CordovaProperty ( target , key ) { Object . defineProperty ( target , key , { enumerable : true , get : ( ) => { if ( checkAvailability ( target , key ) === true ) { return getPlugin ( target . constructor . getPluginRef ( ) ) [ key ] ; } else { return null ; } } , set : value => { if ( checkAvailability ( target , key ) === true ) { getPlugin ( target . constructor . getPluginRef ( ) ) [ key ] = value ; } } } ) ; } private param target param key constructor export function InstanceProperty ( target , key ) { Object . defineProperty ( target , key , { enumerable : true , get : function ( ) { return this . _objectInstance [ key ] ; } , set : function ( value ) { this . _objectInstance [ key ] = value ; } } ) ; } private export function CordovaFunctionOverride ( opts = { } ) { return ( target , methodName , descriptor ) => { return { value : function ( ... args ) { return overrideFunction ( this , methodName , opts ) ; } , enumerable : true } ; } ; }