|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import Field from './Field'; |
| 3 | +import Dropdown from '../widgets/Dropdown'; |
| 4 | +import NumericInput from '../widgets/NumericInput'; |
| 5 | +import PropTypes from 'prop-types'; |
| 6 | +import {connectToContainer} from 'lib'; |
| 7 | +import {isDateTime} from 'plotly.js/src/lib'; |
| 8 | +import {isJSDate} from 'plotly.js/src/lib/dates'; |
| 9 | + |
| 10 | +const MILLISECONDS_IN_SECOND = 1000; |
| 11 | +const MILLISECONDS_IN_MINUTE = MILLISECONDS_IN_SECOND * 60; // eslint-disable-line |
| 12 | +const MILLISECONDS_IN_DAY = MILLISECONDS_IN_MINUTE * 60 * 24; // eslint-disable-line |
| 13 | +const DAYS_IN_MONTH = 30; |
| 14 | +const MONTHS_IN_YEAR = 12; //eslint-disable-line |
| 15 | + |
| 16 | +function getSmallestUnit(milliseconds) { |
| 17 | + const units = { |
| 18 | + seconds: MILLISECONDS_IN_SECOND, |
| 19 | + minutes: MILLISECONDS_IN_MINUTE, |
| 20 | + days: MILLISECONDS_IN_DAY, |
| 21 | + }; |
| 22 | + |
| 23 | + let smallestUnit = 'milliseconds'; |
| 24 | + |
| 25 | + ['seconds', 'minutes', 'days'].forEach(unit => { |
| 26 | + if ( |
| 27 | + milliseconds % units[unit] === 0 && |
| 28 | + (smallestUnit === 'milliseconds' || |
| 29 | + (smallestUnit !== 'milliseconds' && |
| 30 | + milliseconds / units[smallestUnit] > milliseconds / units[unit])) |
| 31 | + ) { |
| 32 | + smallestUnit = unit; |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + return smallestUnit; |
| 37 | +} |
| 38 | + |
| 39 | +class UnconnectedAxisInterval extends Component { |
| 40 | + constructor(props) { |
| 41 | + super(props); |
| 42 | + |
| 43 | + const initialUnit = |
| 44 | + props.fullValue && typeof props.fullValue === 'string' && props.fullValue[0] === 'M' |
| 45 | + ? parseInt(props.fullValue.substring(1), 10) % MONTHS_IN_YEAR === 0 |
| 46 | + ? 'years' |
| 47 | + : 'months' |
| 48 | + : getSmallestUnit(props.fullValue); |
| 49 | + |
| 50 | + this.state = { |
| 51 | + units: initialUnit, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + update(value) { |
| 56 | + let adjustedValue = value < 0 ? 0 : value; |
| 57 | + |
| 58 | + if (this.state.units === 'years') { |
| 59 | + adjustedValue = 'M' + adjustedValue * MONTHS_IN_YEAR; |
| 60 | + } |
| 61 | + |
| 62 | + if (this.state.units === 'months') { |
| 63 | + adjustedValue = 'M' + adjustedValue; |
| 64 | + } |
| 65 | + |
| 66 | + if (this.state.units === 'days') { |
| 67 | + adjustedValue = adjustedValue * MILLISECONDS_IN_DAY; |
| 68 | + } |
| 69 | + |
| 70 | + if (this.state.units === 'minutes') { |
| 71 | + adjustedValue = adjustedValue * MILLISECONDS_IN_MINUTE; |
| 72 | + } |
| 73 | + |
| 74 | + if (this.state.units === 'seconds') { |
| 75 | + adjustedValue = adjustedValue * MILLISECONDS_IN_SECOND; |
| 76 | + } |
| 77 | + |
| 78 | + this.props.updatePlot(adjustedValue); |
| 79 | + } |
| 80 | + |
| 81 | + onUnitChange(value) { |
| 82 | + const isFullValueMonthFormat = |
| 83 | + typeof this.props.fullValue === 'string' && this.props.fullValue[0] === 'M'; |
| 84 | + |
| 85 | + const milliseconds = isFullValueMonthFormat |
| 86 | + ? parseInt(this.props.fullValue.substring(1), 10) * DAYS_IN_MONTH * MILLISECONDS_IN_DAY |
| 87 | + : this.props.fullValue; |
| 88 | + |
| 89 | + this.setState({units: value}); |
| 90 | + |
| 91 | + if (['years', 'months'].includes(value)) { |
| 92 | + this.props.updatePlot('M' + Math.round(milliseconds / MILLISECONDS_IN_DAY / DAYS_IN_MONTH)); |
| 93 | + } else { |
| 94 | + this.props.updatePlot(milliseconds); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + getDisplayValue(value) { |
| 99 | + const numericValue = |
| 100 | + typeof value === 'string' && value[0] === 'M' ? parseInt(value.substring(1), 10) : value; |
| 101 | + |
| 102 | + if (this.state.units === 'years') { |
| 103 | + return numericValue / MONTHS_IN_YEAR; |
| 104 | + } |
| 105 | + if (this.state.units === 'months') { |
| 106 | + return numericValue; |
| 107 | + } |
| 108 | + if (this.state.units === 'days') { |
| 109 | + return Math.round(numericValue / MILLISECONDS_IN_DAY); |
| 110 | + } |
| 111 | + if (this.state.units === 'minutes') { |
| 112 | + return Math.round(numericValue / MILLISECONDS_IN_MINUTE); |
| 113 | + } |
| 114 | + if (this.state.units === 'seconds') { |
| 115 | + return Math.round(numericValue / MILLISECONDS_IN_SECOND); |
| 116 | + } |
| 117 | + if (this.state.units === 'milliseconds') { |
| 118 | + return numericValue; |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + render() { |
| 124 | + const _ = this.context.localize; |
| 125 | + const attrHead = this.props.attr.split('.')[0]; |
| 126 | + const binStartValue = this.props.fullContainer[attrHead].start; |
| 127 | + const BinStartIsDate = |
| 128 | + typeof binStartValue === 'string' && (isDateTime(binStartValue) || isJSDate(binStartValue)); |
| 129 | + |
| 130 | + return BinStartIsDate ? ( |
| 131 | + <Field {...this.props}> |
| 132 | + <Dropdown |
| 133 | + options={[ |
| 134 | + {value: 'years', label: _('Years')}, |
| 135 | + {value: 'months', label: _('Months')}, |
| 136 | + {value: 'days', label: _('Days')}, |
| 137 | + {value: 'minutes', label: _('Minutes')}, |
| 138 | + {value: 'seconds', label: _('Seconds')}, |
| 139 | + {value: 'milliseconds', label: _('Milliseconds')}, |
| 140 | + ]} |
| 141 | + clearable={false} |
| 142 | + onChange={value => this.onUnitChange(value)} |
| 143 | + value={this.state.units} |
| 144 | + /> |
| 145 | + <div style={{height: '7px', width: '100%', display: 'block'}}> </div> |
| 146 | + <NumericInput |
| 147 | + value={this.getDisplayValue(this.props.fullValue)} |
| 148 | + onUpdate={value => this.update(value)} |
| 149 | + editableClassName="AxisInterval-milliseconds" |
| 150 | + /> |
| 151 | + </Field> |
| 152 | + ) : ( |
| 153 | + <Field {...this.props}> |
| 154 | + <NumericInput |
| 155 | + value={this.props.fullValue} |
| 156 | + onUpdate={value => this.props.updatePlot(value)} |
| 157 | + /> |
| 158 | + </Field> |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +UnconnectedAxisInterval.contextTypes = { |
| 164 | + localize: PropTypes.func, |
| 165 | +}; |
| 166 | + |
| 167 | +UnconnectedAxisInterval.propTypes = { |
| 168 | + fullValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), |
| 169 | + updatePlot: PropTypes.func, |
| 170 | + attr: PropTypes.string, |
| 171 | + fullContainer: PropTypes.object, |
| 172 | + ...Field.propTypes, |
| 173 | +}; |
| 174 | + |
| 175 | +export default connectToContainer(UnconnectedAxisInterval); |
0 commit comments