|
| 1 | +import Field from './Field'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import React, {Component} from 'react'; |
| 4 | +import {connectToContainer} from 'lib'; |
| 5 | +import RadioBlocks from '../widgets/RadioBlocks'; |
| 6 | +import Color from './Color'; |
| 7 | +import DataSelector from './DataSelector'; |
| 8 | +import Colorscale from './Colorscale'; |
| 9 | +import {MULTI_VALUED, COLORS} from 'lib/constants'; |
| 10 | + |
| 11 | +class UnconnectedMarkerColor extends Component { |
| 12 | + constructor(props, context) { |
| 13 | + super(props, context); |
| 14 | + |
| 15 | + let type = null; |
| 16 | + if ( |
| 17 | + !props.container.marker || |
| 18 | + (props.container.marker && !props.container.marker.colorsrc) |
| 19 | + ) { |
| 20 | + type = 'constant'; |
| 21 | + } else if ( |
| 22 | + props.container.marker && |
| 23 | + Array.isArray(props.container.marker.color) && |
| 24 | + props.fullContainer.marker && |
| 25 | + Array.isArray(props.fullContainer.marker.color) |
| 26 | + ) { |
| 27 | + type = 'variable'; |
| 28 | + } |
| 29 | + |
| 30 | + this.state = { |
| 31 | + type, |
| 32 | + value: { |
| 33 | + constant: type === 'constant' ? props.fullValue : COLORS.mutedBlue, |
| 34 | + variable: type === 'variable' ? props.fullValue : null, |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + this.setType = this.setType.bind(this); |
| 39 | + this.setValue = this.setValue.bind(this); |
| 40 | + this.setColorScale = this.setColorScale.bind(this); |
| 41 | + } |
| 42 | + |
| 43 | + setType(type) { |
| 44 | + this.setState({type: type}); |
| 45 | + this.props.updatePlot(this.state.value[type]); |
| 46 | + if (type === 'constant') { |
| 47 | + this.context.updateContainer({ |
| 48 | + ['marker.colorsrc']: null, |
| 49 | + ['marker.colorscale']: null, |
| 50 | + }); |
| 51 | + this.setState({colorscale: null}); |
| 52 | + } else { |
| 53 | + this.context.updateContainer({ |
| 54 | + ['marker.color']: null, |
| 55 | + ['marker.colorsrc']: null, |
| 56 | + ['marker.colorscale']: [], |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + setValue(inputValue) { |
| 62 | + const {type} = this.state; |
| 63 | + |
| 64 | + this.setState( |
| 65 | + type === 'constant' |
| 66 | + ? {value: {constant: inputValue}} |
| 67 | + : {value: {variable: inputValue}} |
| 68 | + ); |
| 69 | + this.props.updatePlot(inputValue); |
| 70 | + } |
| 71 | + |
| 72 | + setColorScale(inputValue) { |
| 73 | + this.setState({colorscale: inputValue}); |
| 74 | + this.context.updateContainer({['marker.colorscale']: inputValue}); |
| 75 | + } |
| 76 | + |
| 77 | + render() { |
| 78 | + const {attr, fullValue, container} = this.props; |
| 79 | + const {localize: _} = this.context; |
| 80 | + const {type, value, colorscale} = this.state; |
| 81 | + const options = [ |
| 82 | + {label: _('Constant'), value: 'constant'}, |
| 83 | + {label: _('Variable'), value: 'variable'}, |
| 84 | + ]; |
| 85 | + const multiValued = |
| 86 | + this.props.multiValued || |
| 87 | + (Array.isArray(fullValue) && fullValue.includes(MULTI_VALUED)) || |
| 88 | + (container.marker && container.marker.colorscale === MULTI_VALUED) || |
| 89 | + (container.marker && container.marker.colorsrc === MULTI_VALUED) || |
| 90 | + (container.marker && |
| 91 | + container.marker.color && |
| 92 | + Array.isArray(container.marker.color) && |
| 93 | + container.marker.color.includes(MULTI_VALUED)); |
| 94 | + |
| 95 | + return ( |
| 96 | + <div> |
| 97 | + <Field {...this.props} multiValued={multiValued} attr={attr}> |
| 98 | + <RadioBlocks |
| 99 | + options={options} |
| 100 | + activeOption={type} |
| 101 | + onOptionChange={this.setType} |
| 102 | + /> |
| 103 | + {!type ? null : type === 'constant' ? ( |
| 104 | + <Color |
| 105 | + suppressMultiValuedMessage |
| 106 | + attr="marker.color" |
| 107 | + updatePlot={this.setValue} |
| 108 | + fullValue={value.constant} |
| 109 | + /> |
| 110 | + ) : container.marker && |
| 111 | + container.marker.colorsrc === MULTI_VALUED ? null : ( |
| 112 | + <div> |
| 113 | + <DataSelector suppressMultiValuedMessage attr="marker.color" /> |
| 114 | + {container.marker && |
| 115 | + container.marker.colorscale === MULTI_VALUED ? null : ( |
| 116 | + <Colorscale |
| 117 | + suppressMultiValuedMessage |
| 118 | + attr="marker.colorscale" |
| 119 | + updatePlot={this.setColorScale} |
| 120 | + colorscale={colorscale} |
| 121 | + /> |
| 122 | + )} |
| 123 | + </div> |
| 124 | + )} |
| 125 | + </Field> |
| 126 | + </div> |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +UnconnectedMarkerColor.propTypes = { |
| 132 | + fullValue: PropTypes.any, |
| 133 | + updatePlot: PropTypes.func, |
| 134 | + ...Field.propTypes, |
| 135 | +}; |
| 136 | + |
| 137 | +UnconnectedMarkerColor.contextTypes = { |
| 138 | + localize: PropTypes.func, |
| 139 | + updateContainer: PropTypes.func, |
| 140 | +}; |
| 141 | + |
| 142 | +export default connectToContainer(UnconnectedMarkerColor); |
0 commit comments