2016-11-25 13:31:41 +01:00
|
|
|
import React from 'react'
|
2016-12-24 15:24:22 +01:00
|
|
|
import ReactDOM from 'react-dom'
|
2016-12-28 19:10:27 +01:00
|
|
|
import MapboxGl from 'mapbox-gl/dist/mapbox-gl.js'
|
2017-01-08 22:03:21 +01:00
|
|
|
import MapboxInspect from 'mapbox-gl-inspect'
|
2016-12-24 15:24:22 +01:00
|
|
|
import FeatureLayerTable from './FeatureLayerTable'
|
2017-01-08 22:03:21 +01:00
|
|
|
import FeaturePropertyPopup from './FeaturePropertyPopup'
|
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 style from '../../libs/style.js'
|
2016-12-24 15:14:31 +01:00
|
|
|
import 'mapbox-gl/dist/mapbox-gl.css'
|
2016-12-25 17:46:18 +01:00
|
|
|
import '../../mapboxgl.css'
|
2016-12-20 11:44:22 +01:00
|
|
|
|
2017-01-08 22:03:21 +01:00
|
|
|
function renderLayerPopup(features) {
|
2016-12-24 15:24:22 +01:00
|
|
|
var mountNode = document.createElement('div');
|
2016-12-24 17:24:24 +01:00
|
|
|
ReactDOM.render(<FeatureLayerTable features={features} />, mountNode)
|
2016-12-24 15:24:22 +01:00
|
|
|
return mountNode.innerHTML;
|
|
|
|
}
|
|
|
|
|
2017-01-08 22:03:21 +01:00
|
|
|
function renderPropertyPopup(features) {
|
|
|
|
var mountNode = document.createElement('div');
|
|
|
|
ReactDOM.render(<FeaturePropertyPopup features={features} />, mountNode)
|
|
|
|
return mountNode.innerHTML;
|
|
|
|
}
|
|
|
|
|
2016-12-24 14:42:57 +01:00
|
|
|
export default class MapboxGlMap extends React.Component {
|
2016-12-19 21:21:10 +01:00
|
|
|
static propTypes = {
|
2016-12-24 14:42:57 +01:00
|
|
|
onDataChange: React.PropTypes.func,
|
|
|
|
mapStyle: React.PropTypes.object.isRequired,
|
|
|
|
accessToken: React.PropTypes.string,
|
2016-12-24 15:14:31 +01:00
|
|
|
style: React.PropTypes.object,
|
2017-01-08 22:03:21 +01:00
|
|
|
inspectModeEnabled: React.PropTypes.bool.isRequired,
|
2016-12-19 21:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
2016-12-24 14:42:57 +01:00
|
|
|
onMapLoaded: () => {},
|
|
|
|
onDataChange: () => {},
|
2016-12-19 21:21:10 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 14:52:04 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2017-01-01 14:49:32 +01:00
|
|
|
MapboxGl.accessToken = props.accessToken
|
2016-12-24 17:24:24 +01:00
|
|
|
this.state = {
|
|
|
|
map: null,
|
|
|
|
isPopupOpen: false,
|
|
|
|
popupX: 0,
|
|
|
|
popupY: 0,
|
|
|
|
}
|
2016-12-19 14:52:04 +01:00
|
|
|
}
|
2016-12-24 14:42:57 +01:00
|
|
|
|
2016-11-25 13:31:41 +01:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2017-01-01 14:49:32 +01:00
|
|
|
MapboxGl.accessToken = nextProps.accessToken
|
|
|
|
|
2016-12-20 13:28:50 +01:00
|
|
|
if(!this.state.map) return
|
2016-11-25 13:31:41 +01:00
|
|
|
|
2017-01-08 22:03:21 +01:00
|
|
|
if(!this.props.inspectModeEnabled) {
|
|
|
|
//Mapbox GL now does diffing natively so we don't need to calculate
|
|
|
|
//the necessary operations ourselves!
|
|
|
|
this.state.map.setStyle(nextProps.mapStyle, { diff: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.props.inspectModeEnabled !== nextProps.inspectModeEnabled) {
|
|
|
|
this.inspect.toggleInspector()
|
|
|
|
}
|
2016-11-25 13:31:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const map = new MapboxGl.Map({
|
|
|
|
container: this.container,
|
2016-12-20 16:08:49 +01:00
|
|
|
style: this.props.mapStyle,
|
2016-12-24 15:17:15 +01:00
|
|
|
hash: true,
|
2016-12-24 14:42:57 +01:00
|
|
|
})
|
2016-11-25 13:31:41 +01:00
|
|
|
|
2016-12-24 15:14:31 +01:00
|
|
|
const nav = new MapboxGl.NavigationControl();
|
|
|
|
map.addControl(nav, 'top-right');
|
|
|
|
|
2017-01-08 22:03:21 +01:00
|
|
|
this.inspect = new MapboxInspect({
|
|
|
|
popup: new MapboxGl.Popup({
|
|
|
|
closeButton: false,
|
|
|
|
closeOnClick: false
|
|
|
|
}),
|
|
|
|
showInspectButton: false,
|
|
|
|
renderPopup: features => {
|
|
|
|
if(this.props.inspectModeEnabled) {
|
|
|
|
return renderPropertyPopup(features)
|
|
|
|
} else {
|
|
|
|
return renderLayerPopup(features)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
map.addControl(this.inspect)
|
|
|
|
|
2016-12-24 14:42:57 +01:00
|
|
|
map.on("style.load", () => {
|
2016-11-25 13:31:41 +01:00
|
|
|
this.setState({ map });
|
2016-12-24 14:42:57 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
map.on("data", e => {
|
|
|
|
if(e.dataType !== 'tile') return
|
|
|
|
this.props.onDataChange({
|
|
|
|
map: this.state.map
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <div
|
2016-12-24 17:24:24 +01:00
|
|
|
ref={x => this.container = x}
|
|
|
|
style={{
|
|
|
|
position: "fixed",
|
|
|
|
top: 0,
|
2017-01-08 22:03:21 +01:00
|
|
|
right: 0,
|
2016-12-24 17:24:24 +01:00
|
|
|
bottom: 0,
|
|
|
|
height: "100%",
|
2017-01-05 21:05:34 +01:00
|
|
|
width: "75%",
|
2016-12-24 17:24:24 +01:00
|
|
|
...this.props.style,
|
|
|
|
}}>
|
|
|
|
</div>
|
2016-11-25 13:31:41 +01:00
|
|
|
}
|
|
|
|
}
|