maputnik/src/components/layers/LayerListItem.jsx

127 lines
3.4 KiB
React
Raw Normal View History

2016-12-17 16:43:25 +01:00
import React from 'react'
import PropTypes from 'prop-types'
2016-12-19 11:46:48 +01:00
import Color from 'color'
2017-01-11 09:35:48 +01:00
import classnames from 'classnames'
2016-12-17 16:43:25 +01:00
2016-12-20 19:20:56 +01:00
import CopyIcon from 'react-icons/lib/md/content-copy'
import VisibilityIcon from 'react-icons/lib/md/visibility'
import VisibilityOffIcon from 'react-icons/lib/md/visibility-off'
import DeleteIcon from 'react-icons/lib/md/delete'
2016-12-20 11:44:22 +01:00
import LayerIcon from '../icons/LayerIcon'
import LayerEditor from './LayerEditor'
import {SortableElement, SortableHandle} from 'react-sortable-hoc'
2016-12-18 17:28:59 +01:00
@SortableHandle
class LayerTypeDragHandle extends React.Component {
static propTypes = LayerIcon.propTypes
render() {
return <LayerIcon
{...this.props}
2016-12-22 13:40:23 +01:00
style={{
cursor: 'move',
2017-01-11 14:11:45 +01:00
width: 14,
height: 14,
paddingRight: 3,
2016-12-22 13:40:23 +01:00
}}
2016-12-18 17:28:59 +01:00
/>
}
}
2016-12-17 19:58:30 +01:00
2016-12-20 19:20:56 +01:00
class IconAction extends React.Component {
static propTypes = {
action: PropTypes.string.isRequired,
onClick: PropTypes.func.isRequired,
wdKey: PropTypes.string
2016-12-20 19:20:56 +01:00
}
renderIcon() {
switch(this.props.action) {
case 'duplicate': return <CopyIcon />
2017-01-11 09:35:48 +01:00
case 'show': return <VisibilityIcon />
case 'hide': return <VisibilityOffIcon />
case 'delete': return <DeleteIcon />
2016-12-20 19:20:56 +01:00
}
}
render() {
return <button
tabIndex="-1"
title={this.props.action}
2017-01-11 15:48:15 +01:00
className="maputnik-layer-list-icon-action"
data-wd-key={this.props.wdKey}
onClick={this.props.onClick}
2016-12-20 19:20:56 +01:00
>
{this.renderIcon()}
</button>
2016-12-20 19:20:56 +01:00
}
}
2016-12-17 19:58:30 +01:00
@SortableElement
2016-12-17 16:43:25 +01:00
class LayerListItem extends React.Component {
static propTypes = {
layerId: PropTypes.string.isRequired,
layerType: PropTypes.string.isRequired,
isSelected: PropTypes.bool,
visibility: PropTypes.string,
className: PropTypes.string,
2016-12-20 19:20:56 +01:00
onLayerSelect: PropTypes.func.isRequired,
onLayerCopy: PropTypes.func,
onLayerDestroy: PropTypes.func,
onLayerVisibilityToggle: PropTypes.func,
2016-12-20 19:20:56 +01:00
}
static defaultProps = {
isSelected: false,
visibility: 'visible',
onLayerCopy: () => {},
onLayerDestroy: () => {},
onLayerVisibilityToggle: () => {},
2016-12-20 19:20:56 +01:00
}
static childContextTypes = {
reactIconBase: PropTypes.object
2016-12-20 19:20:56 +01:00
}
getChildContext() {
return {
2017-01-11 14:11:45 +01:00
reactIconBase: { size: 14 }
2016-12-20 19:20:56 +01:00
}
2016-12-17 16:43:25 +01:00
}
render() {
2016-12-17 19:58:30 +01:00
return <li
key={this.props.layerId}
onClick={e => this.props.onLayerSelect(this.props.layerId)}
data-wd-key={"layer-list-item:"+this.props.layerId}
2017-01-11 09:35:48 +01:00
className={classnames({
"maputnik-layer-list-item": true,
"maputnik-layer-list-item-selected": this.props.isSelected,
2017-01-11 18:18:47 +01:00
[this.props.className]: true,
2017-01-11 09:35:48 +01:00
})}>
2016-12-20 19:20:56 +01:00
<LayerTypeDragHandle type={this.props.layerType} />
2017-01-10 21:28:30 +01:00
<span className="maputnik-layer-list-item-id">{this.props.layerId}</span>
2016-12-20 19:20:56 +01:00
<span style={{flexGrow: 1}} />
2017-01-11 09:35:48 +01:00
<IconAction
wdKey={"layer-list-item:"+this.props.layerId+":delete"}
2016-12-20 19:20:56 +01:00
action={'delete'}
onClick={e => this.props.onLayerDestroy(this.props.layerId)}
2016-12-20 19:20:56 +01:00
/>
2017-01-11 09:35:48 +01:00
<IconAction
wdKey={"layer-list-item:"+this.props.layerId+":copy"}
action={'duplicate'}
onClick={e => this.props.onLayerCopy(this.props.layerId)}
2016-12-20 19:20:56 +01:00
/>
2017-01-11 09:35:48 +01:00
<IconAction
wdKey={"layer-list-item:"+this.props.layerId+":toggle-visibility"}
action={this.props.visibility === 'visible' ? 'hide' : 'show'}
onClick={e => this.props.onLayerVisibilityToggle(this.props.layerId)}
2016-12-20 19:20:56 +01:00
/>
2016-12-17 19:58:30 +01:00
</li>
2016-12-17 16:43:25 +01:00
}
}
2016-12-20 19:20:56 +01:00
export default LayerListItem;