maputnik/src/layers/editor.jsx

156 lines
4.7 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
import Immutable from 'immutable'
2016-09-15 09:13:23 +02:00
import Toolbar from 'rebass/dist/Toolbar'
import NavItem from 'rebass/dist/NavItem'
import Space from 'rebass/dist/Space'
2016-12-19 15:40:11 +01:00
import Tabs from 'react-simpletabs'
2016-09-15 09:13:23 +02:00
2016-09-10 14:10:25 +02:00
import theme from '../theme.js'
2016-12-18 18:58:33 +01:00
import SourceEditor from './source.jsx'
2016-12-18 19:43:01 +01:00
import FilterEditor from '../filter/editor.jsx'
2016-12-19 15:56:19 +01:00
import { PropertyGroup } from '../fields/spec.jsx'
2016-12-19 15:40:11 +01:00
2016-09-10 14:47:06 +02:00
import MdVisibility from 'react-icons/lib/md/visibility'
import MdVisibilityOff from 'react-icons/lib/md/visibility-off'
import MdDelete from 'react-icons/lib/md/delete'
import PureRenderMixin from 'react-addons-pure-render-mixin';
2016-09-10 14:47:06 +02:00
2016-12-17 20:19:01 +01:00
import ScrollContainer from '../scrollcontainer.jsx'
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. */
export class LayerEditor extends React.Component {
2016-12-17 15:44:42 +01:00
static propTypes = {
layer: React.PropTypes.object.isRequired,
2016-12-18 18:58:33 +01:00
sources: React.PropTypes.instanceOf(Immutable.Map),
2016-12-17 15:44:42 +01:00
onLayerChanged: React.PropTypes.func,
onLayerDestroyed: React.PropTypes.func,
}
static defaultProps = {
onLayerChanged: () => {},
onLayerDestroyed: () => {},
}
static childContextTypes = {
reactIconBase: React.PropTypes.object
}
constructor(props) {
super(props);
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
}
getChildContext () {
return {
reactIconBase: {
size: theme.fontSizes[4],
color: theme.colors.lowgray,
}
}
}
onPaintChanged(property, newValue) {
let layer = this.props.layer
//TODO: by using immutable records we can avoid this checking if object exists
2016-12-17 21:25:00 +01:00
//
2016-12-17 15:44:42 +01:00
if(!layer.has('paint')) {
layer = layer.set('paint', Immutable.Map())
}
const changedLayer = layer.setIn(['paint', property], newValue)
this.props.onLayerChanged(changedLayer)
}
onLayoutChanged(property, newValue) {
let layer = this.props.layer
//TODO: by using immutable records we can avoid this checking if object exists
if(!layer.has('layout')) {
layer = layer.set('layout', Immutable.Map())
}
const changedLayer = layer.setIn(['layout', property], newValue)
this.props.onLayerChanged(changedLayer)
}
2016-12-19 14:56:10 +01:00
onFilterChanged(newValue) {
let layer = this.props.layer
const changedLayer = layer.set('filter', newValue)
this.props.onLayerChanged(changedLayer)
}
2016-12-17 15:44:42 +01:00
toggleVisibility() {
if(this.props.layer.has('layout') && this.props.layer.getIn(['layout', 'visibility']) === 'none') {
this.onLayoutChanged('visibility', 'visible')
} else {
this.onLayoutChanged('visibility', 'none')
}
}
render() {
let visibleIcon = <MdVisibilityOff />
if(this.props.layer.has('layout') && this.props.layer.getIn(['layout', 'visibility']) === 'none') {
visibleIcon = <MdVisibility />
}
return <div style={{
padding: theme.scale[0],
}}>
2016-12-17 16:09:37 +01:00
<Toolbar>
2016-12-17 15:44:42 +01:00
<NavItem style={{fontWeight: 400}}>
#{this.props.layer.get('id')}
</NavItem>
<Space auto x={1} />
<NavItem onClick={this.toggleVisibility.bind(this)}>
{visibleIcon}
</NavItem>
<NavItem onClick={(e) => this.props.onLayerDestroyed(this.props.layer)}>
<MdDelete />
</NavItem>
</Toolbar>
2016-12-19 15:40:11 +01:00
<Tabs>
<Tabs.Panel title='Paint'>
<div style={{padding: theme.scale[2], paddingRight: 0, backgroundColor: theme.colors.black}}>
2016-12-19 15:56:19 +01:00
<PropertyGroup
onChange={this.onPaintChanged.bind(this)}
layerType={this.props.layer.get('type')}
groupType="paint"
properties={this.props.layer.get('paint', Immutable.Map())}
/>
</div>
</Tabs.Panel>
<Tabs.Panel title='Layout'>
<div style={{padding: theme.scale[2], paddingRight: 0, backgroundColor: theme.colors.black}}>
<PropertyGroup
onChange={this.onLayoutChanged.bind(this)}
layerType={this.props.layer.get('type')}
groupType="layout"
properties={this.props.layer.get('layout', Immutable.Map())}
/>
2016-12-19 15:40:11 +01:00
</div>
</Tabs.Panel>
<Tabs.Panel title='Data'>
<FilterEditor
filter={this.props.layer.get('filter', Immutable.List()).toJSON()}
onChange={f => this.onFilterChanged(Immutable.fromJS(f))}
/>
{this.props.layer.get('type') !== 'background' && <SourceEditor
source={this.props.layer.get('source')}
sourceLayer={this.props.layer.get('source-layer')}
sources={this.props.sources}
onSourceChange={console.log}
onSourceLayerChange={console.log}
/>}
</Tabs.Panel>
</Tabs>
2016-12-17 15:44:42 +01:00
</div>
}
2016-09-10 14:10:25 +02:00
}