Skip to content

Commit

Permalink
[WIP] [ADD TESTS] Move places search logic into service
Browse files Browse the repository at this point in the history
  • Loading branch information
asennikov committed May 1, 2016
1 parent 35d2c26 commit 4c28493
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 204 deletions.
51 changes: 11 additions & 40 deletions addon/components/g-map-address-marker.js
@@ -1,70 +1,41 @@
import Ember from 'ember';
import layout from '../templates/components/g-map-address-marker';
/* global google */

const { computed, observer, run, isPresent, isEmpty, typeOf } = Ember;
const { observer, run, typeOf, inject } = Ember;

const GMapAddressMarkerComponent = Ember.Component.extend({
layout: layout,
classNames: ['g-map-address-marker'],

map: computed.alias('mapContext.map'),
places: inject.service(),

didInsertElement() {
this._super();
this.initPlacesService();
},

mapWasSet: observer('map', function() {
run.once(this, 'initPlacesService');
}),

initPlacesService() {
const map = this.get('map');
let service = this.get('placesService');

if (isPresent(map) && isEmpty(service)) {
service = new google.maps.places.PlacesService(map);
this.set('placesService', service);
this.searchLocation();
}
this._super(...arguments);
this.searchLocation();
},

onAddressChanged: observer('address', function() {
run.once(this, 'searchLocation');
}),

searchLocation() {
const service = this.get('placesService');
const address = this.get('address');

if (isPresent(service) && isPresent(address)) {
const request = { query: address };

service.textSearch(request, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
this.updateLocation(results);
}
});
}
this.get('places').search(address)
.then((lat, lng, results) => this.updateLocation(lat, lng, results));
},

updateLocation(results) {
const lat = results[0].geometry.location.lat();
const lng = results[0].geometry.location.lng();

this.set('lat', lat);
this.set('lng', lng);
updateLocation(lat, lng, results) {
this.setProperties({ lat, lng });
this.sendOnLocationChange(lat, lng, results);
},

sendOnLocationChange() {
sendOnLocationChange(lat, lng, results) {
const { onLocationChange } = this.attrs;

if (typeOf(onLocationChange) === 'function') {
onLocationChange(...arguments);
onLocationChange(lat, lng, results);
} else {
this.sendAction('onLocationChange', ...arguments);
this.sendAction('onLocationChange', lat, lng, results);
}
}
});
Expand Down
67 changes: 14 additions & 53 deletions addon/components/g-map-address-route.js
@@ -1,90 +1,51 @@
import Ember from 'ember';
import layout from '../templates/components/g-map-address-route';
/* global google */

const { computed, observer, run, isPresent, isEmpty, typeOf } = Ember;
const { observer, run, typeOf, inject } = Ember;

const GMapAddressRouteComponent = Ember.Component.extend({
layout: layout,
classNames: ['g-map-address-route'],

map: computed.alias('mapContext.map'),
places: inject.service(),

didInsertElement() {
this._super();
this.initPlacesService();
},

mapWasSet: observer('map', function() {
run.once(this, 'initPlacesService');
}),

initPlacesService() {
const map = this.get('map');
let service = this.get('placesService');

if (isPresent(map) && isEmpty(service)) {
service = new google.maps.places.PlacesService(map);
this.set('placesService', service);
this.searchLocations();
}
this.searchLocations();
},

onAddressChanged: observer('originAddress', 'destinationAddress', function() {
run.once(this, 'searchLocations');
}),

searchLocations() {
const service = this.get('placesService');
const originAddress = this.get('originAddress');
const destinationAddress = this.get('destinationAddress');

if (isPresent(service) && isPresent(originAddress)) {
const originRequest = { query: originAddress };
this.get('places').search(originAddress)
.then((lat, lng, results) => this.updateOriginLocation(lat, lng, results));

service.textSearch(originRequest, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
this.updateOriginLocation(results);
}
});
}

if (isPresent(service) && isPresent(destinationAddress)) {
const destinationRequest = { query: destinationAddress };

service.textSearch(destinationRequest, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
this.updateDestinationLocation(results);
}
});
}
this.get('places').search(destinationAddress)
.then((lat, lng, results) => this.updateDestinationLocation(lat, lng, results));
},

