2023-05-08 21:31:47 +01:00
|
|
|
/* eslint-disable no-unused-vars */
|
2023-05-06 21:53:43 +01:00
|
|
|
hideLoading()
|
|
|
|
versionDisplay()
|
|
|
|
showHideAuthNotice()
|
|
|
|
|
|
|
|
async function findByOrigDest() {
|
|
|
|
showLoading()
|
|
|
|
const formData = await fetchOrigDest()
|
|
|
|
log(`findByOrigDest: Searching for PIS Code for ${JSON.stringify(formData)}`)
|
|
|
|
const endpoint = `pis/origdest/${formData.origin}/${formData.destination}`
|
|
|
|
console.time('findByOrigDest-GET')
|
|
|
|
const json = await getApi(endpoint, auth = true)
|
|
|
|
console.timeEnd('findByOrigDest-GET')
|
|
|
|
if (json == false) {
|
|
|
|
await displayNoData()
|
|
|
|
} else if (json == 401) {
|
|
|
|
await displayUnauthorised()
|
|
|
|
} else {
|
|
|
|
await insertData(json)
|
|
|
|
}
|
|
|
|
document.getElementById('crs-box').style = 'display:none'
|
|
|
|
document.getElementById('result-box').style = 'display:block'
|
|
|
|
hideLoading()
|
|
|
|
}
|
|
|
|
|
2023-06-08 21:30:28 +01:00
|
|
|
async function findByHeadcode() {
|
|
|
|
showLoading()
|
|
|
|
const formData = await fetchHeadcode()
|
|
|
|
window.location.assign(`./train-detail.html?headcode=${formData}`)
|
|
|
|
}
|
|
|
|
|
2023-05-24 20:52:55 +01:00
|
|
|
async function findByPisCode() {
|
|
|
|
showLoading()
|
|
|
|
const formData = await fetchPisCode()
|
|
|
|
log(`findByPisCode: Searching for PIS Code ${formData}`)
|
|
|
|
const endpoint = `pis/code/${formData}`
|
|
|
|
console.time('findByPisCode-GET')
|
|
|
|
const json = await getApi(endpoint, auth = true)
|
|
|
|
console.timeEnd('findByPisCode-GET')
|
|
|
|
if (json == false) {
|
|
|
|
await displayNoData()
|
|
|
|
} else if (json == 401) {
|
|
|
|
await displayUnauthorised()
|
|
|
|
} else {
|
|
|
|
await insertData(json)
|
|
|
|
}
|
|
|
|
document.getElementById('crs-box').style = 'display:none'
|
|
|
|
document.getElementById('result-box').style = 'display:block'
|
|
|
|
hideLoading()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findRandomCode() {
|
|
|
|
showLoading()
|
|
|
|
log('findRandomCode: Fetching random PIS codes')
|
2023-05-25 21:39:22 +01:00
|
|
|
const endpoint = 'pis/code/random'
|
2023-05-24 20:52:55 +01:00
|
|
|
console.time('findRandomCode-GET')
|
|
|
|
const json = await getApi(endpoint, auth = true)
|
|
|
|
console.timeEnd('findRandomCode-GET')
|
|
|
|
if (json == false) {
|
|
|
|
await displayNoData()
|
|
|
|
} else if (json == 401) {
|
|
|
|
await displayUnauthorised()
|
|
|
|
} else {
|
|
|
|
await insertData(json)
|
|
|
|
}
|
|
|
|
document.getElementById('crs-box').style = 'display:none'
|
|
|
|
document.getElementById('result-box').style = 'display:block'
|
|
|
|
hideLoading()
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:53:43 +01:00
|
|
|
async function fetchOrigDest() {
|
|
|
|
var orig = document.getElementById('origin').value
|
|
|
|
var dest = document.getElementById('destination').value
|
|
|
|
return {origin: orig, destination: dest}
|
|
|
|
}
|
|
|
|
|
2023-05-24 20:52:55 +01:00
|
|
|
async function fetchPisCode() {
|
|
|
|
return document.getElementById('pis').value
|
|
|
|
}
|
|
|
|
|
2023-06-08 21:30:28 +01:00
|
|
|
async function fetchHeadcode() {
|
|
|
|
return document.getElementById('headcode').value
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:53:43 +01:00
|
|
|
async function insertData(json) {
|
|
|
|
// Receives the JSON Respose ([{},{}]) containing one or more possible
|
|
|
|
// PIS codes. Display the code and the stops with a method of scrolling between them.
|
|
|
|
// Maybe as a table or a carousel?
|
|
|
|
console.time('insertData')
|
|
|
|
const div = document.getElementById('result-box')
|
|
|
|
let tableData = `<table id="result-table">
|
|
|
|
<tr>
|
|
|
|
<th>Code</th>
|
|
|
|
<th>Stations</th>
|
|
|
|
</tr>`
|
|
|
|
let results = 0
|
|
|
|
for(var i = 0; i < json.length; i++) { // Hopefully can style output with CSS
|
|
|
|
tableData += `<tr><td class="pis-code">${json[i]['code']}</td>
|
|
|
|
<td class="station">${json[i]['stops'].join(', ')}</td></tr>`
|
|
|
|
results++
|
|
|
|
}
|
|
|
|
tableData += '</table>'
|
|
|
|
div.insertAdjacentHTML('beforeend', tableData)
|
|
|
|
document.getElementById('result-count').textContent = results.toString()
|
|
|
|
console.timeEnd('insertData')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function displayNoData() {
|
|
|
|
const msg = '<p id="result-table">No results found</p>'
|
|
|
|
document.getElementById('result-box').insertAdjacentHTML('beforeend', msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function displayUnauthorised() {
|
2023-05-17 21:13:20 +01:00
|
|
|
const msg = '<p id="result-table">Unauthorised - please ensure you are logged into the <a href="../settings.html#railstaff">rail staff version</a></p>'
|
2023-05-06 21:53:43 +01:00
|
|
|
document.getElementById('result-box').insertAdjacentHTML('beforeend', msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function reset() {
|
|
|
|
try {
|
|
|
|
document.getElementById('origin').value = ''
|
|
|
|
document.getElementById('destination').value = ''
|
2023-05-24 20:52:55 +01:00
|
|
|
document.getElementById('pis').value = ''
|
2023-05-06 21:53:43 +01:00
|
|
|
document.getElementById('result-box').style = 'display:none'
|
|
|
|
document.getElementById('result-table').remove()
|
|
|
|
document.getElementById('crs-box').style = 'display:block'
|
|
|
|
document.getElementById('result-count').textContent = 0
|
|
|
|
} catch(e) {
|
|
|
|
log('Nothing to reset')
|
|
|
|
}
|
|
|
|
}
|