Skip to content

Commit

Permalink
fix(plugin): climbing out of a bad merge hole
Browse files Browse the repository at this point in the history
  • Loading branch information
Vheissu committed Jul 10, 2017
1 parent efc6445 commit 1010d04
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 125 deletions.
4 changes: 2 additions & 2 deletions src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class Configure {
apiScript: 'https://maps.googleapis.com/maps/api/js',
apiKey: '',
apiLibraries: '',
region:'',
language:'',
region: '',
language: '',
options: {}
};
}
Expand Down
120 changes: 10 additions & 110 deletions src/google-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,17 @@ const logger = getLogger('aurelia-google-maps');

declare let google: any;

export interface BaseMarker {
export interface Marker {
icon?: string;
label?: string;
title?: string;
draggable?: boolean;
custom?: any;
infoWindow?: { pixelOffset?: number, content: string, position?: number, maxWidth?: number }
}

export interface AddressMarker extends BaseMarker {
address: string;
}

export interface LatLongMarker extends BaseMarker {
latitude: number | string;
longitude: number | string;
}

const isAddressMarker = (marker: Marker): marker is AddressMarker => {
return (<AddressMarker>marker).address !== undefined;
}

const isLatLongMarker = (marker: Marker): marker is LatLongMarker => {
return (<LatLongMarker>marker).latitude !== undefined && (<LatLongMarker>marker).longitude !== undefined;
}

export type Marker = AddressMarker | LatLongMarker;

@noView()
@customElement('google-map')
@inject(Element, TaskQueue, Configure, BindingEngine, EventAggregator, GoogleMapsAPI)
Expand All @@ -59,7 +42,6 @@ export class GoogleMaps {
private bindingEngine: BindingEngine;
private eventAggregator: EventAggregator;
private googleMapsApi: GoogleMapsAPI;
private validMarkers: LatLongMarker[];
private _geocoder: any;

@bindable longitude: number = 0;
Expand Down Expand Up @@ -105,23 +87,23 @@ export class GoogleMaps {
});
});

