2015-11-29 08:26:55 +08:00
var util _1 = require ( '../util' ) ;
2015-11-30 07:20:11 +08:00
exports . getPlugin = function ( pluginRef ) {
return util _1 . get ( window , pluginRef ) ;
} ;
exports . isInstalled = function ( pluginRef ) {
return ! ! exports . getPlugin ( pluginRef ) ;
} ;
exports . pluginWarn = function ( pluginName , method , plugin ) {
if ( method ) {
console . warn ( 'Native: tried calling ' + pluginName + '.' + method +
', but the ' + pluginName + ' plugin is not installed. Install the ' +
plugin + ' plugin' ) ;
}
else {
console . warn ( 'Native: tried accessing the ' + pluginName + ' plugin but it\'s not installed. Install the ' + plugin + ' plugin' ) ;
}
} ;
exports . cordovaWarn = function ( pluginName , method ) {
if ( method ) {
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' ) ;
}
} ;
2015-11-29 08:26:55 +08:00
exports . wrap = function ( pluginObj , methodName , opts ) {
if ( opts === void 0 ) { opts = { } ; }
return function ( ) {
var args = [ ] ;
for ( var _i = 0 ; _i < arguments . length ; _i ++ ) {
args [ _i - 0 ] = arguments [ _i ] ;
}
return new Promise ( function ( resolve , reject ) {
2015-11-30 06:30:15 +08:00
if ( ! window . cordova ) {
2015-11-30 07:20:11 +08:00
exports . cordovaWarn ( pluginObj . name , methodName ) ;
2015-11-30 06:30:15 +08:00
reject ( {
error : 'cordova_not_available'
} ) ;
2015-11-30 07:20:11 +08:00
return ;
2015-11-30 06:30:15 +08:00
}
2015-11-30 09:54:45 +08:00
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
// If the plugin method expects myMethod(success, err, options)
if ( opts . callbackOrder == 'reverse' ) {
args [ 0 ] = resolve ;
args [ 1 ] = reject ;
2015-11-29 08:26:55 +08:00
}
2015-11-30 09:54:45 +08:00
else if ( typeof opts . successIndex !== 'undefined' || typeof opts . errorIndex !== 'undefined' ) {
// If we've specified a success/error index
args [ opts . successIndex ] = resolve ;
2015-11-29 08:26:55 +08:00
args [ opts . errorIndex ] = reject ;
}
2015-11-30 09:54:45 +08:00
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-30 07:20:11 +08:00
var pluginInstance = exports . getPlugin ( pluginObj . pluginRef ) ;
2015-11-29 08:26:55 +08:00
if ( ! pluginInstance ) {
2015-11-30 07:20:11 +08:00
exports . pluginWarn ( pluginObj . name , methodName , pluginObj . name ) ;
2015-11-29 08:26:55 +08:00
reject ( {
error : 'plugin_not_installed'
} ) ;
return ;
}
util _1 . get ( window , pluginObj . pluginRef ) [ methodName ] . apply ( pluginObj , args ) ;
} ) ;
} ;
} ;
2015-11-30 09:54:45 +08:00
/ * *
* Class decorator specifying Plugin metadata . Required for all plugins .
* /
2015-11-29 08:26:55 +08:00
function Plugin ( config ) {
return function ( cls ) {
// Add these fields to the class
for ( var k in config ) {
cls [ k ] = config [ k ] ;
}
return cls ;
} ;
}
exports . Plugin = Plugin ;
2015-11-30 09:54:45 +08:00
/ * *
* Wrap a stub function in a call to a Cordova plugin , checking if both Cordova
* and the required plugin are installed .
* /
2015-11-29 08:26:55 +08:00
function Cordova ( opts ) {
if ( opts === void 0 ) { opts = { } ; }
2015-11-30 09:54:45 +08:00
return function ( target , methodName , descriptor ) {
var originalMethod = descriptor . value ;
return {
value : function ( ) {
var args = [ ] ;
for ( var _i = 0 ; _i < arguments . length ; _i ++ ) {
args [ _i - 0 ] = arguments [ _i ] ;
}
return exports . wrap ( this , methodName , opts ) ( ) ;
}
} ;
2015-11-29 08:26:55 +08:00
} ;
}
exports . Cordova = Cordova ;
2015-11-30 09:54:45 +08:00
/ * *
* Before calling the original method , ensure Cordova and the plugin are installed .
* /
2015-11-30 07:20:11 +08:00
function RequiresPlugin ( target , key , descriptor ) {
var originalMethod = descriptor . value ;
descriptor . value = function ( ) {
var args = [ ] ;
for ( var _i = 0 ; _i < arguments . length ; _i ++ ) {
args [ _i - 0 ] = arguments [ _i ] ;
}
console . log ( 'Calling' , this ) ;
if ( ! window . cordova ) {
exports . cordovaWarn ( this . name , null ) ;
return ;
}
var pluginInstance = exports . getPlugin ( this . pluginRef ) ;
if ( ! pluginInstance ) {
exports . pluginWarn ( this . name , null , this . name ) ;
return ;
}
originalMethod . apply ( this , args ) ;
} ;
return descriptor ;
}
exports . RequiresPlugin = RequiresPlugin ;