Skip to content

Commit

Permalink
fix(pattern): update distance and travel times when pattern is reversed
Browse files Browse the repository at this point in the history
fixes #176
  • Loading branch information
landonreed committed May 22, 2018
1 parent 4fe3b4e commit 5005250
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lib/editor/components/pattern/TripPatternListControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,28 @@ export default class TripPatternListControls extends Component {
title: `Reverse trip pattern?`,
body: `Are you sure you want to reverse this trip pattern?`,
onConfirm: () => {
// Order of pattern stops is irrelevant. Stop sequence must be updated.
const patternStops = [...activePattern.patternStops]
// Reverse sort on existing stopSequence values. NOTE: Updating
// stop sequence for new order is handled in updatePatternStops.
.sort((a, b) => b.stopSequence - a.stopSequence)
.map((ps, i) => ({
...ps,
stopSequence: i
}))
// Travel time will be offset by one stop (travel time for first stop
// should be zero).
let defaultTravelTime = 0
const {patternStops} = activePattern
// Store total distance from last pattern stop
const lastStop = patternStops[patternStops.length - 1]
const totalDistance = lastStop.shapeDistTraveled
// Clone pattern stops and reverse them, updating shape dist traveled
// and travel time for the new order.
const clonedPatternStops = [...patternStops]
.reverse()
.map((ps, i) => {
const patternStop = {
...ps,
shapeDistTraveled: totalDistance - ps.shapeDistTraveled,
defaultTravelTime,
stopSequence: i
}
// Update default travel time for next iteration.
defaultTravelTime = ps.defaultTravelTime
return patternStop
})
updatePatternGeometry({
// Reverse control points
controlPoints: clone(controlPoints).reverse(),
Expand All @@ -92,7 +105,7 @@ export default class TripPatternListControls extends Component {
.map(seg => seg.reverse())
.reverse()
})
updatePatternStops(activePattern, patternStops)
updatePatternStops(activePattern, clonedPatternStops)
saveActiveEntity('trippattern')
}
})
Expand Down

0 comments on commit 5005250

Please sign in to comment.