2016-11-25 13:31:41 +01:00
|
|
|
import React from 'react'
|
2016-12-19 12:51:23 +01:00
|
|
|
import MapboxGl from 'mapbox-gl'
|
2016-11-25 13:31:41 +01:00
|
|
|
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
|
|
|
|
|
2016-12-20 11:44:22 +01:00
|
|
|
import Map from './Map.jsx'
|
|
|
|
import style from '../../libs/style.js'
|
|
|
|
|
|
|
|
export default class MapboxGlMap extends Map {
|
2016-12-19 21:21:10 +01:00
|
|
|
static propTypes = {
|
|
|
|
onMapLoaded: React.PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
onMapLoaded: () => {}
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:52:04 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = { map: null }
|
|
|
|
}
|
2016-11-25 13:31:41 +01:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
const tokenChanged = nextProps.accessToken !== MapboxGl.accessToken
|
|
|
|
|
|
|
|
// If the id has changed a new style has been uplaoded and
|
|
|
|
// it is safer to do a full new render
|
|
|
|
// TODO: might already be handled in diff algorithm?
|
|
|
|
const mapIdChanged = this.props.mapStyle.get('id') !== nextProps.mapStyle.get('id')
|
|
|
|
|
|
|
|
// TODO: If there is no map yet we need to apply the changes later?
|
|
|
|
if(this.state.map) {
|
2016-12-19 14:52:04 +01:00
|
|
|
if(mapIdChanged || tokenChanged) {
|
|
|
|
this.state.map.setStyle(style.toJSON(nextProps.mapStyle))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:31:41 +01:00
|
|
|
style.diffStyles(this.props.mapStyle, nextProps.mapStyle).forEach(change => {
|
|
|
|
|
|
|
|
//TODO: Invalid outline color can cause map to freeze?
|
|
|
|
if(change.command === "setPaintProperty" && change.args[1] === "fill-outline-color" ) {
|
|
|
|
const value = change.args[2]
|
|
|
|
if(validateColor({value}).length > 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(change.command, ...change.args)
|
|
|
|
this.state.map[change.command].apply(this.state.map, change.args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
MapboxGl.accessToken = this.props.accessToken
|
|
|
|
|
|
|
|
const map = new MapboxGl.Map({
|
|
|
|
container: this.container,
|
|
|
|
style: style.toJSON(this.props.mapStyle),
|
|
|
|
});
|
|
|
|
|
|
|
|
map.on("style.load", (...args) => {
|
2016-12-19 21:21:10 +01:00
|
|
|
this.props.onMapLoaded(map)
|
2016-11-25 13:31:41 +01:00
|
|
|
this.setState({ map });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|