|
|
|
/**
|
|
|
|
* @type {string | null}
|
|
|
|
*/
|
|
|
|
var fromStation = null
|
|
|
|
/**
|
|
|
|
* @type {string | null}
|
|
|
|
*/
|
|
|
|
var toStation = null
|
|
|
|
/**
|
|
|
|
* @type {Date | null}
|
|
|
|
*/
|
|
|
|
var departureDate = null
|
|
|
|
var transitKind = {
|
|
|
|
ice: true,
|
|
|
|
ic: true,
|
|
|
|
re: true,
|
|
|
|
rb: true,
|
|
|
|
s: true,
|
|
|
|
bus: true,
|
|
|
|
ferry: true,
|
|
|
|
u: true,
|
|
|
|
tram: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {{id: string, name: string}[]}
|
|
|
|
*/
|
|
|
|
var knownStations = []
|
|
|
|
|
|
|
|
var itineraries = null
|
|
|
|
|
|
|
|
function goToStation(stationId) {
|
|
|
|
var url = new URL(window.location.href)
|
|
|
|
if (!fromStation) {
|
|
|
|
url.searchParams.set('from', stationId)
|
|
|
|
}
|
|
|
|
else if (!toStation) {
|
|
|
|
url.searchParams.set('to', stationId)
|
|
|
|
}
|
|
|
|
// url.searchParams.set('date', new Date().toISOString())
|
|
|
|
window.location.href = url.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDepartureDate(departureDate) {
|
|
|
|
var url = new URL(window.location.href)
|
|
|
|
url.searchParams.set('departureDate', departureDate.toISOString())
|
|
|
|
window.location.href = url.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchNormalize(str) {
|
|
|
|
return str
|
|
|
|
.toLowerCase()
|
|
|
|
.replaceAll('ă', 'a')
|
|
|
|
.replaceAll('â', 'a')
|
|
|
|
.replaceAll('î', 'i')
|
|
|
|
.replaceAll('ș', 's')
|
|
|
|
.replaceAll('ț', 't')
|
|
|
|
}
|
|
|
|
|
|
|
|
var focusedElement = null
|
|
|
|
|
|
|
|
var _rebuildDebounce = null
|
|
|
|
var _rebuildRequested = false
|
|
|
|
function rebuildSuggestions() {
|
|
|
|
if (_rebuildDebounce !== null) {
|
|
|
|
_rebuildRequested = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_rebuildRequested = false
|
|
|
|
_rebuildDebounce = 123
|
|
|
|
|
|
|
|
var suggestionsArea = document.getElementById('suggestionsArea')
|
|
|
|
while (suggestionsArea.childNodes.length > 0) {
|
|
|
|
suggestionsArea.childNodes[0].remove()
|
|
|
|
}
|
|
|
|
|
|
|
|
var suggestions = knownStations.slice()
|
|
|
|
|
|
|
|
suggestions.forEach(function (suggestion, index) {
|
|
|
|
var suggestionLi = document.createElement('li')
|
|
|
|
suggestionsArea.appendChild(suggestionLi)
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
suggestionLi.classList.add('items')
|
|
|
|
suggestionLi.tabIndex = index + 1
|
|
|
|
suggestionLi.style.padding = '2px 0'
|
|
|
|
|
|
|
|
function onAction(e) {
|
|
|
|
goToStation(JSON.stringify(suggestion))
|
|
|
|
}
|
|
|
|
suggestionLi.addEventListener('click', onAction)
|
|
|
|
suggestionLi.addEventListener('keypress', function (e) {
|
|
|
|
if (e.key == 'Enter') {
|
|
|
|
onAction(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
suggestionLi.addEventListener('focus', function (e) {
|
|
|
|
focusedElement = suggestionLi
|
|
|
|
})
|
|
|
|
|
|
|
|
var stationNameP = document.createElement('p')
|
|
|
|
suggestionLi.appendChild(stationNameP)
|
|
|
|
|
|
|
|
stationNameP.textContent = suggestion.name || suggestion.address
|
|
|
|
stationNameP.classList.add('pri', 'stationName')
|
|
|
|
|
|
|
|
// var trainCompanyP = document.createElement('p')
|
|
|
|
// suggestionLi.appendChild(trainCompanyP)
|
|
|
|
|
|
|
|
// trainCompanyP.textContent = suggestion.company
|
|
|
|
// trainCompanyP.classList.add('thi')
|
|
|
|
}, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
_rebuildDebounce = null
|
|
|
|
if (_rebuildRequested) {
|
|
|
|
rebuildSuggestions()
|
|
|
|
}
|
|
|
|
}, 500)
|
|
|
|
}
|
|
|
|
|
|
|
|
var fetchAbortController = new AbortController()
|
|
|
|
function reloadSuggestions() {
|
|
|
|
var stationNameInput = document.getElementById('stationName')
|
|
|
|
var stationName = searchNormalize(stationNameInput.value.trim())
|
|
|
|
|
|
|
|
var locationsUrl = new URL('https://v6.db.transport.rest/locations')
|
|
|
|
locationsUrl.searchParams.set('query', stationName)
|
|
|
|
locationsUrl.searchParams.set('limit', '25')
|
|
|
|
locationsUrl.searchParams.set('fuzzy', 'true')
|
|
|
|
locationsUrl.searchParams.set('stops', 'true')
|
|
|
|
locationsUrl.searchParams.set('addresses', 'true')
|
|
|
|
locationsUrl.searchParams.set('poi', 'true')
|
|
|
|
|
|
|
|
fetchAbortController.abort()
|
|
|
|
fetchAbortController = new AbortController()
|
|
|
|
fetch(locationsUrl.toString(), { signal: fetchAbortController.signal })
|
|
|
|
.then(function (response) {
|
|
|
|
return response.json()
|
|
|
|
})
|
|
|
|
.then(function (data) {
|
|
|
|
if (data) {
|
|
|
|
knownStations = Object.values(data)
|
|
|
|
rebuildSuggestions()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef DbJourney
|
|
|
|
* @prop {'journey'} type
|
|
|
|
* @prop {(DbTrip & DbArrDep & {tripId: string})[]} legs
|
|
|
|
* @prop {string} refreshToken
|
|
|
|
* @prop {DbRemark[]} remarks
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {{journeys: DbJourney[]}} data
|
|
|
|
*/
|
|
|
|
function onItineraries(data) {
|
|
|
|
var contentDiv = document.getElementById('content-div')
|
|
|
|
if (!contentDiv) {
|
|
|
|
contentDiv = document.createElement('div')
|
|
|
|
document.body.insertBefore(contentDiv, document.querySelector('footer'))
|
|
|
|
contentDiv.classList.add('content')
|
|
|
|
contentDiv.id = 'content-div'
|
|
|
|
}
|
|
|
|
|
|
|
|
while (contentDiv.childNodes.length > 0) {
|
|
|
|
contentDiv.childNodes[contentDiv.childNodes.length - 1].remove()
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < data.journeys.length; i++) {
|
|
|
|
var itineraryDiv = document.createElement('div')
|
|
|
|
contentDiv.appendChild(itineraryDiv)
|
|
|
|
|
|
|
|
var heading = document.createElement('h4')
|
|
|
|
itineraryDiv.appendChild(heading)
|
|
|
|
heading.textContent = `Itinerary ${i + 1}`
|
|
|
|
|
|
|
|
var trainsDiv = document.createElement('div')
|
|
|
|
itineraryDiv.appendChild(trainsDiv)
|
|
|
|
trainsDiv.classList.add('itinerary-trains')
|
|
|
|
|
|
|
|
data.journeys[i].legs.forEach(function (train, idx) {
|
|
|
|
var last = idx === data.journeys[i].legs.length - 1
|
|
|
|
|
|
|
|
var trainDiv = document.createElement('div')
|
|
|
|
trainsDiv.appendChild(trainDiv)
|
|
|
|
trainDiv.classList.add('itinerary-train')
|
|
|
|
|
|
|
|
if (idx === 0) {
|
|
|
|
var departureTimeP = document.createElement('p')
|
|
|
|
trainDiv.appendChild(departureTimeP)
|
|
|
|
departureTimeP.classList.add('sec', 'departure', 'time')
|
|
|
|
var departureTimePre = document.createElement('pre')
|
|
|
|
departureTimeP.appendChild(departureTimePre)
|
|
|
|
var departure = new Date(train.plannedDeparture)
|
|
|
|
departureTimePre.textContent = departure.toLocaleTimeString([], { 'hour': '2-digit', 'minute': '2-digit' })
|
|
|
|
|
|
|
|
var departureHeading = document.createElement('h3')
|
|
|
|
trainDiv.appendChild(departureHeading)
|
|
|
|
departureHeading.classList.add('departure', 'station')
|
|
|
|
if (train.origin.type === 'stop' || train.origin.type === 'station') {
|
|
|
|
var departureLink = document.createElement('a')
|
|
|
|
departureHeading.appendChild(departureLink)
|
|
|
|
departureLink.textContent = train.origin.name
|
|
|
|
departureLink.classList.add('no-custom-a', 'items')
|
|
|
|
var departureUrl = new URL('/view-station.html', window.location.origin)
|
|
|
|
departureUrl.searchParams.set('stationId', train.origin.id)
|
|
|
|
departureLink.href = departureUrl.toString()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var departureSpan = document.createElement('span')
|
|
|
|
departureHeading.append(departureSpan)
|
|
|
|
departureSpan.innerText = train.origin.name || train.origin.address
|
|
|
|
}
|
|
|
|
|
|
|
|
if (train.departurePlatform || train.plannedDeparturePlatform) {
|
|
|
|
var departurePlatformP = document.createElement('p')
|
|
|
|
trainDiv.append(departurePlatformP)
|
|
|
|
departurePlatformP.classList.add('sec', 'departure', 'platform')
|
|
|
|
if (train.departurePlatform && train.departurePlatform != train.plannedDeparturePlatform) {
|
|
|
|
departurePlatformP.classList.add('changed')
|
|
|
|
}
|
|
|
|
departurePlatformP.textContent = `${train.departurePlatform || train.plannedDeparturePlatform}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var trainP = document.createElement('p')
|
|
|
|
trainDiv.appendChild(trainP)
|
|
|
|
trainP.classList.add('pri', 'train')
|
|
|
|
if (!train.walking) {
|
|
|
|
var trainLink = document.createElement('a')
|
|
|
|
trainP.appendChild(trainLink)
|
|
|
|
trainLink.innerText = train.line.name
|
|
|
|
trainLink.classList.add('no-custom-a', 'items')
|
|
|
|
if (train.line.product) {
|
|
|
|
if (train.line.productName === 'STB' && train.line.name.startsWith('STB U')) {
|
|
|
|
train.line.product = 'subway'
|
|
|
|
}
|
|
|
|
trainLink.classList.add('product-' + train.line.product)
|
|
|
|
}
|
|
|
|
var trainUrl = new URL('/view-train.html', window.location.origin)
|
|
|
|
trainUrl.searchParams.set('tripId', train.tripId)
|
|
|
|
trainUrl.searchParams.set('startId', train.origin.id)
|
|
|
|
trainUrl.searchParams.set('stopId', train.destination.id)
|
|
|
|
trainLink.href = trainUrl.toString()
|
|
|
|
trainP.appendChild(document.createTextNode(' '))
|
|
|
|
if (train.line.operator) {
|
|
|
|
var trainCompany = document.createElement('span')
|
|
|
|
trainP.appendChild(trainCompany)
|
|
|
|
trainCompany.textContent = '(' + train.line.operator.name + ')'
|
|
|
|
trainCompany.classList.add('company')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var walkingSpan = document.createElement('span')
|
|
|
|
trainP.append(walkingSpan)
|
|
|
|
walkingSpan.classList.add('walking')
|
|
|
|
walkingSpan.innerText = `Walking (${train.distance} m)`
|
|
|
|
}
|
|
|
|
|
|
|
|
var arrivalTimeP = document.createElement('p')
|
|
|
|
trainDiv.appendChild(arrivalTimeP)
|
|
|
|
arrivalTimeP.classList.add('sec', 'arrival', 'time')
|
|
|
|
var arrivalTimePre = document.createElement('pre')
|
|
|
|
arrivalTimeP.appendChild(arrivalTimePre)
|
|
|
|
var arrival = new Date(train.plannedArrival)
|
|
|
|
arrivalTimePre.textContent = arrival.toLocaleTimeString([], { 'hour': '2-digit', 'minute': '2-digit' })
|
|
|
|
|
|
|
|
var arrivalHeading = document.createElement('h3')
|
|
|
|
trainDiv.appendChild(arrivalHeading)
|
|
|
|
arrivalHeading.classList.add('arrival', 'station')
|
|
|
|
if (train.destination.type === 'stop' || train.destination.type === 'station') {
|
|
|
|
var arrivalLink = document.createElement('a')
|
|
|
|
arrivalHeading.appendChild(arrivalLink)
|
|
|
|
arrivalLink.textContent = train.destination.name
|
|
|
|
arrivalLink.classList.add('no-custom-a', 'items')
|
|
|
|
var arrivalUrl = new URL('/view-station.html', window.location.origin)
|
|
|
|
arrivalUrl.searchParams.set('stationId', train.destination.id)
|
|
|
|
arrivalLink.href = arrivalUrl.toString()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var arrivalSpan = document.createElement('span')
|
|
|
|
arrivalHeading.append(arrivalSpan)
|
|
|
|
arrivalSpan.innerText = train.destination.name || train.destination.address
|
|
|
|
}
|
|
|
|
|
|
|
|
if (train.arrivalPlatform || train.plannedArrivalPlatform) {
|
|
|
|
var arrivalPlatformP = document.createElement('p')
|
|
|
|
trainDiv.append(arrivalPlatformP)
|
|
|
|
arrivalPlatformP.classList.add('sec', 'arrival', 'platform')
|
|
|
|
if (train.arrivalPlatform && train.arrivalPlatform != train.plannedArrivalPlatform) {
|
|
|
|
arrivalPlatformP.classList.add('changed')
|
|
|
|
}
|
|
|
|
arrivalPlatformP.textContent = `${train.arrivalPlatform || train.plannedArrivalPlatform}`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!last) {
|
|
|
|
var nextTrain = data.journeys[i].legs[idx + 1]
|
|
|
|
var nextDepartureTimeP = document.createElement('p')
|
|
|
|
trainDiv.appendChild(nextDepartureTimeP)
|
|
|
|
nextDepartureTimeP.classList.add('sec', 'next-departure', 'time')
|
|
|
|
var departureTimePre = document.createElement('pre')
|
|
|
|
nextDepartureTimeP.appendChild(departureTimePre)
|
|
|
|
var departure = new Date(nextTrain.plannedDeparture)
|
|
|
|
departureTimePre.textContent = departure.toLocaleTimeString([], { 'hour': '2-digit', 'minute': '2-digit' })
|
|
|
|
|
|
|
|
if (nextTrain.departurePlatform || nextTrain.plannedDeparturePlatform) {
|
|
|
|
var departurePlatformP = document.createElement('p')
|
|
|
|
trainDiv.append(departurePlatformP)
|
|
|
|
departurePlatformP.classList.add('sec', 'next-departure', 'platform')
|
|
|
|
if (nextTrain.departurePlatform && nextTrain.departurePlatform != nextTrain.plannedDeparturePlatform) {
|
|
|
|
departurePlatformP.classList.add('changed')
|
|
|
|
}
|
|
|
|
departurePlatformP.textContent = `${nextTrain.departurePlatform || nextTrain.plannedDeparturePlatform}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function lsk() {
|
|
|
|
document.getElementById('stationName').focus()
|
|
|
|
}
|
|
|
|
|
|
|
|
function csk() {
|
|
|
|
if (focusedElement == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focusedElement.id === 'stationName') {
|
|
|
|
goToTrain(document.activeElement.value.trim())
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
focusedElement.click()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('load', function (e) {
|
|
|
|
var sp = new URL(window.location.href).searchParams
|
|
|
|
fromStation = sp.get('from')
|
|
|
|
var fromJson = JSON.parse(fromStation || 'null')
|
|
|
|
toStation = sp.get('to')
|
|
|
|
var toJson = JSON.parse(toStation || 'null')
|
|
|
|
var departureDateStr = sp.get('departureDate')
|
|
|
|
if (departureDateStr) {
|
|
|
|
departureDate = new Date(departureDateStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var titleH1 = document.querySelector("header > h1")
|
|
|
|
if (!fromStation) {
|
|
|
|
titleH1.textContent = 'Find Route - From'
|
|
|
|
}
|
|
|
|
else if (!toStation) {
|
|
|
|
titleH1.textContent = 'Find Route - To'
|
|
|
|
}
|
|
|
|
else if (!departureDate) {
|
|
|
|
titleH1.textContent = 'Find Route - Departure Date'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
titleH1.textContent = `${fromJson.name || fromJson.address} - ${toJson.name || toJson.address}`
|
|
|
|
}
|
|
|
|
|
|
|
|
var transitKindJson = JSON.parse(sp.get('transitKind'))
|
|
|
|
if (transitKindJson) {
|
|
|
|
transitKind = transitKindJson
|
|
|
|
}
|
|
|
|
|
|
|
|
var footer = document.querySelector('footer')
|
|
|
|
|
|
|
|
if (!fromStation || !toStation) {
|
|
|
|
// Build station selection UI
|
|
|
|
document.body.insertBefore(
|
|
|
|
h4(
|
|
|
|
label('Station Name').att$('for', 'stationName'),
|
|
|
|
),
|
|
|
|
footer,
|
|
|
|
)
|
|
|
|
// var stationNameH4 = document.createElement('h4')
|
|
|
|
// document.body.insertBefore(stationNameH4, footer)
|
|
|
|
// var stationNameLabel = document.createElement('label')
|
|
|
|
// stationNameH4.appendChild(stationNameLabel)
|
|
|
|
// stationNameLabel.htmlFor = 'stationName'
|
|
|
|
// stationNameLabel.textContent = 'Station Name'
|
|
|
|
|
|
|
|
document.body.insertBefore(
|
|
|
|
input('search').id$('stationName').att$('name', 'stationName').class$('items'),
|
|
|
|
footer,
|
|
|
|
)
|
|
|
|
// var stationNameInput = document.createElement('input')
|
|
|
|
// document.body.insertBefore(stationNameInput, footer)
|
|
|
|
// stationNameInput.type = 'search'
|
|
|
|
// stationNameInput.classList.add('items')
|
|
|
|
// stationNameInput.name = 'stationName'
|
|
|
|
// stationNameInput.id = 'stationName'
|
|
|
|
|
|
|
|
document.body.insertBefore(h4('Suggestions'), footer)
|
|
|
|
// var suggestionsH4 = document.createElement('h4')
|
|
|
|
// document.body.insertBefore(suggestionsH4, footer)
|
|
|
|
// suggestionsH4.textContent = 'Suggestions'
|
|
|
|
|
|
|
|
var contentDiv = document.createElement('div')
|
|
|
|
document.body.insertBefore(contentDiv, footer)
|
|
|
|
contentDiv.classList.add('content')
|
|
|
|
var suggestionsUl = document.createElement('ul')
|
|
|
|
contentDiv.appendChild(suggestionsUl)
|
|
|
|
suggestionsUl.id = 'suggestionsArea'
|
|
|
|
|
|
|
|
document.querySelector('.csk').textContent = 'Search'
|
|
|
|
|
|
|
|
var stationName = document.getElementById('stationName')
|
|
|
|
stationName.addEventListener('input', function (e) {
|
|
|
|
reloadSuggestions()
|
|
|
|
})
|
|
|
|
stationName.addEventListener('focus', function (e) {
|
|
|
|
focusedElement = stationName
|
|
|
|
document.getElementsByClassName('lsk')[0].textContent = ''
|
|
|
|
document.getElementsByClassName('csk')[0].textContent = 'Search'
|
|
|
|
})
|
|
|
|
stationName.addEventListener('blur', function (e) {
|
|
|
|
document.getElementsByClassName('lsk')[0].textContent = 'Search'
|
|
|
|
document.getElementsByClassName('csk')[0].textContent = 'Select'
|
|
|
|
})
|
|
|
|
stationName.addEventListener('keypress', function (e) {
|
|
|
|
if (e.key == 'Enter') {
|
|
|
|
goToStation(stationName.value.trim())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else if (!departureDate) {
|
|
|
|
var departureDateH4 = document.createElement('h4')
|
|
|
|
document.body.insertBefore(departureDateH4, footer)
|
|
|
|
departureDateH4.textContent = 'Departure Date'
|
|
|
|
|
|
|
|
var contentDiv = document.createElement('div')
|
|
|
|
document.body.insertBefore(contentDiv, footer)
|
|
|
|
contentDiv.classList.add('content')
|
|
|
|
var departureDateUl = document.createElement('ul')
|
|
|
|
contentDiv.appendChild(departureDateUl)
|
|
|
|
departureDateUl.id = 'suggestionsArea'
|
|
|
|
|
|
|
|
for (var i = -1, departureOption = (function () { var d = new Date(); d.setDate(d.getDate() - 1); return d })(); i < 30; i++, departureOption.setDate(departureOption.getDate() + 1)) {
|
|
|
|
var suggestionLi = document.createElement('li')
|
|
|
|
departureDateUl.appendChild(suggestionLi)
|
|
|
|
suggestionLi.classList.add('items')
|
|
|
|
suggestionLi.tabIndex = i + 10
|
|
|
|
// Capture
|
|
|
|
;(function () {
|
|
|
|
var d = new Date(departureOption.getTime())
|
|
|
|
function onAction() {
|
|
|
|
setDepartureDate(d)
|
|
|
|
}
|
|
|
|
suggestionLi.addEventListener('click', onAction)
|
|
|
|
suggestionLi.addEventListener('keypress', function (e) {
|
|
|
|
if (e.key == 'Enter') {
|
|
|
|
onAction(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
suggestionLi.addEventListener('focus', function (e) {
|
|
|
|
focusedElement = suggestionLi
|
|
|
|
})
|
|
|
|
if (i === 0) {
|
|
|
|
suggestionLi.focus()
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
var innerP = document.createElement('p')
|
|
|
|
suggestionLi.appendChild(innerP)
|
|
|
|
innerP.classList.add('pri')
|
|
|
|
var innerPre = document.createElement('pre')
|
|
|
|
innerP.appendChild(innerPre)
|
|
|
|
innerPre.textContent = `${departureOption.getDate().toString().padStart(2, '0')}.${(departureOption.getMonth() + 1).toString().padStart(2, '0')}.${departureOption.getFullYear().toString().padStart(4, '0')}`
|
|
|
|
if (i === 0) {
|
|
|
|
innerPre.textContent += ' (today)'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelector('.csk').textContent = 'Select'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var contentDiv = document.createElement('div')
|
|
|
|
document.body.insertBefore(contentDiv, footer)
|
|
|
|
contentDiv.classList.add('content')
|
|
|
|
contentDiv.style.display = 'flex'
|
|
|
|
contentDiv.style.flexDirection = 'column'
|
|
|
|
contentDiv.style.alignItems = 'center'
|
|
|
|
contentDiv.style.justifyContent = 'center'
|
|
|
|
|
|
|
|
var loadingP = document.createElement('p')
|
|
|
|
contentDiv.appendChild(loadingP)
|
|
|
|
loadingP.classList.add('pri')
|
|
|
|
loadingP.textContent = 'Loading data...'
|
|
|
|
|
|
|
|
var url = new URL('https://v6.db.transport.rest/journeys')
|
|
|
|
if (fromJson.type === 'stop' || fromJson.type === 'station') {
|
|
|
|
url.searchParams.set('from', fromJson.id)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (fromJson.type === 'location') {
|
|
|
|
delete fromJson.id
|
|
|
|
}
|
|
|
|
Object.keys(fromJson).forEach(function (key) {
|
|
|
|
url.searchParams.set(`from.${key}`, fromJson[key])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (toJson.type === 'stop' || toJson.type === 'station') {
|
|
|
|
url.searchParams.set('to', toJson.id)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (toJson.type === 'location') {
|
|
|
|
delete toJson.id
|
|
|
|
}
|
|
|
|
Object.keys(toJson).forEach(function (key) {
|
|
|
|
url.searchParams.set(`to.${key}`, toJson[key])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
url.searchParams.set('departure', departureDate.toISOString())
|
|
|
|
url.searchParams.set('results', '20')
|
|
|
|
url.searchParams.set('stopovers', 'true')
|
|
|
|
url.searchParams.set('nationalExpress', transitKind.ice)
|
|
|
|
url.searchParams.set('national', transitKind.ic)
|
|
|
|
url.searchParams.set('regionalExpress', transitKind.re)
|
|
|
|
url.searchParams.set('regional', transitKind.rb)
|
|
|
|
url.searchParams.set('suburban', transitKind.s)
|
|
|
|
url.searchParams.set('bus', transitKind.bus)
|
|
|
|
url.searchParams.set('ferry', transitKind.ferry)
|
|
|
|
url.searchParams.set('subway', transitKind.u)
|
|
|
|
url.searchParams.set('tram', transitKind.tram)
|
|
|
|
|
|
|
|
if (window.localStorage) {
|
|
|
|
this.localStorage.setItem('recent/route', JSON.stringify({
|
|
|
|
$addDate: new Date().toISOString(),
|
|
|
|
from: fromJson.name || fromJson.address,
|
|
|
|
to: toJson.name || toJson.address,
|
|
|
|
queryParams: new URL(window.location.href).search,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch(url.toString())
|
|
|
|
.then(function (response) {
|
|
|
|
return response.json()
|
|
|
|
})
|
|
|
|
.then(function (data) {
|
|
|
|
contentDiv.remove()
|
|
|
|
onItineraries(data)
|
|
|
|
itineraries = data
|
|
|
|
function fetchMore() {
|
|
|
|
console.debug(`Got ${itineraries.journeys.length} journeys, fetching more`)
|
|
|
|
var moreUrl = new URL(url.toString())
|
|
|
|
moreUrl.searchParams.delete('departure')
|
|
|
|
moreUrl.searchParams.set('laterThan', itineraries.laterRef)
|
|
|
|
fetch(moreUrl.toString())
|
|
|
|
.then(function (result) {
|
|
|
|
return result.json()
|
|
|
|
})
|
|
|
|
.then(function (data) {
|
|
|
|
if (data.journeys) {
|
|
|
|
itineraries.journeys = itineraries.journeys.concat(data.journeys)
|
|
|
|
itineraries.laterRef = data.laterRef
|
|
|
|
onItineraries(itineraries)
|
|
|
|
|
|
|
|
if (itineraries.laterRef) {
|
|
|
|
var lastJourney = itineraries.journeys[itineraries.journeys.length - 1]
|
|
|
|
var lastLeg = lastJourney.legs[lastJourney.legs.length - 1]
|
|
|
|
var departureDate = new Date(lastLeg.plannedDeparture)
|
|
|
|
if (departureDate.getTime() - Date.now() < 86400000) {
|
|
|
|
setTimeout(fetchMore, 500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fetchMore()
|
|
|
|
})
|
|
|
|
.catch(function (e) {
|
|
|
|
loadingP.textContent = 'An error has occured'
|
|
|
|
|
|
|
|
var errorP = document.createElement('p')
|
|
|
|
contentDiv.appendChild(errorP)
|
|
|
|
errorP.classList.add('sec')
|
|
|
|
errorP.textContent = e.toString()
|
|
|
|
|
|
|
|
var retryLink = document.createElement('a')
|
|
|
|
contentDiv.appendChild(retryLink)
|
|
|
|
retryLink.classList.add('items')
|
|
|
|
retryLink.textContent = 'Retry'
|
|
|
|
retryLink.href = ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelectorAll('.lsk').forEach(function (lskElem) {
|
|
|
|
lskElem.addEventListener('click', function (e) {
|
|
|
|
lsk()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
document.querySelectorAll('.csk').forEach(function (cskElem) {
|
|
|
|
cskElem.addEventListener('click', function (e) {
|
|
|
|
csk()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
document.body.addEventListener('keydown', function (e) {
|
|
|
|
if (e.key == 'SoftLeft') {
|
|
|
|
lsk()
|
|
|
|
}
|
|
|
|
else if (e.key == 'Enter') {
|
|
|
|
csk()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|