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/lib.board.js

261 lines
7.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-unused-vars */
2023-02-09 20:29:07 +00:00
/* Fetch Functions */
async function publicLdb(stn) {
2023-07-01 22:17:07 +01:00
var url = `${apiEndpoint}/v1/ldb/${stn}`
console.time('Time: Fetch LDB Data')
var resp = await fetch(url)
console.timeEnd('Time: Fetch LDB Data')
return await resp.json()
2023-02-09 20:29:07 +00:00
}
/* Set page heading */
async function setHeaders(title,time) {
var prefix = 'OwlBoard - '
document.title = `${prefix}${title}`
document.getElementById('stn_name').textContent = title
document.getElementById('fetch_time').textContent = time.toLocaleTimeString()
sessionStorage.setItem('board_location', title)
2023-02-09 20:29:07 +00:00
}
/* Display No Trains Message */
async function displayNoTrains() {
log('No Trains', 'WARN')
document.getElementById('no_services').style = 'display: block;'
hideLoading()
2023-02-09 20:29:07 +00:00
}
/* Parse the value of `platform` to account for unknown platforms */
async function parsePlatform(svc){
let platform, changed
if (svc.platform != undefined) {
platform = svc.platform
} else {
platform = '-'
}
if (svc.platformChanged) { // Not present in public API, ready for staff version.
changed = 'changed'
} else {
changed = ''
}
return {num: platform, change: changed}
2023-02-09 20:29:07 +00:00
}
/* Change value of time strings to fit well on small screens */
async function parseTime(string){
let output
let change
switch (string) {
case 'Delayed':
output = 'LATE'
change = 'changed'
break
case 'Cancelled':
output = 'CANC'
change = 'cancelled'
break
case 'On time':
output = 'RT'
2023-05-08 21:44:04 +01:00
change = ''
break
case '':
output = '-'
change = ''
break
case undefined:
output = '-'
change = ''
break
case 'No report':
output = '-'
change = ''
break
case 'undefined':
output = false
change = ''
break
default:
output = string
change = 'changed'
}
return {data: output, changed: change}
2023-02-09 20:29:07 +00:00
}
/* Convert multiple Origin/Destinations to single string */
async function parseName(location) {
if (Array.isArray(location)) {
var name = `${location[0]['locationName']} & ${location[1]['locationName']}`
return name
}
else {
return location.locationName
}
2023-02-09 20:29:07 +00:00
}
// Display Alert Messages
async function displayAlerts(array) {
var counter = 0
var messages = ''
for(var i = 0; i < array.length; i++) {
// Increment counter
counter += 1
// Reset Vars
messages += `<p>${array[i]}</p>`
}
if (counter > 0) {
document.getElementById('alerts_msg').insertAdjacentHTML('beforeend', messages)
document.getElementById('alerts').style = 'display:block'
document.getElementById('alerts_bar').style = 'display:block'
if (counter == 1) {
document.getElementById('alert_bar_note').textContent = `There is ${counter} active alert`
} else if (counter > 1) {
document.getElementById('alert_bar_note').textContent = `There are ${counter} active alerts`
2023-02-09 20:29:07 +00:00
}
return true
}
return false
2023-02-09 20:29:07 +00:00
}
/* Show/Hide alerts box */
async function inflateAlerts() {
document.getElementById('alerts_msg').style = 'display:block;'
document.getElementById('alert_expand_arrow').style = 'transform: rotate(180deg);'
document.getElementById('alerts_bar').setAttribute('onclick', 'deflateAlerts()')
2023-02-09 20:29:07 +00:00
}
async function deflateAlerts() {
document.getElementById('alerts_msg').style = 'display.none;'
document.getElementById('alert_expand_arrow').style = 'transform: rotate(0deg);'
document.getElementById('alerts_bar').setAttribute('onclick', 'inflateAlerts()')
2023-02-09 20:29:07 +00:00
}
/*//// SERVICE DETAIL LISTS ////*/
// Build calling list: -- This outputs calling point data to sessionStorage in the format: key{pre: [{PREVIOUS_Stops}], post: [{POST_STOPS}]}
async function buildCallLists(svc) {
var sSvcId = svc.serviceID
var oSvcData = {
plat: svc.platform,
sta: svc.sta,
eta: svc.eta,
std: svc.std,
etd: svc.etd
}
try {
if (typeof svc.previousCallingPoints.callingPointList.callingPoint != 'undefined') {
let array = await makeArray(svc.previousCallingPoints.callingPointList.callingPoint)
oSvcData.pre = array
}
} catch (err) { /* Do nothing if ERR */ }
try {
if (typeof svc.subsequentCallingPoints.callingPointList.callingPoint != 'undefined') {
let array = await makeArray(svc.subsequentCallingPoints.callingPointList.callingPoint)
oSvcData.post = array
}
} catch (err) { /* Do nothing if ERR */ }
sessionStorage.setItem(sSvcId, JSON.stringify(oSvcData))
2023-02-09 20:29:07 +00:00
}
/* Display calling list: - Read data from sessionStorage and write to DOM. */
async function showCalls(id) {
log(`Showing details for service ${id}`, 'INFO')
var svcDetail = await JSON.parse(sessionStorage.getItem(id))
var pre = ''
var post = ''
if (typeof svcDetail.pre != 'undefined') {
for(var preCall = 0; preCall < svcDetail.pre.length; preCall++) {
pre += await singleCall(svcDetail.pre[preCall])
2023-02-09 20:29:07 +00:00
}
}
if (typeof svcDetail.post != 'undefined') {
for(var postCall = 0; postCall < svcDetail.post.length; postCall++) {
post += await singleCall(svcDetail.post[postCall])
2023-02-09 20:29:07 +00:00
}
}
/* Run retreived data through parsers */
var thisStd = await parseTime(svcDetail.std)
var thisEtd = await parseTime(svcDetail.etd)
var thisSta = await parseTime(svcDetail.sta)
var thisEta = await parseTime(svcDetail.eta)
/* Prepare data for this station */
let sTime
let eTime
let change
if (thisStd.data != '-') {
sTime = `${thisStd.data}`
eTime = `${thisEtd.data}`
change = thisEtd.changed
} else {
sTime = `${thisSta.data}`
eTime = `${thisEta.data}`
change = thisEta.changed
}
let here = `<tr>
<td class="detail-name detail-name-here detail-table-content">${sessionStorage.getItem('board_location')}</td>
2023-02-09 20:29:07 +00:00
<td class="detail-table-content">${sTime}</td>
<td class="detail-table-content ${change}">${eTime}</td>
</tr> `
/* Prepare then insert DOM Data */
let dom = ` <div id="${id}" class="call-data">
2023-02-09 20:29:07 +00:00
<p class="close-data" onclick="hideCalls('${id}')">X</p>
2023-05-08 22:03:07 +01:00
<p class="call-table-description">Click on a stations name to go to it's departure board</p>
2023-02-09 20:29:07 +00:00
<table class="call-table">
<tr>
<th class="detail-name-head">Location</th>
<th class="time">Schedule</th>
<th class="time">Act/Est</th>
</tr>
${pre}
${here}
${post}
</table>
</div>`
document.body.insertAdjacentHTML('beforeend', dom)
document.getElementById(id).style = 'display: block;'
return
2023-02-09 20:29:07 +00:00
}
async function hideCalls(id) {
let element = document.getElementById(id)
element.style = 'display: none;'
element.remove()
return
2023-02-09 20:29:07 +00:00
}
/* Builds the train data information in to a table row */
async function singleCall(data) {
let link = `./board.html?stn=${data.crs}`
let time
if (typeof data.et != 'undefined') {
time = await parseTime(data.et)
} else if (typeof data.at != 'undefined') {
time = await parseTime(data.at)
}
return `<tr>
2023-05-08 21:44:04 +01:00
<td class="detail-name detail-table-content"><a href="${link}">${data.locationName}</a></td>
2023-02-09 20:29:07 +00:00
<td class="detail-table-content">${data.st}</td>
<td class="detail-table-content ${time.changed}">${time.data}</td>
</tr>`
}
/* Error Handler */
async function errorHandler() {
let errCount
if (sessionStorage.getItem('failcount')) {
errCount = parseInt(sessionStorage.getItem('failcount'))
} else {
errCount = 0
}
errCount += 1
sessionStorage.setItem('failcount', errCount.toString())
if (errCount < 10){
await delay(3000)
vibe('err')
location.reload()
} else {
sessionStorage.removeItem('failcount')
window.location.assign('conn-err.html')
}
2023-02-09 20:29:07 +00:00
}