-
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.
Pull infowindows into separate component
- Loading branch information
Showing
9 changed files
with
412 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import Ember from 'ember'; | ||
import layout from '../templates/components/g-map-infowindow'; | ||
import GMapComponent from './g-map'; | ||
import GMapMarkerComponent from './g-map-marker'; | ||
|
||
const { isEmpty, isPresent, observer, computed, run, assert } = Ember; | ||
|
||
const GMapInfowindowComponent = Ember.Component.extend({ | ||
layout: layout, | ||
classNames: ['g-map-marker'], | ||
|
||
map: computed.alias('mapContext.map'), | ||
marker: computed.alias('mapContext.marker'), | ||
|
||
init() { | ||
this._super(arguments); | ||
|
||
let mapContext = this.get('mapContext'); | ||
let hasMap = mapContext instanceof GMapComponent; | ||
let hasMarker = mapContext instanceof GMapMarkerComponent; | ||
assert('Must be inside {{#g-map}} or {{#g-map-marker}} components with context set', | ||
hasMarker || hasMap); | ||
|
||
this.set('hasMarker', hasMarker); | ||
}, | ||
|
||
didInsertElement() { | ||
this._super(); | ||
if (isEmpty(this.get('infowindow'))) { | ||
let infowindow = new google.maps.InfoWindow({ | ||
content: this.get('element') | ||
}); | ||
this.set('infowindow', infowindow); | ||
} | ||
this.setPosition(); | ||
this.setMap(); | ||
this.setMarker(); | ||
}, | ||
|
||
willDestroyElement() { | ||
this.close(); | ||
}, | ||
|
||
close() { | ||
let infowindow = this.get('infowindow'); | ||
if (isPresent(infowindow)) { | ||
infowindow.close(); | ||
} | ||
}, | ||
|
||
mapWasSet: observer('map', function() { | ||
run.once(this, 'setMap'); | ||
}), | ||
|
||
setMap() { | ||
let map = this.get('map'); | ||
let hasMarker = this.get('hasMarker'); | ||
let infowindow = this.get('infowindow'); | ||
|
||
if (isPresent(infowindow) && isPresent(map) && !hasMarker) { | ||
infowindow.open(map); | ||
} | ||
}, | ||
|
||
markerWasSet: observer('marker', function() { | ||
run.once(this, 'setMarker'); | ||
}), | ||
|
||
setMarker() { | ||
let map = this.get('map'); | ||
let marker = this.get('marker'); | ||
let infowindow = this.get('infowindow'); | ||
|
||
if (isPresent(infowindow) && isPresent(map) && isPresent(marker)) { | ||
marker.addListener('click', function() { | ||
infowindow.open(map, marker); | ||
}); | ||
} | ||
}, | ||
|
||
coordsChanged: observer('lat', 'lng', function() { | ||
run.once(this, 'setPosition'); | ||
}), | ||
|
||
setPosition() { | ||
let infowindow = this.get('infowindow'); | ||
let lat = this.get('lat'); | ||
let lng = this.get('lng'); | ||
|
||
if (isPresent(infowindow) && isPresent(lat) && isPresent(lng)) { | ||
let position = new google.maps.LatLng(lat, lng); | ||
infowindow.setPosition(position); | ||
} | ||
} | ||
}); | ||
|
||
GMapInfowindowComponent.reopenClass({ | ||
positionalParams: ['mapContext'] | ||
}); | ||
|
||
export default GMapInfowindowComponent; |
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,6 @@ | ||
{{#if hasBlock}} | ||
{{yield}} | ||
{{else}} | ||
<h1>{{title}}</h1> | ||
<p>{{message}}</p> | ||
{{/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 |
---|---|---|
@@ -1 +1 @@ | ||
{{yield}} | ||
{{yield this}} |
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-infowindow'; |
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,54 @@ | ||
import { moduleForComponent, test } from 'ember-qunit'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
moduleForComponent('g-map-infowindow', 'Integration | Component | g map infowindow', { | ||
integration: true | ||
}); | ||
|
||
test('it renders inside {{g-map}} component', function(assert) { | ||
assert.expect(2); | ||
|
||
this.render(hbs` | ||
{{#g-map as |context|}} | ||
{{g-map-infowindow context}} | ||
{{/g-map}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), ''); | ||
|
||
this.render(hbs` | ||
{{#g-map as |context|}} | ||
{{#g-map-infowindow context}} | ||
template block text | ||
{{/g-map-infowindow}} | ||
{{/g-map}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), 'template block text'); | ||
}); | ||
|
||
test('it renders inside {{g-map-marker}} component', function(assert) { | ||
assert.expect(2); | ||
|
||
this.render(hbs` | ||
{{#g-map as |context|}} | ||
{{#g-map-marker context as |markerContext|}} | ||
{{g-map-infowindow markerContext}} | ||
{{/g-map-marker}} | ||
{{/g-map}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), ''); | ||
|
||
this.render(hbs` | ||
{{#g-map as |context|}} | ||
{{#g-map-marker context as |markerContext|}} | ||
{{#g-map-infowindow markerContext}} | ||
template block text | ||
{{/g-map-infowindow}} | ||
{{/g-map-marker}} | ||
{{/g-map}} | ||
`); | ||
|
||
assert.equal(this.$().text().trim(), 'template block text'); | ||
}); |
Oops, something went wrong.