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.
268 lines
7.8 KiB
268 lines
7.8 KiB
var trainNumber |
|
var date |
|
|
|
var showKm = false |
|
|
|
var trainData = null |
|
|
|
function onTrainData(data) { |
|
var title = document.getElementsByTagName('h1')[0] |
|
title.textContent = '' |
|
title.appendChild(document.createTextNode('Train ')) |
|
var rankSpan = document.createElement('span') |
|
rankSpan.textContent = data.rank |
|
rankSpan.classList.add(data.rank) |
|
title.appendChild(rankSpan) |
|
title.appendChild(document.createTextNode(` ${data.number}`)) |
|
|
|
document.getElementById('company').textContent = data.operator |
|
|
|
document.getElementById('route-from').textContent = data.route.from |
|
document.getElementById('route-to').textContent = data.route.to |
|
|
|
if (data.status) { |
|
document.getElementById('status').classList.remove('hidden') |
|
|
|
var statusDelay = document.getElementById('status-delay') |
|
while (statusDelay.childNodes.length > 0) { |
|
statusDelay.childNodes[0].remove() |
|
} |
|
var delayString = '' |
|
var delayMinutes = data.status.delay |
|
if (delayMinutes === 0) { |
|
delayString = 'No delay' |
|
statusDelay.appendChild(document.createTextNode(delayString)) |
|
} |
|
else { |
|
var early = false |
|
if (delayMinutes < 0) { |
|
early = true |
|
delayMinutes = -delayMinutes |
|
} |
|
|
|
if (delayMinutes >= 60) { |
|
var hours = Math.floor(delayMinutes / 60) |
|
delayMinutes = delayMinutes % 60 |
|
delayString += hours.toString() |
|
delayString += ' hour' |
|
if (hours > 1) { |
|
delayString += 's' |
|
} |
|
} |
|
if (delayMinutes > 0) { |
|
if (delayString.length > 0) { |
|
delayString += ' and ' |
|
} |
|
delayString += delayMinutes.toString() |
|
delayString += ' minute' |
|
if (delayMinutes > 1) { |
|
delayString += 's' |
|
} |
|
} |
|
delayString += ' ' |
|
statusDelay.appendChild(document.createTextNode(delayString)) |
|
|
|
var kindSpan = document.createElement('span') |
|
statusDelay.appendChild(kindSpan) |
|
if (early) { |
|
kindSpan.textContent = 'early' |
|
kindSpan.classList.add('early') |
|
} |
|
else { |
|
kindSpan.textContent = 'late' |
|
kindSpan.classList.add('late') |
|
} |
|
} |
|
|
|
var statusLocation = document.getElementById('status-location') |
|
while (statusLocation.childNodes.length > 0) { |
|
statusLocation.childNodes[0].remove() |
|
} |
|
var stateString = '' |
|
if (data.status.state === 'arrival') { |
|
stateString += 'when arriving at ' |
|
} |
|
else if (data.status.state === 'departure') { |
|
stateString += 'when departing from ' |
|
} |
|
else if (data.status.state === 'passing') { |
|
stateString += 'while passing through ' |
|
} |
|
statusLocation.appendChild(document.createTextNode(stateString)) |
|
var stationSpan = document.createElement('span') |
|
statusLocation.appendChild(stationSpan) |
|
stationSpan.textContent = data.status.station |
|
stationSpan.classList.add('station') |
|
} |
|
else { |
|
document.getElementById('status').classList.add('hidden') |
|
} |
|
|
|
var stationsDiv = document.getElementById('stations') |
|
while (stationsDiv.childNodes.length > 0) { |
|
stationsDiv.childNodes[0].remove() |
|
} |
|
|
|
var separator = document.createElement('h4') |
|
stationsDiv.appendChild(separator) |
|
separator.textContent = 'Stations' |
|
|
|
var stationsList = document.createElement('ul') |
|
stationsDiv.appendChild(stationsList) |
|
|
|
data.stations.forEach(function (station) { |
|
var stationItem = document.createElement('li') |
|
stationsList.appendChild(stationItem) |
|
stationItem.classList.add('stationItem') |
|
|
|
var stationName = document.createElement('p') |
|
stationItem.appendChild(stationName) |
|
stationName.textContent = station.name |
|
stationName.classList.add('pri', 'name') |
|
|
|
if (station.arrival) { |
|
var stationArrival = document.createElement('div') |
|
stationItem.appendChild(stationArrival) |
|
stationArrival.classList.add('arrival') |
|
|
|
var originalArr = document.createElement('p') |
|
stationArrival.appendChild(originalArr) |
|
var arrDate = new Date(station.arrival.scheduleTime) |
|
originalArr.textContent = `${arrDate.getHours().toString().padStart(2, "0")}:${arrDate.getMinutes().toString().padStart(2, "0")}` |
|
originalArr.classList.add('pri') |
|
if (station.arrival.status && station.arrival.status.delay != 0) { |
|
originalArr.classList.remove('pri') |
|
originalArr.classList.add('thi', 'original') |
|
|
|
var actualArr = document.createElement('p') |
|
stationArrival.appendChild(actualArr) |
|
arrDate.setMinutes(arrDate.getMinutes() + station.arrival.status.delay) |
|
actualArr.textContent = `${arrDate.getHours().toString().padStart(2, "0")}:${arrDate.getMinutes().toString().padStart(2, "0")}` |
|
actualArr.classList.add('pri', station.arrival.status.delay > 0 ? 'late' : 'early') |
|
if (!station.arrival.status.real) { |
|
actualArr.classList.add('not-real') |
|
} |
|
} |
|
} |
|
|
|
if (station.departure) { |
|
var stationDeparture = document.createElement('div') |
|
stationItem.appendChild(stationDeparture) |
|
stationDeparture.classList.add('departure') |
|
|
|
var originalDep = document.createElement('p') |
|
stationDeparture.appendChild(originalDep) |
|
var depDate = new Date(station.departure.scheduleTime) |
|
originalDep.textContent = `${depDate.getHours().toString().padStart(2, "0")}:${depDate.getMinutes().toString().padStart(2, "0")}` |
|
originalDep.classList.add('pri') |
|
if (station.departure.status && station.departure.status.delay != 0) { |
|
originalDep.classList.remove('pri') |
|
originalDep.classList.add('thi', 'original') |
|
|
|
var actualDep = document.createElement('p') |
|
stationDeparture.appendChild(actualDep) |
|
depDate.setMinutes(depDate.getMinutes() + station.departure.status.delay) |
|
actualDep.textContent = `${depDate.getHours().toString().padStart(2, "0")}:${depDate.getMinutes().toString().padStart(2, "0")}` |
|
actualDep.classList.add('pri', station.departure.status.delay > 0 ? 'late' : 'early') |
|
if (!station.departure.status.real) { |
|
actualDep.classList.add('not-real') |
|
} |
|
} |
|
} |
|
|
|
var stationKm = document.createElement('p') |
|
stationItem.appendChild(stationKm) |
|
stationKm.textContent = `${station.km} km` |
|
stationKm.classList.add('thi', 'km') |
|
if (!showKm) { |
|
stationKm.classList.add('hidden') |
|
} |
|
|
|
if (station.platform) { |
|
var stationPlatform = document.createElement('p') |
|
stationItem.appendChild(stationPlatform) |
|
stationPlatform.textContent = `platform ${station.platform}` |
|
stationPlatform.classList.add('thi', 'platform') |
|
} |
|
}) |
|
} |
|
|
|
var refreshStopToken = null |
|
function refresh() { |
|
fetch(`https://scraper.infotren.dcdev.ro/v2/train/${trainNumber}?date=${date.toISOString()}`) |
|
.then(function (response) { |
|
return response.json() |
|
}) |
|
.then(function (response) { |
|
trainData = response |
|
onTrainData(response) |
|
}) |
|
.then(function () { |
|
refreshStopToken = setTimeout(function () { |
|
refresh() |
|
}, 60000) |
|
}) |
|
} |
|
|
|
window.addEventListener('unload', function (e) { |
|
if (refreshStopToken != null) { |
|
clearTimeout(refreshStopToken) |
|
} |
|
}) |
|
|
|
function rsk() { |
|
refresh() |
|
} |
|
|
|
window.addEventListener('load', function (e) { |
|
if (!new URL(window.location.href).searchParams.has('train')) { |
|
window.history.back() |
|
this.setTimeout(function () { |
|
var url = new URL(window.location.href) |
|
url.pathname = 'train.html' |
|
window.location.href = url.toString() |
|
}, 100) |
|
} |
|
|
|
var sp = new URL(window.location.href).searchParams |
|
|
|
trainNumber = sp.get('train') |
|
date = sp.has('date') ? new Date(sp.get('date')) : new Date() |
|
|
|
document.querySelectorAll('.rsk').forEach(function (rskElem) { |
|
rskElem.addEventListener('click', function (e) { |
|
rsk() |
|
}) |
|
}) |
|
|
|
var content = document.getElementsByClassName('content')[0] |
|
content.focus() |
|
content.addEventListener('keydown', function (e) { |
|
switch (e.key) { |
|
case 'ArrowUp': |
|
content.scrollBy(0, -50) |
|
break |
|
case 'ArrowDown': |
|
content.scrollBy(0, 50) |
|
break |
|
case 'SoftRight': |
|
rsk() |
|
break |
|
case '*': |
|
showKm = !showKm |
|
document.querySelectorAll('.km').forEach(function (kmItem) { |
|
if (showKm) { |
|
kmItem.classList.remove('hidden') |
|
} |
|
else { |
|
kmItem.classList.add('hidden') |
|
} |
|
}) |
|
break |
|
default: |
|
console.log(e.key) |
|
} |
|
}) |
|
|
|
refresh() |
|
})
|
|
|