This repository has been archived on 2023-08-24. You can view files and clone it, but cannot push or open issues or pull requests.
OwlBoard/static/js/boards.js

210 lines
7.0 KiB
JavaScript
Raw Normal View History

// Set page headers
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()
2022-12-19 23:52:27 +00:00
}
// Determine what should display in 'platform' column
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}
}
// Use different time strings to default to make better 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 {
if (data.previousCallingPoints.callingPointList.callingPoint) {
var preCallPoint = data.previousCallingPoints.callingPointList.callingPoint;
}
} catch (err) {
console.log(`Data not found error, ${err}`)
}
try {
if (data.subsequentCallingPoints.callingPointList.callingPoint) {
var postCallPoint = data.subsequentCallingPoints.callingPointList.callingPoint;
}
} catch (err) {
console.log(`Data not found error, ${err}`)
}
var procPre = "";
var procPost = "";
try {
if (preCallPoint) {
for(var i = 0; i < preCallPoint.length; i++) {
var proc = await buildPastCalls(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 = `
<div class="call-data" id="${data.serviceID}">
<p class="close-data" onclick="hideCalls('${data.serviceID}')">X</p>
<table class="calling-at">
<caption>Calling at:</caption>
<tr>
<th class="detail-name-head">Location</th>
<th class="time">Schedule</th>
<th class="time">Exp.</th>
</tr>
${procPost}
</table>
<table class="called-at">
<caption>Previous stops:</caption>
<tr>
<th class="detail-name-head">Location</th>
<th class="time">Schedule</th>
<th class="time">Actual</th>
</tr>
${procPre}
</table>
</div>`
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 += `<p>${array[i]}</p>`;
}
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" // Adjust margin of train table div.
}
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()")
}
/*
Compress buildCalls() and buildPastCalls() into a single function
that can selectively work with either data.et or data.at.
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 `<tr>
<td class="detail-name detail-table-content">${data.locationName}</td>
<td class="detail-table-content">${data.st}</td>
<td class="detail-table-content ${time.changed}">${time.data}</td>
</tr>`
}
*/
async function buildCalls(data) {
var timeEt = await parseTime(data.et)
return `<tr>
<td class="detail-name detail-table-content">${data.locationName}</td>
<td class="detail-table-content">${data.st}</td>
<td class="detail-table-content ${timeEt.changed}">${timeEt.data}</td>
</tr>`
}
async function buildPastCalls(data) {
var timeEt = await parseTime(data.at)
return `<tr>
<td class="detail-name detail-table-content">${data.locationName}</td>
<td class="detail-table-content">${data.st}</td>
<td class="detail-table-content ${timeEt.changed}">${timeEt.data}</td>
</tr>`
2022-12-19 23:52:27 +00:00
}