-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathedit-project-bounds.js
118 lines (105 loc) · 2.46 KB
/
edit-project-bounds.js
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
import {latLngBounds} from 'leaflet'
import React, {Component, PropTypes} from 'react'
import {FeatureGroup, Marker, Rectangle} from 'react-leaflet'
import {connect} from 'react-redux'
import {DEFAULT_BOUNDS, save} from './actions/project'
function mapStateToProps ({
project
}, {
projectId
}) {
const currentProject = project.projectsById[projectId] || {}
return {
bounds: currentProject.bounds || DEFAULT_BOUNDS,
isLoaded: !!currentProject,
project: currentProject
}
}
function mapDispatchToProps (dispatch) {
return {
save: (project) => dispatch(save(project))
}
}
class EditProjectBounds extends Component {
static propTypes = {
bounds: PropTypes.shape({
north: PropTypes.number,
east: PropTypes.number,
south: PropTypes.number,
west: PropTypes.number
}),
isLoaded: PropTypes.bool.isRequired,
project: PropTypes.object,
save: PropTypes.func
}
static contextTypes = {
map: PropTypes.object
}
fitBounds () {
const {isLoaded} = this.props
if (isLoaded) {
const {map} = this.context
map.fitBounds([this.sw(), this.ne()])
}
}
componentDidMount () {
this.fitBounds()
}
componentDidUpdate () {
this.fitBounds()
}
ne () {
const {bounds} = this.props
const {north, east} = bounds
return [north, east]
}
sw () {
const {bounds} = this.props
const {south, west} = bounds
return [south, west]
}
saveBounds = (bounds) => {
const {project, save} = this.props
save({
...project,
bounds: {
north: bounds.getNorth(),
east: bounds.getEast(),
south: bounds.getSouth(),
west: bounds.getWest()
}
})
}
render () {
const {isLoaded} = this.props
const sw = this.sw()
const ne = this.ne()
if (isLoaded) {
return (
<FeatureGroup>
<Marker
draggable
onDragEnd={(e) => {
this.saveBounds(latLngBounds(e.target.getLatLng(), ne))
}}
position={sw}
/>
<Marker
draggable
onDragEnd={(e) => {
this.saveBounds(latLngBounds(e.target.getLatLng(), sw))
}}
position={ne}
/>
<Rectangle
bounds={[sw, ne]}
weight={2}
/>
</FeatureGroup>
)
} else {
return <g />
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(EditProjectBounds)