import React from 'react' import JSONEditor from './JSONEditor' import FilterEditor from '../filter/FilterEditor' import PropertyGroup from '../fields/PropertyGroup' import LayerEditorGroup from './LayerEditorGroup' import LayerTypeBlock from './LayerTypeBlock' import LayerIdBlock from './LayerIdBlock' import LayerSourceBlock from './LayerSourceBlock' import LayerSourceLayerBlock from './LayerSourceLayerBlock' import InputBlock from '../inputs/InputBlock' import MultiButtonInput from '../inputs/MultiButtonInput' import { changeType, changeProperty } from '../../libs/layer' import layout from '../../config/layout.json' class UnsupportedLayer extends React.Component { render() { return
} } function layoutGroups(layerType) { const layerGroup = { title: 'Layer', type: 'layer' } const filterGroup = { title: 'Filter', type: 'filter' } const editorGroup = { title: 'JSON Editor', type: 'jsoneditor' } return [layerGroup, filterGroup].concat(layout[layerType].groups).concat([editorGroup]) } /** Layer editor supporting multiple types of layers. */ export default class LayerEditor extends React.Component { static propTypes = { layer: React.PropTypes.object.isRequired, sources: React.PropTypes.object, vectorLayers: React.PropTypes.object, spec: React.PropTypes.object.isRequired, onLayerChanged: React.PropTypes.func, onLayerIdChange: React.PropTypes.func, } static defaultProps = { onLayerChanged: () => {}, onLayerIdChange: () => {}, onLayerDestroyed: () => {}, } static childContextTypes = { reactIconBase: React.PropTypes.object } constructor(props) { super(props) //TODO: Clean this up and refactor into function const editorGroups = {} layoutGroups(this.props.layer.type).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 }) } getChildContext () { return { reactIconBase: { size: 14, color: '#8e8e8e', } } } changeProperty(group, property, newValue) { this.props.onLayerChanged(changeProperty(this.props.layer, group, property, newValue)) } onGroupToggle(groupTitle, active) { const changedActiveGroups = { ...this.state.editorGroups, [groupTitle]: active, } this.setState({ editorGroups: changedActiveGroups }) } renderGroupType(type, fields) { switch(type) { case 'layer': return
this.props.onLayerIdChange(this.props.layer.id, newId)} /> this.props.onLayerChanged(changeType(this.props.layer, newType))} /> {this.props.layer.type !== 'background' && this.changeProperty(null, 'source', v)} /> } {this.props.layer.type !== 'raster' && this.props.layer.type !== 'background' && this.changeProperty(null, 'source-layer', v)} /> }
case 'filter': return
this.changeProperty(null, 'filter', f)} />
case 'properties': return case 'jsoneditor': return default: return null } } render() { const layerType = this.props.layer.type const groups = layoutGroups(layerType).filter(group => { return !(layerType === 'background' && group.type === 'source') }).map(group => { return {this.renderGroupType(group.type, group.fields)} }) return
{groups}
} }