2015-11-28 18:26:55 -06:00
import { get } from '../util' ;
2015-12-01 13:33:08 -06:00
import { publishAngular1Service } from '../ng1' ;
2015-11-28 18:26:55 -06:00
declare var window ;
declare var Promise ;
2015-12-01 13:33:08 -06:00
declare var $q ;
2015-11-28 18:26:55 -06:00
2016-02-05 15:05:46 -06:00
import { Observable } from 'rxjs/Observable' ;
2015-11-30 12:34:54 -06:00
2016-03-12 18:52:25 -05:00
/ * *
* @private
* @param pluginRef
* @returns { null | * }
* /
2016-04-25 06:22:17 -04:00
export const getPlugin = function ( pluginRef : string ) : any {
2015-11-29 17:20:11 -06:00
return get ( window , pluginRef ) ;
2016-03-06 15:27:26 -05:00
} ;
2016-03-12 18:52:25 -05:00
/ * *
* @private
* @param pluginRef
* @returns { boolean }
* /
2016-04-25 06:22:17 -04:00
export const isInstalled = function ( pluginRef : string ) : boolean {
2015-11-29 17:20:11 -06:00
return ! ! getPlugin ( pluginRef ) ;
2016-03-06 15:27:26 -05:00
} ;
2016-03-12 18:52:25 -05:00
/ * *
* @private
* @param pluginObj
* @param method
* /
2016-04-25 06:22:17 -04:00
export const pluginWarn = function ( pluginObj : any , method : string ) {
2016-03-04 13:52:57 -06:00
var pluginName = pluginObj . name ;
var plugin = pluginObj . plugin ;
2016-04-29 23:56:49 -04:00
if ( method ) {
2016-03-04 13:52:57 -06:00
console . warn ( 'Native: tried calling ' + pluginName + '.' + method + ', but the ' + pluginName + ' plugin is not installed.' ) ;
2015-11-29 17:20:11 -06:00
} else {
2016-03-04 13:52:57 -06:00
console . warn ( 'Native: tried accessing the ' + pluginName + ' plugin but it\'s not installed.' ) ;
2015-11-29 17:20:11 -06:00
}
2016-03-04 13:52:57 -06:00
console . warn ( 'Install the ' + pluginName + ' plugin: \'cordova plugin add ' + plugin + '\'' ) ;
2016-03-06 15:27:26 -05:00
} ;
2016-03-12 18:52:25 -05:00
/ * *
* @private
* @param pluginName
* @param method
* /
2016-04-25 06:22:17 -04:00
export const cordovaWarn = function ( pluginName : string , method : string ) {
2016-04-29 23:56:49 -04:00
if ( method ) {
2015-11-29 17:20:11 -06:00
console . warn ( 'Native: tried calling ' + pluginName + '.' + method + ', but Cordova is not available. Make sure to include cordova.js or run in a device/simulator' ) ;
} else {
console . warn ( 'Native: tried accessing the ' + pluginName + ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator' ) ;
}
2016-03-06 15:27:26 -05:00
} ;
2016-04-29 23:56:49 -04:00
function setIndex ( args : any [ ] , opts : any = { } , resolve? : Function , reject? : Function ) : any {
// If the plugin method expects myMethod(success, err, options)
if ( opts . callbackOrder === 'reverse' ) {
// Get those arguments in the order [resolve, reject, ...restOfArgs]
args . unshift ( reject ) ;
args . unshift ( resolve ) ;
} else if ( typeof opts . successIndex !== 'undefined' || typeof opts . errorIndex !== 'undefined' ) {
// If we've specified a success/error index
args . splice ( opts . successIndex , 0 , resolve ) ;
args . splice ( opts . errorIndex , 0 , reject ) ;
} else {
// Otherwise, let's tack them on to the end of the argument list
// which is 90% of cases
args . push ( resolve ) ;
args . push ( reject ) ;
}
2015-11-29 17:20:11 -06:00
2016-04-29 23:56:49 -04:00
return args ;
2016-03-29 06:50:03 -04:00
}
2016-03-29 17:53:51 -04:00
2016-04-29 23:56:49 -04:00
function callCordovaPlugin ( pluginObj : any , methodName : string , args : any [ ] , opts : any = { } , resolve? : Function , reject? : Function ) {
2015-11-30 12:34:54 -06:00
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
2016-04-29 23:56:49 -04:00
args = setIndex ( args , opts , resolve , reject ) ;
2015-11-30 12:34:54 -06:00
let pluginInstance = getPlugin ( pluginObj . pluginRef ) ;
2016-04-29 23:56:49 -04:00
if ( ! pluginInstance ) {
2016-04-29 21:46:35 -04:00
// Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation).
2016-04-29 23:56:49 -04:00
if ( ! window . cordova ) {
2016-04-29 21:46:35 -04:00
cordovaWarn ( pluginObj . name , methodName ) ;
return {
error : 'cordova_not_available'
2016-04-29 23:56:49 -04:00
} ;
2016-04-29 21:46:35 -04:00
}
2015-11-30 12:34:54 -06:00
2016-04-29 21:46:35 -04:00
pluginWarn ( pluginObj , methodName ) ;
return {
error : 'plugin_not_installed'
} ;
}
2015-11-30 12:34:54 -06:00
2015-11-30 13:27:25 -06:00
// TODO: Illegal invocation needs window context
return get ( window , pluginObj . pluginRef ) [ methodName ] . apply ( pluginInstance , args ) ;
2015-11-30 12:34:54 -06:00
}
2015-12-01 13:33:08 -06:00
function getPromise ( cb ) {
2016-04-29 23:56:49 -04:00
if ( window . Promise ) {
2016-03-04 13:53:59 -06:00
// console.log('Native promises available...');
2015-12-01 13:33:08 -06:00
return new Promise ( ( resolve , reject ) = > {
cb ( resolve , reject ) ;
2016-04-29 23:56:49 -04:00
} ) ;
} else if ( window . angular ) {
2015-12-01 13:33:08 -06:00
let $q = window . angular . injector ( [ 'ng' ] ) . get ( '$q' ) ;
2016-03-04 13:53:59 -06:00
// console.log('Loaded $q', $q);
2015-12-01 13:33:08 -06:00
return $q ( ( resolve , reject ) = > {
cb ( resolve , reject ) ;
} ) ;
} else {
console . error ( 'No Promise support or polyfill found. To enable Ionic Native support, please add the es6-promise polyfill before this script, or run with a library like Angular 1/2 or on a recent browser.' ) ;
}
}
2016-04-29 23:56:49 -04:00
function wrapPromise ( pluginObj : any , methodName : string , args : any [ ] , opts : any = { } ) {
2016-04-27 13:00:57 -05:00
let pluginResult , rej ;
const p = getPromise ( ( resolve , reject ) = > {
pluginResult = callCordovaPlugin ( pluginObj , methodName , args , opts , resolve , reject ) ;
rej = reject ;
} ) ;
// Angular throws an error on unhandled rejection, but in this case we have already printed
// a warning that Cordova is undefined or the plugin is uninstalled, so there is no reason
// to error
if ( pluginResult && pluginResult . error ) {
p . catch ( ( ) = > { } ) ;
rej ( pluginResult . error ) ;
}
return p ;
2015-11-30 12:34:54 -06:00
}
2016-04-29 23:56:49 -04:00
function wrapObservable ( pluginObj : any , methodName : string , args : any [ ] , opts : any = { } ) {
2015-11-30 13:27:25 -06:00
return new Observable ( observer = > {
2015-11-30 14:38:52 -06:00
let pluginResult = callCordovaPlugin ( pluginObj , methodName , args , opts , observer . next . bind ( observer ) , observer . error . bind ( observer ) ) ;
2016-04-27 13:00:57 -05:00
if ( pluginResult && pluginResult . error ) {
observer . error ( pluginResult . error ) ;
}
2015-11-30 12:34:54 -06:00
return ( ) = > {
2015-11-30 14:38:52 -06:00
try {
2016-04-29 23:56:49 -04:00
if ( opts . clearWithArgs ) {
2016-02-08 12:42:42 -06:00
return get ( window , pluginObj . pluginRef ) [ opts . clearFunction ] . apply ( pluginObj , args ) ;
}
2016-02-08 13:07:26 -06:00
return get ( window , pluginObj . pluginRef ) [ opts . clearFunction ] . call ( pluginObj , pluginResult ) ;
2016-04-29 23:56:49 -04:00
} catch ( e ) {
2015-11-30 14:38:52 -06:00
console . warn ( 'Unable to clear the previous observable watch for' , pluginObj . name , methodName ) ;
2016-03-04 13:53:59 -06:00
console . error ( e ) ;
2015-11-30 14:38:52 -06:00
}
2016-04-29 23:56:49 -04:00
} ;
2015-11-30 12:34:54 -06:00
} ) ;
}
2016-04-29 23:56:49 -04:00
function callInstance ( pluginObj : any , methodName : string , args : any [ ] , opts : any = { } , resolve? : Function , reject? : Function ) {
args = setIndex ( args , opts , resolve , reject ) ;
return pluginObj . _objectInstance [ methodName ] . apply ( pluginObj . _objectInstance , args ) ;
2016-03-29 06:50:03 -04:00
}
2016-04-29 23:56:49 -04:00
function wrapInstance ( pluginObj : any , methodName : string , args : any [ ] , opts : any = { } ) {
2016-04-29 21:46:35 -04:00
return ( . . . args ) = > {
2016-04-25 06:22:17 -04:00
if ( opts . sync ) {
2016-04-29 21:46:35 -04:00
return callInstance ( pluginObj , methodName , args , opts ) ;
2016-04-25 06:22:17 -04:00
} else if ( opts . observable ) {
2016-04-29 21:46:35 -04:00
return new Observable ( observer = > {
2016-04-29 23:56:49 -04:00
let pluginResult = callInstance ( pluginObj , methodName , args , opts , observer . next . bind ( observer ) , observer . error . bind ( observer ) ) ;
2016-04-29 21:46:35 -04:00
return ( ) = > {
try {
2016-04-29 23:56:49 -04:00
if ( opts . clearWithArgs ) {
2016-04-29 21:46:35 -04:00
return pluginObj . _objectInstance [ opts . clearFunction ] . apply ( pluginObj . _objectInstance , args ) ;
2016-04-25 06:22:17 -04:00
}
2016-04-29 21:46:35 -04:00
return pluginObj . _objectInstance [ opts . clearFunction ] . call ( pluginObj , pluginResult ) ;
2016-04-29 23:56:49 -04:00
} catch ( e ) {
2016-04-29 21:46:35 -04:00
console . warn ( 'Unable to clear the previous observable watch for' , pluginObj . name , methodName ) ;
console . error ( e ) ;
2016-03-29 17:49:53 -04:00
}
2016-04-29 23:56:49 -04:00
} ;
2016-04-29 21:46:35 -04:00
} ) ;
2016-04-25 06:22:17 -04:00
} else {
2016-04-29 21:46:35 -04:00
return getPromise ( ( resolve , reject ) = > {
callInstance ( pluginObj , methodName , args , opts , resolve , reject ) ;
} ) ;
2016-04-25 06:22:17 -04:00
}
2016-04-29 23:56:49 -04:00
} ;
2016-03-29 06:50:03 -04:00
}
2016-03-13 15:30:21 -04:00
/ * *
* Wrap the event with an observable
* @param event
* @returns { Observable }
* /
2016-04-29 23:56:49 -04:00
function wrapEventObservable ( event : string ) : Observable < any > {
2016-03-13 15:30:21 -04:00
return new Observable ( observer = > {
2016-04-29 23:56:49 -04:00
let callback = ( status : any ) = > observer . next ( status ) ;
2016-03-13 15:30:21 -04:00
window . addEventListener ( event , callback , false ) ;
return ( ) = > window . removeEventListener ( event , callback , false ) ;
} ) ;
}
2016-03-12 18:52:25 -05:00
/ * *
* @private
* @param pluginObj
* @param methodName
* @param opts
* @returns { function ( . . . [ any ] ) : ( undefined | * | Observable | * | * ) }
* /
2016-04-29 23:56:49 -04:00
export const wrap = function ( pluginObj : any , methodName : string , opts : any = { } ) {
2015-11-30 12:34:54 -06:00
return ( . . . args ) = > {
2016-04-29 23:56:49 -04:00
if ( opts . sync )
2016-02-09 14:45:57 -06:00
return callCordovaPlugin ( pluginObj , methodName , args , opts ) ;
2016-03-13 15:30:21 -04:00
else if ( opts . observable )
2015-11-30 12:34:54 -06:00
return wrapObservable ( pluginObj , methodName , args , opts ) ;
2016-03-13 15:30:21 -04:00
else if ( opts . eventObservable && opts . event )
return wrapEventObservable ( opts . event ) ;
else
2015-11-30 12:34:54 -06:00
return wrapPromise ( pluginObj , methodName , args , opts ) ;
2016-04-29 23:56:49 -04:00
} ;
2016-03-06 15:27:26 -05:00
} ;
2015-11-28 18:26:55 -06:00
2015-11-29 19:54:45 -06:00
/ * *
2016-03-12 18:52:25 -05:00
* @private
*
2015-11-29 19:54:45 -06:00
* Class decorator specifying Plugin metadata . Required for all plugins .
2016-03-12 18:30:16 -05:00
*
* @usage
* ` ` ` ts
* @Plugin ( {
* name : 'MyPlugin' ,
* plugin : 'cordova-plugin-myplugin' ,
* pluginRef : 'window.myplugin'
* } )
* export class MyPlugin {
*
* // Plugin wrappers, properties, and functions go here ...
*
* }
* ` ` `
2015-11-29 19:54:45 -06:00
* /
2015-11-28 18:26:55 -06:00
export function Plugin ( config ) {
2016-04-25 06:22:17 -04:00
return function ( cls ) {
2015-11-28 18:26:55 -06:00
// Add these fields to the class
2016-02-09 14:45:57 -06:00
for ( let k in config ) {
2015-11-28 18:26:55 -06:00
cls [ k ] = config [ k ] ;
}
2016-04-25 06:22:17 -04:00
cls [ 'installed' ] = function ( ) {
2015-12-01 13:33:08 -06:00
return ! ! getPlugin ( config . pluginRef ) ;
2016-03-06 15:27:26 -05:00
} ;
2015-12-01 13:33:08 -06:00
2015-11-28 18:26:55 -06:00
return cls ;
2016-04-29 23:56:49 -04:00
} ;
2015-11-28 18:26:55 -06:00
}
2015-11-29 19:54:45 -06:00
/ * *
2016-03-12 18:52:25 -05:00
* @private
*
2015-11-29 19:54:45 -06:00
* Wrap a stub function in a call to a Cordova plugin , checking if both Cordova
* and the required plugin are installed .
* /
2016-04-29 23:56:49 -04:00
export function Cordova ( opts : any = { } ) {
2016-04-25 06:22:17 -04:00
return ( target : Object , methodName : string , descriptor : TypedPropertyDescriptor < any > ) = > {
2015-11-29 19:54:45 -06:00
let originalMethod = descriptor . value ;
return {
2016-04-25 06:22:17 -04:00
value : function ( . . . args : any [ ] ) {
2015-11-29 21:50:58 -06:00
return wrap ( this , methodName , opts ) . apply ( this , args ) ;
2015-11-29 19:54:45 -06:00
}
2016-04-29 23:56:49 -04:00
} ;
} ;
2015-11-28 18:26:55 -06:00
}
2015-11-29 17:20:11 -06:00
2016-03-29 17:49:53 -04:00
/ * *
* @private
*
* Wrap an instance method
* /
2016-04-29 23:56:49 -04:00
export function CordovaInstance ( opts : any = { } ) {
return ( target : Object , methodName : string ) = > {
return {
value : function ( . . . args : any [ ] ) {
return wrapInstance ( this , methodName , opts ) . apply ( this , args ) ;
}
} ;
} ;
2016-03-29 06:50:03 -04:00
}
2015-11-29 19:54:45 -06:00
/ * *
2016-03-12 18:52:25 -05:00
* @private
*
*
2015-11-29 19:54:45 -06:00
* Before calling the original method , ensure Cordova and the plugin are installed .
* /
2016-04-25 06:22:17 -04:00
export function CordovaProperty ( target : Function , key : string , descriptor : TypedPropertyDescriptor < any > ) {
2016-03-04 13:56:22 -06:00
let originalMethod = descriptor . get ;
2015-11-29 17:20:11 -06:00
2016-04-25 06:22:17 -04:00
descriptor . get = function ( . . . args : any [ ] ) {
2016-04-29 23:56:49 -04:00
if ( ! window . cordova ) {
2015-11-29 17:20:11 -06:00
cordovaWarn ( this . name , null ) ;
2016-03-04 13:56:22 -06:00
return { } ;
2015-11-29 17:20:11 -06:00
}
let pluginInstance = getPlugin ( this . pluginRef ) ;
2016-04-29 23:56:49 -04:00
if ( ! pluginInstance ) {
2016-03-04 13:52:57 -06:00
pluginWarn ( this , key ) ;
2016-04-29 23:56:49 -04:00
return { } ;
2015-11-29 17:20:11 -06:00
}
2016-02-16 17:40:54 -06:00
return originalMethod . apply ( this , args ) ;
2016-03-06 15:27:26 -05:00
} ;
2015-11-29 17:20:11 -06:00
return descriptor ;
}
2016-04-30 13:31:10 -04:00
/ * *
* @private
* @param target
* @param key
* @param descriptor
* @constructor
* /
export function InstanceProperty ( target : Function , key : string , descriptor : TypedPropertyDescriptor < any > ) {
descriptor . get = function ( ) {
return this . _objectInstance [ key ] ;
} ;
descriptor . get = function ( . . . args : any [ ] ) {
return this . _objectInstance [ key ] = args [ 0 ] ;
} ;
return descriptor ;
}