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

83 lines
2.0 KiB
JavaScript

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)
if (typeof data == 'number' || !data) {
log('train-detail.get: Status: ' + data, 'ERR')
return []
}
log(data, 'DBUG')
return data
}
async function parse(data) {
if (data.length === 1) {
document.getElementById('train_options').style = 'display:none;'
displayOne(data[0])
}
displayOptions(data)
document.getElementById('train_data').style = 'display:block;'
}
async function displayOptions(data) {
return data
}
async function displayOne(object) {
// Display a single service immediately
const dataTableHead = `
<p id='data_headcode'>${object['headcode']}</p>
<p id='data_piscode'>PIS Code: ${object['pis']}</p>
<table id='data_table'>
`
const dataTableClose = `
</table>`
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)
}
const displayBox = document.getElementById('train_data')
displayBox.insertAdjacentHTML('beforeend', dataTableHead + stopRows + dataTableClose)
}
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 = '-'
}
return `
<tr>
<td id='data_tiploc'>${data_tiploc}</td>
<td id='public_arr'>${public_arr}</td>
<td id='public_dep'>${public_dep}</td>
</tr>
`
}