|
| 1 | +import React, { PropTypes } from 'react' |
| 2 | +import { connect } from 'react-redux' |
| 3 | +import { MapLayer, Marker, Popup } from 'react-leaflet' |
| 4 | +import { divIcon } from 'leaflet' |
| 5 | + |
| 6 | +import { hasTransit } from '../../util/itinerary' |
| 7 | +import { findStopsWithinBBox, clearStops } from '../../actions/api' |
| 8 | + |
| 9 | +class StopsOverlay extends MapLayer { |
| 10 | + static propTypes = { |
| 11 | + minZoom: PropTypes.number, |
| 12 | + queryMode: PropTypes.string, |
| 13 | + stops: PropTypes.array, |
| 14 | + refreshStops: PropTypes.func |
| 15 | + } |
| 16 | + |
| 17 | + static defaultProps = { |
| 18 | + minZoom: 15 |
| 19 | + } |
| 20 | + |
| 21 | + componentDidMount () { |
| 22 | + // set up pan/zoom listeners |
| 23 | + this.context.map.on('zoomend', () => { this._refreshStops() }) |
| 24 | + this.context.map.on('dragend', () => { this._refreshStops() }) |
| 25 | + } |
| 26 | + |
| 27 | + _refreshStops () { |
| 28 | + if (this.context.map.getZoom() < this.props.minZoom) { |
| 29 | + this.props.clearStops() |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + const bounds = this.context.map.getBounds() |
| 34 | + const params = { |
| 35 | + minLat: bounds.getSouth(), |
| 36 | + maxLat: bounds.getNorth(), |
| 37 | + minLon: bounds.getWest(), |
| 38 | + maxLon: bounds.getEast() |
| 39 | + } |
| 40 | + this.props.refreshStops(params) |
| 41 | + } |
| 42 | + |
| 43 | + createLeafletElement () { |
| 44 | + } |
| 45 | + |
| 46 | + updateLeafletElement () { |
| 47 | + } |
| 48 | + |
| 49 | + render () { |
| 50 | + const { minZoom, queryMode, stops } = this.props |
| 51 | + |
| 52 | + // don't render if below zoom threshold or transit not currently selected |
| 53 | + if (this.context.map.getZoom() < minZoom || !hasTransit(queryMode)) return null |
| 54 | + |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + {stops.map((stop) => { |
| 58 | + const icon = divIcon({ |
| 59 | + iconSize: [20, 20], |
| 60 | + iconAnchor: [12, 15], |
| 61 | + popupAnchor: [1, -6], |
| 62 | + html: `<span class="fa-stack" style="opacity: 1.0" style={{ background: #00f }}> |
| 63 | + <i class="fa fa-circle fa-stack-1x" style="color: #ffffff"></i> |
| 64 | + <i class="fa fa-circle-o fa-stack-1x" style="color: #000000"></i> |
| 65 | + </span>`, |
| 66 | + className: '' |
| 67 | + }) |
| 68 | + |
| 69 | + const idArr = stop.id.split(':') |
| 70 | + |
| 71 | + return ( |
| 72 | + <Marker |
| 73 | + icon={icon} |
| 74 | + key={stop.id} |
| 75 | + position={[stop.lat, stop.lon]} |
| 76 | + > |
| 77 | + <Popup> |
| 78 | + <div> |
| 79 | + <div style={{ fontWeight: 'bold', fontSize: 18 }}>{stop.name}</div> |
| 80 | + <div><b>Agency:</b> {idArr[0]}</div> |
| 81 | + <div><b>Stop ID:</b> {idArr[1]}</div> |
| 82 | + </div> |
| 83 | + </Popup> |
| 84 | + </Marker> |
| 85 | + ) |
| 86 | + })} |
| 87 | + </div> |
| 88 | + ) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// connect to the redux store |
| 93 | + |
| 94 | +const mapStateToProps = (state, ownProps) => { |
| 95 | + return { |
| 96 | + stops: state.otp.overlay.transit.stops, |
| 97 | + queryMode: state.otp.currentQuery.mode |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const mapDispatchToProps = { |
| 102 | + refreshStops: findStopsWithinBBox, |
| 103 | + clearStops: clearStops |
| 104 | +} |
| 105 | + |
| 106 | +export default connect(mapStateToProps, mapDispatchToProps)(StopsOverlay) |
0 commit comments