Files
awesome-cordova-plugins/src/plugins/plugin.ts
T

69 lines
1.6 KiB
TypeScript
Raw Normal View History

2015-11-28 18:26:55 -06:00
import {get} from '../util';
declare var window;
declare var Promise;
export const wrap = function(pluginObj, methodName, opts: any = {}) {
return (...args) => {
return new Promise((resolve, reject) => {
2015-11-29 16:30:15 -06:00
if(!window.cordova) {
console.warn('Native: tried calling ' + pluginObj.name + '.' + methodName + ', but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
reject({
error: 'cordova_not_available'
})
}
2015-11-28 18:26:55 -06:00
if(typeof opts.successIndex !== 'undefined') {
args[opts.successIndex] = resolve;
}
if(typeof opts.errorIndex !== 'undefined') {
args[opts.errorIndex] = reject;
}
let pluginInstance = get(window, pluginObj.pluginRef);
if(!pluginInstance) {
console.warn('Native: tried calling ' + pluginObj.name + '.' + methodName + ', but the ' + pluginObj.name + ' plugin is not installed. Install the ' + pluginObj.plugin + ' plugin');
reject({
error: 'plugin_not_installed'
});
return;
}
get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
})
}
}
class PluginDecotor {
cls: any;
config: any;
constructor(cls, config) {
this.cls = cls;
this.config = config;
}
}
export function Plugin(config) {
return function(cls) {
// Add these fields to the class
for(let k in config) {
cls[k] = config[k];
}
return cls;
}
}
export function Cordova(opts:any = {}) {
return function(obj, methodName) {
if(opts.promise) {
console.log('TODO: Promise');
}
2015-11-28 18:36:38 -06:00
obj[methodName] = wrap(obj, methodName, opts).bind(obj);
2015-11-28 18:26:55 -06:00
}
}