mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-05-02 00:07:23 +08:00
Yea toast
This commit is contained in:
Vendored
+62
-39
@@ -1,4 +1,5 @@
|
||||
var util_1 = require('../util');
|
||||
var Rx_1 = require('@reactivex/rxjs/dist/cjs/Rx');
|
||||
exports.getPlugin = function (pluginRef) {
|
||||
return util_1.get(window, pluginRef);
|
||||
};
|
||||
@@ -23,52 +24,73 @@ exports.cordovaWarn = function (pluginName, method) {
|
||||
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');
|
||||
}
|
||||
};
|
||||
function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
|
||||
if (opts === void 0) { opts = {}; }
|
||||
if (!window.cordova) {
|
||||
exports.cordovaWarn(pluginObj.name, methodName);
|
||||
reject({
|
||||
error: 'cordova_not_available'
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
|
||||
// If we've specified a success/error index
|
||||
args.splice(opts.successIndex, 0, resolve);
|
||||
args.splice(opts.errorIndex, 0, reject);
|
||||
}
|
||||
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);
|
||||
}
|
||||
var pluginInstance = exports.getPlugin(pluginObj.pluginRef);
|
||||
if (!pluginInstance) {
|
||||
exports.pluginWarn(pluginObj.name, methodName, pluginObj.name);
|
||||
reject({
|
||||
error: 'plugin_not_installed'
|
||||
});
|
||||
return;
|
||||
}
|
||||
console.log('Cordova calling', pluginObj.name, methodName, args);
|
||||
return util_1.get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
|
||||
}
|
||||
function wrapPromise(pluginObj, methodName, args, opts) {
|
||||
if (opts === void 0) { opts = {}; }
|
||||
return new Promise(function (resolve, reject) {
|
||||
callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
|
||||
});
|
||||
}
|
||||
function wrapObservable(pluginObj, methodName, args, opts) {
|
||||
if (opts === void 0) { opts = {}; }
|
||||
return Rx_1.Observable.create(function (observer) {
|
||||
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.onNext, observer.onError);
|
||||
return function () {
|
||||
return util_1.get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
|
||||
};
|
||||
});
|
||||
}
|
||||
exports.wrap = function (pluginObj, methodName, opts) {
|
||||
if (opts === void 0) { opts = {}; }
|
||||
console.log('Wrap', pluginObj.name, methodName);
|
||||
return function () {
|
||||
var args = [];
|
||||
for (var _i = 0; _i < arguments.length; _i++) {
|
||||
args[_i - 0] = arguments[_i];
|
||||
}
|
||||
console.log('Wrap CALLED', pluginObj.name, methodName, args);
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (!window.cordova) {
|
||||
exports.cordovaWarn(pluginObj.name, methodName);
|
||||
reject({
|
||||
error: 'cordova_not_available'
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
|
||||
// If we've specified a success/error index
|
||||
args.splice(opts.successIndex, resolve);
|
||||
args.splice(opts.errorIndex, reject);
|
||||
}
|
||||
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);
|
||||
}
|
||||
var pluginInstance = exports.getPlugin(pluginObj.pluginRef);
|
||||
if (!pluginInstance) {
|
||||
exports.pluginWarn(pluginObj.name, methodName, pluginObj.name);
|
||||
reject({
|
||||
error: 'plugin_not_installed'
|
||||
});
|
||||
return;
|
||||
}
|
||||
console.log('Cordova calling', pluginObj.name, methodName, args);
|
||||
util_1.get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
|
||||
});
|
||||
if (opts.observable) {
|
||||
console.log("Wrapping observable");
|
||||
return wrapObservable(pluginObj, methodName, args, opts);
|
||||
}
|
||||
else {
|
||||
return wrapPromise(pluginObj, methodName, args, opts);
|
||||
}
|
||||
};
|
||||
};
|
||||
/**
|
||||
@@ -129,3 +151,4 @@ function RequiresPlugin(target, key, descriptor) {
|
||||
return descriptor;
|
||||
}
|
||||
exports.RequiresPlugin = RequiresPlugin;
|
||||
//# sourceMappingURL=plugin.js.map
|
||||
Reference in New Issue
Block a user