2016-12-24 14:42:57 +01:00
|
|
|
import React from 'react'
|
2016-12-24 15:14:31 +01:00
|
|
|
import ReactDOM from 'react-dom'
|
2016-12-28 19:10:27 +01:00
|
|
|
import MapboxGl from 'mapbox-gl/dist/mapbox-gl.js'
|
2016-12-24 14:42:57 +01:00
|
|
|
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
|
|
|
|
import colors from '../../config/colors'
|
|
|
|
import style from '../../libs/style'
|
2016-12-25 17:46:18 +01:00
|
|
|
import FeaturePropertyPopup from './FeaturePropertyPopup'
|
2016-12-29 16:54:58 +01:00
|
|
|
import { colorHighlightedLayer, generateColoredLayers } from '../../libs/stylegen'
|
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-24 14:42:57 +01:00
|
|
|
|
2016-12-29 16:54:58 +01:00
|
|
|
function convertInspectStyle(mapStyle, sources, highlightedLayer) {
|
2016-12-29 17:30:01 +01:00
|
|
|
const coloredLayers = generateColoredLayers(sources)
|
2016-12-29 16:54:58 +01:00
|
|
|
|
2016-12-29 17:30:01 +01:00
|
|
|
const layer = colorHighlightedLayer(highlightedLayer)
|
2016-12-29 16:54:58 +01:00
|
|
|
if(layer) {
|
|
|
|
coloredLayers.push(layer)
|
|
|
|
}
|
|
|
|
|
2016-12-24 14:42:57 +01:00
|
|
|
const newStyle = {
|
|
|
|
...mapStyle,
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
"id": "background",
|
|
|
|
"type": "background",
|
|
|
|
"paint": {
|
|
|
|
"background-color": colors.black,
|
|
|
|
}
|
|
|
|
},
|
2016-12-29 16:54:58 +01:00
|
|
|
...coloredLayers,
|
2016-12-24 14:42:57 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
return newStyle
|
|
|
|
}
|
|
|
|
|
2016-12-24 17:24:24 +01:00
|
|
|
function renderPopup(features) {
|
2016-12-24 15:14:31 +01:00
|
|
|
var mountNode = document.createElement('div');
|
2016-12-25 17:46:18 +01:00
|
|
|
ReactDOM.render(<FeaturePropertyPopup features={features} />, mountNode)
|
2016-12-24 15:14:31 +01:00
|
|
|
return mountNode.innerHTML;
|
|
|
|
}
|
|
|
|
|
2016-12-24 14:42:57 +01:00
|
|
|
export default class InspectionMap extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
onDataChange: React.PropTypes.func,
|
|
|
|
sources: React.PropTypes.object,
|
|
|
|
originalStyle: React.PropTypes.object,
|
2016-12-29 16:54:58 +01:00
|
|
|
highlightedLayer: React.PropTypes.object,
|
2016-12-24 15:14:31 +01:00
|
|
|
style: React.PropTypes.object,
|
2016-12-24 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
onMapLoaded: () => {},
|
|
|
|
onTileLoaded: () => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = { map: null }
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if(!this.state.map) return
|
|
|
|
|
2016-12-29 16:54:58 +01:00
|
|
|
this.state.map.setStyle(convertInspectStyle(nextProps.mapStyle, this.props.sources, nextProps.highlightedLayer), { diff: true})
|
2016-12-24 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
MapboxGl.accessToken = this.props.accessToken
|
|
|
|
|
|
|
|
const map = new MapboxGl.Map({
|
|
|
|
container: this.container,
|
2016-12-29 16:54:58 +01:00
|
|
|
style: convertInspectStyle(this.props.mapStyle, this.props.sources, this.props.highlightedLayer),
|
2016-12-24 15:17:15 +01:00
|
|
|
hash: true,
|
2016-12-24 14:42:57 +01:00
|
|
|
})
|
|
|
|
|
2016-12-24 17:24:24 +01:00
|
|
|
const nav = new MapboxGl.NavigationControl();
|
|
|
|
map.addControl(nav, 'top-right');
|
2016-12-24 15:14:31 +01:00
|
|
|
|
2016-12-24 14:42:57 +01:00
|
|
|
map.on("style.load", () => {
|
|
|
|
this.setState({ map });
|
|
|
|
})
|
|
|
|
|
|
|
|
map.on("data", e => {
|
|
|
|
if(e.dataType !== 'tile') return
|
|
|
|
this.props.onDataChange({
|
|
|
|
map: this.state.map
|
|
|
|
})
|
|
|
|
})
|
2016-12-24 15:14:31 +01:00
|
|
|
|
2016-12-29 17:00:36 +01:00
|
|
|
map.on('click', this.displayPopup.bind(this))
|
|
|
|
map.on('mousemove', function(e) {
|
|
|
|
var features = map.queryRenderedFeatures(e.point, { layers: this.layers })
|
|
|
|
map.getCanvas().style.cursor = (features.length) ? 'pointer' : ''
|
|
|
|
})
|
2016-12-24 15:14:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
displayPopup(e) {
|
2016-12-24 17:24:24 +01:00
|
|
|
const features = this.state.map.queryRenderedFeatures(e.point, {
|
|
|
|
layers: this.layers
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!features.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the popup and set its coordinates
|
|
|
|
// based on the feature found.
|
|
|
|
const popup = new MapboxGl.Popup()
|
|
|
|
.setLngLat(e.lngLat)
|
|
|
|
.setHTML(renderPopup(features))
|
|
|
|
.addTo(this.state.map)
|
2016-12-24 14:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <div
|
|
|
|
ref={x => this.container = x}
|
|
|
|
style={{
|
|
|
|
position: "fixed",
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
height: "100%",
|
|
|
|
width: "100%",
|
2016-12-24 15:14:31 +01:00
|
|
|
...this.props.style,
|
2016-12-24 14:42:57 +01:00
|
|
|
}}></div>
|
|
|
|
}
|
|
|
|
}
|