Remove duplicated features from popups (#41)

This commit is contained in:
Grzegorz Wolański 2017-12-05 23:27:17 +01:00
parent 4479473b37
commit ace6812e89
2 changed files with 42 additions and 5 deletions

View file

@ -4,13 +4,26 @@ import InputBlock from '../inputs/InputBlock'
import StringInput from '../inputs/StringInput' import StringInput from '../inputs/StringInput'
import LayerIcon from '../icons/LayerIcon' import LayerIcon from '../icons/LayerIcon'
function groupFeaturesBySourceLayer(features) { function groupFeaturesBySourceLayer(features) {
const sources = {} const sources = {}
let returnedFeatures = {};
features.forEach(feature => { features.forEach(feature => {
sources[feature.layer['source-layer']] = sources[feature.layer['source-layer']] || [] if(returnedFeatures.hasOwnProperty(feature.layer.id)) {
sources[feature.layer['source-layer']].push(feature) 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
}
}) })
return sources return sources
} }
@ -34,6 +47,7 @@ class FeatureLayerPopup extends React.Component {
paddingRight: 3 paddingRight: 3
}}/> }}/>
{feature.layer.id} {feature.layer.id}
{feature.counter && <span> × {feature.counter}</span>}
</label> </label>
}) })
return <div key={vectorLayerId}> return <div key={vectorLayerId}>

View file

@ -23,7 +23,7 @@ function renderProperties(feature) {
function renderFeature(feature) { function renderFeature(feature) {
return <div key={feature.id}> return <div key={feature.id}>
<div className="maputnik-popup-layer-id">{feature.layer['source-layer']}</div> <div className="maputnik-popup-layer-id">{feature.layer['source-layer']}{feature.inspectModeCounter && <span> × {feature.inspectModeCounter}</span>}</div>
<InputBlock key={"property-type"} label={"$type"}> <InputBlock key={"property-type"} label={"$type"}>
<StringInput value={feature.geometry.type} style={{backgroundColor: 'transparent'}} /> <StringInput value={feature.geometry.type} style={{backgroundColor: 'transparent'}} />
</InputBlock> </InputBlock>
@ -31,13 +31,36 @@ function renderFeature(feature) {
</div> </div>
} }
function removeDuplicatedFeatures(features) {
let uniqueFeatures = [];
features.forEach(feature => {
const featureIndex = uniqueFeatures.findIndex(feature2 => {
return feature.layer['source-layer'] === feature2.layer['source-layer']
&& JSON.stringify(feature.properties) === JSON.stringify(feature2.properties)
})
if(featureIndex === -1) {
uniqueFeatures.push(feature)
} else {
if(uniqueFeatures[featureIndex].hasOwnProperty('counter')) {
uniqueFeatures[featureIndex].inspectModeCounter++
} else {
uniqueFeatures[featureIndex].inspectModeCounter = 2
}
}
})
return uniqueFeatures
}
class FeaturePropertyPopup extends React.Component { class FeaturePropertyPopup extends React.Component {
static propTypes = { static propTypes = {
features: PropTypes.array features: PropTypes.array
} }
render() { render() {
const features = this.props.features const features = removeDuplicatedFeatures(this.props.features)
return <div className="maputnik-feature-property-popup"> return <div className="maputnik-feature-property-popup">
{features.map(renderFeature)} {features.map(renderFeature)}
</div> </div>