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'
|
2017-10-01 21:49:32 +02:00
|
|
|
import StringInput from '../inputs/StringInput'
|
|
|
|
import SelectInput from '../inputs/SelectInput'
|
2016-12-25 19:00:21 +01:00
|
|
|
import DocLabel from './DocLabel'
|
2017-01-11 13:34:38 +01:00
|
|
|
import InputBlock from '../inputs/InputBlock'
|
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'
|
2017-10-01 21:49:32 +02:00
|
|
|
import MdInsertChart from 'react-icons/lib/md/insert-chart'
|
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
|
|
|
|
|
|
|
function isZoomField(value) {
|
2017-10-01 21:49:32 +02:00
|
|
|
return typeof value === 'object' && value.stops && typeof value.property === 'undefined'
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDataField(value) {
|
|
|
|
return typeof value === 'object' && value.stops && typeof value.property !== 'undefined'
|
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
|
|
|
|
*/
|
2017-10-10 21:23:20 +02:00
|
|
|
export default class FunctionSpecProperty extends React.Component {
|
2016-12-20 11:44:22 +01:00
|
|
|
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,
|
2017-06-26 23:54:46 +02:00
|
|
|
React.PropTypes.array
|
2016-12-20 11:44:22 +01:00
|
|
|
]),
|
|
|
|
}
|
|
|
|
|
2016-12-31 13:42:25 +01:00
|
|
|
addStop() {
|
|
|
|
const stops = this.props.value.stops.slice(0)
|
|
|
|
const lastStop = stops[stops.length - 1]
|
2017-10-01 21:49:32 +02:00
|
|
|
if (typeof lastStop[0] === "object") {
|
|
|
|
stops.push([
|
2017-10-10 17:30:06 +02:00
|
|
|
{zoom: lastStop[0].zoom + 1, value: lastStop[0].value},
|
2017-10-01 21:49:32 +02:00
|
|
|
lastStop[1]
|
|
|
|
])
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stops.push([lastStop[0] + 1, lastStop[1]])
|
|
|
|
}
|
2016-12-31 13:42:25 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-10 17:30:06 +02:00
|
|
|
getDataFunctionTypes(functionType) {
|
|
|
|
if (functionType === "interpolated") {
|
|
|
|
return ["categorical", "interval", "exponential"]
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ["categorical", "interval"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 21:49:32 +02:00
|
|
|
makeDataFunction() {
|
|
|
|
const dataFunc = {
|
|
|
|
property: "",
|
2017-10-10 17:30:06 +02:00
|
|
|
type: "categorical",
|
2017-10-01 21:49:32 +02:00
|
|
|
stops: [
|
|
|
|
[{zoom: 6, value: 0}, this.props.value],
|
|
|
|
[{zoom: 10, value: 0}, this.props.value]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
this.props.onChange(this.props.fieldName, dataFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
changeStop(changeIdx, stopData, value) {
|
2016-12-31 13:42:25 +01:00
|
|
|
const stops = this.props.value.stops.slice(0)
|
2017-10-01 21:49:32 +02:00
|
|
|
stops[changeIdx] = [stopData, value]
|
2016-12-31 13:42:25 +01:00
|
|
|
const changedValue = {
|
|
|
|
...this.props.value,
|
|
|
|
stops: stops,
|
|
|
|
}
|
|
|
|
this.props.onChange(this.props.fieldName, changedValue)
|
|
|
|
}
|
|
|
|
|
2017-10-01 21:49:32 +02:00
|
|
|
changeDataProperty(propName, propVal) {
|
2017-10-11 12:58:32 +02:00
|
|
|
if (propVal) {
|
|
|
|
this.props.value[propName] = propVal
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete this.props.value[propName]
|
|
|
|
}
|
2017-10-01 21:49:32 +02:00
|
|
|
this.props.onChange(this.props.fieldName, this.props.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDataProperty() {
|
|
|
|
const dataFields = this.props.value.stops.map((stop, idx) => {
|
|
|
|
const zoomLevel = stop[0].zoom
|
|
|
|
const dataLevel = stop[0].value
|
|
|
|
const value = stop[1]
|
|
|
|
const deleteStopBtn = <DeleteStopButton onClick={this.deleteStop.bind(this, idx)} />
|
2017-10-11 15:01:55 +02:00
|
|
|
|
|
|
|
const dataProps = {
|
|
|
|
label: "Data value",
|
|
|
|
value: dataLevel,
|
|
|
|
onChange: newData => this.changeStop(idx, { zoom: zoomLevel, value: newData }, value)
|
|
|
|
}
|
|
|
|
const dataInput = this.props.value.type === "categorical" ? <StringInput {...dataProps} /> : <NumberInput {...dataProps} />
|
|
|
|
|
2017-10-01 21:49:32 +02:00
|
|
|
return <InputBlock key={idx} action={deleteStopBtn}>
|
|
|
|
<div className="maputnik-data-spec-property-stop-edit">
|
|
|
|
<NumberInput
|
|
|
|
value={zoomLevel}
|
|
|
|
onChange={newZoom => this.changeStop(idx, {zoom: newZoom, value: dataLevel}, value)}
|
|
|
|
min={0}
|
|
|
|
max={22}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="maputnik-data-spec-property-stop-data">
|
2017-10-11 15:01:55 +02:00
|
|
|
{dataInput}
|
2017-10-01 21:49:32 +02:00
|
|
|
</div>
|
|
|
|
<div className="maputnik-data-spec-property-stop-value">
|
|
|
|
<SpecField
|
|
|
|
fieldName={this.props.fieldName}
|
|
|
|
fieldSpec={this.props.fieldSpec}
|
|
|
|
value={value}
|
|
|
|
onChange={(_, newValue) => this.changeStop(idx, {zoom: zoomLevel, value: dataLevel}, newValue)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</InputBlock>
|
|
|
|
})
|
|
|
|
|
|
|
|
return <div className="maputnik-data-spec-block">
|
|
|
|
<div className="maputnik-data-spec-property">
|
|
|
|
<InputBlock
|
|
|
|
doc={this.props.fieldSpec.doc}
|
|
|
|
label={labelFromFieldName(this.props.fieldName)}
|
|
|
|
>
|
|
|
|
<div className="maputnik-data-spec-property-group">
|
|
|
|
<DocLabel
|
|
|
|
label="Property"
|
|
|
|
doc={"Input a data property to base styles off of."}
|
|
|
|
/>
|
|
|
|
<div className="maputnik-data-spec-property-input">
|
|
|
|
<StringInput
|
|
|
|
value={this.props.value.property}
|
|
|
|
onChange={propVal => this.changeDataProperty("property", propVal)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="maputnik-data-spec-property-group">
|
|
|
|
<DocLabel
|
|
|
|
label="Type"
|
2017-10-10 17:30:06 +02:00
|
|
|
doc={"Select a type of data scale (default is 'categorical')."}
|
2017-10-01 21:49:32 +02:00
|
|
|
/>
|
|
|
|
<div className="maputnik-data-spec-property-input">
|
|
|
|
<SelectInput
|
2017-10-10 17:30:06 +02:00
|
|
|
value={this.props.value.type}
|
2017-10-01 21:49:32 +02:00
|
|
|
onChange={propVal => this.changeDataProperty("type", propVal)}
|
2017-10-10 17:30:06 +02:00
|
|
|
options={this.getDataFunctionTypes(this.props.fieldSpec.function)}
|
2017-10-01 21:49:32 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-10-05 12:50:47 +02:00
|
|
|
<div className="maputnik-data-spec-property-group">
|
|
|
|
<DocLabel
|
|
|
|
label="Default"
|
|
|
|
doc={"Input a default value for data if not covered by the scales."}
|
|
|
|
/>
|
|
|
|
<div className="maputnik-data-spec-property-input">
|
|
|
|
<SpecField
|
|
|
|
fieldName={this.props.fieldName}
|
|
|
|
fieldSpec={this.props.fieldSpec}
|
|
|
|
value={this.props.value.default}
|
2017-10-11 12:58:32 +02:00
|
|
|
onChange={(_, propVal) => this.changeDataProperty("default", propVal)}
|
2017-10-05 12:50:47 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-10-01 21:49:32 +02:00
|
|
|
</InputBlock>
|
|
|
|
</div>
|
|
|
|
{dataFields}
|
|
|
|
<Button
|
|
|
|
className="maputnik-add-stop"
|
|
|
|
onClick={this.addStop.bind(this)}
|
|
|
|
>
|
|
|
|
Add stop
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
2017-01-11 11:35:33 +01:00
|
|
|
renderZoomProperty() {
|
|
|
|
const zoomFields = this.props.value.stops.map((stop, idx) => {
|
|
|
|
const zoomLevel = stop[0]
|
|
|
|
const value = stop[1]
|
2017-01-11 13:34:38 +01:00
|
|
|
const deleteStopBtn= <DeleteStopButton onClick={this.deleteStop.bind(this, idx)} />
|
2017-01-11 11:35:33 +01:00
|
|
|
|
2017-01-11 13:34:38 +01:00
|
|
|
return <InputBlock
|
|
|
|
key={zoomLevel}
|
|
|
|
doc={this.props.fieldSpec.doc}
|
|
|
|
label={labelFromFieldName(this.props.fieldName)}
|
|
|
|
action={deleteStopBtn}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<div className="maputnik-zoom-spec-property-stop-edit">
|
|
|
|
<NumberInput
|
|
|
|
value={zoomLevel}
|
|
|
|
onChange={changedStop => this.changeStop(idx, changedStop, value)}
|
|
|
|
min={0}
|
|
|
|
max={22}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="maputnik-zoom-spec-property-stop-value">
|
|
|
|
<SpecField
|
|
|
|
fieldName={this.props.fieldName}
|
|
|
|
fieldSpec={this.props.fieldSpec}
|
|
|
|
value={value}
|
|
|
|
onChange={(_, newValue) => this.changeStop(idx, zoomLevel, newValue)}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-12-20 11:44:22 +01:00
|
|
|
</div>
|
2017-01-11 13:34:38 +01:00
|
|
|
</InputBlock>
|
2017-01-11 11:35:33 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return <div className="maputnik-zoom-spec-property">
|
|
|
|
{zoomFields}
|
|
|
|
<Button
|
|
|
|
className="maputnik-add-stop"
|
|
|
|
onClick={this.addStop.bind(this)}
|
|
|
|
>
|
|
|
|
Add stop
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
}
|
2017-01-09 16:08:22 +01:00
|
|
|
|
2017-01-11 11:35:33 +01:00
|
|
|
renderProperty() {
|
2017-10-05 13:08:55 +02:00
|
|
|
const functionBtn = <MakeFunctionButtons
|
|
|
|
fieldSpec={this.props.fieldSpec}
|
|
|
|
onZoomClick={this.makeZoomFunction.bind(this)}
|
|
|
|
onDataClick={this.makeDataFunction.bind(this)}
|
|
|
|
/>
|
2017-01-11 13:34:38 +01:00
|
|
|
return <InputBlock
|
|
|
|
doc={this.props.fieldSpec.doc}
|
|
|
|
label={labelFromFieldName(this.props.fieldName)}
|
2017-10-01 21:49:32 +02:00
|
|
|
action={functionBtn}
|
2017-01-11 13:34:38 +01:00
|
|
|
>
|
|
|
|
<SpecField {...this.props} />
|
|
|
|
</InputBlock>
|
2017-01-11 11:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-01-15 17:10:38 +01:00
|
|
|
const propClass = this.props.fieldSpec.default === this.props.value ? "maputnik-default-property" : "maputnik-modified-property"
|
2017-10-01 21:49:32 +02:00
|
|
|
let specField
|
|
|
|
if (isZoomField(this.props.value)) {
|
|
|
|
specField = this.renderZoomProperty()
|
|
|
|
}
|
|
|
|
else if (isDataField(this.props.value)) {
|
|
|
|
specField = this.renderDataProperty()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
specField = this.renderProperty()
|
|
|
|
}
|
2017-01-15 17:10:38 +01:00
|
|
|
return <div className={propClass}>
|
2017-10-01 21:49:32 +02:00
|
|
|
{specField}
|
2017-01-15 17:10:38 +01:00
|
|
|
</div>
|
2016-12-20 11:44:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 21:49:32 +02:00
|
|
|
function MakeFunctionButtons(props) {
|
2017-10-05 13:08:55 +02:00
|
|
|
let makeZoomButton, makeDataButton
|
|
|
|
if (props.fieldSpec['zoom-function']) {
|
|
|
|
makeZoomButton = <Button
|
2017-10-01 21:49:32 +02:00
|
|
|
className="maputnik-make-zoom-function"
|
|
|
|
onClick={props.onZoomClick}
|
|
|
|
>
|
|
|
|
<DocLabel
|
|
|
|
label={<FunctionIcon />}
|
|
|
|
cursorTargetStyle={{ cursor: 'pointer' }}
|
|
|
|
doc={"Turn property into a zoom function to enable a map feature to change with map's zoom level."}
|
|
|
|
/>
|
|
|
|
</Button>
|
2017-10-05 13:08:55 +02:00
|
|
|
|
2017-10-10 17:30:06 +02:00
|
|
|
if (props.fieldSpec['property-function'] && ['piecewise-constant', 'interpolated'].indexOf(props.fieldSpec['function']) !== -1) {
|
2017-10-05 13:08:55 +02:00
|
|
|
makeDataButton = <Button
|
|
|
|
className="maputnik-make-data-function"
|
|
|
|
onClick={props.onDataClick}
|
|
|
|
>
|
|
|
|
<DocLabel
|
|
|
|
label={<MdInsertChart />}
|
|
|
|
cursorTargetStyle={{ cursor: 'pointer' }}
|
|
|
|
doc={"Turn property into a data function to enable a map feature to change according to data properties and the map's zoom level."}
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
}
|
2017-10-10 17:30:06 +02:00
|
|
|
return <div>{makeDataButton}{makeZoomButton}</div>
|
2017-10-05 13:08:55 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null
|
|
|
|
}
|
2017-01-11 13:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function DeleteStopButton(props) {
|
|
|
|
return <Button
|
|
|
|
className="maputnik-delete-stop"
|
|
|
|
onClick={props.onClick}
|
|
|
|
>
|
|
|
|
<DocLabel
|
|
|
|
label={<DeleteIcon />}
|
|
|
|
doc={"Remove zoom level stop."}
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
|
2016-12-20 11:44:22 +01:00
|
|
|
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
|
|
|
}
|