-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
238 lines (207 loc) · 5.85 KB
/
index.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
'use strict'
const h = require('virtual-dom/h')
const colors = require('vbb-line-colors')
const products = require('vbb-util/products')
const ms = require('ms')
const flatten = require('lodash/flatten')
const renderTransferPosition = require('./lib/render-transfer-position')
const cls = 'vbb-journey-ui-'
const pedestrians = [
'🚶🏻♀️', '🚶🏼♀️', '🚶🏽♀️', '🚶🏾♀️', '🚶🏿♀️',
'🚶🏻♂️', '🚶🏼♂️', '🚶🏽♂️', '🚶🏾♂️', '🚶🏿♂️'
]
const dirArrow = h('abbr', {title: 'in direction of'}, '→')
const setup = (formatTime, formatDelay, actions = {}) => {
// todo: NOVE_ENV === 'dev'
if ('function' !== typeof formatTime) {
throw new Error('formatTime must be a function.')
}
if ('function' !== typeof formatDelay) {
throw new Error('formatDelay must be a function.')
}
const renderTime = (d) => {
return h('time', {
datetime: d.toISOString()
}, formatTime(d))
}
const pedestrian = pedestrians[Math.floor(Math.random() * pedestrians.length)]
const renderMode = (leg, i, details) => {
if (leg.mode === 'walking') {
const t = new Date(leg.arrival) - new Date(leg.departure)
const s = [pedestrian, 'walk']
if (!Number.isNaN(t)) s.push('for', ms(t, {long: true}))
// todo: distance
return h('li', {
className: cls + 'leg ' + cls + 'walking',
style: {borderLeftColor: '#999'}
}, [
h('div', {
className: cls + 'details'
}, s.join(' '))
])
}
return renderLine(leg, i, details)
}
const renderCycle = (leg) => {
let res = null
if (leg.alternatives) {
let d = Infinity
for (let a of leg.alternatives) {
if (a.line.id !== leg.line.id) continue
const aD = new Date(a.when)
if (aD < d) d = aD
}
res = h('span', {}, [
'also at ', renderTime(d)
])
} else if (leg.cycle && 'number' === typeof leg.cycle.min) {
const c = leg.cycle
let msg = 'every ' + ms(c.min * 1000)
if ('number' === typeof c.max && c.max !== c.min) {
msg += '–' + ms(c.max * 1000)
}
res = h('span', {}, msg)
}
return res
}
const renderLine = (leg, i, details) => {
const line = leg.line
let color = {}
let symbol = null
if (line.product) {
symbol = h('img', {
className: cls + 'product',
alt: line.product,
src: `https://raw.githubusercontent.com/derhuerst/vbb-logos/v2/${line.product}.svg`
})
if (colors[line.product] && colors[line.product][line.name]) {
color = colors[line.product][line.name]
} else if (products[line.product]) {
color = {fg: '#fff', bg: products[line.product].color}
}
}
const _stopovers = []
if (details) {
for (let stopover of leg.stopovers.slice(1, -1)) {
_stopovers.push(h('li', {}, renderStopover(stopover.stop, color.bg)))
}
}
const l = leg.stopovers.length
const label = (l - 1) + ' ' + (l === 2 ? 'stop' : 'stops')
const nrOfStopovers = leg.stopovers ? h('span', {
className: cls + 'link',
'ev-click': details ? () => actions.hideLegDetails(i) : () => actions.showLegDetails(i)
}, label) : null
const duration = new Date(leg.arrival) - new Date(leg.departure)
const cycle = renderCycle(leg)
let transferPosition = null
if (details && 'number' === typeof leg.arrivalPosition) {
transferPosition = h('div', {
className: cls + 'transfer-position'
}, [
renderTransferPosition(leg.arrivalPosition)
])
}
return h('li', {
className: cls + 'leg',
style: {
borderLeftColor: color.bg || '#999'
}
}, [
h('div', {className: cls + 'line-container'}, [
h('span', {
className: cls + 'line',
style: {
backgroundColor: color.bg || '#555',
color: color.fg || '#fff'
}
}, line.name || '?'),
]),
symbol,
leg.direction ? h('span', {className: cls + 'direction'}, [
' ', dirArrow, ' ', leg.direction
]) : null,
h('div', {
className: cls + 'details'
}, flatten([
h('abbr', {
title: ms(duration, {long: true})
}, [
ms(duration) + ' ride'
]),
' · ',
cycle ? [cycle, ' · '] : [],
nrOfStopovers,
transferPosition
])),
_stopovers.length > 0 ? h('ul', {
className: cls + 'details'
}, _stopovers) : null
])
}
const selectStop = (stop) => {
const stationId = stop.station ? stop.station.id : null
return actions.selectStop(stop.id, stationId)
}
const renderStopover = (stop, color) =>
h('div', {
className: cls + 'link ' + cls + 'stopover',
style: {borderBottomColor: color},
'ev-click': () => selectStop(stop)
}, stop.name)
const renderStop = (stop) =>
h('div', {
className: cls + 'link',
'ev-click': () => selectStop(stop)
}, stop.name)
const renderStopoverTime = (stop, departure, delay) => {
const els = [
h('div', {
className: cls + 'name'
}, [renderStop(stop)])
]
if ('number' === typeof delay) {
els.push(h('div', {
className: cls + 'delay'
}, [
formatDelay(delay)
]))
}
departure = new Date(departure)
if (!Number.isNaN(+departure)) {
els.splice(1, 0, h('div', {
className: cls + 'when'
}, [
renderTime(departure)
]))
}
return h('li', {
className: cls + 'stopover'
}, els)
}
const renderJourney = (journey, detailsFor = []) => {
if (!journey) return null
const legs = []
for (let i = 0; i < journey.legs.length; i++) {
const leg = journey.legs[i]
legs.push(
renderStopoverTime(leg.origin, leg.departure, leg.departureDelay),
renderMode(leg, i, detailsFor.includes(i))
)
const nextLeg = journey.legs[i + 1]
const renderDest = !nextLeg || ( // leg.dest !== nextLeg.origin ?
leg.destination &&
nextLeg.origin &&
nextLeg.origin.id !== leg.destination.id
)
if (renderDest) {
legs.push(renderStopoverTime(leg.destination, leg.arrival, leg.arrivalDelay))
}
}
return h('ul', {
className: cls + 'journey'
}, legs)
}
return renderJourney
}
module.exports = setup