@@ -3,7 +3,7 @@ import polyline from '@mapbox/polyline'
3
3
import objectPath from 'object-path'
4
4
import React , { Component , PropTypes } from 'react'
5
5
import { connect } from 'react-redux'
6
- import { Map } from 'react-leaflet'
6
+ import { Map , Popup } from 'react-leaflet'
7
7
8
8
import { setLocation } from '../../actions/map'
9
9
import { constructLocation } from '../../util/map'
@@ -15,11 +15,21 @@ class BaseMap extends Component {
15
15
mapClick : PropTypes . func ,
16
16
setLocation : PropTypes . func // TODO: rename from action name to avoid namespace conflict?
17
17
}
18
- _onClick = ( e ) => {
19
- const location = constructLocation ( e . latlng )
20
- const reverseGeocode = true
21
- if ( ! this . props . isFromSet ) this . props . setLocation ( { type : 'from' , location, reverseGeocode} )
22
- else if ( ! this . props . isToSet ) this . props . setLocation ( { type : 'to' , location, reverseGeocode} )
18
+ state = { }
19
+ _onClickTo = ( ) => {
20
+ const { setLocation} = this . props
21
+ const location = constructLocation ( this . state . popupPosition )
22
+ setLocation ( { type : 'to' , location, reverseGeocode : true } )
23
+ }
24
+
25
+ _onClickFrom = ( ) => {
26
+ const { setLocation} = this . props
27
+ const location = constructLocation ( this . state . popupPosition )
28
+ setLocation ( { type : 'from' , location, reverseGeocode : true } )
29
+ }
30
+
31
+ _onRightClick = ( e ) => {
32
+ this . setState ( { popupPosition : e . latlng } )
23
33
}
24
34
// TODO: make map controlled component
25
35
_mapBoundsChanged = ( e ) => {
@@ -72,18 +82,21 @@ class BaseMap extends Component {
72
82
children
73
83
} = this . props
74
84
const position = [ config . map . initLat , config . map . initLon ]
85
+ const { popupPosition} = this . state
75
86
// const position = [+mapState.lat, +mapState.lon]
76
87
// const zoom = +mapState.zoom
77
88
const zoom = config . map . initZoom || 13
78
89
const bounds = null // mapState.bounds
90
+ // TODO: currently mapProps is unused, but may later be used for controlling
91
+ // map location state
79
92
const mapProps = {
80
93
ref : 'map' ,
81
94
className : 'map' ,
82
95
// center: position,
83
96
// bounds: mapState.bounds || null,
84
97
// zoom: config.initZoom,
85
98
// zoom: +mapState.zoom,
86
- onClick : this . _onClick
99
+ onContextMenu : this . _onRightClick
87
100
// onMoveEnd: this._mapBoundsChanged,
88
101
// onZoomEnd: this._mapBoundsChanged,
89
102
}
@@ -101,9 +114,30 @@ class BaseMap extends Component {
101
114
className = 'map'
102
115
center = { position }
103
116
zoom = { config . map . initZoom || 13 }
104
- onClick = { this . _onClick }
117
+ onContextMenu = { this . _onRightClick }
105
118
>
106
119
{ children }
120
+ { popupPosition
121
+ ? < Popup ref = 'clickPopup'
122
+ key = { popupPosition . toString ( ) } // hack to ensure the popup opens only on right click
123
+ position = { popupPosition } // FIXME: onOpen and onClose don't seem to work?
124
+ // onOpen={() => this.setState({popupPosition: null})}
125
+ // onClose={() => this.setState({popupPosition: null})}
126
+ >
127
+ < span >
128
+ Plan a trip:{ ' ' }
129
+ < a role = 'button'
130
+ onClick = { this . _onClickFrom } >
131
+ From here
132
+ </ a > { ' ' } |{ ' ' }
133
+ < a role = 'button'
134
+ onClick = { this . _onClickTo } >
135
+ To here
136
+ </ a >
137
+ </ span >
138
+ </ Popup >
139
+ : null
140
+ }
107
141
</ Map >
108
142
)
109
143
}
0 commit comments