-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from asennikov/address-marker
Address marker
- Loading branch information
Showing
10 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Ember from 'ember'; | ||
import layout from '../templates/components/g-map-address-marker'; | ||
/* global google */ | ||
|
||
const { computed, observer, run, isPresent, isEmpty } = Ember; | ||
|
||
const GMapAddressMarkerComponent = Ember.Component.extend({ | ||
layout: layout, | ||
classNames: ['g-map-address-marker'], | ||
|
||
map: computed.alias('mapContext.map'), | ||
|
||
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.updateLocation(); | ||
} | ||
}, | ||
|
||
onAddressChanged: observer('address', function() { | ||
run.once(this, 'updateLocation'); | ||
}), | ||
|
||
updateLocation() { | ||
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.set('lat', results[0].geometry.location.lat()); | ||
this.set('lng', results[0].geometry.location.lng()); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
GMapAddressMarkerComponent.reopenClass({ | ||
positionalParams: ['mapContext'] | ||
}); | ||
|
||
export default GMapAddressMarkerComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{{#if hasBlock}} | ||
{{#g-map-marker mapContext lat=lat lng=lng icon=icon group=group as |markerContext|}} | ||
{{yield markerContext}} | ||
{{/g-map-marker}} | ||
{{else}} | ||
{{g-map-marker mapContext lat=lat lng=lng icon=icon group=group}} | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-g-map/components/g-map-address-marker'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.g-map-canvas { | ||
width: 600px; | ||
width: 100%; | ||
height: 600px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { moduleForComponent, test } from 'ember-qunit'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
moduleForComponent('g-map-address-marker', 'Integration | Component | g map address marker', { | ||
integration: true | ||
}); | ||
|
||
test('it renders', function(assert) { | ||
assert.expect(2); | ||
|
||
// Set any properties with this.set('myProperty', 'value'); | ||
// Handle any actions with this.on('myAction', function(val) { ... }); | ||
|
||
this.render(hbs` | ||
{{#g-map as |context|}} | ||
{{g-map-address-marker context}} | ||
{{/g-map}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), ''); | ||
|
||
// Template block usage: | ||
this.render(hbs` | ||
{{#g-map as |context|}} | ||
{{#g-map-address-marker context}} | ||
template block text | ||
{{/g-map-address-marker}} | ||
{{/g-map}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), 'template block text'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import Ember from 'ember'; | ||
import { moduleForComponent } from 'ember-qunit'; | ||
import test from '../../ember-sinon-qunit/test'; | ||
import GMapComponent from 'ember-g-map/components/g-map'; | ||
import sinon from 'sinon'; | ||
|
||
const { run } = Ember; | ||
|
||
let fakePlacesService; | ||
let component; | ||
|
||
moduleForComponent('g-map-address-marker', 'Unit | Component | g map address marker', { | ||
// Specify the other units that are required for this test | ||
// needs: ['component:foo', 'helper:bar'], | ||
unit: true, | ||
|
||
beforeEach() { | ||
fakePlacesService = { | ||
textSearch: sinon.stub() | ||
}; | ||
sinon.stub(google.maps.places, 'PlacesService').returns(fakePlacesService); | ||
component = this.subject({ | ||
mapContext: new GMapComponent() | ||
}); | ||
}, | ||
|
||
afterEach() { | ||
google.maps.places.PlacesService.restore(); | ||
} | ||
}); | ||
|
||
test('it calls `initPlacesService` on `didInsertElement`', function() { | ||
component.initPlacesService = sinon.stub(); | ||
component.trigger('didInsertElement'); | ||
sinon.assert.calledOnce(component.initPlacesService); | ||
}); | ||
|
||
test('it constructs new `PlacesService` on `initPlacesService` call', function(assert) { | ||
run(() => component.set('map', {})); | ||
run(() => component.initPlacesService()); | ||
|
||
sinon.assert.calledOnce(google.maps.places.PlacesService); | ||
assert.equal(component.get('placesService'), fakePlacesService); | ||
}); | ||
|
||
test('new `PlacesService` isn\'t being constructed if it\'s already present in component', function() { | ||
run(() => component.setProperties({ | ||
placesService: fakePlacesService, | ||
map: {} | ||
})); | ||
run(() => component.initPlacesService()); | ||
|
||
sinon.assert.notCalled(google.maps.places.PlacesService); | ||
}); | ||
|
||
test('it triggers `initPlacesService` on `mapContext.map` change', function() { | ||
run(() => component.set('mapContext', { map: '' })); | ||
component.initPlacesService = sinon.spy(); | ||
run(() => component.set('mapContext.map', {})); | ||
sinon.assert.calledOnce(component.initPlacesService); | ||
}); | ||
|
||
test('it triggers `updateLocation` on `initPlacesService` call', function() { | ||
component.updateLocation = sinon.spy(); | ||
|
||
run(() => component.set('map', {})); | ||
run(() => component.initPlacesService()); | ||
|
||
sinon.assert.calledOnce(component.updateLocation); | ||
}); | ||
|
||
test('it triggers `updateLocation` on `address` change', function() { | ||
component.updateLocation = sinon.spy(); | ||
run(() => component.set('address', 'query string')); | ||
sinon.assert.calledOnce(component.updateLocation); | ||
}); | ||
|
||
test('it calls `textSearch` of placesService on `updateLocation`', function() { | ||
run(() => component.set('address', 'query string')); | ||
run(() => component.set('placesService', fakePlacesService)); | ||
|
||
fakePlacesService.textSearch = sinon.stub(); | ||
run(() => component.updateLocation()); | ||
|
||
const correctRequest = { query: 'query string' }; | ||
|
||
sinon.assert.calledOnce(fakePlacesService.textSearch); | ||
sinon.assert.calledWith(fakePlacesService.textSearch, correctRequest); | ||
}); | ||
|
||
test('it sets `lat` & `lng` of the first textSearch result on `updateLocation`', function(assert) { | ||
const results = [{ | ||
geometry: { | ||
location: { | ||
lat: () => 12, | ||
lng: () => -20 | ||
} | ||
} | ||
}, { | ||
geometry: { | ||
location: { | ||
lat: () => 24, | ||
lng: () => 100 | ||
} | ||
} | ||
}]; | ||
const status = google.maps.places.PlacesServiceStatus.OK; | ||
fakePlacesService.textSearch.callsArgWith(1, results, status); | ||
|
||
run(() => component.set('address', 'query string')); | ||
run(() => component.set('placesService', fakePlacesService)); | ||
|
||
run(() => component.updateLocation()); | ||
|
||
assert.equal(component.get('lat'), 12); | ||
assert.equal(component.get('lng'), -20); | ||
|
||
fakePlacesService.textSearch = sinon.stub(); | ||
}); |