2016-12-20 11:44:22 +01:00
|
|
|
import React from 'react'
|
|
|
|
import Color from 'color'
|
|
|
|
|
2016-12-25 18:30:23 +01:00
|
|
|
import Button from '../Button'
|
2016-12-20 11:44:22 +01:00
|
|
|
import SpecField from './SpecField'
|
2016-12-31 12:17:02 +01:00
|
|
|
import NumberInput from '../inputs/NumberInput'
|
2016-12-25 19:00:21 +01:00
|
|
|
import DocLabel from './DocLabel'
|
2016-12-20 11:44:22 +01:00
|
|
|
|
2016-12-25 18:30:23 +01:00
|
|
|
import AddIcon from 'react-icons/lib/md/add-circle-outline'
|
|
|
|
import DeleteIcon from 'react-icons/lib/md/delete'
|
2017-01-09 16:08:22 +01:00
|
|
|
import FunctionIcon from 'react-icons/lib/md/functions'
|
2016-12-25 18:30:23 +01:00
|
|
|
|
2016-12-28 16:48:49 +01:00
|
|
|
import capitalize from 'lodash.capitalize'
|
2016-12-20 11:44:22 +01:00
|
|
|
import input from '../../config/input.js'
|
|
|
|
import colors from '../../config/colors.js'
|
2016-12-25 18:30:23 +01:00
|
|
|
import { margins, fontSizes } from '../../config/scales.js'
|
2016-12-20 11:44:22 +01:00
|
|
|
|
|
|
|
function isZoomField(value) {
|
2016-12-20 16:08:49 +01:00
|
|
|
return typeof value === 'object' && value.stops
|
2016-12-20 11:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Supports displaying spec field for zoom function objects
|
|
|
|
* https://www.mapbox.com/mapbox-gl-style-spec/#types-function-zoom-property
|
|
|
|
*/
|
|
|
|
export default class ZoomSpecField extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
onChange: React.PropTypes.func.isRequired,
|
|
|
|
fieldName: React.PropTypes.string.isRequired,
|
|
|
|
fieldSpec: React.PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
value: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.object,
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.number,
|
|
|
|
React.PropTypes.bool,
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
|
2016-12-31 13:42:25 +01:00
|
|
|
addStop() {
|
|
|
|
const stops = this.props.value.stops.slice(0)
|
|
|
|
const lastStop = stops[stops.length - 1]
|
|
|
|
stops.push([lastStop[0] + 1, lastStop[1]])
|
|
|
|
|
|
|
|
const changedValue = {
|
|
|
|
...this.props.value,
|
|
|
|
stops: stops,
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.onChange(this.props.fieldName, changedValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteStop(stopIdx) {
|
|
|
|
const stops = this.props.value.stops.slice(0)
|
|
|
|
stops.splice(stopIdx, 1)
|
|
|
|
|
|
|
|
let changedValue = {
|
|
|
|
...this.props.value,
|
|
|
|
stops: stops,
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stops.length === 1) {
|
|
|
|
changedValue = stops[0][1]
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.onChange(this.props.fieldName, changedValue)
|
|
|
|
}
|
|
|
|
|
2017-01-09 16:08:22 +01:00
|
|
|
makeZoomFunction() {
|
|
|
|
const zoomFunc = {
|
|
|
|
stops: [
|
|
|
|
[6, this.props.value],
|
|
|
|
[10, this.props.value]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
this.props.onChange(this.props.fieldName, zoomFunc)
|
|
|
|
}
|
|
|
|
|
2016-12-31 13:42:25 +01:00
|
|
|
changeStop(changeIdx, zoomLevel, value) {
|
|
|
|
const stops = this.props.value.stops.slice(0)
|
|
|
|
stops[changeIdx] = [zoomLevel, value]
|
|
|
|
const changedValue = {
|
|
|
|
...this.props.value,
|
|
|
|
stops: stops,
|
|
|
|
}
|
|
|
|
this.props.onChange(this.props.fieldName, changedValue)
|
|
|
|
}
|
|
|
|
|
2016-12-20 11:44:22 +01:00
|
|
|
render() {
|
|
|
|
if(isZoomField(this.props.value)) {
|
2017-01-09 16:08:22 +01:00
|
|
|
let label = <DocLabel
|
|
|
|
label={labelFromFieldName(this.props.fieldName)}
|
|
|
|
doc={this.props.fieldSpec.doc}
|
|
|
|
style={{
|
|
|
|
width: '50%',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
2016-12-25 18:30:23 +01:00
|
|
|
const zoomFields = this.props.value.stops.map((stop, idx) => {
|
2016-12-31 12:17:02 +01:00
|
|
|
label = <DocLabel
|
|
|
|
doc={this.props.fieldSpec.doc}
|
2017-01-09 21:39:35 +01:00
|
|
|
style={{ width: '41%'}}
|
2016-12-31 12:17:02 +01:00
|
|
|
label={idx > 0 ? "" : labelFromFieldName(this.props.fieldName)}
|
|
|
|
/>
|
2016-12-25 18:30:23 +01:00
|
|
|
|
|
|
|
if(idx === 1) {
|
2017-01-09 21:39:35 +01:00
|
|
|
label = <label style={{...input.label, width: '41%'}}>
|
2016-12-31 13:42:25 +01:00
|
|
|
<Button
|
|
|
|
style={{fontSize: fontSizes[5]}}
|
|
|
|
onClick={this.addStop.bind(this)}
|
|
|
|
>
|
2016-12-25 18:30:23 +01:00
|
|
|
Add stop
|
|
|
|
</Button>
|
|
|
|
</label>
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:08:49 +01:00
|
|
|
const zoomLevel = stop[0]
|
|
|
|
const value = stop[1]
|
2016-12-20 11:44:22 +01:00
|
|
|
|
2016-12-31 12:17:02 +01:00
|
|
|
return <div key={zoomLevel} style={{
|
|
|
|
...input.property,
|
|
|
|
marginLeft: 0,
|
|
|
|
marginRight: 0
|
|
|
|
}}>
|
2016-12-20 11:44:22 +01:00
|
|
|
{label}
|
2016-12-31 13:42:25 +01:00
|
|
|
<Button
|
|
|
|
style={{backgroundColor: null}}
|
|
|
|
onClick={this.deleteStop.bind(this, idx)}
|
|
|
|
>
|
2016-12-25 18:30:23 +01:00
|
|
|
<DeleteIcon />
|
|
|
|
</Button>
|
2016-12-31 12:17:02 +01:00
|
|
|
<NumberInput
|
2016-12-20 11:44:22 +01:00
|
|
|
style={{
|
2017-01-09 21:39:35 +01:00
|
|
|
width: '7.5%'
|
2016-12-20 11:44:22 +01:00
|
|
|
}}
|
|
|
|
value={zoomLevel}
|
2016-12-31 13:42:25 +01:00
|
|
|
onChange={changedStop => this.changeStop(idx, changedStop, value)}
|
2016-12-20 11:44:22 +01:00
|
|
|
min={0}
|
|
|
|
max={22}
|
|
|
|
/>
|
2016-12-31 13:42:25 +01:00
|
|
|
<SpecField
|
|
|
|
fieldName={this.props.fieldName}
|
|
|
|
fieldSpec={this.props.fieldSpec}
|
2016-12-25 18:30:23 +01:00
|
|
|
value={value}
|
2016-12-31 13:42:25 +01:00
|
|
|
onChange={(_, newValue) => this.changeStop(idx, zoomLevel, newValue)}
|
2016-12-25 18:30:23 +01:00
|
|
|
style={{
|
2017-01-09 21:39:35 +01:00
|
|
|
width: '41%',
|
|
|
|
marginLeft: '1.5%',
|
2016-12-25 18:30:23 +01:00
|
|
|
}}
|
|
|
|
/>
|
2016-12-20 11:44:22 +01:00
|
|
|
</div>
|
2016-12-20 16:08:49 +01:00
|
|
|
})
|
2016-12-31 12:17:02 +01:00
|
|
|
|
2016-12-25 18:30:23 +01:00
|
|
|
return <div style={input.property}>
|
2016-12-20 11:44:22 +01:00
|
|
|
{zoomFields}
|
|
|
|
</div>
|
|
|
|
} else {
|
2017-01-09 16:08:22 +01:00
|
|
|
|
|
|
|
if(this.props.fieldSpec['zoom-function']) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-20 11:44:22 +01:00
|
|
|
return <div style={input.property}>
|
2017-01-09 16:08:22 +01:00
|
|
|
<DocLabel
|
|
|
|
label={labelFromFieldName(this.props.fieldName)}
|
|
|
|
doc={this.props.fieldSpec.doc}
|
|
|
|
style={{
|
2017-01-09 21:39:35 +01:00
|
|
|
width: this.props.fieldSpec['zoom-function'] ? '41%' : '50%',
|
2017-01-09 16:08:22 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{this.props.fieldSpec['zoom-function'] &&
|
|
|
|
<Button
|
|
|
|
style={{backgroundColor: null}}
|
|
|
|
onClick={this.makeZoomFunction.bind(this)}
|
|
|
|
>
|
|
|
|
<FunctionIcon />
|
|
|
|
</Button>
|
|
|
|
}
|
2017-01-09 21:39:35 +01:00
|
|
|
<SpecField {...this.props} style={{ width: '50%' } }/>
|
2016-12-20 11:44:22 +01:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function labelFromFieldName(fieldName) {
|
|
|
|
let label = fieldName.split('-').slice(1).join(' ')
|
2016-12-28 16:48:49 +01:00
|
|
|
return capitalize(label)
|
2016-12-20 11:44:22 +01:00
|
|
|
}
|