maputnik/src/components/layers/LayerList.jsx

116 lines
3.4 KiB
React
Raw Normal View History

2016-09-10 14:10:25 +02:00
import React from 'react'
2016-12-20 11:44:22 +01:00
import PureRenderMixin from 'react-addons-pure-render-mixin';
import cloneDeep from 'lodash.clonedeep'
2016-09-15 09:13:23 +02:00
import Heading from 'rebass/dist/Heading'
import Toolbar from 'rebass/dist/Toolbar'
import NavItem from 'rebass/dist/NavItem'
import Space from 'rebass/dist/Space'
2016-12-20 11:44:22 +01:00
import LayerListItem from './LayerListItem'
import ScrollContainer from '../ScrollContainer'
import style from '../../libs/style.js'
2016-12-20 11:44:22 +01:00
import { margins } from '../../config/scales.js'
2016-09-10 14:10:25 +02:00
2016-12-17 19:58:30 +01:00
import {SortableContainer, SortableHandle, arrayMove} from 'react-sortable-hoc';
2016-12-17 16:09:37 +01:00
2016-12-17 19:58:30 +01:00
const layerListPropTypes = {
2016-12-20 16:08:49 +01:00
layers: React.PropTypes.array.isRequired,
2016-12-20 19:20:56 +01:00
selectedLayerIndex: React.PropTypes.number.isRequired,
onLayersChange: React.PropTypes.func.isRequired,
onLayerSelect: React.PropTypes.func,
2016-12-17 19:58:30 +01:00
}
// List of collapsible layer editors
@SortableContainer
class LayerListContainer extends React.Component {
static propTypes = {...layerListPropTypes}
2016-12-17 16:09:37 +01:00
static defaultProps = {
onLayerSelect: () => {},
2016-12-04 17:03:36 +01:00
}
constructor(props) {
super(props)
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
}
onLayerDestroy(layerId) {
const remainingLayers = this.props.layers.slice(0)
const idx = style.indexOfLayer(remainingLayers, layerId)
remainingLayers.splice(idx, 1);
this.props.onLayersChange(remainingLayers)
}
onLayerCopy(layerId) {
const changedLayers = this.props.layers.slice(0)
const idx = style.indexOfLayer(changedLayers, layerId)
const clonedLayer = cloneDeep(changedLayers[idx])
clonedLayer.id = clonedLayer.id + "-copy"
changedLayers.splice(idx, 0, clonedLayer)
this.props.onLayersChange(changedLayers)
}
onLayerVisibilityToggle(layerId) {
const changedLayers = this.props.layers.slice(0)
const idx = style.indexOfLayer(changedLayers, layerId)
const layer = { ...changedLayers[idx] }
const changedLayout = 'layout' in layer ? {...layer.layout} : {}
changedLayout.visibility = changedLayout.visibility === 'none' ? 'visible' : 'none'
layer.layout = changedLayout
changedLayers[idx] = layer
this.props.onLayersChange(changedLayers)
2016-12-04 17:03:36 +01:00
}
render() {
2016-12-20 16:08:49 +01:00
const layerPanels = this.props.layers.map((layer, index) => {
const layerId = layer.id
2016-12-17 16:43:25 +01:00
return <LayerListItem
2016-12-17 19:58:30 +01:00
index={index}
2016-12-17 16:43:25 +01:00
key={layerId}
layerId={layerId}
2016-12-20 16:08:49 +01:00
layerType={layer.type}
visibility={(layer.layout || {}).visibility}
2016-12-20 19:20:56 +01:00
isSelected={index === this.props.selectedLayerIndex}
onLayerSelect={this.props.onLayerSelect}
onLayerDestroy={this.onLayerDestroy.bind(this)}
onLayerCopy={this.onLayerCopy.bind(this)}
onLayerVisibilityToggle={this.onLayerVisibilityToggle.bind(this)}
2016-12-17 16:43:25 +01:00
/>
2016-12-17 19:58:30 +01:00
})
2016-12-17 20:19:01 +01:00
return <ScrollContainer>
2016-12-20 11:44:22 +01:00
<ul style={{ padding: margins[1], margin: 0 }}>
2016-12-17 20:19:01 +01:00
{layerPanels}
</ul>
</ScrollContainer>
2016-12-17 19:58:30 +01:00
}
}
2016-12-20 11:44:22 +01:00
export default class LayerList extends React.Component {
2016-12-17 19:58:30 +01:00
static propTypes = {...layerListPropTypes}
constructor(props) {
super(props)
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
}
onSortEnd(move) {
const { oldIndex, newIndex } = move
if(oldIndex === newIndex) return
2016-12-20 16:08:49 +01:00
let layers = this.props.layers.slice(0)
2016-12-17 19:58:30 +01:00
layers = arrayMove(layers, oldIndex, newIndex)
this.props.onLayersChange(layers)
2016-12-17 19:58:30 +01:00
}
render() {
return <LayerListContainer
{...this.props}
onSortEnd={this.onSortEnd.bind(this)}
useDragHandle={true}
/>
2016-12-04 17:03:36 +01:00
}
2016-09-10 14:10:25 +02:00
}