2016-09-12 19:44:28 +02:00
|
|
|
import React from 'react'
|
2016-12-17 17:40:44 +01:00
|
|
|
import color from 'color'
|
|
|
|
|
2016-09-12 19:44:28 +02:00
|
|
|
import GlSpec from 'mapbox-gl-style-spec/reference/latest.min.js'
|
2016-12-20 11:44:22 +01:00
|
|
|
import ColorField from './ColorField'
|
2016-12-26 11:48:52 +01:00
|
|
|
import NumberInput from '../inputs/NumberInput'
|
|
|
|
import CheckboxInput from '../inputs/CheckboxInput'
|
|
|
|
import StringInput from '../inputs/StringInput'
|
|
|
|
import SelectInput from '../inputs/SelectInput'
|
2016-12-28 16:48:49 +01:00
|
|
|
import MultiButtonInput from '../inputs/MultiButtonInput'
|
2016-12-29 22:37:54 +01:00
|
|
|
import ArrayInput from '../inputs/ArrayInput'
|
2016-12-30 18:13:41 +01:00
|
|
|
import FontInput from '../inputs/FontInput'
|
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'
|
2016-09-12 19:44:28 +02:00
|
|
|
|
2016-12-17 20:36:43 +01:00
|
|
|
function labelFromFieldName(fieldName) {
|
|
|
|
let label = fieldName.split('-').slice(1).join(' ')
|
|
|
|
if(label.length > 0) {
|
|
|
|
label = label.charAt(0).toUpperCase() + label.slice(1);
|
|
|
|
}
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
|
2016-12-29 22:12:36 +01:00
|
|
|
function optionsLabelLength(options) {
|
|
|
|
let sum = 0;
|
|
|
|
options.forEach(([_, label]) => {
|
|
|
|
sum += label.length
|
|
|
|
})
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:40:44 +01:00
|
|
|
/** Display any field from the Mapbox GL style spec and
|
|
|
|
* choose the correct field component based on the @{fieldSpec}
|
|
|
|
* to display @{value}. */
|
2016-12-20 11:44:22 +01:00
|
|
|
export default class SpecField extends React.Component {
|
2016-12-17 17:40:44 +01:00
|
|
|
static propTypes = {
|
2016-12-20 11:44:22 +01:00
|
|
|
onChange: React.PropTypes.func.isRequired,
|
|
|
|
fieldName: React.PropTypes.string.isRequired,
|
|
|
|
fieldSpec: React.PropTypes.object.isRequired,
|
2016-12-17 17:40:44 +01:00
|
|
|
value: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.number,
|
2016-12-29 22:37:54 +01:00
|
|
|
React.PropTypes.array,
|
2016-12-17 17:40:44 +01:00
|
|
|
]),
|
2016-12-19 16:21:22 +01:00
|
|
|
/** Override the style of the field */
|
|
|
|
style: React.PropTypes.object,
|
2016-12-17 17:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-12-20 14:13:37 +01:00
|
|
|
const commonProps = {
|
|
|
|
style: this.props.style,
|
|
|
|
value: this.props.value,
|
2017-01-10 09:51:18 +01:00
|
|
|
default: this.props.fieldSpec.default,
|
2016-12-20 14:13:37 +01:00
|
|
|
name: this.props.fieldName,
|
|
|
|
onChange: newValue => this.props.onChange(this.props.fieldName, newValue)
|
|
|
|
}
|
2016-12-17 17:40:44 +01:00
|
|
|
switch(this.props.fieldSpec.type) {
|
|
|
|
case 'number': return (
|
2016-12-26 11:48:52 +01:00
|
|
|
<NumberInput
|
2016-12-20 14:13:37 +01:00
|
|
|
{...commonProps}
|
2016-12-21 10:31:38 +01:00
|
|
|
min={this.props.fieldSpec.minimum}
|
|
|
|
max={this.props.fieldSpec.maximum}
|
2016-12-17 17:40:44 +01:00
|
|
|
/>
|
|
|
|
)
|
2016-12-28 16:48:49 +01:00
|
|
|
case 'enum':
|
|
|
|
const options = Object.keys(this.props.fieldSpec.values).map(v => [v, capitalize(v)])
|
2016-12-29 22:12:36 +01:00
|
|
|
|
|
|
|
if(options.length <= 3 && optionsLabelLength(options) <= 20) {
|
2016-12-28 16:48:49 +01:00
|
|
|
return <MultiButtonInput
|
|
|
|
{...commonProps}
|
|
|
|
options={options}
|
|
|
|
/>
|
|
|
|
} else {
|
|
|
|
return <SelectInput
|
|
|
|
{...commonProps}
|
|
|
|
options={options}
|
|
|
|
/>
|
|
|
|
}
|
2016-12-17 17:40:44 +01:00
|
|
|
case 'string': return (
|
2016-12-26 11:48:52 +01:00
|
|
|
<StringInput
|
2016-12-20 14:13:37 +01:00
|
|
|
{...commonProps}
|
2016-12-17 17:40:44 +01:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
case 'color': return (
|
|
|
|
<ColorField
|
2016-12-20 14:13:37 +01:00
|
|
|
{...commonProps}
|
2016-12-18 20:08:20 +01:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
case 'boolean': return (
|
2016-12-26 11:48:52 +01:00
|
|
|
<CheckboxInput
|
2016-12-20 14:13:37 +01:00
|
|
|
{...commonProps}
|
2016-12-17 17:40:44 +01:00
|
|
|
/>
|
|
|
|
)
|
2016-12-30 18:13:41 +01:00
|
|
|
case 'array':
|
|
|
|
if(this.props.fieldName === 'text-font') {
|
|
|
|
return <FontInput
|
|
|
|
{...commonProps}
|
|
|
|
/>
|
|
|
|
} else {
|
|
|
|
return <ArrayInput
|
|
|
|
{...commonProps}
|
|
|
|
type={this.props.fieldSpec.value}
|
|
|
|
length={this.props.fieldSpec.length}
|
|
|
|
/>
|
|
|
|
}
|
2016-12-17 17:40:44 +01:00
|
|
|
default: return null
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 19:44:28 +02:00
|
|
|
}
|