mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2024-12-28 15:41:14 +01:00
Better inspection hover
This commit is contained in:
parent
8f561d8a27
commit
4db5c7cf68
6 changed files with 109 additions and 97 deletions
|
@ -7,6 +7,7 @@ class InputBlock extends React.Component {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
label: React.PropTypes.string.isRequired,
|
label: React.PropTypes.string.isRequired,
|
||||||
children: React.PropTypes.element.isRequired,
|
children: React.PropTypes.element.isRequired,
|
||||||
|
style: React.PropTypes.object,
|
||||||
}
|
}
|
||||||
|
|
||||||
onChange(e) {
|
onChange(e) {
|
||||||
|
@ -19,6 +20,7 @@ class InputBlock extends React.Component {
|
||||||
display: 'block',
|
display: 'block',
|
||||||
marginTop: margins[2],
|
marginTop: margins[2],
|
||||||
marginBottom: margins[2],
|
marginBottom: margins[2],
|
||||||
|
...this.props.style,
|
||||||
}}>
|
}}>
|
||||||
<label style={input.label}>{this.props.label}</label>
|
<label style={input.label}>{this.props.label}</label>
|
||||||
{this.props.children}
|
{this.props.children}
|
||||||
|
|
|
@ -1,18 +1,48 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
import InputBlock from '../inputs/InputBlock'
|
||||||
|
import StringInput from '../inputs/StringInput'
|
||||||
|
|
||||||
|
import colors from '../../config/colors'
|
||||||
|
import { margins, fontSizes } from '../../config/scales'
|
||||||
|
|
||||||
|
function renderProperties(feature) {
|
||||||
|
return Object.keys(feature.properties).map(propertyName => {
|
||||||
|
const property = feature.properties[propertyName]
|
||||||
|
return <InputBlock label={propertyName} style={{marginTop: 0, marginBottom: 0}}>
|
||||||
|
<StringInput value={property} style={{backgroundColor: 'transparent'}}/>
|
||||||
|
</InputBlock>
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const Panel = (props) => {
|
||||||
|
return <div style={{
|
||||||
|
backgroundColor: colors.gray,
|
||||||
|
padding: margins[0],
|
||||||
|
fontSize: fontSizes[5],
|
||||||
|
lineHeight: 1.2,
|
||||||
|
}}>{props.children}</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderFeature(feature) {
|
||||||
|
return <div>
|
||||||
|
<Panel>Source</Panel>
|
||||||
|
<InputBlock label={feature.layer['source-layer']} style={{marginTop: 0, marginBottom: 0}}>
|
||||||
|
</InputBlock>
|
||||||
|
<Panel>Layers</Panel>
|
||||||
|
<InputBlock label={feature.layer.id} style={{marginTop: 0, marginBottom: 0}}>
|
||||||
|
</InputBlock>
|
||||||
|
<Panel>Properties</Panel>
|
||||||
|
{renderProperties(feature)}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
class FeatureLayerTable extends React.Component {
|
class FeatureLayerTable extends React.Component {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const feature = this.props.feature
|
const features = this.props.features
|
||||||
const rows = <tr>
|
return <div>
|
||||||
<td className="debug-prop-key">{feature.layer.id}</td>
|
{features.map(renderFeature)}
|
||||||
</tr>
|
</div>
|
||||||
return <table
|
|
||||||
style={{
|
|
||||||
color: 'black',
|
|
||||||
}}
|
|
||||||
className="debug-props">
|
|
||||||
<tbody>{rows}</tbody>
|
|
||||||
</table>;;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
import React from 'react'
|
|
||||||
|
|
||||||
|
|
||||||
function items(object) {
|
|
||||||
let arr = [];
|
|
||||||
for (var key in object) {
|
|
||||||
arr.push({ key, value: object[key] });
|
|
||||||
}
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function displayValue(value) {
|
|
||||||
if (typeof value === "undefined" || value === null) return value;
|
|
||||||
if (value instanceof Date) return value.toLocaleString();
|
|
||||||
if (typeof value === "object" ||
|
|
||||||
typeof value === "number" ||
|
|
||||||
typeof value === "string") return value.toString();
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
class FeaturePropertyTable extends React.Component {
|
|
||||||
render() {
|
|
||||||
const rows = items(this.props.feature.properties)
|
|
||||||
.map(i => {
|
|
||||||
return <tr key={i.key} className="debug-prop">
|
|
||||||
<td className="debug-prop-key">{i.key}</td>
|
|
||||||
<td className="debug-prop-value">{displayValue(i.value)}</td>
|
|
||||||
</tr>;
|
|
||||||
});
|
|
||||||
return <table
|
|
||||||
style={{
|
|
||||||
color: 'black',
|
|
||||||
}}
|
|
||||||
className="debug-props">
|
|
||||||
<tbody>{rows}</tbody>
|
|
||||||
</table>;;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export default FeaturePropertyTable
|
|
|
@ -4,7 +4,7 @@ import MapboxGl from 'mapbox-gl'
|
||||||
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
|
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
|
||||||
import colors from '../../config/colors'
|
import colors from '../../config/colors'
|
||||||
import style from '../../libs/style'
|
import style from '../../libs/style'
|
||||||
import FeaturePropertyTable from './FeaturePropertyTable'
|
import FeatureLayerTable from './FeatureLayerTable'
|
||||||
import { generateColoredLayers } from '../../libs/stylegen'
|
import { generateColoredLayers } from '../../libs/stylegen'
|
||||||
import 'mapbox-gl/dist/mapbox-gl.css'
|
import 'mapbox-gl/dist/mapbox-gl.css'
|
||||||
|
|
||||||
|
@ -25,9 +25,9 @@ function convertInspectStyle(mapStyle, sources) {
|
||||||
return newStyle
|
return newStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderFeaturePropertyTable(feature) {
|
function renderPopup(features) {
|
||||||
var mountNode = document.createElement('div');
|
var mountNode = document.createElement('div');
|
||||||
ReactDOM.render(<FeaturePropertyTable feature={feature} />, mountNode);
|
ReactDOM.render(<FeatureLayerTable features={features} />, mountNode)
|
||||||
return mountNode.innerHTML;
|
return mountNode.innerHTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,8 +64,8 @@ export default class InspectionMap extends React.Component {
|
||||||
hash: true,
|
hash: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
const nav = new MapboxGl.NavigationControl();
|
const nav = new MapboxGl.NavigationControl();
|
||||||
map.addControl(nav, 'top-right');
|
map.addControl(nav, 'top-right');
|
||||||
|
|
||||||
map.on("style.load", () => {
|
map.on("style.load", () => {
|
||||||
this.setState({ map });
|
this.setState({ map });
|
||||||
|
@ -82,29 +82,20 @@ export default class InspectionMap extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
displayPopup(e) {
|
displayPopup(e) {
|
||||||
const features = this.state.map.queryRenderedFeatures(e.point, {
|
const features = this.state.map.queryRenderedFeatures(e.point, {
|
||||||
layers: this.layers
|
layers: this.layers
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!features.length) {
|
if (!features.length) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const feature = features[0]
|
|
||||||
|
|
||||||
/*
|
// Populate the popup and set its coordinates
|
||||||
const clickEvent = e.originalEvent
|
// based on the feature found.
|
||||||
const x = clickEvent.pageX
|
const popup = new MapboxGl.Popup()
|
||||||
const y = clickEvent.pageY
|
.setLngLat(e.lngLat)
|
||||||
|
.setHTML(renderPopup(features))
|
||||||
console.log(e)
|
.addTo(this.state.map)
|
||||||
console.log('Show feature', feature)
|
|
||||||
*/
|
|
||||||
// Populate the popup and set its coordinates
|
|
||||||
// based on the feature found.
|
|
||||||
const popup = new MapboxGl.Popup()
|
|
||||||
.setLngLat(e.lngLat)
|
|
||||||
.setHTML(renderFeaturePropertyTable(feature))
|
|
||||||
.addTo(this.state.map)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
|
@ -2,13 +2,14 @@ import React from 'react'
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom'
|
||||||
import MapboxGl from 'mapbox-gl'
|
import MapboxGl from 'mapbox-gl'
|
||||||
import FeatureLayerTable from './FeatureLayerTable'
|
import FeatureLayerTable from './FeatureLayerTable'
|
||||||
|
import Popup from './Popup'
|
||||||
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
|
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
|
||||||
import style from '../../libs/style.js'
|
import style from '../../libs/style.js'
|
||||||
import 'mapbox-gl/dist/mapbox-gl.css'
|
import 'mapbox-gl/dist/mapbox-gl.css'
|
||||||
|
|
||||||
function renderTable(feature) {
|
function renderPopup(features) {
|
||||||
var mountNode = document.createElement('div');
|
var mountNode = document.createElement('div');
|
||||||
ReactDOM.render(<FeatureLayerTable feature={feature} />, mountNode);
|
ReactDOM.render(<FeatureLayerTable features={features} />, mountNode)
|
||||||
return mountNode.innerHTML;
|
return mountNode.innerHTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +28,12 @@ export default class MapboxGlMap extends React.Component {
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
this.state = { map: null }
|
this.state = {
|
||||||
|
map: null,
|
||||||
|
isPopupOpen: false,
|
||||||
|
popupX: 0,
|
||||||
|
popupY: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
|
@ -69,27 +75,24 @@ export default class MapboxGlMap extends React.Component {
|
||||||
layers: this.layers
|
layers: this.layers
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!features.length) {
|
console.log('Click on features', features)
|
||||||
return
|
|
||||||
}
|
|
||||||
const feature = features[0]
|
|
||||||
console.log('Click on feature', feature)
|
|
||||||
const popup = new MapboxGl.Popup()
|
const popup = new MapboxGl.Popup()
|
||||||
.setLngLat(e.lngLat)
|
.setLngLat(e.lngLat)
|
||||||
.setHTML(renderTable(feature))
|
.setHTML(renderPopup(features))
|
||||||
.addTo(this.state.map)
|
.addTo(this.state.map)
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return <div
|
return <div
|
||||||
ref={x => this.container = x}
|
ref={x => this.container = x}
|
||||||
style={{
|
style={{
|
||||||
position: "fixed",
|
position: "fixed",
|
||||||
top: 0,
|
top: 0,
|
||||||
bottom: 0,
|
bottom: 0,
|
||||||
height: "100%",
|
height: "100%",
|
||||||
width: "100%",
|
width: "100%",
|
||||||
...this.props.style,
|
...this.props.style,
|
||||||
}}></div>
|
}}>
|
||||||
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,3 +15,31 @@
|
||||||
color: rgb(142, 142, 142) !important;
|
color: rgb(142, 142, 142) !important;
|
||||||
box-shadow: none !important;
|
box-shadow: none !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.mapboxgl-popup-tip {
|
||||||
|
border-top-color: rgb(28, 31, 36) !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mapboxgl-popup-content {
|
||||||
|
background-color: rgb(28, 31, 36) !important;
|
||||||
|
border-radius: 0px !important;
|
||||||
|
box-shadow: rgba(0, 0, 0, 0.298039) 0px 0px 5px 0px;
|
||||||
|
padding: 0px !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mapboxgl-popup-close-button {
|
||||||
|
color: white !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mapboxgl-ctrl-group {
|
||||||
|
background: rgb(28, 31, 36) !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mapboxgl-ctrl-group > button {
|
||||||
|
background-color: rgb(28, 31, 36) !important;
|
||||||
|
border-color: rgb(28, 31, 36) !important;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background-color: rgb(86, 83, 83);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue