2016-09-09 00:10:54 +02:00
|
|
|
import React from 'react'
|
2016-09-10 00:43:41 +02:00
|
|
|
import MapboxGl from 'mapbox-gl';
|
|
|
|
import diffStyles from 'mapbox-gl-style-spec/lib/diff'
|
2016-09-09 11:29:18 +02:00
|
|
|
import theme from './theme.js'
|
2016-09-10 14:47:06 +02:00
|
|
|
import Immutable from 'immutable'
|
2016-09-08 19:47:29 +02:00
|
|
|
|
|
|
|
export class Map extends React.Component {
|
2016-09-09 15:49:23 +02:00
|
|
|
static propTypes = {
|
2016-09-10 15:15:17 +02:00
|
|
|
mapStyle: React.PropTypes.instanceOf(Immutable.Map).isRequired,
|
|
|
|
}
|
2016-09-09 15:49:23 +02:00
|
|
|
|
2016-09-10 00:43:41 +02:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-09-10 14:47:06 +02:00
|
|
|
// If the id has changed a new style has been uplaoded and
|
|
|
|
// it is safer to do a full new render
|
|
|
|
const mapIdChanged = this.props.mapStyle.get('id') !== nextProps.mapStyle.get('id')
|
|
|
|
if(mapIdChanged) {
|
|
|
|
this.state.map.setStyle(nextProps.mapStyle.toJS())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: If there is no map yet we need to apply the changes later?
|
|
|
|
// How to deal with that?
|
|
|
|
if(this.state.map) {
|
|
|
|
//TODO: Write own diff algo that operates on immutable collections
|
|
|
|
// Should be able to improve performance since we can only compare
|
|
|
|
// by reference
|
2016-09-10 13:42:23 +02:00
|
|
|
const changes = diffStyles(this.props.mapStyle.toJS(), nextProps.mapStyle.toJS())
|
2016-09-10 00:43:41 +02:00
|
|
|
changes.forEach(change => {
|
2016-09-10 14:47:06 +02:00
|
|
|
this.state.map[change.command].apply(this.state.map, change.args);
|
2016-09-10 00:43:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 15:15:17 +02:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
return nextProps.mapStyle !== this.props.mapStyle
|
|
|
|
}
|
2016-09-10 00:43:41 +02:00
|
|
|
|
|
|
|
componentDidMount() {
|
2016-09-10 14:47:06 +02:00
|
|
|
//TODO: Read MapboxGL token from settings
|
2016-09-10 00:43:41 +02:00
|
|
|
MapboxGl.accessToken = "pk.eyJ1IjoibW9yZ2Vua2FmZmVlIiwiYSI6IjIzcmN0NlkifQ.0LRTNgCc-envt9d5MzR75w";
|
2016-09-10 14:47:06 +02:00
|
|
|
|
2016-09-10 00:43:41 +02:00
|
|
|
const map = new MapboxGl.Map({
|
|
|
|
container: this.container,
|
2016-09-10 13:42:23 +02:00
|
|
|
style: this.props.mapStyle.toJS(),
|
2016-09-10 00:43:41 +02:00
|
|
|
});
|
|
|
|
|
2016-09-10 15:15:17 +02:00
|
|
|
map.on("style.load", (...args) => {
|
|
|
|
this.setState({ map });
|
|
|
|
});
|
2016-09-10 00:43:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 19:47:29 +02:00
|
|
|
render() {
|
2016-09-10 00:43:41 +02:00
|
|
|
return <div ref={x => this.container = x}></div>
|
2016-09-08 19:47:29 +02:00
|
|
|
}
|
|
|
|
}
|