this.eventAggregator.subscribe('startMarkerHighlight', function(data: any) {
this.eventAggregator.subscribe('startMarkerHighlight', function (data: any) {
let mrkr: any = self._renderedMarkers[data.index];
mrkr.setIcon(mrkr.custom.altIcon);
mrkr.setZIndex((<any>window).google.maps.Marker.MAX_ZINDEX + 1);
});

this.eventAggregator.subscribe('stopMarkerHighLight', function(data: any) {
this.eventAggregator.subscribe('stopMarkerHighLight', function (data: any) {
let mrkr: any = self._renderedMarkers[data.index];
mrkr.setIcon( mrkr.custom.defaultIcon);
mrkr.setIcon(mrkr.custom.defaultIcon);
});

this.eventAggregator.subscribe('panToMarker', function(data: any) {
this.eventAggregator.subscribe('panToMarker', function (data: any) {
self.map.panTo(self._renderedMarkers[data.index].position);
self.map.setZoom(17);
});

this.eventAggregator.subscribe(`clearMarkers`, function() {
this.eventAggregator.subscribe(`clearMarkers`, function () {
this.clearMarkers();
});
}
Expand All @@ -131,7 +113,7 @@ export class GoogleMaps {
return;
}

this._renderedMarkers.forEach(function(marker: any) {
this._renderedMarkers.forEach(function (marker: any) {
marker.setMap(null);
});

Expand All @@ -158,7 +140,6 @@ export class GoogleMaps {
mapTypeId: mapTypeId
});


this.map = new (<any>window).google.maps.Map(this.element, options);
if (this.mapLoaded) {
this.mapLoaded(this.map);
Expand Down Expand Up @@ -224,7 +205,7 @@ export class GoogleMaps {
*
*/
renderMarker(marker: Marker): Promise<void> {
let markerLatLng = new (<any>window).google.maps.LatLng(parseFloat(<string>(<LatLongMarker>marker).latitude), parseFloat(<string>(<LatLongMarker>marker).longitude));
let markerLatLng = new (<any>window).google.maps.LatLng(parseFloat(<string>marker.latitude), parseFloat(<string>marker.longitude));

return this._mapPromise.then(() => {
// Create the marker
Expand Down Expand Up @@ -298,69 +279,6 @@ export class GoogleMaps {
});
}

/**
* Geocodes an address, once the Google Map script
* has been properly loaded and promise instantiated.
*
* @param address string
* @param geocoder any
*
*/
geocodeAddress(address: string) {
this.geocode(address).then(firstResult => {
this.setCenter(firstResult.geometry.location);
this.createMarker({
map: this.map,
position: firstResult.geometry.location
}).then((createdMarker: any) => {
this._locationByAddressMarkers.push(createdMarker);
this.eventAggregator.publish(LOCATIONADDED, Object.assign(createdMarker, { placeId: firstResult.place_id }));
});
}).catch(console.info);
}

/**
* Geocodes Address and returns the coordinates once the google map has been properly initialized
*
* @param marker string
*
*/
addressMarkerToMarker(marker: AddressMarker): Promise<void | BaseMarker> {
return this.geocode(marker.address).then(firstResults => {
return {
... marker,
latitude: firstResults.geometry.location.lat(),
longitude: firstResults.geometry.location.lng(),
};
}).catch(console.info);
}

/**
* Geocodes Address and returns the firstresults object after google maps has initialized
*
* @param address string
*
*/
private geocode(address: string): Promise<any> {
return this._mapPromise.then(() => {
return new Promise((resolve, reject) => {
this.geocoder.geocode({ 'address': address }, (results: any, status: string) => {
if (status !== (<any>window).google.maps.GeocoderStatus.OK) {
reject(new Error(`Failed to geocode address '${address}' with status: ${status}`));
}
resolve(results[0]);
});
});
});
}

private get geocoder() {
if (!this._geocoder) {
this._geocoder = new (<any>window).google.maps.Geocoder;
}
return this._geocoder;
}

/**
* Get Current Position
*
Expand Down Expand Up @@ -462,26 +380,8 @@ export class GoogleMaps {

// Render all markers again
this._mapPromise.then(() => {
Promise.all<LatLongMarker>(
newValue.map(marker => {
if (isAddressMarker(marker) && !isLatLongMarker(marker)) {
return this.addressMarkerToMarker(marker);
} else {
}
return marker;
})
).then(validMarkers => {
// Addresses that fail to parse return undefined (because the error is caught earlier in the promise chain)
this.validMarkers = validMarkers.filter(marker => typeof marker !== 'undefined');
return Promise.all(this.validMarkers.map(this.renderMarker.bind(this)));
}).then(() => {
/**
* We queue up a task to update the bounds, because in the case of multiple bound properties changing all at once,
* we need to let Aurelia handle updating the other properties before we actually trigger a re-render of the map
*/
this.taskQueue.queueTask(() => {
this.zoomToMarkerBounds();
});
let markerPromises = newValue.map(marker => {
return this.renderMarker(marker);
});

// Wait until all of the renderMarker calls have been resolved
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FrameworkConfiguration} from 'aurelia-framework';
import { FrameworkConfiguration } from 'aurelia-framework';
import { PLATFORM, DOM } from 'aurelia-pal';

import { Configure } from './configure';
Expand All @@ -11,7 +11,7 @@ export function configure(aurelia: FrameworkConfiguration, configCallback?: (con
DOM.injectStyles(`google-map { display: block; height: 350px; }`);

// Do we have a callback function?
if (configCallback !== undefined && typeof(configCallback) === 'function') {
if (configCallback !== undefined && typeof (configCallback) === 'function') {
configCallback(instance);
}

Expand Down
12 changes: 6 additions & 6 deletions test/unit/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ describe('configure', () => {
expect(sut._config).toEqual({
apiScript: 'https://maps.googleapis.com/maps/api/js',
apiKey: '',
region:'',
language:'',
region: '',
language: '',
apiLibraries: '',
options: {}
});
});

it('set a new option using options', () => {
expect(sut.get('test-option')).toBeUndefined();
sut.options({'test-option': 'test value123'});

sut.options({ 'test-option': 'test value123' });

expect(sut.get('test-option')).toEqual('test value123');
});


it('override default option using options', () => {
expect(sut.get('apiScript')).toEqual('https://maps.googleapis.com/maps/api/js');
sut.options({'apiScript': 'http://facebook.com/123'});

sut.options({ 'apiScript': 'http://facebook.com/123' });

expect(sut.get('apiScript')).toEqual('http://facebook.com/123');
});
Expand Down
10 changes: 5 additions & 5 deletions test/unit/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {configure} from '../../src/index';
import { configure } from '../../src/index';
import { Configure } from '../../src/configure';
import {FrameworkConfiguration} from 'aurelia-framework';
import { FrameworkConfiguration } from 'aurelia-framework';

let mockFrameWorkConfiguration = {
container: {
get: function(instance: string) {
get: function (instance: string) {
return instance;
}
},
globalResources: function(resources: any): FrameworkConfiguration {
globalResources: function (resources: any): FrameworkConfiguration {
return resources;
}
} as FrameworkConfiguration;
Expand All @@ -24,7 +24,7 @@ describe('index', () => {
});

it('configure options callback', () => {
let configureCallback = function(instance: any) {
let configureCallback = function (instance: any) {
return instance;
}

Expand Down

0 comments on commit 1010d04

Please sign in to comment.