|
| 1 | +import {Draggable} from 'leaflet' |
| 2 | +import throttle from 'lodash.throttle' |
1 | 3 | import React, { Component, PropTypes } from 'react' |
2 | 4 | import { Marker } from 'react-leaflet' |
3 | | - |
4 | | -import { handlePatternEdit, handleControlPointDragEnd } from '../../util/map' |
| 5 | +import { shallowEqual } from 'react-pure-render' |
5 | 6 |
|
6 | 7 | export default class ControlPoint extends Component { |
7 | 8 | static propTypes = { |
8 | | - position: PropTypes.array |
| 9 | + activePattern: PropTypes.object, |
| 10 | + icon: PropTypes.object, |
| 11 | + index: PropTypes.number, |
| 12 | + handleControlPointDrag: PropTypes.func, |
| 13 | + handleControlPointDragEnd: PropTypes.func, |
| 14 | + next: PropTypes.object, |
| 15 | + permanent: PropTypes.bool, |
| 16 | + position: PropTypes.array, |
| 17 | + previous: PropTypes.object, |
| 18 | + removeControlPoint: PropTypes.func, |
| 19 | + updateActiveEntity: PropTypes.func, |
| 20 | + updateControlPoint: PropTypes.func, |
| 21 | + updatePatternCoordinates: PropTypes.func |
9 | 22 | } |
10 | 23 | constructor (props) { |
11 | 24 | super(props) |
12 | 25 | this.state = { |
13 | | - timer: null, |
14 | 26 | latlng: null |
15 | 27 | } |
16 | 28 | } |
| 29 | + shouldComponentUpdate (nextProps) { |
| 30 | + // TODO: fix this hack that keeps unknown position change (perhaps react-leaflet is updating it) from triggering |
| 31 | + // a component update, which funks with the position of the marker |
| 32 | + return !shallowEqual(nextProps.controlPoint.snap, this.props.controlPoint.snap) |
| 33 | + } |
| 34 | + _onClick = (e) => { |
| 35 | + console.log('control point clicked', e) |
| 36 | + // only remove controlPoint if it's not based on pattern stop (i.e., has permanent prop) |
| 37 | + if (!this.props.permanent) { |
| 38 | + this.props.removeControlPoint(this.props.activePattern, this.props.index, this.props.previous, this.props.next) |
| 39 | + } |
| 40 | + } |
| 41 | + _onDrag = () => { |
| 42 | + const throttled = throttle(this.handleDrag, 750) |
| 43 | + throttled() |
| 44 | + } |
| 45 | + handleDrag = () => { |
| 46 | + const latlng = this.refs.marker.leafletElement.getLatLng() |
| 47 | + this.setState({latlng}) |
| 48 | + this.props.handleControlPointDrag(latlng, this.props.previous, this.props.next, this.props.activePattern) |
| 49 | + } |
| 50 | + _onDragEnd = (e) => { |
| 51 | + this.setState({latlng: null}) |
| 52 | + this.props.handleControlPointDragEnd(this.props.index, this.props.controlPoint, e, this.props.activePattern) |
| 53 | + } |
17 | 54 | render () { |
18 | | - const { position, icon, previous, next, activePattern, index, permanent, removeControlPoint, updateActiveEntity, updateControlPoint, editSettings } = this.props |
19 | | - const { patternCoordinates, followStreets } = editSettings |
| 55 | + if (this.props.index === 1) console.log(this.props) |
| 56 | + const { position, icon } = this.props |
| 57 | + const {latlng} = this.state |
| 58 | + const markerPosition = latlng ? [latlng.lat, latlng.lng] : position |
20 | 59 | return ( |
21 | 60 | <Marker |
22 | | - position={this.state.latlng || position} |
| 61 | + position={markerPosition} |
23 | 62 | icon={icon} |
24 | 63 | ref='marker' |
25 | 64 | zIndexOffset={1000} |
26 | 65 | draggable |
27 | | - onDragStart={(e) => { |
28 | | - const timerFunction = () => { |
29 | | - const latlng = this.refs.marker.leafletElement.getLatLng() |
30 | | - // console.log(latlng) |
31 | | - handlePatternEdit(latlng, this.props.previous, this.props.next, this.props.activePattern, followStreets, patternCoordinates) |
32 | | - .then(coords => { |
33 | | - this.setState({latlng}) |
34 | | - this.props.updatePatternCoordinates(coords) |
35 | | - }) |
36 | | - } |
37 | | - timerFunction() |
38 | | - const timer = setInterval(timerFunction, 500) |
39 | | - this.setState({timer}) |
40 | | - }} |
41 | | - onDragEnd={(e) => { |
42 | | - // console.log('drag end') |
43 | | - // clear timer |
44 | | - if (this.state.timer) clearInterval(this.state.timer) |
45 | | - const { snap, distTraveled } = handleControlPointDragEnd(e, patternCoordinates) |
46 | | - updateActiveEntity(activePattern, 'trippattern', {shape: {type: 'LineString', coordinates: patternCoordinates}}) |
47 | | - updateControlPoint(index, snap, distTraveled) |
48 | | - this.setState({latlng: null}) |
49 | | - }} |
50 | | - onClick={(e) => { |
51 | | - console.log('control point clicked', e) |
52 | | - // only remove controlPoint if it's not based on pattern stop (i.e., has permanent prop) |
53 | | - if (!permanent) { |
54 | | - removeControlPoint(activePattern, index, previous, next) |
55 | | - } |
56 | | - }} |
| 66 | + onDrag={throttle(this.handleDrag, 500)} |
| 67 | + onDragEnd={this._onDragEnd} |
| 68 | + onClick={this._onClick} |
57 | 69 | color='black' |
58 | 70 | /> |
59 | 71 | ) |
|
0 commit comments