-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathdefault-map.js
242 lines (222 loc) · 8.23 KB
/
default-map.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import BaseMap from '@opentripplanner/base-map'
import coreUtils from '@opentripplanner/core-utils'
import React, { Component } from 'react'
import { connect } from 'react-redux'
import styled from 'styled-components'
import {
bikeRentalQuery,
carRentalQuery,
vehicleRentalQuery
} from '../../actions/api'
import { updateOverlayVisibility } from '../../actions/config'
import {
setLocation,
setMapPopupLocation,
setMapPopupLocationAndGeocode
} from '../../actions/map'
import BoundsUpdatingOverlay from './bounds-updating-overlay'
import EndpointsOverlay from './connected-endpoints-overlay'
import ParkAndRideOverlay from './connected-park-and-ride-overlay'
import RouteViewerOverlay from './connected-route-viewer-overlay'
import StopViewerOverlay from './connected-stop-viewer-overlay'
import StopsOverlay from './connected-stops-overlay'
import TransitiveOverlay from './connected-transitive-overlay'
import TripViewerOverlay from './connected-trip-viewer-overlay'
import VehicleRentalOverlay from './connected-vehicle-rental-overlay'
import ElevationPointMarker from './elevation-point-marker'
import PointPopup from './point-popup'
import TileOverlay from './tile-overlay'
import ZipcarOverlay from './zipcar-overlay'
const MapContainer = styled.div`
height: 100%;
width: 100%;
.map {
height: 100%;
width: 100%;
}
* {
box-sizing: unset;
}
`
class DefaultMap extends Component {
/**
* Checks whether the modes have changed between old and new queries and
* whether to update the map overlays accordingly (e.g., to show rental vehicle
* options on the map).
*/
_handleQueryChange = (oldQuery, newQuery) => {
const { overlays } = this.props
if (overlays && oldQuery.mode) {
// Determine any added/removed modes
const oldModes = oldQuery.mode.split(',')
const newModes = newQuery.mode.split(',')
const removed = oldModes.filter(m => !newModes.includes(m))
const added = newModes.filter(m => !oldModes.includes(m))
const overlayVisibility = {}
for (const oConfig of overlays) {
if (!oConfig.modes || oConfig.modes.length !== 1) continue
// TODO: support multi-mode overlays
const overlayMode = oConfig.modes[0]
if (
(
overlayMode === 'CAR_RENT' ||
overlayMode === 'CAR_HAIL' ||
overlayMode === 'MICROMOBILITY_RENT'
) &&
oConfig.companies
) {
// Special handling for company-based mode overlays (e.g. carshare, car-hail)
const overlayCompany = oConfig.companies[0] // TODO: handle multi-company overlays
if (added.includes(overlayMode)) {
// Company-based mode was just selected; enable overlay iff overlay's company is active
if (newQuery.companies.includes(overlayCompany)) overlayVisibility[oConfig.name] = true
} else if (removed.includes(overlayMode)) {
// Company-based mode was just deselected; disable overlay (regardless of company)
overlayVisibility[oConfig.name] = false
} else if (newModes.includes(overlayMode) && oldQuery.companies !== newQuery.companies) {
// Company-based mode remains selected but companies change
overlayVisibility[oConfig.name] = newQuery.companies.includes(overlayCompany)
}
} else { // Default handling for other modes
if (added.includes(overlayMode)) overlayVisibility[oConfig.name] = true
if (removed.includes(overlayMode)) overlayVisibility[oConfig.name] = false
}
}
// Only trigger update action if there are overlays to update.
if (Object.keys(overlayVisibility).length > 0) {
this.props.updateOverlayVisibility(overlayVisibility)
}
}
}
onMapClick = (e) => {
this.props.setMapPopupLocationAndGeocode(e)
}
onPopupClosed = () => {
this.props.setMapPopupLocation({ location: null })
}
onSetLocationFromPopup = (payload) => {
const { setLocation, setMapPopupLocation } = this.props
setMapPopupLocation({ location: null })
setLocation(payload)
}
componentDidUpdate (prevProps) {
// Check if any overlays should be toggled due to mode change
this._handleQueryChange(prevProps.query, this.props.query)
}
render () {
const {
bikeRentalQuery,
bikeRentalStations,
carRentalQuery,
carRentalStations,
itineraryView,
mapConfig,
mapPopupLocation,
vehicleRentalQuery,
vehicleRentalStations
} = this.props
const center = mapConfig && mapConfig.initLat && mapConfig.initLon
? [mapConfig.initLat, mapConfig.initLon]
: null
const popup = mapPopupLocation && {
contents: (
<PointPopup
mapPopupLocation={mapPopupLocation}
onSetLocationFromPopup={this.onSetLocationFromPopup}
/>
),
location: [mapPopupLocation.lat, mapPopupLocation.lon]
}
return (
<MapContainer>
<BaseMap
baseLayers={mapConfig.baseLayers}
center={center}
maxZoom={mapConfig.maxZoom}
onClick={this.onMapClick}
onPopupClosed={this.onPopupClosed}
popup={popup}
zoom={mapConfig.initZoom || 13}
>
{/* The default overlays */}
<BoundsUpdatingOverlay />
<EndpointsOverlay />
<RouteViewerOverlay />
<StopViewerOverlay />
{/*
HACK: Use the key prop to force a remount and full resizing of transitive
if the map container size changes,
per https://linguinecode.com/post/4-methods-to-re-render-react-component
Without it, transitive resolution will not match the map,
and transitive will appear blurry after e.g. the narrative is expanded.
*/}
<TransitiveOverlay key={itineraryView ? `transitive-${itineraryView}` : 'transitive-default'} />
<TripViewerOverlay />
<ElevationPointMarker />
{/* The configurable overlays */}
{mapConfig.overlays && mapConfig.overlays.map((overlayConfig, k) => {
switch (overlayConfig.type) {
case 'bike-rental': return (
<VehicleRentalOverlay
key={k}
{...overlayConfig}
refreshVehicles={bikeRentalQuery}
stations={bikeRentalStations}
/>
)
case 'car-rental': return (
<VehicleRentalOverlay
key={k}
{...overlayConfig}
refreshVehicles={carRentalQuery}
stations={carRentalStations}
/>
)
case 'park-and-ride':
return <ParkAndRideOverlay key={k} {...overlayConfig} />
case 'stops': return <StopsOverlay key={k} {...overlayConfig} />
case 'tile': return <TileOverlay key={k} {...overlayConfig} />
case 'micromobility-rental': return (
<VehicleRentalOverlay
key={k}
{...overlayConfig}
refreshVehicles={vehicleRentalQuery}
stations={vehicleRentalStations}
/>
)
case 'zipcar': return <ZipcarOverlay key={k} {...overlayConfig} />
default: return null
}
})}
</BaseMap>
</MapContainer>
)
}
}
// connect to the redux store
const mapStateToProps = (state, ownProps) => {
const overlays = state.otp.config.map && state.otp.config.map.overlays
? state.otp.config.map.overlays
: []
const urlParams = coreUtils.query.getUrlParams()
return {
bikeRentalStations: state.otp.overlay.bikeRental.stations,
carRentalStations: state.otp.overlay.carRental.stations,
itineraryView: urlParams.ui_itineraryView,
mapConfig: state.otp.config.map,
mapPopupLocation: state.otp.ui.mapPopupLocation,
overlays,
query: state.otp.currentQuery,
vehicleRentalStations: state.otp.overlay.vehicleRental.stations
}
}
const mapDispatchToProps = {
bikeRentalQuery,
carRentalQuery,
setLocation,
setMapPopupLocation,
setMapPopupLocationAndGeocode,
updateOverlayVisibility,
vehicleRentalQuery
}
export default connect(mapStateToProps, mapDispatchToProps)(DefaultMap)