all files / src/explore/components/controls/ SpatialControl.jsx

38.16% Statements 29/76
0% Branches 0/44
0% Functions 0/13
37.33% Lines 28/75
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                                                                                                                                                                                                                                                                                                                                                                                
import React from 'react';
import PropTypes from 'prop-types';
import {
  Row, Col, Button, Label, OverlayTrigger, Popover,
} from 'react-bootstrap';
import 'react-datetime/css/react-datetime.css';
 
import ControlHeader from '../ControlHeader';
import SelectControl from './SelectControl';
import PopoverSection from '../../../components/PopoverSection';
import Checkbox from '../../../components/Checkbox';
import { t } from '../../../locales';
 
const spatialTypes = {
  latlong: 'latlong',
  delimited: 'delimited',
  geohash: 'geohash',
};
 
const propTypes = {
  onChange: PropTypes.func,
  value: PropTypes.object,
  animation: PropTypes.bool,
  choices: PropTypes.array,
};
 
const defaultProps = {
  onChange: () => {},
  animation: true,
  choices: [],
};
 
export default class SpatialControl extends React.Component {
  constructor(props) {
    super(props);
    const v = props.value || {};
    let defaultCol;
    if (props.choices.length > 0) {
      defaultCol = props.choices[0][0];
    }
    this.state = {
      type: v.type || spatialTypes.latlong,
      delimiter: v.delimiter || ',',
      latCol: v.latCol || defaultCol,
      lonCol: v.lonCol || defaultCol,
      lonlatCol: v.lonlatCol || defaultCol,
      reverseCheckbox: v.reverseCheckbox || false,
      geohashCol: v.geohashCol || defaultCol,
      value: null,
      errors: [],
    };
    this.toggleCheckbox = this.toggleCheckbox.bind(this);
    this.onChange = this.onChange.bind(this);
  }
  componentDidMount() {
    this.onChange();
  }
  onChange() {
    const type = this.state.type;
    const value = { type };
    const errors = [];
    const errMsg = t('Invalid lat/long configuration.');
    if (type === spatialTypes.latlong) {
      value.latCol = this.state.latCol;
      value.lonCol = this.state.lonCol;
      if (!value.lonCol || !value.latCol) {
        errors.push(errMsg);
      }
    } else if (type === spatialTypes.delimited) {
      value.lonlatCol = this.state.lonlatCol;
      value.delimiter = this.state.delimiter;
      value.reverseCheckbox = this.state.reverseCheckbox;
      if (!value.lonlatCol || !value.delimiter) {
        errors.push(errMsg);
      }
    } else if (type === spatialTypes.geohash) {
      value.geohashCol = this.state.geohashCol;
      if (!value.geohashCol) {
        errors.push(errMsg);
      }
    }
    this.setState({ value, errors });
    this.props.onChange(value, errors);
  }
  setType(type) {
    this.setState({ type }, this.onChange);
  }
  close() {
    this.refs.trigger.hide();
  }
  toggleCheckbox() {
    this.setState({ reverseCheckbox: !this.state.reverseCheckbox }, this.onChange);
  }
  renderLabelContent() {
    if (this.state.errors.length > 0) {
      return 'N/A';
    }
    if (this.state.type === spatialTypes.latlong) {
      return `${this.state.lonCol} | ${this.state.latCol}`;
    } else if (this.state.type === spatialTypes.delimited) {
      return `${this.state.lonlatCol}`;
    } else if (this.state.type === spatialTypes.geohash) {
      return `${this.state.geohashCol}`;
    }
    return null;
  }
  renderSelect(name, type) {
    return (
      <SelectControl
        name={name}
        choices={this.props.choices}
        value={this.state[name]}
        clearable={false}
        onFocus={() => {
          this.setType(type);
        }}
        onChange={(value) => {
          this.setState({ [name]: value }, this.onChange);
        }}
      />
    );
  }
  renderPopover() {
    return (
      <Popover id="filter-popover">
        <div style={{ width: '300px' }}>
          <PopoverSection
            title={t('Longitude & Latitude columns')}
            isSelected={this.state.type === spatialTypes.latlong}
            onSelect={this.setType.bind(this, spatialTypes.latlong)}
          >
            <Row>
              <Col md={6}>
                Longitude
                {this.renderSelect('lonCol', spatialTypes.latlong)}
              </Col>
              <Col md={6}>
                Latitude
                {this.renderSelect('latCol', spatialTypes.latlong)}
              </Col>
            </Row>
          </PopoverSection>
          <PopoverSection
            title={t('Delimited long & lat single column')}
            info={t(
              'Multiple formats accepted, look the geopy.points ' +
              'Python library for more details')}
            isSelected={this.state.type === spatialTypes.delimited}
            onSelect={this.setType.bind(this, spatialTypes.delimited)}
          >
            <Row>
              <Col md={6}>
                Column
                {this.renderSelect('lonlatCol', spatialTypes.delimited)}
              </Col>
              <Col md={6}>
                {t('Reverse lat/long ')}
                <Checkbox checked={this.state.reverseCheckbox} onChange={this.toggleCheckbox} />
              </Col>
            </Row>
          </PopoverSection>
          <PopoverSection
            title={t('Geohash')}
            isSelected={this.state.type === spatialTypes.geohash}
            onSelect={this.setType.bind(this, spatialTypes.geohash)}
          >
            <Row>
              <Col md={6}>
                Column
                {this.renderSelect('geohashCol', spatialTypes.geohash)}
              </Col>
            </Row>
          </PopoverSection>
          <div className="clearfix">
            <Button
              bsSize="small"
              className="float-left ok"
              bsStyle="primary"
              onClick={this.close.bind(this)}
            >
              Ok
            </Button>
          </div>
        </div>
      </Popover>
    );
  }
  render() {
    return (
      <div>
        <ControlHeader {...this.props} />
        <OverlayTrigger
          animation={this.props.animation}
          container={document.body}
          trigger="click"
          rootClose
          ref="trigger"
          placement="right"
          overlay={this.renderPopover()}
        >
          <Label style={{ cursor: 'pointer' }}>
            {this.renderLabelContent()}
          </Label>
        </OverlayTrigger>
      </div>
    );
  }
}
 
SpatialControl.propTypes = propTypes;
SpatialControl.defaultProps = defaultProps;