51 lines
1003 B
TypeScript
Raw Normal View History

2015-11-24 16:37:29 -08:00
interface Window { Native: any }
2015-11-24 14:04:16 -06:00
import {PluginConfig} from './plugin-config'
import {promisifyCordova} from './cordova';
2015-11-24 15:00:16 -06:00
import {get} from './util';
2015-11-24 14:37:34 -06:00
let wrappedPlugins = {}
2015-11-24 14:04:16 -06:00
2015-11-24 14:25:43 -06:00
let promised;
2015-11-24 14:37:34 -06:00
2015-11-24 14:44:30 -06:00
function newPluginClass(config) {
let obj = {
installed: () => {
2015-11-24 16:55:09 -06:00
return !!obj.plugin();
2015-11-24 14:51:47 -06:00
},
2015-11-24 14:44:30 -06:00
2015-11-24 16:55:09 -06:00
// Get the plugin by checking the plugin ref path on window
plugin: () => {
return get(window, config.pluginRef)
},
pluginName: config.plugin
2015-11-24 14:51:47 -06:00
};
2015-11-24 14:44:30 -06:00
return obj;
}
2015-11-24 14:37:34 -06:00
// Go through each registered plugin
2015-11-24 16:55:09 -06:00
for(let i = 0; i < PluginConfig.length; i++) {
let plugin = PluginConfig[i];
2015-11-24 14:04:16 -06:00
2015-11-24 14:37:34 -06:00
// Create the wrapped class
let cls = newPluginClass(plugin);
2015-11-24 14:04:16 -06:00
2015-11-24 14:37:34 -06:00
promised = plugin.promise || [];
2015-11-24 14:04:16 -06:00
2015-11-24 16:55:09 -06:00
for(let j = 0; j < promised.length; j++) {
let method = promised[j];
2015-11-24 14:51:47 -06:00
let p = promisifyCordova(cls, plugin.id, method)
2015-11-24 14:37:34 -06:00
cls[method] = p;
2015-11-24 14:04:16 -06:00
}
2015-11-24 14:37:34 -06:00
// Save the plugin object
wrappedPlugins[plugin.className] = cls;
}
2015-11-24 18:10:34 -06:00
export default wrappedPlugins;
2015-11-24 14:04:16 -06:00
2015-11-24 16:37:29 -08:00
window['Native'] = wrappedPlugins;