maputnik/src/components/layers/LayerEditor.jsx

170 lines
4.4 KiB
React
Raw Normal View History

2016-09-10 14:10:25 +02:00
import React from 'react'
2016-09-10 16:53:58 +02:00
2016-12-22 15:27:58 +01:00
import JSONEditor from './JSONEditor'
2016-12-20 11:44:22 +01:00
import SourceEditor from './SourceEditor'
import FilterEditor from '../filter/FilterEditor'
import PropertyGroup from '../fields/PropertyGroup'
2016-12-21 11:24:32 +01:00
import LayerEditorGroup from './LayerEditorGroup'
2016-12-22 13:40:23 +01:00
import LayerSettings from './LayerSettings'
2016-12-19 15:40:11 +01:00
2016-12-20 11:44:22 +01:00
import layout from '../../config/layout.json'
2016-12-21 14:46:51 +01:00
import { margins, fontSizes } from '../../config/scales'
import colors from '../../config/colors'
2016-12-17 20:19:01 +01:00
2016-09-10 14:10:25 +02:00
class UnsupportedLayer extends React.Component {
2016-12-17 15:44:42 +01:00
render() {
return <div></div>
}
2016-09-10 14:10:25 +02:00
}
/** Layer editor supporting multiple types of layers. */
2016-12-20 11:44:22 +01:00
export default class LayerEditor extends React.Component {
2016-12-17 15:44:42 +01:00
static propTypes = {
layer: React.PropTypes.object.isRequired,
2016-12-20 16:08:49 +01:00
sources: React.PropTypes.object,
vectorLayers: React.PropTypes.object,
2016-12-17 15:44:42 +01:00
onLayerChanged: React.PropTypes.func,
2016-12-22 15:49:36 +01:00
onLayerIdChange: React.PropTypes.func,
2016-12-17 15:44:42 +01:00
}
static defaultProps = {
onLayerChanged: () => {},
2016-12-22 15:49:36 +01:00
onLayerIdChange: () => {},
2016-12-17 15:44:42 +01:00
onLayerDestroyed: () => {},
}
static childContextTypes = {
reactIconBase: React.PropTypes.object
}
constructor(props) {
2016-12-21 11:24:32 +01:00
super(props)
//TODO: Clean this up and refactor into function
const editorGroups = {}
layout[this.props.layer.type].groups.forEach(group => {
editorGroups[group.title] = true
})
this.state = { editorGroups }
}
componentWillReceiveProps(nextProps) {
const additionalGroups = { ...this.state.editorGroups }
layout[nextProps.layer.type].groups.forEach(group => {
if(!(group.title in additionalGroups)) {
additionalGroups[group.title] = true
}
})
this.setState({
editorGroups: additionalGroups
})
2016-12-17 15:44:42 +01:00
}
getChildContext () {
return {
reactIconBase: {
2016-12-21 14:46:51 +01:00
size: fontSizes[4],
color: colors.lowgray,
2016-12-17 15:44:42 +01:00
}
}
}
/** A {@property} in either the paint our layout {@group} has changed
* to a {@newValue}.
*/
onPropertyChange(group, property, newValue) {
2016-12-22 15:49:36 +01:00
if(group) {
this.props.onLayerChanged({
...this.props.layer,
[group]: {
...this.props.layer[group],
[property]: newValue
}
})
} else {
this.props.onLayerChanged({
...this.props.layer,
[property]: newValue
2016-12-22 15:49:36 +01:00
})
}
2016-12-17 15:44:42 +01:00
}
onFilterChange(newValue) {
const changedLayer = {
...this.props.layer,
filter: newValue
}
2016-12-19 14:56:10 +01:00
this.props.onLayerChanged(changedLayer)
}
2016-12-21 11:24:32 +01:00
onGroupToggle(groupTitle, active) {
const changedActiveGroups = {
...this.state.editorGroups,
[groupTitle]: active,
}
this.setState({
editorGroups: changedActiveGroups
})
}
renderGroupType(type, fields) {
switch(type) {
case 'settings': return <LayerSettings
id={this.props.layer.id}
type={this.props.layer.type}
2016-12-22 15:49:36 +01:00
onTypeChange={v => this.onPropertyChange(null, 'type', v)}
onIdChange={newId => this.props.onLayerIdChange(this.props.layer.id, newId)}
/>
case 'source': return <div>
2016-12-26 12:21:41 +01:00
{this.props.layer.filter &&
2016-12-21 11:24:32 +01:00
<FilterEditor
filter={this.props.layer.filter}
properties={this.props.vectorLayers[this.props.layer['source-layer']]}
onChange={f => this.onFilterChange(f)}
/>
2016-12-26 12:21:41 +01:00
}
2016-12-21 11:24:32 +01:00
<SourceEditor
source={this.props.layer.source}
sourceLayer={this.props.layer['source-layer']}
sources={this.props.sources}
onSourceChange={console.log}
onSourceLayerChange={console.log}
/>
</div>
case 'properties': return <PropertyGroup
layer={this.props.layer}
groupFields={fields}
onChange={this.onPropertyChange.bind(this)}
/>
2016-12-22 15:27:58 +01:00
case 'jsoneditor': return <JSONEditor
layer={this.props.layer}
onChange={this.props.onLayerChanged}
/>
default: return null
2016-12-21 11:24:32 +01:00
}
}
2016-12-21 11:24:32 +01:00
render() {
const layerType = this.props.layer.type
const layoutGroups = layout[layerType].groups.filter(group => {
return !(this.props.layer.type === 'background' && group.type === 'source')
}).map(group => {
return <LayerEditorGroup
key={group.title}
title={group.title}
isActive={this.state.editorGroups[group.title]}
onActiveToggle={this.onGroupToggle.bind(this, group.title)}
>
{this.renderGroupType(group.type, group.fields)}
</LayerEditorGroup>
})
2016-12-22 13:40:23 +01:00
2016-12-22 12:06:04 +01:00
return <div>
{layoutGroups}
2016-12-17 15:44:42 +01:00
</div>
}
2016-09-10 14:10:25 +02:00
}