-
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 #5 from asennikov/infowindow-component
Independent Info Window component
- Loading branch information
Showing
11 changed files
with
508 additions
and
116 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,120 @@ | ||
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, typeOf } = 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 = this.buildInfowindow(); | ||
this.set('infowindow', infowindow); | ||
} | ||
this.setPosition(); | ||
this.setMap(); | ||
this.setMarker(); | ||
}, | ||
|
||
willDestroyElement() { | ||
this.close(); | ||
}, | ||
|
||
buildInfowindow() { | ||
let infowindow = new google.maps.InfoWindow({ | ||
content: this.get('element'), | ||
disableAutoPan: true | ||
}); | ||
|
||
if (isPresent(this.get('attrs.onClose'))) { | ||
this.attachCloseEvent(infowindow); | ||
} | ||
return infowindow; | ||
}, | ||
|
||
attachCloseEvent(infowindow) { | ||
infowindow.addListener('closeclick', () => { | ||
const { onClose } = this.attrs; | ||
if (typeOf(onClose) === 'function') { | ||
onClose(); | ||
} else { | ||
this.sendAction('onClose'); | ||
} | ||
}); | ||
}, | ||
|
||
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', () => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,15 @@ | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Controller.extend({ | ||
firstMarker: { | ||
lat: 37.7933, | ||
lng: -122.4167 | ||
}, | ||
|
||
secondMarker: { | ||
lat: 37.7733, | ||
lng: -122.4167 | ||
}, | ||
|
||
randomVariable: 111, | ||
|
||
actions: { | ||
refresh() { | ||
this.set('randomVariable', Math.floor(Math.random() * 1000)); | ||
}, | ||
|
||
onInfowindowClosed() { | ||
window.alert('Info Window Closed!'); | ||
} | ||
} | ||
}); |
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,18 +1,29 @@ | ||
<h2 id="title">Ember-g-map Component</h2> | ||
|
||
{{#g-map shouldFit=true as |context|}} | ||
{{g-map-marker context lat=37.7933 lng=-122.4167}} | ||
{{g-map-marker context lat=37.7933 lng=-122.4467}} | ||
|
||
{{#g-map-marker context lat=37.7733 lng=-122.4167 withInfowindow=true}} | ||
<h1>Pretty Infowindow!</h1> | ||
<p>It can has bound variables:</p> | ||
<p>Random: {{randomVariable}}</p> | ||
{{#g-map-marker context lat=37.7433 lng=-122.3867 as |markerContext|}} | ||
{{#g-map-infowindow markerContext}} | ||
<h1>Bound InfoWindow</h1> | ||
<p>This InfoWindow is bound to a marker.</p> | ||
<p>It can has bound variables:</p> | ||
<p>Random: {{randomVariable}}</p> | ||
|
||
<p>And elements with actions:</p> | ||
<button {{action "refresh"}}>Refresh</button> | ||
<p>And elements with actions:</p> | ||
<button {{action "refresh"}}>Refresh</button> | ||
{{/g-map-infowindow}} | ||
{{/g-map-marker}} | ||
|
||
{{#g-map-infowindow context lat=37.7333 lng=-122.4367 onClose="onInfowindowClosed"}} | ||
<h1>Independent InfoWindow</h1> | ||
<p>This InfoWindow can be placed anywhere on the map.</p> | ||
{{/g-map-infowindow}} | ||
|
||
{{g-map-infowindow context title="Blockless Infowindow" message="Contains plain text" | ||
lat=37.7633 lng=-122.4367 onClose=(action "onInfowindowClosed")}} | ||
|
||
{{g-map-route context | ||
originLat=37.7933 originLng=-122.4167 | ||
destinationLat=37.7733 destinationLng=-122.4167}} | ||
originLat=37.7933 originLng=-122.4467 | ||
destinationLat=37.7433 destinationLng=-122.3867}} | ||
{{/g-map}} |
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.