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

118 lines
3.2 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:13:34 +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
}
2023-06-09 12:21:53 +01:00
// If there are no public stops, show non-public locations
if (!publicStops.length) {
for (const stop of object['stops']) {
stopRows += await createPublicStopTableRow(stop, 'wtt')
}
}
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
}
2023-06-09 12:21:53 +01:00
async function createPublicStopTableRow(stop, type='public') {
let arr, dep
let data_tiploc = stop['tiploc']
if (type === 'wtt') {
arr = stop['wttArrival'], dep = stop['wttDeparture']
}
if (type === 'public') {
arr = stop['publicArrival'], dep = stop['publicDeparture']
}
if (typeof data_tiploc === 'undefined') {
data_tiploc = ''
}
2023-06-09 12:21:53 +01:00
if (typeof arr === 'undefined') {
arr = '-'
}
2023-06-09 12:21:53 +01:00
if (typeof dep === 'undefined') {
dep = '-'
}
2023-06-07 23:31:51 +01:00
return `
<tr>
<td id='data_tiploc'>${data_tiploc}</td>
2023-06-09 12:21:53 +01:00
<td id='public_arr'>${arr}</td>
<td id='public_dep'>${dep}</td>
2023-06-07 23:31:51 +01:00
</tr>
`
}