1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var rxjs = require('rxjs');
|
6 |
|
7 | function checkReady() {
|
8 | if (typeof process === 'undefined') {
|
9 | var win_1 = typeof window !== 'undefined' ? window : {};
|
10 | var DEVICE_READY_TIMEOUT_1 = 5000;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var before_1 = Date.now();
|
16 | var didFireReady_1 = false;
|
17 | win_1.document.addEventListener('deviceready', function () {
|
18 | console.log("Ionic Native: deviceready event fired after " + (Date.now() - before_1) + " ms");
|
19 | didFireReady_1 = true;
|
20 | });
|
21 | setTimeout(function () {
|
22 | if (!didFireReady_1 && win_1.cordova) {
|
23 | 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.");
|
24 | }
|
25 | }, DEVICE_READY_TIMEOUT_1);
|
26 | }
|
27 | }
|
28 |
|
29 | var ERR_CORDOVA_NOT_AVAILABLE = { error: 'cordova_not_available' };
|
30 | var ERR_PLUGIN_NOT_INSTALLED = { error: 'plugin_not_installed' };
|
31 | function getPromise(callback) {
|
32 | var tryNativePromise = function () {
|
33 | if (Promise) {
|
34 | return new Promise(function (resolve, reject) {
|
35 | callback(resolve, reject);
|
36 | });
|
37 | }
|
38 | else {
|
39 | 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.');
|
40 | }
|
41 | };
|
42 | if (typeof window !== 'undefined' && window.angular) {
|
43 | var doc = window.document;
|
44 | var injector = window.angular.element(doc.querySelector('[ng-app]') || doc.body).injector();
|
45 | if (injector) {
|
46 | var $q = injector.get('$q');
|
47 | return $q(function (resolve, reject) {
|
48 | callback(resolve, reject);
|
49 | });
|
50 | }
|
51 | 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.");
|
52 | }
|
53 | return tryNativePromise();
|
54 | }
|
55 | function wrapPromise(pluginObj, methodName, args, opts) {
|
56 | if (opts === void 0) { opts = {}; }
|
57 | var pluginResult, rej;
|
58 | var p = getPromise(function (resolve, reject) {
|
59 | if (opts.destruct) {
|
60 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function () {
|
61 | var args = [];
|
62 | for (var _i = 0; _i < arguments.length; _i++) {
|
63 | args[_i] = arguments[_i];
|
64 | }
|
65 | return resolve(args);
|
66 | }, function () {
|
67 | var args = [];
|
68 | for (var _i = 0; _i < arguments.length; _i++) {
|
69 | args[_i] = arguments[_i];
|
70 | }
|
71 | return reject(args);
|
72 | });
|
73 | }
|
74 | else {
|
75 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
|
76 | }
|
77 | rej = reject;
|
78 | });
|
79 |
|
80 |
|
81 |
|
82 | if (pluginResult && pluginResult.error) {
|
83 | p.catch(function () { });
|
84 | typeof rej === 'function' && rej(pluginResult.error);
|
85 | }
|
86 | return p;
|
87 | }
|
88 | function wrapOtherPromise(pluginObj, methodName, args, opts) {
|
89 | if (opts === void 0) { opts = {}; }
|
90 | return getPromise(function (resolve, reject) {
|
91 | var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts);
|
92 | if (pluginResult) {
|
93 | if (pluginResult.error) {
|
94 | reject(pluginResult.error);
|
95 | }
|
96 | else if (pluginResult.then) {
|
97 | pluginResult.then(resolve).catch(reject);
|
98 | }
|
99 | }
|
100 | else {
|
101 | reject({ error: 'unexpected_error' });
|
102 | }
|
103 | });
|
104 | }
|
105 | function wrapObservable(pluginObj, methodName, args, opts) {
|
106 | if (opts === void 0) { opts = {}; }
|
107 | return new rxjs.Observable(function (observer) {
|
108 | var pluginResult;
|
109 | if (opts.destruct) {
|
110 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function () {
|
111 | var args = [];
|
112 | for (var _i = 0; _i < arguments.length; _i++) {
|
113 | args[_i] = arguments[_i];
|
114 | }
|
115 | return observer.next(args);
|
116 | }, function () {
|
117 | var args = [];
|
118 | for (var _i = 0; _i < arguments.length; _i++) {
|
119 | args[_i] = arguments[_i];
|
120 | }
|
121 | return observer.error(args);
|
122 | });
|
123 | }
|
124 | else {
|
125 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
126 | }
|
127 | if (pluginResult && pluginResult.error) {
|
128 | observer.error(pluginResult.error);
|
129 | observer.complete();
|
130 | }
|
131 | return function () {
|
132 | try {
|
133 | if (opts.clearFunction) {
|
134 | if (opts.clearWithArgs) {
|
135 | return callCordovaPlugin(pluginObj, opts.clearFunction, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
136 | }
|
137 | return callCordovaPlugin(pluginObj, opts.clearFunction, []);
|
138 | }
|
139 | }
|
140 | catch (e) {
|
141 | console.warn('Unable to clear the previous observable watch for', pluginObj.constructor.getPluginName(), methodName);
|
142 | console.warn(e);
|
143 | }
|
144 | };
|
145 | });
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | function wrapEventObservable(event, element) {
|
155 | element =
|
156 | typeof window !== 'undefined' && element
|
157 | ? get$1(window, element)
|
158 | : element || (typeof window !== 'undefined' ? window : {});
|
159 | return rxjs.fromEvent(element, event);
|
160 | }
|
161 | function checkAvailability(plugin, methodName, pluginName) {
|
162 | var pluginRef, pluginInstance, pluginPackage;
|
163 | if (typeof plugin === 'string') {
|
164 | pluginRef = plugin;
|
165 | }
|
166 | else {
|
167 | pluginRef = plugin.constructor.getPluginRef();
|
168 | pluginName = plugin.constructor.getPluginName();
|
169 | pluginPackage = plugin.constructor.getPluginInstallName();
|
170 | }
|
171 | pluginInstance = getPlugin(pluginRef);
|
172 | if (!pluginInstance || (!!methodName && typeof pluginInstance[methodName] === 'undefined')) {
|
173 | if (typeof window === 'undefined' || !window.cordova) {
|
174 | cordovaWarn(pluginName, methodName);
|
175 | return ERR_CORDOVA_NOT_AVAILABLE;
|
176 | }
|
177 | pluginWarn(pluginName, pluginPackage, methodName);
|
178 | return ERR_PLUGIN_NOT_INSTALLED;
|
179 | }
|
180 | return true;
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | function instanceAvailability(pluginObj, methodName) {
|
187 | return pluginObj._objectInstance && (!methodName || typeof pluginObj._objectInstance[methodName] !== 'undefined');
|
188 | }
|
189 | function setIndex(args, opts, resolve, reject) {
|
190 | if (opts === void 0) { opts = {}; }
|
191 |
|
192 | if (opts.sync) {
|
193 | return args;
|
194 | }
|
195 |
|
196 | if (opts.callbackOrder === 'reverse') {
|
197 |
|
198 | args.unshift(reject);
|
199 | args.unshift(resolve);
|
200 | }
|
201 | else if (opts.callbackStyle === 'node') {
|
202 | args.push(function (err, result) {
|
203 | if (err) {
|
204 | reject(err);
|
205 | }
|
206 | else {
|
207 | resolve(result);
|
208 | }
|
209 | });
|
210 | }
|
211 | else if (opts.callbackStyle === 'object' && opts.successName && opts.errorName) {
|
212 | var obj = {};
|
213 | obj[opts.successName] = resolve;
|
214 | obj[opts.errorName] = reject;
|
215 | args.push(obj);
|
216 | }
|
217 | else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
|
218 | var setSuccessIndex = function () {
|
219 |
|
220 | if (opts.successIndex > args.length) {
|
221 | args[opts.successIndex] = resolve;
|
222 | }
|
223 | else {
|
224 | args.splice(opts.successIndex, 0, resolve);
|
225 | }
|
226 | };
|
227 | var setErrorIndex = function () {
|
228 |
|
229 |
|
230 | if (opts.errorIndex > args.length) {
|
231 | args[opts.errorIndex] = reject;
|
232 | }
|
233 | else {
|
234 | args.splice(opts.errorIndex, 0, reject);
|
235 | }
|
236 | };
|
237 | if (opts.successIndex > opts.errorIndex) {
|
238 | setErrorIndex();
|
239 | setSuccessIndex();
|
240 | }
|
241 | else {
|
242 | setSuccessIndex();
|
243 | setErrorIndex();
|
244 | }
|
245 | }
|
246 | else {
|
247 |
|
248 |
|
249 | args.push(resolve);
|
250 | args.push(reject);
|
251 | }
|
252 | return args;
|
253 | }
|
254 | function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
|
255 | if (opts === void 0) { opts = {}; }
|
256 |
|
257 |
|
258 | args = setIndex(args, opts, resolve, reject);
|
259 | var availabilityCheck = checkAvailability(pluginObj, methodName);
|
260 | if (availabilityCheck === true) {
|
261 | var pluginInstance = getPlugin(pluginObj.constructor.getPluginRef());
|
262 | return pluginInstance[methodName].apply(pluginInstance, args);
|
263 | }
|
264 | else {
|
265 | return availabilityCheck;
|
266 | }
|
267 | }
|
268 | function callInstance(pluginObj, methodName, args, opts, resolve, reject) {
|
269 | if (opts === void 0) { opts = {}; }
|
270 | args = setIndex(args, opts, resolve, reject);
|
271 | if (instanceAvailability(pluginObj, methodName)) {
|
272 | return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
273 | }
|
274 | }
|
275 | function getPlugin(pluginRef) {
|
276 | if (typeof window !== 'undefined') {
|
277 | return get$1(window, pluginRef);
|
278 | }
|
279 | return null;
|
280 | }
|
281 | function get$1(element, path) {
|
282 | var paths = path.split('.');
|
283 | var obj = element;
|
284 | for (var i = 0; i < paths.length; i++) {
|
285 | if (!obj) {
|
286 | return null;
|
287 | }
|
288 | obj = obj[paths[i]];
|
289 | }
|
290 | return obj;
|
291 | }
|
292 | function pluginWarn(pluginName, plugin, method) {
|
293 | if (method) {
|
294 | console.warn('Native: tried calling ' + pluginName + '.' + method + ', but the ' + pluginName + ' plugin is not installed.');
|
295 | }
|
296 | else {
|
297 | console.warn("Native: tried accessing the " + pluginName + " plugin but it's not installed.");
|
298 | }
|
299 | if (plugin) {
|
300 | console.warn("Install the " + pluginName + " plugin: 'ionic cordova plugin add " + plugin + "'");
|
301 | }
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | function cordovaWarn(pluginName, method) {
|
309 | if (typeof process === 'undefined') {
|
310 | if (method) {
|
311 | console.warn('Native: tried calling ' +
|
312 | pluginName +
|
313 | '.' +
|
314 | method +
|
315 | ', but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
|
316 | }
|
317 | else {
|
318 | console.warn('Native: tried accessing the ' +
|
319 | pluginName +
|
320 | ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
|
321 | }
|
322 | }
|
323 | }
|
324 |
|
325 |
|
326 |
|
327 | var wrap = function (pluginObj, methodName, opts) {
|
328 | if (opts === void 0) { opts = {}; }
|
329 | return function () {
|
330 | var args = [];
|
331 | for (var _i = 0; _i < arguments.length; _i++) {
|
332 | args[_i] = arguments[_i];
|
333 | }
|
334 | if (opts.sync) {
|
335 |
|
336 | return callCordovaPlugin(pluginObj, methodName, args, opts);
|
337 | }
|
338 | else if (opts.observable) {
|
339 | return wrapObservable(pluginObj, methodName, args, opts);
|
340 | }
|
341 | else if (opts.eventObservable && opts.event) {
|
342 | return wrapEventObservable(opts.event, opts.element);
|
343 | }
|
344 | else if (opts.otherPromise) {
|
345 | return wrapOtherPromise(pluginObj, methodName, args, opts);
|
346 | }
|
347 | else {
|
348 | return wrapPromise(pluginObj, methodName, args, opts);
|
349 | }
|
350 | };
|
351 | };
|
352 |
|
353 |
|
354 |
|
355 | function wrapInstance(pluginObj, methodName, opts) {
|
356 | if (opts === void 0) { opts = {}; }
|
357 | return function () {
|
358 | var args = [];
|
359 | for (var _i = 0; _i < arguments.length; _i++) {
|
360 | args[_i] = arguments[_i];
|
361 | }
|
362 | if (opts.sync) {
|
363 | return callInstance(pluginObj, methodName, args, opts);
|
364 | }
|
365 | else if (opts.observable) {
|
366 | return new rxjs.Observable(function (observer) {
|
367 | var pluginResult;
|
368 | if (opts.destruct) {
|
369 | pluginResult = callInstance(pluginObj, methodName, args, opts, function () {
|
370 | var args = [];
|
371 | for (var _i = 0; _i < arguments.length; _i++) {
|
372 | args[_i] = arguments[_i];
|
373 | }
|
374 | return observer.next(args);
|
375 | }, function () {
|
376 | var args = [];
|
377 | for (var _i = 0; _i < arguments.length; _i++) {
|
378 | args[_i] = arguments[_i];
|
379 | }
|
380 | return observer.error(args);
|
381 | });
|
382 | }
|
383 | else {
|
384 | pluginResult = callInstance(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
385 | }
|
386 | if (pluginResult && pluginResult.error) {
|
387 | observer.error(pluginResult.error);
|
388 | }
|
389 | return function () {
|
390 | try {
|
391 | if (opts.clearWithArgs) {
|
392 | return callInstance(pluginObj, opts.clearFunction, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
393 | }
|
394 | return callInstance(pluginObj, opts.clearFunction, []);
|
395 | }
|
396 | catch (e) {
|
397 | console.warn('Unable to clear the previous observable watch for', pluginObj.constructor.getPluginName(), methodName);
|
398 | console.warn(e);
|
399 | }
|
400 | };
|
401 | });
|
402 | }
|
403 | else if (opts.otherPromise) {
|
404 | return getPromise(function (resolve, reject) {
|
405 | var result;
|
406 | if (opts.destruct) {
|
407 | result = callInstance(pluginObj, methodName, args, opts, function () {
|
408 | var args = [];
|
409 | for (var _i = 0; _i < arguments.length; _i++) {
|
410 | args[_i] = arguments[_i];
|
411 | }
|
412 | return resolve(args);
|
413 | }, function () {
|
414 | var args = [];
|
415 | for (var _i = 0; _i < arguments.length; _i++) {
|
416 | args[_i] = arguments[_i];
|
417 | }
|
418 | return reject(args);
|
419 | });
|
420 | }
|
421 | else {
|
422 | result = callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
423 | }
|
424 | if (result && result.then) {
|
425 | result.then(resolve, reject);
|
426 | }
|
427 | else {
|
428 | reject();
|
429 | }
|
430 | });
|
431 | }
|
432 | else {
|
433 | var pluginResult_1, rej_1;
|
434 | var p = getPromise(function (resolve, reject) {
|
435 | if (opts.destruct) {
|
436 | pluginResult_1 = callInstance(pluginObj, methodName, args, opts, function () {
|
437 | var args = [];
|
438 | for (var _i = 0; _i < arguments.length; _i++) {
|
439 | args[_i] = arguments[_i];
|
440 | }
|
441 | return resolve(args);
|
442 | }, function () {
|
443 | var args = [];
|
444 | for (var _i = 0; _i < arguments.length; _i++) {
|
445 | args[_i] = arguments[_i];
|
446 | }
|
447 | return reject(args);
|
448 | });
|
449 | }
|
450 | else {
|
451 | pluginResult_1 = callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
452 | }
|
453 | rej_1 = reject;
|
454 | });
|
455 |
|
456 |
|
457 |
|
458 | if (pluginResult_1 && pluginResult_1.error) {
|
459 | p.catch(function () { });
|
460 | typeof rej_1 === 'function' && rej_1(pluginResult_1.error);
|
461 | }
|
462 | return p;
|
463 | }
|
464 | };
|
465 | }
|
466 |
|
467 |
|
468 |
|
469 |
|
470 | function get(element, path) {
|
471 | var paths = path.split('.');
|
472 | var obj = element;
|
473 | for (var i = 0; i < paths.length; i++) {
|
474 | if (!obj) {
|
475 | return null;
|
476 | }
|
477 | obj = obj[paths[i]];
|
478 | }
|
479 | return obj;
|
480 | }
|
481 |
|
482 | var IonicNativePlugin = (function () {
|
483 | function IonicNativePlugin() {
|
484 | }
|
485 | |
486 |
|
487 |
|
488 |
|
489 | IonicNativePlugin.installed = function () {
|
490 | var isAvailable = checkAvailability(this.pluginRef) === true;
|
491 | return isAvailable;
|
492 | };
|
493 | |
494 |
|
495 |
|
496 | IonicNativePlugin.getPlugin = function () {
|
497 | if (typeof window !== 'undefined') {
|
498 | return get(window, this.pluginRef);
|
499 | }
|
500 | return null;
|
501 | };
|
502 | /**
|
503 | * Returns the plugin's name
|
504 | */
|
505 | IonicNativePlugin.getPluginName = function () {
|
506 | var pluginName = this.pluginName;
|
507 | return pluginName;
|
508 | };
|
509 | |
510 |
|
511 |
|
512 | IonicNativePlugin.getPluginRef = function () {
|
513 | var pluginRef = this.pluginRef;
|
514 | return pluginRef;
|
515 | };
|
516 | |
517 |
|
518 |
|
519 | IonicNativePlugin.getPluginInstallName = function () {
|
520 | var plugin = this.plugin;
|
521 | return plugin;
|
522 | };
|
523 | |
524 |
|
525 |
|
526 | IonicNativePlugin.getSupportedPlatforms = function () {
|
527 | var platform = this.platforms;
|
528 | return platform;
|
529 | };
|
530 | IonicNativePlugin.pluginName = '';
|
531 | IonicNativePlugin.pluginRef = '';
|
532 | IonicNativePlugin.plugin = '';
|
533 | IonicNativePlugin.repo = '';
|
534 | IonicNativePlugin.platforms = [];
|
535 | IonicNativePlugin.install = '';
|
536 | return IonicNativePlugin;
|
537 | }());
|
538 |
|
539 | function cordova(pluginObj, methodName, config, args) {
|
540 | return wrap(pluginObj, methodName, config).apply(this, args);
|
541 | }
|
542 |
|
543 | function overrideFunction(pluginObj, methodName) {
|
544 | return new rxjs.Observable(function (observer) {
|
545 | var availabilityCheck = checkAvailability(pluginObj, methodName);
|
546 | if (availabilityCheck === true) {
|
547 | var pluginInstance_1 = getPlugin(pluginObj.constructor.getPluginRef());
|
548 | pluginInstance_1[methodName] = observer.next.bind(observer);
|
549 | return function () { return (pluginInstance_1[methodName] = function () { }); };
|
550 | }
|
551 | else {
|
552 | observer.error(availabilityCheck);
|
553 | observer.complete();
|
554 | }
|
555 | });
|
556 | }
|
557 | function cordovaFunctionOverride(pluginObj, methodName, args) {
|
558 | return overrideFunction(pluginObj, methodName);
|
559 | }
|
560 |
|
561 | function cordovaInstance(pluginObj, methodName, config, args) {
|
562 | args = Array.from(args);
|
563 | return wrapInstance(pluginObj, methodName, config).apply(this, args);
|
564 | }
|
565 |
|
566 | function cordovaPropertyGet(pluginObj, key) {
|
567 | if (checkAvailability(pluginObj, key) === true) {
|
568 | return getPlugin(pluginObj.constructor.getPluginRef())[key];
|
569 | }
|
570 | return null;
|
571 | }
|
572 | function cordovaPropertySet(pluginObj, key, value) {
|
573 | if (checkAvailability(pluginObj, key) === true) {
|
574 | getPlugin(pluginObj.constructor.getPluginRef())[key] = value;
|
575 | }
|
576 | }
|
577 |
|
578 | function instancePropertyGet(pluginObj, key) {
|
579 | if (pluginObj._objectInstance && pluginObj._objectInstance[key]) {
|
580 | return pluginObj._objectInstance[key];
|
581 | }
|
582 | return null;
|
583 | }
|
584 | function instancePropertySet(pluginObj, key, value) {
|
585 | if (pluginObj._objectInstance) {
|
586 | pluginObj._objectInstance[key] = value;
|
587 | }
|
588 | }
|
589 |
|
590 | checkReady();
|
591 |
|
592 | exports.IonicNativePlugin = IonicNativePlugin;
|
593 | exports.checkAvailability = checkAvailability;
|
594 | exports.cordova = cordova;
|
595 | exports.cordovaFunctionOverride = cordovaFunctionOverride;
|
596 | exports.cordovaInstance = cordovaInstance;
|
597 | exports.cordovaPropertyGet = cordovaPropertyGet;
|
598 | exports.cordovaPropertySet = cordovaPropertySet;
|
599 | exports.getPromise = getPromise;
|
600 | exports.instanceAvailability = instanceAvailability;
|
601 | exports.instancePropertyGet = instancePropertyGet;
|
602 | exports.instancePropertySet = instancePropertySet;
|
603 | exports.wrap = wrap;
|