maputnik/src/components/map/FeatureLayerPopup.jsx

71 lines
1.9 KiB
React
Raw Normal View History

2016-12-24 15:24:22 +01:00
import React from 'react'
2017-11-08 09:47:36 +01:00
import PropTypes from 'prop-types'
2016-12-24 17:24:24 +01:00
import InputBlock from '../inputs/InputBlock'
import StringInput from '../inputs/StringInput'
2016-12-25 17:46:18 +01:00
import LayerIcon from '../icons/LayerIcon'
2016-12-24 17:24:24 +01:00
2016-12-25 17:46:18 +01:00
function groupFeaturesBySourceLayer(features) {
const sources = {}
let returnedFeatures = {};
2016-12-25 17:46:18 +01:00
features.forEach(feature => {
if(returnedFeatures.hasOwnProperty(feature.layer.id)) {
returnedFeatures[feature.layer.id]++
const featureObject = sources[feature.layer['source-layer']].find(f => f.layer.id === feature.layer.id)
featureObject.counter = returnedFeatures[feature.layer.id]
} else {
sources[feature.layer['source-layer']] = sources[feature.layer['source-layer']] || []
sources[feature.layer['source-layer']].push(feature)
returnedFeatures[feature.layer.id] = 1
}
2016-12-25 17:46:18 +01:00
})
2016-12-25 17:46:18 +01:00
return sources
}
2016-12-24 17:24:24 +01:00
2017-01-09 23:04:08 +01:00
class FeatureLayerPopup extends React.Component {
2017-11-08 09:47:36 +01:00
static propTypes = {
2018-01-08 22:18:30 +01:00
onLayerSelect: PropTypes.func.isRequired,
2017-11-08 09:47:36 +01:00
features: PropTypes.array
}
2016-12-24 15:24:22 +01:00
render() {
2016-12-25 17:46:18 +01:00
const sources = groupFeaturesBySourceLayer(this.props.features)
const items = Object.keys(sources).map(vectorLayerId => {
2017-01-09 23:04:08 +01:00
const layers = sources[vectorLayerId].map((feature, idx) => {
return <label
key={idx}
2017-01-11 11:35:33 +01:00
className="maputnik-popup-layer"
2018-01-08 22:18:30 +01:00
onClick={() => {
this.props.onLayerSelect(feature.layer.id)
}}
2017-01-11 11:35:33 +01:00
>
2016-12-25 17:46:18 +01:00
<LayerIcon type={feature.layer.type} style={{
2017-01-11 11:35:33 +01:00
width: 14,
height: 14,
paddingRight: 3
2016-12-25 17:46:18 +01:00
}}/>
{feature.layer.id}
{feature.counter && <span> × {feature.counter}</span>}
2016-12-25 17:46:18 +01:00
</label>
})
2017-01-10 09:38:27 +01:00
return <div key={vectorLayerId}>
2017-01-11 11:35:33 +01:00
<div className="maputnik-popup-layer-id">{vectorLayerId}</div>
2016-12-25 17:46:18 +01:00
{layers}
</div>
})
2017-01-11 11:35:33 +01:00
return <div className="maputnik-feature-layer-popup">
2016-12-25 17:46:18 +01:00
{items}
2016-12-24 17:24:24 +01:00
</div>
2016-12-24 15:24:22 +01:00
}
}
2017-01-09 23:04:08 +01:00
export default FeatureLayerPopup