Skip to content

Commit e04aac7

Browse files
authored
Merge pull request #177 from opentripplanner/store-router-id-master
Persist router id (master branch)
2 parents e2baeea + 3d1bbb5 commit e04aac7

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

Diff for: lib/actions/ui.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ export function matchContentToUrl (location) {
7070
// Parse comma separated params (ensuring numbers are parsed correctly).
7171
let [lat, lon, zoom, routerId] = id ? idToParams(id) : []
7272
if (!lat || !lon) {
73-
// Attempt to parse path. (Legacy UI otp.js used slashes in the
74-
// pathname to specify lat, lon, etc.)
73+
// Attempt to parse path if lat/lon not found. (Legacy UI otp.js used
74+
// slashes in the pathname to specify lat, lon, etc.)
7575
[,, lat, lon, zoom, routerId] = idToParams(location.pathname, '/')
7676
}
77+
console.log(lat, lon, zoom, routerId)
7778
// Update map location/zoom and optionally override router ID.
78-
dispatch(setMapCenter({ lat, lon }))
79-
dispatch(setMapZoom({ zoom }))
79+
if (+lat && +lon) dispatch(setMapCenter({ lat, lon }))
80+
if (+zoom) dispatch(setMapZoom({ zoom }))
81+
// If router ID is provided, override the default routerId.
8082
if (routerId) dispatch(setRouterId(routerId))
8183
dispatch(setMainPanelContent(null))
8284
break
@@ -88,6 +90,10 @@ export function matchContentToUrl (location) {
8890
}
8991
}
9092

93+
/**
94+
* Split the path id into its parts (according to specified delimiter). Parse
95+
* numbers if detected.
96+
*/
9197
function idToParams (id, delimiter = ',') {
9298
return id.split(delimiter).map(s => isNaN(s) ? s : +s)
9399
}

Diff for: lib/components/app/responsive-webapp.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,20 @@ class ResponsiveWebapp extends Component {
105105
{ enableHighAccuracy: true }
106106
)
107107
}
108-
108+
// Handle routing to a specific part of the app (e.g. stop viewer) on page
109+
// load. (This happens prior to routing request in case special routerId is
110+
// set from URL.)
111+
this.props.matchContentToUrl(this.props.location)
109112
if (location && location.search) {
110113
// Set search params and plan trip if routing enabled and a query exists
111114
// in the URL.
112115
this.props.parseUrlQueryString()
113116
}
114-
// Handle routing to a specific part of the app (e.g. stop viewer) on page
115-
// load.
116-
this.props.matchContentToUrl(this.props.location)
117117
}
118118

119119
componentWillUnmount () {
120120
// Remove on back button press listener.
121-
window.removeEventListener('popstate')
121+
window.removeEventListener('popstate', this.props.handleBackButtonPress)
122122
}
123123

124124
render () {

Diff for: lib/components/narrative/trip-tools.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,24 @@ class CopyUrlButton extends Component {
7777
this.state = { showCopied: false }
7878
}
7979

80+
_resetState = () => this.setState({ showCopied: false })
81+
8082
_onClick = () => {
81-
copyToClipboard(window.location.href)
83+
// If special routerId has been set in session storage, construct copy URL
84+
// for itinerary with #/start/ prefix to set routerId on page load.
85+
const routerId = window.sessionStorage.getItem('routerId')
86+
let url = window.location.href
87+
if (routerId) {
88+
const parts = url.split('#')
89+
if (parts.length === 2) {
90+
url = `${parts[0]}#/start/x/x/x/${routerId}${parts[1]}`
91+
} else {
92+
console.warn('URL not formatted as expected, copied URL will not contain session routerId.', routerId)
93+
}
94+
}
95+
copyToClipboard(url)
8296
this.setState({ showCopied: true })
83-
setTimeout(() => { this.setState({ showCopied: false }) }, 2000)
97+
window.setTimeout(this._resetState, 2000)
8498
}
8599

86100
render () {

Diff for: lib/reducers/create-otp-reducer.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ export function getInitialState (userDefinedConfig, initialQuery) {
119119
if (config.locations) {
120120
locations.push(...config.locations.map(l => ({ ...l, type: 'suggested' })))
121121
}
122+
// Check for alternative routerId in session storage. This is generally used
123+
// for testing single GTFS feed OTP graphs that are deployed to feed-specific
124+
// routers (e.g., https://otp.server.com/otp/routers/non_default_router).
125+
// This routerId session value is initially set by visiting otp-rr
126+
// with the path /start/:latLonZoomRouter, which dispatches the SET_ROUTER_ID
127+
// action and stores the value in sessionStorage.
128+
// Note: this mechanism assumes that the OTP API path is otp/routers/default.
129+
const routerId = window.sessionStorage.getItem('routerId')
130+
// If routerId is found, update the config.api.path (keep the original config
131+
// value at originalPath in case it needs to be reverted.)
132+
if (routerId) {
133+
config.api.originalPath = userDefinedConfig.api.path
134+
config.api.path = `/otp/routers/${routerId}`
135+
}
122136
let queryModes = currentQuery.mode.split(',')
123137

124138
// If 'TRANSIT' is included in the mode list, replace it with individual modes
@@ -540,11 +554,23 @@ function createOtpReducer (config, initialQuery) {
540554
}
541555
})
542556
case 'SET_ROUTER_ID':
543-
const routerId = action.payload || 'default'
557+
const routerId = action.payload
558+
// Store original path value in originalPath variable.
559+
const originalPath = config.api.originalPath || config.api.path || '/otp/routers/default'
560+
const path = routerId
561+
? `/otp/routers/${routerId}`
562+
// If routerId is null, revert to the original config's API path (or
563+
// the standard path if that is not found).
564+
: originalPath
565+
// Store routerId in session storage (persists through page reloads but
566+
// not when a new tab/window is opened).
567+
if (routerId) window.sessionStorage.setItem('routerId', routerId)
568+
else window.sessionStorage.removeItem('routerId')
544569
return update(state, {
545570
config: {
546571
api: {
547-
path: { $set: `/otp/routers/${routerId}` }
572+
path: { $set: path },
573+
originalPath: { $set: originalPath }
548574
}
549575
}
550576
})

0 commit comments

Comments
 (0)