updateOriginLocation(results) {
const lat = results[0].geometry.location.lat();
const lng = results[0].geometry.location.lng();

this.set('originLat', lat);
this.set('originLng', lng);
updateOriginLocation(lat, lng, results) {
this.setProperties({ originLat: lat, originLng: lng });
this.sendOnLocationsChange(lat, lng, results);
},

updateDestinationLocation(results) {
const lat = results[0].geometry.location.lat();
const lng = results[0].geometry.location.lng();

this.set('destinationLat', lat);
this.set('destinationLng', lng);
updateDestinationLocation(lat, lng, results) {
this.setProperties({ destinationLat: lat, destinationLng: lng });
this.sendOnLocationsChange(lat, lng, results);
},

sendOnLocationsChange() {
sendOnLocationsChange(lat, lng, results) {
const { onLocationsChange } = this.attrs;

if (typeOf(onLocationsChange) === 'function') {
onLocationsChange(...arguments);
onLocationsChange(lat, lng, results);
} else {
this.sendAction('onLocationsChange', ...arguments);
this.sendAction('onLocationsChange', lat, lng, results);
}
}
});
Expand Down
39 changes: 7 additions & 32 deletions addon/components/g-map-route-address-waypoint.js
@@ -1,56 +1,31 @@
import Ember from 'ember';
import layout from '../templates/components/g-map-route-address-waypoint';

const { isEmpty, isPresent, observer, computed, run } = Ember;
const { observer, run, inject } = Ember;

const GMapRouteAddressWaypointComponent = Ember.Component.extend({
layout: layout,
classNames: ['g-map-route-address-waypoint'],

map: computed.alias('routeContext.map'),
places: inject.service(),

didInsertElement() {
this._super();
this.initPlacesService();
this.searchLocation();
},

initPlacesService: Ember.observer('map', function() {
const map = this.get('map');
let service = this.get('placesService');

if (isPresent(map) && isEmpty(service)) {
service = new google.maps.places.PlacesService(map);
this.set('placesService', service);

this.searchLocation();
}
}),

onAddressChanged: observer('address', function() {
run.once(this, 'searchLocation');
}),

searchLocation() {
const service = this.get('placesService');
const address = this.get('address');

if (isPresent(service) && isPresent(address)) {
const request = { query: address };

service.textSearch(request, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
this.updateLocation(results);
}
});
}
this.get('places').search(address)
.then((lat, lng) => this.updateLocation(lat, lng));
},

updateLocation(results) {
const lat = results[0].geometry.location.lat();
const lng = results[0].geometry.location.lng();

this.set('lat', lat);
this.set('lng', lng);
updateLocation(lat, lng) {
this.setProperties({ lat, lng });
}
});

Expand Down
9 changes: 7 additions & 2 deletions addon/components/g-map.js
@@ -1,13 +1,16 @@
import Ember from 'ember';
import layout from '../templates/components/g-map';
/* global google */

const { isEmpty, isPresent, computed, observer, run } = Ember;
const { isEmpty, isPresent, computed, observer, run, inject } = Ember;

export default Ember.Component.extend({
layout: layout,
classNames: ['g-map'],
bannedOptions: Ember.A(['center', 'zoom']),

places: inject.service(),

init() {
this._super();
this.set('markers', Ember.A());
Expand All @@ -32,7 +35,9 @@ export default Ember.Component.extend({
if (isEmpty(this.get('map'))) {
const canvas = this.$().find('.g-map-canvas').get(0);
const options = this.get('permittedOptions');
this.set('map', new google.maps.Map(canvas, options));
const map = new google.maps.Map(canvas, options);
this.set('map', map);
this.get('places').start(map);
}
this.setZoom();
this.setCenter();
Expand Down
35 changes: 35 additions & 0 deletions addon/services/places.js
@@ -0,0 +1,35 @@
import Ember from 'ember';

const { isPresent, isEmpty, RSVP } = Ember;

export default Ember.Service.extend({
init() {
this._super(...arguments);
this.internalService = null;
},

start(map) {
if (isEmpty(this.get('internalService'))) {
this.set('internalService', new google.maps.places.PlacesService(map));
}
},

search(address) {
const service = this.get('internalService');

if (isPresent(service) && isPresent(address)) {
const request = { query: address };

return RSVP.Promise((resolve, reject) => {
service.textSearch(request, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
const firstLocation = results[0].geometry.location;
resolve(firstLocation.lat(), firstLocation.lng(), results);
} else {
reject();
}
});
});
}
}
});
1 change: 1 addition & 0 deletions app/services/places.js
@@ -0,0 +1 @@
export { default } from 'ember-g-map/services/places';

0 comments on commit 4c28493

Please sign in to comment.