This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
web/js/train-detail.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-06-07 23:31:51 +01:00
versionDisplay()
let data // Maybe this is a bad idea?
init()
async function init() {
const headcode = await getQuery('headcode')
if (headcode == 'false') {
parse([]) // Pass an empty array to parse()
}
const res = await get(headcode)
parse(res)
}
async function get(headcode) {
const apiPath = `train/headcode/today/${headcode}`
data = await getApi(apiPath, auth = true)
2023-06-07 23:37:55 +01:00
if (typeof data == 'number' || !data) {
2023-06-07 23:31:51 +01:00
log('train-detail.get: Status: ' + data, 'ERR')
return []
}
log(data, 'DBUG')
return data
}
async function parse(data) {
2023-06-08 14:09:36 +01:00
if (data.length <= 1) {
2023-06-07 23:31:51 +01:00
document.getElementById('train_options').style = 'display:none;'
displayOne(data[0])
}
displayOptions(data)
document.getElementById('train_data').style = 'display:block;'
2023-06-07 23:31:51 +01:00
}
async function displayOptions(data) {
2023-06-08 14:09:36 +01:00
for (service in data) {
const serviceData = data[service]
const lastStop = serviceData['stops'][(serviceData['stops'].length - 1)]
log(`displayOptions: data[${service}] = ${serviceData}`, 'dbug')
let button = `
<button class='service_button' onclick='displayOne(data[${service}])'>
<span class='service_toc'>GW</span>
<span class='service_origin_time'>${serviceData['stops'][0]['wttDeparture']}</span>
<span class='service_origin_tiploc'>${serviceData['stops'][0]['tiploc']}</span>
to
<span class='service_dest_tiploc'>${lastStop['tiploc']}</span>
</button>
`
document.getElementById('train_options').insertAdjacentHTML('beforeend', button)
}
document.getElementById('train_options').style = 'display:block;'
2023-06-07 23:31:51 +01:00
return data
}
async function displayOne(object) {
2023-06-08 14:09:36 +01:00
// Display a single service
const dataTableHead = `
2023-06-07 23:31:51 +01:00
<p id='data_headcode'>${object['headcode']}</p>
<p id='data_piscode'>PIS Code: ${object['pis']}</p>
<table id='data_table'>
2023-06-08 10:55:31 +01:00
<tr>
2023-06-08 11:17:46 +01:00
<th>Location</th>
2023-06-08 10:55:31 +01:00
<th>Arr.</th>
<th>Dep.</th>
</tr>
2023-06-07 23:31:51 +01:00
`
const dataTableClose = `
</table>`
2023-06-07 23:31:51 +01:00
let publicStops = []
for (const stop of object['stops']) {
if (stop['isPublic']) {
publicStops.push(stop)
}
}
console.log(publicStops)
stopRows = ''
for (const stop of publicStops) {
stopRows += await createPublicStopTableRow(stop)
2023-06-07 23:31:51 +01:00
}
const displayBox = document.getElementById('train_data')
2023-06-08 11:37:26 +01:00
displayBox.innerHTML = ''
displayBox.insertAdjacentHTML('beforeend', dataTableHead + stopRows + dataTableClose)
2023-06-08 11:13:36 +01:00
displayBox.style = 'display:block;'
2023-06-07 23:31:51 +01:00
}
async function createPublicStopTableRow(stop) {
let data_tiploc = stop['tiploc'], public_arr = stop['publicArrival'],
public_dep = stop['publicDeparture']
if (typeof data_tiploc === 'undefined') {
data_tiploc = ''
}
if (typeof public_arr === 'undefined') {
public_arr = '-'
}
if (typeof public_dep === 'undefined') {
public_dep = '-'
}
2023-06-07 23:31:51 +01:00
return `
<tr>
<td id='data_tiploc'>${data_tiploc}</td>
<td id='public_arr'>${public_arr}</td>
<td id='public_dep'>${public_dep}</td>
2023-06-07 23:31:51 +01:00
</tr>
`
}