123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- 'use strict';
- var rxjs = require('rxjs');
- /**
- *
- */
- function checkReady() {
- if (typeof process === 'undefined') {
- var win_1 = typeof window !== 'undefined' ? window : {};
- var DEVICE_READY_TIMEOUT_1 = 5000;
- // To help developers using cordova, we listen for the device ready event and
- // log an error if it didn't fire in a reasonable amount of time. Generally,
- // when this happens, developers should remove and reinstall plugins, since
- // an inconsistent plugin is often the culprit.
- var before_1 = Date.now();
- var didFireReady_1 = false;
- win_1.document.addEventListener('deviceready', function () {
- console.log("Ionic Native: deviceready event fired after " + (Date.now() - before_1) + " ms");
- didFireReady_1 = true;
- });
- setTimeout(function () {
- if (!didFireReady_1 && win_1.cordova) {
- console.warn("Ionic Native: deviceready did not fire within " + DEVICE_READY_TIMEOUT_1 + "ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.");
- }
- }, DEVICE_READY_TIMEOUT_1);
- }
- }
- var ERR_CORDOVA_NOT_AVAILABLE = { error: 'cordova_not_available' };
- var ERR_PLUGIN_NOT_INSTALLED = { error: 'plugin_not_installed' };
- /**
- * @param callback
- */
- function getPromise(callback) {
- var tryNativePromise = function () {
- if (Promise) {
- return new Promise(function (resolve, reject) {
- callback(resolve, reject);
- });
- }
- else {
- console.error('No Promise support or polyfill found. To enable Ionic Native support, please add the es6-promise polyfill before this script, or run with a library like Angular or on a recent browser.');
- }
- };
- if (typeof window !== 'undefined' && window.angular) {
- var doc = window.document;
- var injector = window.angular.element(doc.querySelector('[ng-app]') || doc.body).injector();
- if (injector) {
- var $q = injector.get('$q');
- return $q(function (resolve, reject) {
- callback(resolve, reject);
- });
- }
- console.warn("Angular 1 was detected but $q couldn't be retrieved. This is usually when the app is not bootstrapped on the html or body tag. Falling back to native promises which won't trigger an automatic digest when promises resolve.");
- }
- return tryNativePromise();
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param args
- * @param opts
- */
- function wrapPromise(pluginObj, methodName, args, opts) {
- if (opts === void 0) { opts = {}; }
- var pluginResult, rej;
- var p = getPromise(function (resolve, reject) {
- if (opts.destruct) {
- pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return resolve(args);
- }, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return reject(args);
- });
- }
- else {
- pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
- }
- rej = reject;
- });
- // Angular throws an error on unhandled rejection, but in this case we have already printed
- // a warning that Cordova is undefined or the plugin is uninstalled, so there is no reason
- // to error
- if (pluginResult && pluginResult.error) {
- p.catch(function () { });
- typeof rej === 'function' && rej(pluginResult.error);
- }
- return p;
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param args
- * @param opts
- */
- function wrapOtherPromise(pluginObj, methodName, args, opts) {
- if (opts === void 0) { opts = {}; }
- return getPromise(function (resolve, reject) {
- var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts);
- if (pluginResult) {
- if (pluginResult.error) {
- reject(pluginResult.error);
- }
- else if (pluginResult.then) {
- pluginResult.then(resolve).catch(reject);
- }
- }
- else {
- reject({ error: 'unexpected_error' });
- }
- });
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param args
- * @param opts
- */
- function wrapObservable(pluginObj, methodName, args, opts) {
- if (opts === void 0) { opts = {}; }
- return new rxjs.Observable(function (observer) {
- var pluginResult;
- if (opts.destruct) {
- pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return observer.next(args);
- }, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return observer.error(args);
- });
- }
- else {
- pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
- }
- if (pluginResult && pluginResult.error) {
- observer.error(pluginResult.error);
- observer.complete();
- }
- return function () {
- try {
- if (opts.clearFunction) {
- if (opts.clearWithArgs) {
- return callCordovaPlugin(pluginObj, opts.clearFunction, args, opts, observer.next.bind(observer), observer.error.bind(observer));
- }
- return callCordovaPlugin(pluginObj, opts.clearFunction, []);
- }
- }
- catch (e) {
- console.warn('Unable to clear the previous observable watch for', pluginObj.constructor.getPluginName(), methodName);
- console.warn(e);
- }
- };
- });
- }
- /**
- * Wrap the event with an observable
- *
- * @private
- * @param event event name
- * @param element The element to attach the event listener to
- * @returns {Observable}
- */
- function wrapEventObservable(event, element) {
- element =
- typeof window !== 'undefined' && element
- ? get$1(window, element)
- : element || (typeof window !== 'undefined' ? window : {});
- return rxjs.fromEvent(element, event);
- }
- /**
- * @param plugin
- * @param methodName
- * @param pluginName
- */
- function checkAvailability(plugin, methodName, pluginName) {
- var pluginRef, pluginPackage;
- if (typeof plugin === 'string') {
- pluginRef = plugin;
- }
- else {
- pluginRef = plugin.constructor.getPluginRef();
- pluginName = plugin.constructor.getPluginName();
- pluginPackage = plugin.constructor.getPluginInstallName();
- }
- var pluginInstance = getPlugin(pluginRef);
- if (!pluginInstance || (!!methodName && typeof pluginInstance[methodName] === 'undefined')) {
- if (typeof window === 'undefined' || !window.cordova) {
- cordovaWarn(pluginName, methodName);
- return ERR_CORDOVA_NOT_AVAILABLE;
- }
- pluginWarn(pluginName, pluginPackage, methodName);
- return ERR_PLUGIN_NOT_INSTALLED;
- }
- return true;
- }
- /**
- * Checks if _objectInstance exists and has the method/property
- *
- * @param pluginObj
- * @param methodName
- * @private
- */
- function instanceAvailability(pluginObj, methodName) {
- return pluginObj._objectInstance && (!methodName || typeof pluginObj._objectInstance[methodName] !== 'undefined');
- }
- /**
- * @param args
- * @param opts
- * @param resolve
- * @param reject
- */
- function setIndex(args, opts, resolve, reject) {
- if (opts === void 0) { opts = {}; }
- // ignore resolve and reject in case sync
- if (opts.sync) {
- return args;
- }
- // If the plugin method expects myMethod(success, err, options)
- if (opts.callbackOrder === 'reverse') {
- // Get those arguments in the order [resolve, reject, ...restOfArgs]
- args.unshift(reject);
- args.unshift(resolve);
- }
- else if (opts.callbackStyle === 'node') {
- args.push(function (err, result) {
- if (err) {
- reject(err);
- }
- else {
- resolve(result);
- }
- });
- }
- else if (opts.callbackStyle === 'object' && opts.successName && opts.errorName) {
- var obj = {};
- obj[opts.successName] = resolve;
- obj[opts.errorName] = reject;
- args.push(obj);
- }
- else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
- var setSuccessIndex = function () {
- // If we've specified a success/error index
- if (opts.successIndex > args.length) {
- args[opts.successIndex] = resolve;
- }
- else {
- args.splice(opts.successIndex, 0, resolve);
- }
- };
- var setErrorIndex = function () {
- // We don't want that the reject cb gets spliced into the position of an optional argument that has not been
- // defined and thus causing non expected behavior.
- if (opts.errorIndex > args.length) {
- args[opts.errorIndex] = reject; // insert the reject fn at the correct specific index
- }
- else {
- args.splice(opts.errorIndex, 0, reject); // otherwise just splice it into the array
- }
- };
- if (opts.successIndex > opts.errorIndex) {
- setErrorIndex();
- setSuccessIndex();
- }
- else {
- setSuccessIndex();
- setErrorIndex();
- }
- }
- 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);
- }
- return args;
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param args
- * @param opts
- * @param resolve
- * @param reject
- */
- function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
- if (opts === void 0) { opts = {}; }
- // Try to figure out where the success/error callbacks need to be bound
- // to our promise resolve/reject handlers.
- args = setIndex(args, opts, resolve, reject);
- var availabilityCheck = checkAvailability(pluginObj, methodName);
- if (availabilityCheck === true) {
- var pluginInstance = getPlugin(pluginObj.constructor.getPluginRef());
- // eslint-disable-next-line prefer-spread
- return pluginInstance[methodName].apply(pluginInstance, args);
- }
- else {
- return availabilityCheck;
- }
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param args
- * @param opts
- * @param resolve
- * @param reject
- */
- function callInstance(pluginObj, methodName, args, opts, resolve, reject) {
- if (opts === void 0) { opts = {}; }
- args = setIndex(args, opts, resolve, reject);
- if (instanceAvailability(pluginObj, methodName)) {
- // eslint-disable-next-line prefer-spread
- return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
- }
- }
- /**
- * @param pluginRef
- */
- function getPlugin(pluginRef) {
- if (typeof window !== 'undefined') {
- return get$1(window, pluginRef);
- }
- return null;
- }
- /**
- * @param element
- * @param path
- */
- function get$1(element, path) {
- var paths = path.split('.');
- var obj = element;
- for (var i = 0; i < paths.length; i++) {
- if (!obj) {
- return null;
- }
- obj = obj[paths[i]];
- }
- return obj;
- }
- /**
- * @param pluginName
- * @param plugin
- * @param method
- */
- function pluginWarn(pluginName, plugin, method) {
- if (method) {
- console.warn('Native: tried calling ' + pluginName + '.' + method + ', but the ' + pluginName + ' plugin is not installed.');
- }
- else {
- console.warn("Native: tried accessing the " + pluginName + " plugin but it's not installed.");
- }
- if (plugin) {
- console.warn("Install the " + pluginName + " plugin: 'ionic cordova plugin add " + plugin + "'");
- }
- }
- /**
- * @private
- * @param pluginName
- * @param method
- */
- function cordovaWarn(pluginName, method) {
- if (typeof process === 'undefined') {
- 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');
- }
- }
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param opts
- * @private
- */
- var wrap = function (pluginObj, methodName, opts) {
- if (opts === void 0) { opts = {}; }
- return function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- if (opts.sync) {
- // Sync doesn't wrap the plugin with a promise or observable, it returns the result as-is
- return callCordovaPlugin(pluginObj, methodName, args, opts);
- }
- else if (opts.observable) {
- return wrapObservable(pluginObj, methodName, args, opts);
- }
- else if (opts.eventObservable && opts.event) {
- return wrapEventObservable(opts.event, opts.element);
- }
- else if (opts.otherPromise) {
- return wrapOtherPromise(pluginObj, methodName, args, opts);
- }
- else {
- return wrapPromise(pluginObj, methodName, args, opts);
- }
- };
- };
- /**
- * @param pluginObj
- * @param methodName
- * @param opts
- * @private
- */
- function wrapInstance(pluginObj, methodName, opts) {
- if (opts === void 0) { opts = {}; }
- return function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- if (opts.sync) {
- return callInstance(pluginObj, methodName, args, opts);
- }
- else if (opts.observable) {
- return new rxjs.Observable(function (observer) {
- var pluginResult;
- if (opts.destruct) {
- pluginResult = callInstance(pluginObj, methodName, args, opts, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return observer.next(args);
- }, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return observer.error(args);
- });
- }
- else {
- pluginResult = callInstance(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
- }
- if (pluginResult && pluginResult.error) {
- observer.error(pluginResult.error);
- }
- return function () {
- try {
- if (opts.clearWithArgs) {
- return callInstance(pluginObj, opts.clearFunction, args, opts, observer.next.bind(observer), observer.error.bind(observer));
- }
- return callInstance(pluginObj, opts.clearFunction, []);
- }
- catch (e) {
- console.warn('Unable to clear the previous observable watch for', pluginObj.constructor.getPluginName(), methodName);
- console.warn(e);
- }
- };
- });
- }
- else if (opts.otherPromise) {
- return getPromise(function (resolve, reject) {
- var result;
- if (opts.destruct) {
- result = callInstance(pluginObj, methodName, args, opts, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return resolve(args);
- }, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return reject(args);
- });
- }
- else {
- result = callInstance(pluginObj, methodName, args, opts, resolve, reject);
- }
- if (result && result.then) {
- result.then(resolve, reject);
- }
- else {
- reject();
- }
- });
- }
- else {
- var pluginResult_1, rej_1;
- var p = getPromise(function (resolve, reject) {
- if (opts.destruct) {
- pluginResult_1 = callInstance(pluginObj, methodName, args, opts, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return resolve(args);
- }, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- return reject(args);
- });
- }
- else {
- pluginResult_1 = callInstance(pluginObj, methodName, args, opts, resolve, reject);
- }
- rej_1 = reject;
- });
- // Angular throws an error on unhandled rejection, but in this case we have already printed
- // a warning that Cordova is undefined or the plugin is uninstalled, so there is no reason
- // to error
- if (pluginResult_1 && pluginResult_1.error) {
- p.catch(function () { });
- typeof rej_1 === 'function' && rej_1(pluginResult_1.error);
- }
- return p;
- }
- };
- }
- /**
- * @param element
- * @param path
- * @private
- */
- function get(element, path) {
- var paths = path.split('.');
- var obj = element;
- for (var i = 0; i < paths.length; i++) {
- if (!obj) {
- return null;
- }
- obj = obj[paths[i]];
- }
- return obj;
- }
- var AwesomeCordovaNativePlugin = /** @class */ (function () {
- function AwesomeCordovaNativePlugin() {
- }
- /**
- * Returns a boolean that indicates whether the plugin is installed
- *
- * @returns {boolean}
- */
- AwesomeCordovaNativePlugin.installed = function () {
- var isAvailable = checkAvailability(this.pluginRef) === true;
- return isAvailable;
- };
- /**
- * Returns the original plugin object
- */
- AwesomeCordovaNativePlugin.getPlugin = function () {
- if (typeof window !== 'undefined') {
- return get(window, this.pluginRef);
- }
- return null;
- };
- /**
- * Returns the plugin's name
- */
- AwesomeCordovaNativePlugin.getPluginName = function () {
- var pluginName = this.pluginName;
- return pluginName;
- };
- /**
- * Returns the plugin's reference
- */
- AwesomeCordovaNativePlugin.getPluginRef = function () {
- var pluginRef = this.pluginRef;
- return pluginRef;
- };
- /**
- * Returns the plugin's install name
- */
- AwesomeCordovaNativePlugin.getPluginInstallName = function () {
- var plugin = this.plugin;
- return plugin;
- };
- /**
- * Returns the plugin's supported platforms
- */
- AwesomeCordovaNativePlugin.getSupportedPlatforms = function () {
- var platform = this.platforms;
- return platform;
- };
- AwesomeCordovaNativePlugin.pluginName = '';
- AwesomeCordovaNativePlugin.pluginRef = '';
- AwesomeCordovaNativePlugin.plugin = '';
- AwesomeCordovaNativePlugin.repo = '';
- AwesomeCordovaNativePlugin.platforms = [];
- AwesomeCordovaNativePlugin.install = '';
- return AwesomeCordovaNativePlugin;
- }());
- /**
- * @param pluginObj
- * @param methodName
- * @param config
- * @param args
- */
- function cordova(pluginObj, methodName, config, args) {
- return wrap(pluginObj, methodName, config).apply(this, args);
- }
- /**
- * @param pluginObj
- * @param methodName
- */
- function overrideFunction(pluginObj, methodName) {
- return new rxjs.Observable(function (observer) {
- var availabilityCheck = checkAvailability(pluginObj, methodName);
- if (availabilityCheck === true) {
- var pluginInstance_1 = getPlugin(pluginObj.constructor.getPluginRef());
- pluginInstance_1[methodName] = observer.next.bind(observer);
- return function () { return (pluginInstance_1[methodName] = function () { }); };
- }
- else {
- observer.error(availabilityCheck);
- observer.complete();
- }
- });
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param args
- */
- function cordovaFunctionOverride(pluginObj, methodName, args) {
- return overrideFunction(pluginObj, methodName);
- }
- /**
- * @param pluginObj
- * @param methodName
- * @param config
- * @param args
- */
- function cordovaInstance(pluginObj, methodName, config, args) {
- args = Array.from(args);
- return wrapInstance(pluginObj, methodName, config).apply(this, args);
- }
- /**
- * @param pluginObj
- * @param key
- */
- function cordovaPropertyGet(pluginObj, key) {
- if (checkAvailability(pluginObj, key) === true) {
- return getPlugin(pluginObj.constructor.getPluginRef())[key];
- }
- return null;
- }
- /**
- * @param pluginObj
- * @param key
- * @param value
- */
- function cordovaPropertySet(pluginObj, key, value) {
- if (checkAvailability(pluginObj, key) === true) {
- getPlugin(pluginObj.constructor.getPluginRef())[key] = value;
- }
- }
- /**
- * @param pluginObj
- * @param key
- */
- function instancePropertyGet(pluginObj, key) {
- if (pluginObj._objectInstance && pluginObj._objectInstance[key]) {
- return pluginObj._objectInstance[key];
- }
- return null;
- }
- /**
- * @param pluginObj
- * @param key
- * @param value
- */
- function instancePropertySet(pluginObj, key, value) {
- if (pluginObj._objectInstance) {
- pluginObj._objectInstance[key] = value;
- }
- }
- checkReady();
- exports.AwesomeCordovaNativePlugin = AwesomeCordovaNativePlugin;
- exports.checkAvailability = checkAvailability;
- exports.cordova = cordova;
- exports.cordovaFunctionOverride = cordovaFunctionOverride;
- exports.cordovaInstance = cordovaInstance;
- exports.cordovaPropertyGet = cordovaPropertyGet;
- exports.cordovaPropertySet = cordovaPropertySet;
- exports.getPromise = getPromise;
- exports.instanceAvailability = instanceAvailability;
- exports.instancePropertyGet = instancePropertyGet;
- exports.instancePropertySet = instancePropertySet;
- exports.wrap = wrap;
|