@opentripplanner/vehicle-rental-overlay 1.4.2 → 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,284 +0,0 @@
1
- // Removed as core-utils is typescripted. TODO: Remove when typescripting!
2
- /* eslint-disable react/forbid-prop-types */
3
- import BaseMap from "@opentripplanner/base-map";
4
- import PropTypes from "prop-types";
5
- import React from "react";
6
- import { CircleMarker } from "react-leaflet";
7
- import { action } from "@storybook/addon-actions";
8
- import { boolean } from "@storybook/addon-knobs";
9
-
10
- import VehicleRentalOverlay from ".";
11
- import bikeRentalStations from "../__mocks__/bike-rental-stations.json";
12
- import carRentalStations from "../__mocks__/car-rental-stations.json";
13
- import eScooterStations from "../__mocks__/e-scooter-rental-stations.json";
14
- import { HubAndFloatingBike } from "./DefaultMarkers";
15
-
16
- import "../../../node_modules/leaflet/dist/leaflet.css";
17
-
18
- const center = [45.518092, -122.671202];
19
-
20
- /**
21
- * Creates an example Circle component to render entities
22
- * using a fixed size, fill color, and stroke color.
23
- */
24
- const MyCircle = ({ fillColor = "gray", pixels, strokeColor }) => {
25
- const newStrokeColor = strokeColor || fillColor;
26
-
27
- const GeneratedCircle = ({ children, entity: station }) => (
28
- <CircleMarker
29
- center={[station.y, station.x]}
30
- color={newStrokeColor}
31
- fillColor={fillColor}
32
- fillOpacity={1}
33
- radius={pixels}
34
- weight={1}
35
- >
36
- {children}
37
- </CircleMarker>
38
- );
39
- GeneratedCircle.propTypes = {
40
- children: PropTypes.node,
41
- // entity: coreUtils.types.stationType.isRequired
42
- entity: PropTypes.object.isRequired
43
- };
44
- GeneratedCircle.defaultProps = {
45
- children: null
46
- };
47
- return GeneratedCircle;
48
- };
49
-
50
- const bikeMapSymbols = [
51
- {
52
- dockStrokeColor: "#000000",
53
- fillColor: "#FF2E28",
54
- minZoom: 0,
55
- pixels: 4,
56
- type: "circle"
57
- },
58
- {
59
- dockStrokeColor: "#000000",
60
- fillColor: "#FF2E28",
61
- minZoom: 14,
62
- pixels: 6,
63
- type: "circle"
64
- },
65
- {
66
- minZoom: 18,
67
- type: "hubAndFloatingBike"
68
- }
69
- ];
70
- // Bike symbols using new symbols prop.
71
- const bikeSymbols = [
72
- {
73
- getType: station => (station.isFloatingBike ? "floatingBike" : "dock"),
74
- minZoom: 0,
75
- symbol: MyCircle({ fillColor: "#FF2E28", pixels: 3 }),
76
- symbolByType: {
77
- dock: MyCircle({
78
- fillColor: "#FF2E28",
79
- pixels: 4,
80
- strokeColor: "#000000"
81
- })
82
- }
83
- },
84
- {
85
- getType: station => (station.isFloatingBike ? "floatingBike" : "dock"),
86
- minZoom: 14,
87
- symbol: MyCircle({ fillColor: "#FF2E28", pixels: 5 }),
88
- symbolByType: {
89
- dock: MyCircle({
90
- fillColor: "#FF2E28",
91
- pixels: 6,
92
- strokeColor: "#000000"
93
- })
94
- }
95
- },
96
- {
97
- minZoom: 18,
98
- symbol: HubAndFloatingBike
99
- }
100
- ];
101
- const carMapSymbols = [
102
- {
103
- fillColor: "#009cde",
104
- minZoom: 0,
105
- pixels: 4,
106
- type: "circle"
107
- },
108
- {
109
- fillColor: "#009cde",
110
- minZoom: 14,
111
- pixels: 6,
112
- type: "circle"
113
- },
114
- {
115
- fillColor: "#009cde",
116
- minZoom: 18,
117
- type: "marker"
118
- }
119
- ];
120
- const configCompanies = [
121
- {
122
- id: "BIKETOWN",
123
- label: "Biketown",
124
- modes: "BICYCLE_RENT"
125
- },
126
- {
127
- id: "CAR2GO",
128
- label: "car2go",
129
- modes: "CAR_RENT"
130
- },
131
- {
132
- id: "RAZOR",
133
- label: "Razor",
134
- modes: "MICROMOBILITY_RENT"
135
- },
136
- {
137
- id: "SHARED",
138
- label: "Shared",
139
- modes: "MICROMOBILITY_RENT"
140
- }
141
- ];
142
- const EScooterMapSymbols = [
143
- {
144
- fillColor: "#F80600",
145
- minZoom: 0,
146
- pixels: 4,
147
- strokeColor: "#CCCCCC",
148
- type: "circle"
149
- },
150
- // You can combine predefined symbols (type = "<type>")
151
- // and external symbols (symbol = Component<({ entity, zoom })>.
152
- // (the color and pixel properties are ignored if you use the symbol syntax.).
153
- {
154
- minZoom: 14,
155
- symbol: MyCircle({
156
- fillColor: "#F80600",
157
- pixels: 6,
158
- strokeColor: "#CCCCCC"
159
- })
160
- },
161
- {
162
- fillColor: "#F80600",
163
- minZoom: 18,
164
- type: "marker"
165
- }
166
- ];
167
- const setLocation = action("setLocation");
168
-
169
- const INITIAL_ZOOM = 13;
170
-
171
- const ZoomControlledMapWithVehicleRentalOverlay = ({
172
- companies,
173
- getStationName,
174
- mapSymbols,
175
- refreshVehicles,
176
- stations,
177
- visible
178
- }) => (
179
- // Caution, <BaseMap> must be a direct parent of <VehicleRentalOverlay>.
180
- // Therefore, do not place <BaseMap> in a decorator at this time.
181
- <BaseMap center={center} zoom={INITIAL_ZOOM}>
182
- <VehicleRentalOverlay
183
- configCompanies={configCompanies}
184
- companies={companies}
185
- getStationName={getStationName}
186
- setLocation={setLocation}
187
- mapSymbols={mapSymbols}
188
- name="Rentals"
189
- refreshVehicles={refreshVehicles}
190
- stations={stations}
191
- visible={visible}
192
- />
193
- </BaseMap>
194
- );
195
-
196
- ZoomControlledMapWithVehicleRentalOverlay.propTypes = {
197
- companies: PropTypes.arrayOf(PropTypes.string.isRequired),
198
- getStationName: PropTypes.func,
199
- // mapSymbols: coreUtils.types.vehicleRentalMapOverlaySymbolsType,
200
- mapSymbols: PropTypes.object,
201
- refreshVehicles: PropTypes.func.isRequired,
202
- // stations: PropTypes.arrayOf(coreUtils.types.stationType.isRequired)
203
- // .isRequired,
204
- stations: PropTypes.arrayOf(PropTypes.object.isRequired).isRequired,
205
- visible: PropTypes.bool
206
- };
207
-
208
- ZoomControlledMapWithVehicleRentalOverlay.defaultProps = {
209
- companies: null,
210
- getStationName: undefined,
211
- mapSymbols: null,
212
- visible: true
213
- };
214
-
215
- function customStationName(_, station) {
216
- return `🛴 (ID: ${station.id})`;
217
- }
218
-
219
- export default {
220
- title: "VehicleRentalOverlay",
221
- component: VehicleRentalOverlay
222
- };
223
-
224
- export const RentalBicycles = () => (
225
- <ZoomControlledMapWithVehicleRentalOverlay
226
- companies={["BIKETOWN"]}
227
- mapSymbols={bikeMapSymbols}
228
- refreshVehicles={action("refresh bicycles")}
229
- stations={bikeRentalStations}
230
- />
231
- );
232
-
233
- export const RentalBicyclesVisibilityControlledByKnob = () => {
234
- const isOverlayVisible = boolean(
235
- "Toggle visibility of vehicle rental overlay",
236
- false
237
- );
238
- return (
239
- <ZoomControlledMapWithVehicleRentalOverlay
240
- companies={["BIKETOWN"]}
241
- mapSymbols={bikeMapSymbols}
242
- refreshVehicles={action("refresh bicycles")}
243
- stations={bikeRentalStations}
244
- visible={isOverlayVisible}
245
- />
246
- );
247
- };
248
-
249
- export const RentalBicyclesUsingNewSymbolsProp = () => (
250
- <ZoomControlledMapWithVehicleRentalOverlay
251
- companies={["BIKETOWN"]}
252
- refreshVehicles={action("refresh bicycles")}
253
- mapSymbols={bikeSymbols}
254
- stations={bikeRentalStations}
255
- />
256
- );
257
-
258
- export const RentalCars = () => (
259
- <ZoomControlledMapWithVehicleRentalOverlay
260
- companies={["CAR2GO"]}
261
- mapSymbols={carMapSymbols}
262
- refreshVehicles={action("refresh cars")}
263
- stations={carRentalStations}
264
- />
265
- );
266
-
267
- export const RentalEScooters = () => (
268
- <ZoomControlledMapWithVehicleRentalOverlay
269
- companies={["SHARED"]}
270
- mapSymbols={EScooterMapSymbols}
271
- refreshVehicles={action("refresh E-scooters")}
272
- stations={eScooterStations}
273
- />
274
- );
275
-
276
- export const RentalEScootersWithCustomNaming = () => (
277
- <ZoomControlledMapWithVehicleRentalOverlay
278
- companies={["SHARED"]}
279
- getStationName={customStationName}
280
- mapSymbols={EScooterMapSymbols}
281
- refreshVehicles={action("refresh E-scooters")}
282
- stations={eScooterStations}
283
- />
284
- );
package/src/bike-icons.js DELETED
@@ -1,23 +0,0 @@
1
- import { divIcon } from "leaflet";
2
- import React from "react";
3
- import ReactDOMServer from "react-dom/server";
4
-
5
- import * as S from "./styled";
6
-
7
- export const floatingBikeIcon = divIcon({
8
- iconSize: [24, 24],
9
- iconAnchor: [12, 24],
10
- popupAnchor: [0, -12],
11
- html: ReactDOMServer.renderToStaticMarkup(<S.OutOfHubBikeIcon />),
12
- className: ""
13
- });
14
-
15
- export const hubIcons = S.hubIcons.map(StyledIcon =>
16
- divIcon({
17
- iconSize: [24, 24],
18
- iconAnchor: [12, 24],
19
- popupAnchor: [0, -12],
20
- html: ReactDOMServer.renderToStaticMarkup(<StyledIcon />),
21
- className: ""
22
- })
23
- );
package/src/index.js DELETED
@@ -1,371 +0,0 @@
1
- import flatten from "flat";
2
- import { Styled as BaseMapStyled } from "@opentripplanner/base-map";
3
- import coreUtils from "@opentripplanner/core-utils";
4
- import FromToLocationPicker from "@opentripplanner/from-to-location-picker";
5
- import ZoomBasedMarkers from "@opentripplanner/zoom-based-markers";
6
- import PropTypes from "prop-types";
7
- import React from "react";
8
- import { FormattedMessage, injectIntl } from "react-intl";
9
- import { FeatureGroup, MapLayer, Popup, withLeaflet } from "react-leaflet";
10
-
11
- import {
12
- GenericMarker,
13
- HubAndFloatingBike,
14
- SharedBikeCircle
15
- } from "./DefaultMarkers";
16
-
17
- // Load the default messages.
18
- import defaultEnglishMessages from "../i18n/en-US.yml";
19
-
20
- // HACK: We should flatten the messages loaded above because
21
- // the YAML loaders behave differently between webpack and our version of jest:
22
- // - the yaml loader for webpack returns a nested object,
23
- // - the yaml loader for jest returns messages with flattened ids.
24
- const defaultMessages = flatten(defaultEnglishMessages);
25
-
26
- function makeDefaultGetStationName(intl) {
27
- return function defaultGetStationName(configCompanies, station) {
28
- const stationNetworks = coreUtils.itinerary.getCompaniesLabelFromNetworks(
29
- station.networks,
30
- configCompanies
31
- );
32
- let stationName = station.name || station.id;
33
- if (station.isFloatingBike) {
34
- stationName = intl.formatMessage(
35
- {
36
- defaultMessage:
37
- defaultEnglishMessages["otpUi.VehicleRentalOverlay.floatingBike"],
38
- description: "Popup title for a free-floating bike",
39
- id: "otpUi.VehicleRentalOverlay.floatingBike"
40
- },
41
- { name: stationName }
42
- );
43
- } else if (station.isFloatingCar) {
44
- stationName = intl.formatMessage(
45
- {
46
- defaultMessage:
47
- defaultEnglishMessages["otpUi.VehicleRentalOverlay.floatingCar"],
48
- description: "Popup title for a free-floating car",
49
- id: "otpUi.VehicleRentalOverlay.floatingCar"
50
- },
51
- {
52
- company: stationNetworks,
53
- name: stationName
54
- }
55
- );
56
- } else if (station.isFloatingVehicle) {
57
- // assumes that all floating vehicles are E-scooters
58
- stationName = intl.formatMessage(
59
- {
60
- defaultMessage:
61
- defaultEnglishMessages[
62
- "otpUi.VehicleRentalOverlay.floatingEScooter"
63
- ],
64
- description: "Popup title for a free-floating e-scooter",
65
- id: "otpUi.VehicleRentalOverlay.floatingEScooter"
66
- },
67
- { company: stationNetworks }
68
- );
69
- }
70
- return stationName;
71
- };
72
- }
73
-
74
- /**
75
- * This vehicle rental overlay can be used to render vehicle rentals of various
76
- * types. This layer can be configured to show different styles of markers at
77
- * different zoom levels.
78
- */
79
- class VehicleRentalOverlay extends MapLayer {
80
- constructor(props) {
81
- super(props);
82
- this.state = {
83
- zoom: null
84
- };
85
- }
86
-
87
- /**
88
- * This helper method will be passed to the ZoomBasedMarkers symbolTransform prop.
89
- * It creates a component that inserts a popup
90
- * as a child of the specified symbol from the mapSymbols prop.
91
- */
92
- renderSymbolWithPopup = Symbol => {
93
- const SymbolWrapper = ({ entity: station, zoom }) => (
94
- <Symbol entity={station} zoom={zoom}>
95
- {this.renderPopupForStation(
96
- station,
97
- station.bikesAvailable !== undefined && !station.isFloatingBike
98
- )}
99
- </Symbol>
100
- );
101
- SymbolWrapper.propTypes = {
102
- // entity: coreUtils.types.stationType.isRequired,
103
- zoom: PropTypes.number.isRequired
104
- };
105
-
106
- return SymbolWrapper;
107
- };
108
-
109
- /**
110
- * Convert map symbols to zoomBasedSymbolType.
111
- */
112
- convertToZoomMarkerSymbols = mapSymbols =>
113
- mapSymbols.map(mapSymbol => {
114
- // If mapSymbol uses zoomBasedSymbolType, use it as is.
115
- if (mapSymbol.symbol) {
116
- return mapSymbol;
117
- }
118
-
119
- // Otherwise, convert into zoomBasedType (no support for symbols by type).
120
- let symbol;
121
- switch (mapSymbol.type) {
122
- case "circle":
123
- symbol = SharedBikeCircle(mapSymbol);
124
- break;
125
- case "hubAndFloatingBike":
126
- symbol = HubAndFloatingBike;
127
- break;
128
- default:
129
- symbol = GenericMarker(mapSymbol);
130
- }
131
-
132
- return {
133
- minZoom: mapSymbol.minZoom,
134
- symbol
135
- };
136
- });
137
-
138
- createLeafletElement() {}
139
-
140
- updateLeafletElement() {}
141
-
142
- startRefreshing() {
143
- const { refreshVehicles } = this.props;
144
-
145
- // Create the timer only if refreshVehicles is a valid function.
146
- if (typeof refreshVehicles === "function") {
147
- // initial station retrieval
148
- refreshVehicles();
149
-
150
- // set up timer to refresh stations periodically
151
- this.refreshTimer = setInterval(() => {
152
- refreshVehicles();
153
- }, 30000); // defaults to every 30 sec. TODO: make this configurable?
154
- }
155
- }
156
-
157
- stopRefreshing() {
158
- if (this.refreshTimer) clearInterval(this.refreshTimer);
159
- }
160
-
161
- /**
162
- * When the layer is added (or toggled on, or its visibility becomes true),
163
- * start refreshing vehicle positions.
164
- */
165
- onOverlayAdded = () => {
166
- this.startRefreshing();
167
- };
168
-
169
- /**
170
- * When the layer is removed (or toggled off, or its visibility becomes false),
171
- * stop refreshing vehicle positions.
172
- */
173
- onOverlayRemoved = () => {
174
- this.stopRefreshing();
175
- };
176
-
177
- /**
178
- * Listen to changes on the BaseMap's center or zoom.
179
- * @param viewport The viewport data. See https://github.com/PaulLeCam/react-leaflet/blob/master/example/components/viewport.js for details.
180
- */
181
- onViewportChanged = viewport => {
182
- const { zoom: newZoom } = viewport;
183
- if (this.state.zoom !== newZoom) {
184
- this.setState({ zoom: newZoom });
185
- }
186
- };
187
-
188
- /**
189
- * Upon mounting, see whether the vehicles should be fetched,
190
- * and also call the register overlay prop that the
191
- * @opentripplanner/base-map package has injected to listen to zoom/position changes.
192
- */
193
- componentDidMount() {
194
- const { leaflet, registerOverlay, visible } = this.props;
195
- this.setState({
196
- zoom: leaflet.map.getZoom()
197
- });
198
- if (visible) this.startRefreshing();
199
- if (typeof registerOverlay === "function") {
200
- registerOverlay(this);
201
- }
202
- }
203
-
204
- componentWillUnmount() {
205
- this.stopRefreshing();
206
- }
207
-
208
- /**
209
- * Render some popup html for a station. This contains custom logic for
210
- * displaying rental vehicles in the TriMet MOD website that might not be
211
- * applicable to other regions.
212
- */
213
- renderPopupForStation = (station, stationIsHub = false) => {
214
- const { configCompanies, getStationName, intl, setLocation } = this.props;
215
- const getStationNameFunc =
216
- getStationName || makeDefaultGetStationName(intl);
217
- const stationName = getStationNameFunc(configCompanies, station);
218
- const location = {
219
- lat: station.y,
220
- lon: station.x,
221
- name: stationName
222
- };
223
- return (
224
- <Popup>
225
- <BaseMapStyled.MapOverlayPopup>
226
- <BaseMapStyled.PopupTitle>{stationName}</BaseMapStyled.PopupTitle>
227
-
228
- {/* render dock info if it is available */}
229
- {stationIsHub && (
230
- <BaseMapStyled.PopupRow>
231
- <div>
232
- <FormattedMessage
233
- defaultMessage={
234
- defaultMessages["otpUi.VehicleRentalOverlay.availableBikes"]
235
- }
236
- description="Label text for the number of bikes available"
237
- id="otpUi.VehicleRentalOverlay.availableBikes"
238
- values={{ value: station.bikesAvailable }}
239
- />
240
- </div>
241
- <div>
242
- <FormattedMessage
243
- defaultMessage={
244
- defaultMessages["otpUi.VehicleRentalOverlay.availableDocks"]
245
- }
246
- description="Label text for the number of docks available"
247
- id="otpUi.VehicleRentalOverlay.availableDocks"
248
- values={{ value: station.spacesAvailable }}
249
- />
250
- </div>
251
- </BaseMapStyled.PopupRow>
252
- )}
253
-
254
- {/* Set as from/to toolbar */}
255
- <BaseMapStyled.PopupRow>
256
- <FromToLocationPicker
257
- label
258
- location={location}
259
- setLocation={setLocation}
260
- />
261
- </BaseMapStyled.PopupRow>
262
- </BaseMapStyled.MapOverlayPopup>
263
- </Popup>
264
- );
265
- };
266
-
267
- render() {
268
- const { companies, leaflet, mapSymbols, stations } = this.props;
269
- const { zoom = leaflet.map.getZoom() } = this.state;
270
- // Render an empty FeatureGroup if the rental vehicles should not be visible
271
- // on the map. Otherwise previous stations may still be shown due to some
272
- // react-leaflet internals, maybe? Also, do not return null because that will
273
- // prevent the overlay from appearing in the layer controls.
274
-
275
- let filteredStations = stations;
276
- if (companies) {
277
- filteredStations = stations.filter(
278
- station =>
279
- station.networks.filter(value => companies.includes(value)).length > 0
280
- );
281
- }
282
-
283
- if (!filteredStations || filteredStations.length === 0) {
284
- return <FeatureGroup />;
285
- }
286
-
287
- // Convert map symbols for this overlay to zoomBasedSymbolType.
288
- const symbols = this.convertToZoomMarkerSymbols(mapSymbols);
289
-
290
- return (
291
- <FeatureGroup>
292
- <ZoomBasedMarkers
293
- entities={filteredStations}
294
- symbols={symbols}
295
- symbolTransform={this.renderSymbolWithPopup}
296
- zoom={zoom}
297
- />
298
- </FeatureGroup>
299
- );
300
- }
301
- }
302
-
303
- VehicleRentalOverlay.props = {
304
- /**
305
- * The entire companies config array.
306
- */
307
- // configCompanies: PropTypes.arrayOf(coreUtils.types.companyType.isRequired)
308
- // .isRequired,
309
- /**
310
- * A list of companies that are applicable to just this instance of the
311
- * overlay.
312
- */
313
- companies: PropTypes.arrayOf(PropTypes.string.isRequired),
314
- /**
315
- * An optional custom function to create a string name of a particular vehicle
316
- * rental station. This function takes two arguments of the configCompanies
317
- * prop and a vehicle rental station. The function must return a string.
318
- */
319
- getStationName: PropTypes.func,
320
- /**
321
- * A configuration of what map markers or symbols to show at various
322
- * zoom levels.
323
- */
324
- // mapSymbols: coreUtils.types.vehicleRentalMapOverlaySymbolsType,
325
- /**
326
- * If specified, a function that will be triggered every 30 seconds whenever this layer is
327
- * visible.
328
- */
329
- refreshVehicles: PropTypes.func,
330
- /**
331
- * A callback for when a user clicks on setting this stop as either the from
332
- * or to location of a new search.
333
- *
334
- * This will be dispatched with the following argument:
335
- *
336
- * ```js
337
- * {
338
- * location: {
339
- * lat: number,
340
- * lon: number,
341
- * name: string
342
- * },
343
- * locationType: "from" or "to"
344
- * }
345
- * ```
346
- */
347
- setLocation: PropTypes.func.isRequired,
348
- /**
349
- * A list of the vehicle rental stations specific to this overlay instance.
350
- */
351
- // stations: PropTypes.arrayOf(coreUtils.types.stationType),
352
- /**
353
- * Whether the overlay is currently visible.
354
- */
355
- visible: PropTypes.bool
356
- };
357
-
358
- VehicleRentalOverlay.defaultProps = {
359
- getStationName: null,
360
- mapSymbols: [
361
- {
362
- zoom: 0,
363
- symbol: GenericMarker
364
- }
365
- ],
366
- refreshVehicles: null,
367
- stations: [],
368
- visible: false
369
- };
370
-
371
- export default withLeaflet(injectIntl(VehicleRentalOverlay));