2016-12-25 17:46:18 +01:00
|
|
|
|
import React from 'react'
|
2017-11-08 09:47:36 +01:00
|
|
|
|
import PropTypes from 'prop-types'
|
2016-12-25 17:46:18 +01:00
|
|
|
|
import InputBlock from '../inputs/InputBlock'
|
|
|
|
|
import StringInput from '../inputs/StringInput'
|
|
|
|
|
|
2017-01-10 09:38:27 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-25 17:46:18 +01:00
|
|
|
|
function renderProperties(feature) {
|
|
|
|
|
return Object.keys(feature.properties).map(propertyName => {
|
|
|
|
|
const property = feature.properties[propertyName]
|
2017-01-11 11:35:33 +01:00
|
|
|
|
return <InputBlock key={propertyName} label={propertyName}>
|
2017-01-10 09:38:27 +01:00
|
|
|
|
<StringInput value={displayValue(property)} style={{backgroundColor: 'transparent'}}/>
|
2016-12-25 17:46:18 +01:00
|
|
|
|
</InputBlock>
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderFeature(feature) {
|
2019-01-06 05:35:11 +01:00
|
|
|
|
return <div key={`${feature.sourceLayer}-${feature.id}`}>
|
2017-12-05 23:27:17 +01:00
|
|
|
|
<div className="maputnik-popup-layer-id">{feature.layer['source-layer']}{feature.inspectModeCounter && <span> × {feature.inspectModeCounter}</span>}</div>
|
2017-01-18 10:03:15 +01:00
|
|
|
|
<InputBlock key={"property-type"} label={"$type"}>
|
|
|
|
|
<StringInput value={feature.geometry.type} style={{backgroundColor: 'transparent'}} />
|
|
|
|
|
</InputBlock>
|
2016-12-25 17:46:18 +01:00
|
|
|
|
{renderProperties(feature)}
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-05 23:27:17 +01:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 17:30:01 +01:00
|
|
|
|
class FeaturePropertyPopup extends React.Component {
|
2017-11-08 09:47:36 +01:00
|
|
|
|
static propTypes = {
|
|
|
|
|
features: PropTypes.array
|
|
|
|
|
}
|
2016-12-25 17:46:18 +01:00
|
|
|
|
|
|
|
|
|
render() {
|
2017-12-05 23:27:17 +01:00
|
|
|
|
const features = removeDuplicatedFeatures(this.props.features)
|
2017-01-11 11:35:33 +01:00
|
|
|
|
return <div className="maputnik-feature-property-popup">
|
2016-12-25 17:46:18 +01:00
|
|
|
|
{features.map(renderFeature)}
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-12-29 17:30:01 +01:00
|
|
|
|
export default FeaturePropertyPopup
|