/* Fetch Functions */ async function publicLdb(stn) { var url = `${window.location.origin}/api/v1/ldb/${stn}`; var resp = await fetch(url); return await resp.json(); } /* 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() } /* Display No Trains Message */ async function displayNoTrains() { document.getElementById('no_services').style = "display: block;"; clearLoading(); } /* Parse the value of `platform` to account for unknown platforms */ async function parsePlatform(svc){ if (svc.platform != undefined) { var platform = svc.platform; } else { var platform = "-"; } if (svc.platformChanged) { // Not present in public API, ready for staff version. var changed = "changed"; } else { var changed = ""; } return {num: platform, change: changed} } /* Change value of time strings to fit well on small screens */ async function parseTime(string){ switch (string) { case "Delayed": var output = "LATE"; var change = "changed"; break; case "Cancelled": var output = "CANC"; var change = "cancelled"; break; case "On time": var output = "RT"; var change = ""; break; case "": var output = "-"; var change = ""; break; case undefined: var output = "-"; var change = ""; break; case "No report": var output = "-"; var change = ""; break; default: var output = string; var change = "changed"; } return {data: output, changed: change}; } /* 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; } } // Build calling list: -- This is a string if only one callingPoint is present, needs adapting to work with that async function buildCallLists(data) { try { /* Check if previous calling points exist */ if (data.previousCallingPoints.callingPointList.callingPoint) { var preCallPoint = data.previousCallingPoints.callingPointList.callingPoint; } } catch (err) { /* Log if ERR */ log(`Error reading previous calling points: ${err}`, "INFO") } try { /* Check if subsequent calling points exist */ if (data.subsequentCallingPoints.callingPointList.callingPoint) { var postCallPoint = data.subsequentCallingPoints.callingPointList.callingPoint; } } catch (err) { /* Log if ERR */ log(`Error reading future calling points: ${err}`, "INFO") } var procPre = ""; var procPost = ""; try { if (preCallPoint) { for(var i = 0; i < preCallPoint.length; i++) { var proc = await buildCalls(preCallPoint[i]); procPre = `${procPre}\n${proc}` } } } catch (err) { console.log(`Error reading previous stops. ${err}`) } try { if (postCallPoint) { for (var i = 0; i < postCallPoint.length; i++) { var proc = await buildCalls(postCallPoint[i]); procPost = `${procPost}\n${proc}` } } } catch (err) { console.log(`Error reading next stops. ${err}`) } var div = `

X

${procPost}
Calling at:
Location Schedule Exp.
${procPre}
Previous stops:
Location Schedule Actual
` document.body.insertAdjacentHTML("beforeend", div) return; } // Display calling list: - The calling list should be built on demand. async function showCalls(id) { document.getElementById(id).style = "display: block;"; return; } async function hideCalls(id) { document.getElementById(id).style = "display: none;"; return; } // 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 += `

${array[i]}

`; } document.getElementById("alerts_msg").insertAdjacentHTML("beforeend", messages); 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` } document.getElementById("alerts").style = "display:block" document.getElementById("alerts_bar").style = "display:block" document.getElementById("output").style = "margin-top:95px" /* The margin for the train table needs to be adjusted if the alert box exists. */ } /* 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()") } 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()") } /* Builds the train data information in to a table row */ async function buildCalls(data) { if (typeof data.et != "undefined") { var time = await parseTime(data.et) } else if (typeof data.at != "undefined") { var time = await parseTime(data.at) } return ` ${data.locationName} ${data.st} ${time.data} ` }