maputnik/src/layers/list.jsx

94 lines
2.6 KiB
React
Raw Normal View History

2016-09-10 14:10:25 +02:00
import React from 'react'
import Immutable from 'immutable'
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-09-10 14:10:25 +02:00
import { LayerEditor } from './editor.jsx'
2016-12-17 16:43:25 +01:00
import LayerListItem from './listitem.jsx'
import PureRenderMixin from 'react-addons-pure-render-mixin';
2016-12-17 16:09:37 +01:00
import theme from '../theme.js'
2016-12-17 20:19:01 +01:00
import ScrollContainer from '../scrollcontainer.jsx'
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 = {
layers: React.PropTypes.instanceOf(Immutable.OrderedMap),
onLayersChanged: React.PropTypes.func.isRequired,
onLayerSelected: React.PropTypes.func,
}
// List of collapsible layer editors
@SortableContainer
class LayerListContainer extends React.Component {
static propTypes = {...layerListPropTypes}
2016-12-17 16:09:37 +01:00
static defaultProps = {
onLayerSelected: () => {},
2016-12-04 17:03:36 +01:00
}
constructor(props) {
super(props)
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
}
onLayerDestroyed(deletedLayer) {
const remainingLayers = this.props.layers.delete(deletedLayer.get('id'))
this.props.onLayersChanged(remainingLayers)
}
onLayerChanged(layer) {
const changedLayers = this.props.layers.set(layer.get('id'), layer)
this.props.onLayersChanged(changedLayers)
}
render() {
2016-12-17 19:58:30 +01:00
const layerPanels = this.props.layers.toIndexedSeq().map((layer, index) => {
2016-12-17 16:09:37 +01:00
const layerId = layer.get('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-18 17:28:59 +01:00
layerType={layer.get('type')}
2016-12-17 16:43:25 +01:00
onLayerSelected={this.props.onLayerSelected}
/>
2016-12-17 19:58:30 +01:00
})
2016-12-17 20:19:01 +01:00
return <ScrollContainer>
2016-12-18 17:28:59 +01:00
<ul style={{ padding: theme.scale[1], margin: 0 }}>
2016-12-17 20:19:01 +01:00
{layerPanels}
</ul>
</ScrollContainer>
2016-12-17 19:58:30 +01:00
}
}
export class LayerList extends React.Component {
static propTypes = {...layerListPropTypes}
constructor(props) {
super(props)
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
}
onSortEnd(move) {
const { oldIndex, newIndex } = move
if(oldIndex === newIndex) return
//TODO: Implement this more performant for immutable collections
// instead of converting back and forth
let layers = this.props.layers.toArray()
layers = arrayMove(layers, oldIndex, newIndex)
layers = Immutable.OrderedMap(layers.map(l => [l.get('id'), l]))
this.props.onLayersChanged(layers)
}
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
}