You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
4.3 KiB

window.addEventListener('load', function (e) {
if (window.localStorage) {
var recentViewTrainStr = localStorage.getItem('recent/view-train')
if (recentViewTrainStr) {
/**
* @type {object}
* @property {?string} trainNumber
* @property {?string} name
* @property {string} tripId
* @property {string} date
* @property {string} $addDate
*/
var recentViewTrain = JSON.parse(recentViewTrainStr)
var addDate = new Date(recentViewTrain.$addDate)
addDate.setHours(addDate.getHours() + 6) // store recents for 6 hours
if (addDate.getTime() > Date.now()) {
var recentViewTrainLi = document.createElement('li')
var recentViewTrainLink = document.createElement('a')
recentViewTrainLi.appendChild(recentViewTrainLink)
var recentViewTrainLinkUrl = new URL('/view-train.html', window.location.origin)
recentViewTrainLinkUrl.searchParams.append('tripId', recentViewTrain.tripId)
recentViewTrainLinkUrl.searchParams.append('date', recentViewTrain.date)
recentViewTrainLink.href = recentViewTrainLinkUrl.toString()
recentViewTrainLink.classList.add('items')
recentViewTrainLink.innerText = `Recent train: ${recentViewTrain.name || "..."}`
if (recentViewTrain.trainNumber) {
recentViewTrainLink.innerText = `Recent train: ${recentViewTrain.name || "..."} (${recentViewTrain.trainNumber})`
}
fetch(`https://v6.db.transport.rest/trips/${encodeURI(recentViewTrain.tripId)}`)
.then(function (result) {
if (result.ok) {
return result.json()
}
else {
return Promise.reject('Response not okay')
}
})
.then(function (result) {
recentViewTrainLink.innerText = `Recent train: ${result.trip.line.name} (${result.trip.line.fahrtNr})`
})
var myTrainLi = document.getElementById("my-train-li")
myTrainLi.parentNode.insertBefore(recentViewTrainLi, myTrainLi.nextSibling)
}
}
var recentRouteStr = localStorage.getItem('recent/route')
if (recentRouteStr) {
/**
* @type {object}
* @property {string} queryParams
* @property {string} from
* @property {string} to
* @property {string} $addDate
*/
var recentRoute = JSON.parse(recentRouteStr)
var addDate = new Date(recentRoute.$addDate)
addDate.setHours(addDate.getHours() + 12) // store recents for 6 hours
if (addDate.getTime() > Date.now()) {
var recentRouteLi = document.createElement('li')
var recentRouteLink = document.createElement('a')
recentRouteLi.appendChild(recentRouteLink)
var recentRouteLinkUrl = new URL('/config-route.html', window.location.origin)
recentRouteLinkUrl.search = recentRoute.queryParams
recentRouteLink.href = recentRouteLinkUrl.toString()
recentRouteLink.classList.add('items')
recentRouteLink.innerText = `Recent route: ${recentRoute.from}${recentRoute.to}`
var routesLi = document.getElementById("routes-li")
routesLi.parentNode.insertBefore(recentRouteLi, routesLi.nextSibling)
}
}
}
})
window.addEventListener('beforeinstallprompt', function (e) {
var installAppLi = document.createElement('li')
var installAppLink = document.createElement('a')
installAppLi.appendChild(installAppLink)
installAppLink.href = '#'
installAppLink.classList.add('items')
installAppLink.innerText = 'Install application'
installAppLink.addEventListener('click', function (clickE) {
e.prompt().then(function (_) {
installAppLi.remove()
})
})
var routesLi = document.getElementById("routes-li")
routesLi.parentNode.insertBefore(installAppLi, routesLi)
})