mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-04-13 00:00:10 +08:00
new wrap
This commit is contained in:
Vendored
+50
-10
@@ -1,4 +1,28 @@
|
||||
var util_1 = require('../util');
|
||||
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');
|
||||
}
|
||||
};
|
||||
exports.wrap = function (pluginObj, methodName, opts) {
|
||||
if (opts === void 0) { opts = {}; }
|
||||
return function () {
|
||||
@@ -8,10 +32,11 @@ exports.wrap = function (pluginObj, methodName, opts) {
|
||||
}
|
||||
return new Promise(function (resolve, reject) {
|
||||
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');
|
||||
exports.cordovaWarn(pluginObj.name, methodName);
|
||||
reject({
|
||||
error: 'cordova_not_available'
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (typeof opts.successIndex !== 'undefined') {
|
||||
args[opts.successIndex] = resolve;
|
||||
@@ -19,9 +44,9 @@ exports.wrap = function (pluginObj, methodName, opts) {
|
||||
if (typeof opts.errorIndex !== 'undefined') {
|
||||
args[opts.errorIndex] = reject;
|
||||
}
|
||||
var pluginInstance = util_1.get(window, pluginObj.pluginRef);
|
||||
var pluginInstance = exports.getPlugin(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');
|
||||
exports.pluginWarn(pluginObj.name, methodName, pluginObj.name);
|
||||
reject({
|
||||
error: 'plugin_not_installed'
|
||||
});
|
||||
@@ -31,13 +56,6 @@ exports.wrap = function (pluginObj, methodName, opts) {
|
||||
});
|
||||
};
|
||||
};
|
||||
var PluginDecotor = (function () {
|
||||
function PluginDecotor(cls, config) {
|
||||
this.cls = cls;
|
||||
this.config = config;
|
||||
}
|
||||
return PluginDecotor;
|
||||
})();
|
||||
function Plugin(config) {
|
||||
return function (cls) {
|
||||
// Add these fields to the class
|
||||
@@ -58,3 +76,25 @@ function Cordova(opts) {
|
||||
};
|
||||
}
|
||||
exports.Cordova = Cordova;
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user