kai.infodb/index.js

93 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2022-12-27 19:08:42 +02:00
window.addEventListener('load', function (e) {
if (window.localStorage) {
2023-10-24 08:53:55 +02:00
var recentViewTrainStr = localStorage.getItem('recent/view-train')
if (recentViewTrainStr) {
2022-12-27 19:08:42 +02:00
/**
2023-10-24 08:53:55 +02:00
* @type {object}
* @property {?string} trainNumber
* @property {?string} name
* @property {string} tripId
2022-12-27 19:08:42 +02:00
* @property {string} date
* @property {string} $addDate
*/
2023-10-24 08:53:55 +02:00
var recentViewTrain = JSON.parse(recentViewTrainStr)
2022-12-27 19:08:42 +02:00
var addDate = new Date(recentViewTrain.$addDate)
2023-10-24 08:53:55 +02:00
addDate.setHours(addDate.getHours() + 6) // store recents for 6 hours
2022-12-27 19:08:42 +02:00
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)
2023-10-24 08:53:55 +02:00
recentViewTrainLinkUrl.searchParams.append('tripId', recentViewTrain.tripId)
2022-12-27 19:08:42 +02:00
recentViewTrainLinkUrl.searchParams.append('date', recentViewTrain.date)
recentViewTrainLink.href = recentViewTrainLinkUrl.toString()
recentViewTrainLink.classList.add('items')
2023-10-24 08:53:55 +02:00
recentViewTrainLink.innerText = `Recent train: ${recentViewTrain.name || "..."}`
if (recentViewTrain.trainNumber) {
recentViewTrainLink.innerText = `Recent train: ${recentViewTrain.name || "..."} (${recentViewTrain.trainNumber})`
}
2022-12-27 19:08:42 +02:00
2023-10-24 08:53:55 +02:00
fetch(`https://v6.db.transport.rest/trips/${encodeURI(recentViewTrain.tripId)}`)
2022-12-27 19:08:42 +02:00
.then(function (result) {
if (result.ok) {
return result.json()
}
2023-10-24 08:53:55 +02:00
else {
return Promise.reject('Response not okay')
}
2022-12-27 19:08:42 +02:00
})
.then(function (result) {
2023-10-24 08:53:55 +02:00
recentViewTrainLink.innerText = `Recent train: ${result.trip.line.name} (${result.trip.line.fahrtNr})`
2022-12-27 19:08:42 +02:00
})
var myTrainLi = document.getElementById("my-train-li")
myTrainLi.parentNode.insertBefore(recentViewTrainLi, myTrainLi.nextSibling)
}
}
2023-10-24 08:53:55 +02:00
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)
}
}
2022-12-27 19:08:42 +02:00
}
})
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)
})