From 7d1686ef93d61b92f50fad559fae5d5a0609e65f Mon Sep 17 00:00:00 2001 From: Ramon Henrique Ornelas Date: Sat, 8 Oct 2016 21:29:53 -0300 Subject: [PATCH] refactor(googlemaps): throw warnings plugin_not_installed (#655) * refactor(googlemaps): adjust imports to warnings console and delete @author comments * refactor(googlemaps): throw warnings plugin_not_installed * refactor(googlemaps): delete warnings of the methods added Promise.reject case plugin_not_instalet --- src/plugins/googlemaps.ts | 69 +++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/src/plugins/googlemaps.ts b/src/plugins/googlemaps.ts index b98c73de4..f2b20c065 100644 --- a/src/plugins/googlemaps.ts +++ b/src/plugins/googlemaps.ts @@ -1,11 +1,6 @@ -import {Cordova, CordovaInstance, Plugin, InstanceProperty} from './plugin'; +import { Cordova, CordovaInstance, Plugin, InstanceProperty, getPlugin, pluginWarn } from './plugin'; import { Observable } from 'rxjs/Observable'; - -/** - * @private - * Created by Ibrahim on 3/29/2016. - */ declare var plugin: any; /** @@ -88,12 +83,13 @@ export const GoogleMapsAnimation = { * }); * ``` */ -@Plugin({ +let pluginMap = { pluginRef: 'plugin.google.maps.Map', plugin: 'cordova-plugin-googlemaps', repo: 'https://github.com/mapsplugin/cordova-plugin-googlemaps', install: 'ionic plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"' -}) +}; +@Plugin(pluginMap) export class GoogleMap { _objectInstance: any; @@ -106,8 +102,14 @@ export class GoogleMap { static isAvailable(): Promise { return; } constructor(element: string|HTMLElement, options?: any) { - if (typeof element === 'string') element = document.getElementById(element); - this._objectInstance = plugin.google.maps.Map.getMap(element, options); + if (!!getPlugin('plugin.google.maps.Map')) { + if (typeof element === 'string') { + element = document.getElementById(element); + } + this._objectInstance = plugin.google.maps.Map.getMap(element, options); + } else { + pluginWarn(pluginMap); + } } /** @@ -116,6 +118,12 @@ export class GoogleMap { * @return {Observable} */ on(event: any): Observable { + if (!this._objectInstance) { + return new Observable((observer) => { + observer.error({ error: 'plugin_not_installed' }); + }); + } + return new Observable( (observer) => { this._objectInstance.on(event, observer.next.bind(observer)); @@ -130,6 +138,9 @@ export class GoogleMap { * @return {Promise} */ one(event: any): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( resolve => this._objectInstance.one(event, resolve) ); @@ -207,7 +218,10 @@ export class GoogleMap { @CordovaInstance({ sync: true }) setAllGesturesEnabled(enabled: boolean): void { } - addMarker(options: GoogleMapsMarkerOptions): Promise { + addMarker(options: GoogleMapsMarkerOptions): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( (resolve, reject) => { this._objectInstance.addMarker(options, (marker: any) => { @@ -221,7 +235,10 @@ export class GoogleMap { ); } - addCircle(options: GoogleMapsCircleOptions): Promise { + addCircle(options: GoogleMapsCircleOptions): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( (resolve, reject) => { this._objectInstance.addCircle(options, (circle: any) => { @@ -235,7 +252,10 @@ export class GoogleMap { ); } - addPolygon(options: GoogleMapsPolygonOptions): Promise { + addPolygon(options: GoogleMapsPolygonOptions): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( (resolve, reject) => { this._objectInstance.addPolygon(options, (polygon: any) => { @@ -249,7 +269,10 @@ export class GoogleMap { ); } - addPolyline(options: GoogleMapsPolylineOptions): Promise { + addPolyline(options: GoogleMapsPolylineOptions): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( (resolve, reject) => { this._objectInstance.addPolyline(options, (polyline: any) => { @@ -263,7 +286,10 @@ export class GoogleMap { ); } - addTileOverlay(options: GoogleMapsTileOverlayOptions): Promise { + addTileOverlay(options: GoogleMapsTileOverlayOptions): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( (resolve, reject) => { this._objectInstance.addTileOverlay(options, (tileOverlay: any) => { @@ -277,7 +303,10 @@ export class GoogleMap { ); } - addGroundOverlay(options: GoogleMapsGroundOverlayOptions): Promise { + addGroundOverlay(options: GoogleMapsGroundOverlayOptions): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( (resolve, reject) => { this._objectInstance.addGroundOverlay(options, (groundOverlay: any) => { @@ -291,7 +320,10 @@ export class GoogleMap { ); } - addKmlOverlay(options: GoogleMapsKmlOverlayOptions): Promise { + addKmlOverlay(options: GoogleMapsKmlOverlayOptions): Promise { + if (!this._objectInstance) { + return Promise.reject({ error: 'plugin_not_installed' }); + } return new Promise( (resolve, reject) => { this._objectInstance.addKmlOverlay(options, (kmlOverlay: any) => { @@ -942,9 +974,10 @@ export class Geocoder { * @param {GeocoderRequest} request Request object with either an address or a position * @returns {Promise} */ - static geocode(request: GeocoderRequest): Promise { + static geocode(request: GeocoderRequest): Promise { return new Promise((resolve, reject) => { if (!plugin || !plugin.google || !plugin.google.maps || !plugin.google.maps.Geocoder) { + pluginWarn(pluginMap); reject({ error: 'plugin_not_installed' }); } else { plugin.google.maps.Geocoder.geocode(request, resolve);