1 | import { fromEvent, Observable } from 'rxjs';
|
2 | export var ERR_CORDOVA_NOT_AVAILABLE = { error: 'cordova_not_available' };
|
3 | export var ERR_PLUGIN_NOT_INSTALLED = { error: 'plugin_not_installed' };
|
4 | export function getPromise(callback) {
|
5 | var tryNativePromise = function () {
|
6 | if (Promise) {
|
7 | return new Promise(function (resolve, reject) {
|
8 | callback(resolve, reject);
|
9 | });
|
10 | }
|
11 | else {
|
12 | 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.');
|
13 | }
|
14 | };
|
15 | if (typeof window !== 'undefined' && window.angular) {
|
16 | var doc = window.document;
|
17 | var injector = window.angular
|
18 | .element(doc.querySelector('[ng-app]') || doc.body)
|
19 | .injector();
|
20 | if (injector) {
|
21 | var $q = injector.get('$q');
|
22 | return $q(function (resolve, reject) {
|
23 | callback(resolve, reject);
|
24 | });
|
25 | }
|
26 | 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.");
|
27 | }
|
28 | return tryNativePromise();
|
29 | }
|
30 | export function wrapPromise(pluginObj, methodName, args, opts) {
|
31 | if (opts === void 0) { opts = {}; }
|
32 | var pluginResult, rej;
|
33 | var p = getPromise(function (resolve, reject) {
|
34 | if (opts.destruct) {
|
35 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function () {
|
36 | var args = [];
|
37 | for (var _i = 0; _i < arguments.length; _i++) {
|
38 | args[_i] = arguments[_i];
|
39 | }
|
40 | return resolve(args);
|
41 | }, function () {
|
42 | var args = [];
|
43 | for (var _i = 0; _i < arguments.length; _i++) {
|
44 | args[_i] = arguments[_i];
|
45 | }
|
46 | return reject(args);
|
47 | });
|
48 | }
|
49 | else {
|
50 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
|
51 | }
|
52 | rej = reject;
|
53 | });
|
54 |
|
55 |
|
56 |
|
57 | if (pluginResult && pluginResult.error) {
|
58 | p.catch(function () { });
|
59 | typeof rej === 'function' && rej(pluginResult.error);
|
60 | }
|
61 | return p;
|
62 | }
|
63 | function wrapOtherPromise(pluginObj, methodName, args, opts) {
|
64 | if (opts === void 0) { opts = {}; }
|
65 | return getPromise(function (resolve, reject) {
|
66 | var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts);
|
67 | if (pluginResult) {
|
68 | if (pluginResult.error) {
|
69 | reject(pluginResult.error);
|
70 | }
|
71 | else if (pluginResult.then) {
|
72 | pluginResult.then(resolve).catch(reject);
|
73 | }
|
74 | }
|
75 | else {
|
76 | reject({ error: 'unexpected_error' });
|
77 | }
|
78 | });
|
79 | }
|
80 | function wrapObservable(pluginObj, methodName, args, opts) {
|
81 | if (opts === void 0) { opts = {}; }
|
82 | return new Observable(function (observer) {
|
83 | var pluginResult;
|
84 | if (opts.destruct) {
|
85 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function () {
|
86 | var args = [];
|
87 | for (var _i = 0; _i < arguments.length; _i++) {
|
88 | args[_i] = arguments[_i];
|
89 | }
|
90 | return observer.next(args);
|
91 | }, function () {
|
92 | var args = [];
|
93 | for (var _i = 0; _i < arguments.length; _i++) {
|
94 | args[_i] = arguments[_i];
|
95 | }
|
96 | return observer.error(args);
|
97 | });
|
98 | }
|
99 | else {
|
100 | pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
101 | }
|
102 | if (pluginResult && pluginResult.error) {
|
103 | observer.error(pluginResult.error);
|
104 | observer.complete();
|
105 | }
|
106 | return function () {
|
107 | try {
|
108 | if (opts.clearFunction) {
|
109 | if (opts.clearWithArgs) {
|
110 | return callCordovaPlugin(pluginObj, opts.clearFunction, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
111 | }
|
112 | return callCordovaPlugin(pluginObj, opts.clearFunction, []);
|
113 | }
|
114 | }
|
115 | catch (e) {
|
116 | console.warn('Unable to clear the previous observable watch for', pluginObj.constructor.getPluginName(), methodName);
|
117 | console.warn(e);
|
118 | }
|
119 | };
|
120 | });
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function wrapEventObservable(event, element) {
|
130 | element = (typeof window !== 'undefined' && element) ? get(window, element) : element || (typeof window !== 'undefined' ? window : {});
|
131 | return fromEvent(element, event);
|
132 | }
|
133 | export function checkAvailability(plugin, methodName, pluginName) {
|
134 | var pluginRef, pluginInstance, pluginPackage;
|
135 | if (typeof plugin === 'string') {
|
136 | pluginRef = plugin;
|
137 | }
|
138 | else {
|
139 | pluginRef = plugin.constructor.getPluginRef();
|
140 | pluginName = plugin.constructor.getPluginName();
|
141 | pluginPackage = plugin.constructor.getPluginInstallName();
|
142 | }
|
143 | pluginInstance = getPlugin(pluginRef);
|
144 | if (!pluginInstance || (!!methodName && typeof pluginInstance[methodName] === 'undefined')) {
|
145 | if (typeof window === 'undefined' || !window.cordova) {
|
146 | cordovaWarn(pluginName, methodName);
|
147 | return ERR_CORDOVA_NOT_AVAILABLE;
|
148 | }
|
149 | pluginWarn(pluginName, pluginPackage, methodName);
|
150 | return ERR_PLUGIN_NOT_INSTALLED;
|
151 | }
|
152 | return true;
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | export function instanceAvailability(pluginObj, methodName) {
|
159 | return (pluginObj._objectInstance &&
|
160 | (!methodName || typeof pluginObj._objectInstance[methodName] !== 'undefined'));
|
161 | }
|
162 | export function setIndex(args, opts, resolve, reject) {
|
163 | if (opts === void 0) { opts = {}; }
|
164 |
|
165 | if (opts.sync) {
|
166 | return args;
|
167 | }
|
168 |
|
169 | if (opts.callbackOrder === 'reverse') {
|
170 |
|
171 | args.unshift(reject);
|
172 | args.unshift(resolve);
|
173 | }
|
174 | else if (opts.callbackStyle === 'node') {
|
175 | args.push(function (err, result) {
|
176 | if (err) {
|
177 | reject(err);
|
178 | }
|
179 | else {
|
180 | resolve(result);
|
181 | }
|
182 | });
|
183 | }
|
184 | else if (opts.callbackStyle === 'object' && opts.successName && opts.errorName) {
|
185 | var obj = {};
|
186 | obj[opts.successName] = resolve;
|
187 | obj[opts.errorName] = reject;
|
188 | args.push(obj);
|
189 | }
|
190 | else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
|
191 | var setSuccessIndex = function () {
|
192 |
|
193 | if (opts.successIndex > args.length) {
|
194 | args[opts.successIndex] = resolve;
|
195 | }
|
196 | else {
|
197 | args.splice(opts.successIndex, 0, resolve);
|
198 | }
|
199 | };
|
200 | var setErrorIndex = function () {
|
201 |
|
202 |
|
203 | if (opts.errorIndex > args.length) {
|
204 | args[opts.errorIndex] = reject;
|
205 | }
|
206 | else {
|
207 | args.splice(opts.errorIndex, 0, reject);
|
208 | }
|
209 | };
|
210 | if (opts.successIndex > opts.errorIndex) {
|
211 | setErrorIndex();
|
212 | setSuccessIndex();
|
213 | }
|
214 | else {
|
215 | setSuccessIndex();
|
216 | setErrorIndex();
|
217 | }
|
218 | }
|
219 | else {
|
220 |
|
221 |
|
222 | args.push(resolve);
|
223 | args.push(reject);
|
224 | }
|
225 | return args;
|
226 | }
|
227 | export function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
|
228 | if (opts === void 0) { opts = {}; }
|
229 |
|
230 |
|
231 | args = setIndex(args, opts, resolve, reject);
|
232 | var availabilityCheck = checkAvailability(pluginObj, methodName);
|
233 | if (availabilityCheck === true) {
|
234 | var pluginInstance = getPlugin(pluginObj.constructor.getPluginRef());
|
235 | return pluginInstance[methodName].apply(pluginInstance, args);
|
236 | }
|
237 | else {
|
238 | return availabilityCheck;
|
239 | }
|
240 | }
|
241 | export function callInstance(pluginObj, methodName, args, opts, resolve, reject) {
|
242 | if (opts === void 0) { opts = {}; }
|
243 | args = setIndex(args, opts, resolve, reject);
|
244 | if (instanceAvailability(pluginObj, methodName)) {
|
245 | return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
246 | }
|
247 | }
|
248 | export function getPlugin(pluginRef) {
|
249 | if (typeof window !== 'undefined') {
|
250 | return get(window, pluginRef);
|
251 | }
|
252 | return null;
|
253 | }
|
254 | export function get(element, path) {
|
255 | var paths = path.split('.');
|
256 | var obj = element;
|
257 | for (var i = 0; i < paths.length; i++) {
|
258 | if (!obj) {
|
259 | return null;
|
260 | }
|
261 | obj = obj[paths[i]];
|
262 | }
|
263 | return obj;
|
264 | }
|
265 | export function pluginWarn(pluginName, plugin, method) {
|
266 | if (method) {
|
267 | console.warn('Native: tried calling ' +
|
268 | pluginName +
|
269 | '.' +
|
270 | method +
|
271 | ', but the ' +
|
272 | pluginName +
|
273 | ' plugin is not installed.');
|
274 | }
|
275 | else {
|
276 | console.warn("Native: tried accessing the " + pluginName + " plugin but it's not installed.");
|
277 | }
|
278 | if (plugin) {
|
279 | console.warn("Install the " + pluginName + " plugin: 'ionic cordova plugin add " + plugin + "'");
|
280 | }
|
281 | }
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | export function cordovaWarn(pluginName, method) {
|
288 | if (typeof process === 'undefined') {
|
289 | if (method) {
|
290 | console.warn('Native: tried calling ' +
|
291 | pluginName +
|
292 | '.' +
|
293 | method +
|
294 | ', but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
|
295 | }
|
296 | else {
|
297 | console.warn('Native: tried accessing the ' +
|
298 | pluginName +
|
299 | ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
|
300 | }
|
301 | }
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 | export var wrap = function (pluginObj, methodName, opts) {
|
307 | if (opts === void 0) { opts = {}; }
|
308 | return function () {
|
309 | var args = [];
|
310 | for (var _i = 0; _i < arguments.length; _i++) {
|
311 | args[_i] = arguments[_i];
|
312 | }
|
313 | if (opts.sync) {
|
314 |
|
315 | return callCordovaPlugin(pluginObj, methodName, args, opts);
|
316 | }
|
317 | else if (opts.observable) {
|
318 | return wrapObservable(pluginObj, methodName, args, opts);
|
319 | }
|
320 | else if (opts.eventObservable && opts.event) {
|
321 | return wrapEventObservable(opts.event, opts.element);
|
322 | }
|
323 | else if (opts.otherPromise) {
|
324 | return wrapOtherPromise(pluginObj, methodName, args, opts);
|
325 | }
|
326 | else {
|
327 | return wrapPromise(pluginObj, methodName, args, opts);
|
328 | }
|
329 | };
|
330 | };
|
331 |
|
332 |
|
333 |
|
334 | export function wrapInstance(pluginObj, methodName, opts) {
|
335 | if (opts === void 0) { opts = {}; }
|
336 | return function () {
|
337 | var args = [];
|
338 | for (var _i = 0; _i < arguments.length; _i++) {
|
339 | args[_i] = arguments[_i];
|
340 | }
|
341 | if (opts.sync) {
|
342 | return callInstance(pluginObj, methodName, args, opts);
|
343 | }
|
344 | else if (opts.observable) {
|
345 | return new Observable(function (observer) {
|
346 | var pluginResult;
|
347 | if (opts.destruct) {
|
348 | pluginResult = callInstance(pluginObj, methodName, args, opts, function () {
|
349 | var args = [];
|
350 | for (var _i = 0; _i < arguments.length; _i++) {
|
351 | args[_i] = arguments[_i];
|
352 | }
|
353 | return observer.next(args);
|
354 | }, function () {
|
355 | var args = [];
|
356 | for (var _i = 0; _i < arguments.length; _i++) {
|
357 | args[_i] = arguments[_i];
|
358 | }
|
359 | return observer.error(args);
|
360 | });
|
361 | }
|
362 | else {
|
363 | pluginResult = callInstance(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
364 | }
|
365 | if (pluginResult && pluginResult.error) {
|
366 | observer.error(pluginResult.error);
|
367 | }
|
368 | return function () {
|
369 | try {
|
370 | if (opts.clearWithArgs) {
|
371 | return callInstance(pluginObj, opts.clearFunction, args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
372 | }
|
373 | return callInstance(pluginObj, opts.clearFunction, []);
|
374 | }
|
375 | catch (e) {
|
376 | console.warn('Unable to clear the previous observable watch for', pluginObj.constructor.getPluginName(), methodName);
|
377 | console.warn(e);
|
378 | }
|
379 | };
|
380 | });
|
381 | }
|
382 | else if (opts.otherPromise) {
|
383 | return getPromise(function (resolve, reject) {
|
384 | var result;
|
385 | if (opts.destruct) {
|
386 | result = callInstance(pluginObj, methodName, args, opts, function () {
|
387 | var args = [];
|
388 | for (var _i = 0; _i < arguments.length; _i++) {
|
389 | args[_i] = arguments[_i];
|
390 | }
|
391 | return resolve(args);
|
392 | }, function () {
|
393 | var args = [];
|
394 | for (var _i = 0; _i < arguments.length; _i++) {
|
395 | args[_i] = arguments[_i];
|
396 | }
|
397 | return reject(args);
|
398 | });
|
399 | }
|
400 | else {
|
401 | result = callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
402 | }
|
403 | if (result && result.then) {
|
404 | result.then(resolve, reject);
|
405 | }
|
406 | else {
|
407 | reject();
|
408 | }
|
409 | });
|
410 | }
|
411 | else {
|
412 | var pluginResult_1, rej_1;
|
413 | var p = getPromise(function (resolve, reject) {
|
414 | if (opts.destruct) {
|
415 | pluginResult_1 = callInstance(pluginObj, methodName, args, opts, function () {
|
416 | var args = [];
|
417 | for (var _i = 0; _i < arguments.length; _i++) {
|
418 | args[_i] = arguments[_i];
|
419 | }
|
420 | return resolve(args);
|
421 | }, function () {
|
422 | var args = [];
|
423 | for (var _i = 0; _i < arguments.length; _i++) {
|
424 | args[_i] = arguments[_i];
|
425 | }
|
426 | return reject(args);
|
427 | });
|
428 | }
|
429 | else {
|
430 | pluginResult_1 = callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
431 | }
|
432 | rej_1 = reject;
|
433 | });
|
434 |
|
435 |
|
436 |
|
437 | if (pluginResult_1 && pluginResult_1.error) {
|
438 | p.catch(function () { });
|
439 | typeof rej_1 === 'function' && rej_1(pluginResult_1.error);
|
440 | }
|
441 | return p;
|
442 | }
|
443 | };
|
444 | }
|
445 |
|
\ | No newline at end of file |