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

105 lines
2.8 KiB
TypeScript
Raw Normal View History

2015-11-29 08:26:55 +08:00
import {get} from '../util';
declare var window;
declare var Promise;
2015-11-30 07:20:11 +08:00
export const getPlugin = function(pluginRef: string): any {
return get(window, pluginRef);
}
export const isInstalled = function(pluginRef: string): boolean {
return !!getPlugin(pluginRef);
}
export const pluginWarn = function(pluginName: string, method: string, plugin: string) {
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');
}
}
export const cordovaWarn = function(pluginName: string, method: string) {
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
export const wrap = function(pluginObj, methodName, opts: any = {}) {
return (...args) => {
return new Promise((resolve, reject) => {
2015-11-30 06:30:15 +08:00
if(!window.cordova) {
2015-11-30 07:20:11 +08:00
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-29 08:26:55 +08:00
if(typeof opts.successIndex !== 'undefined') {
args[opts.successIndex] = resolve;
}
if(typeof opts.errorIndex !== 'undefined') {
args[opts.errorIndex] = reject;
}
2015-11-30 07:20:11 +08:00
let pluginInstance = getPlugin(pluginObj.pluginRef);
2015-11-29 08:26:55 +08:00
if(!pluginInstance) {
2015-11-30 07:20:11 +08:00
pluginWarn(pluginObj.name, methodName, pluginObj.name);
2015-11-29 08:26:55 +08:00
reject({
error: 'plugin_not_installed'
});
return;
}
get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
})
}
}
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-29 08:36:38 +08:00
obj[methodName] = wrap(obj, methodName, opts).bind(obj);
2015-11-29 08:26:55 +08:00
}
}
2015-11-30 07:20:11 +08:00
export function RequiresPlugin(target: Function, key: string, descriptor: TypedPropertyDescriptor<any>) {
let originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
console.log('Calling', this);
if(!window.cordova) {
cordovaWarn(this.name, null);
return;
}
let pluginInstance = getPlugin(this.pluginRef);
if(!pluginInstance) {
pluginWarn(this.name, null, this.name);
return;
}
originalMethod.apply(this, args);
}
return descriptor;
}