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/tiploc-finder.js

91 lines
3.6 KiB
JavaScript

const url = "https://tiger.worldline.global/";
const view = "/staff;scrollbar=true";
console.group("Athena - Departure Boards")
console.info("Initialising");
/* Get lookup data from JSON file */
let dataGlobal;
const getData = async () => {
console.info("Loading Tiger lookup file");
// Probably put a loading wheel here!
const response = await fetch("./assets/tiger_codes.json");
const data = await response.json();
dataGlobal = data;
return data;
};
(async () => {
await getData();
console.info("Lookup file loaded");
console.info(dataGlobal);
console.info("Initialisation complete");
// Probably end the loading wheel here.
console.groupEnd();
})();
/* Read Lookup textbox entry and call the gotoInfoBoard() function */
function getTextEntry() {
console.group("Athena: function: getTextEntry()");
console.info("Getting contents of textbox 'crs-lookup'");
let forLookup = document.getElementById("crs-lookup").value;
console.info("You have entered: " + forLookup.toUpperCase());
console.info("Calling gotoInfoBoard(" + forLookup.toLowerCase() + ")");
console.groupEnd()
gotoInfoBoard(forLookup);
};
/* Runs basic validation of data type and then process that and finally
redirects to Tiger using a built URL */
function gotoInfoBoard(station) {
// Firstly start a console group for the function
console.group("Athena: function: gotoInfoBoard(" + station.toLowerCase() + ")");
//Determins if code is TIPLOC and if so redirects immediately
if (station.length > 3 && station.length < 8) {
console.info(station.length + "characters, presumed to be tiploc.");
console.info("Redirecting to " + station + " departure board");
window.location = url + station.toUpperCase() + view;
//If code is CRS then process further to determine the TIPLOC then redirect
} else if (station.length == 3) {
console.info("CRS entered: " + station.toUpperCase() + ", required lookup");
console.info("Finding object");
let crs_to_find = station.toLowerCase();
console.debug("Converted string to lower case:" + crs_to_find);
//Check if the entered CRS exists in the lookup file
console.info("Checking if CRS code exists in lookup data")
let hasKey = dataGlobal.hasOwnProperty(crs_to_find);
if (hasKey) { //If key exists then log and continue
console.info(crs_to_find.toUpperCase() + " does exist in lookup file");
} else { //If key does not exist then stop further processing
console.warn(crs_to_find.toUpperCase() + "does not exist in lookup file");
console.error(crs_to_find.toUpperCase() + " is an incorrect code or not supported.");
alert(crs_to_find.toUpperCase() + " is not a valid or supported CRS, " +
"please see the help section for more information");
console.groupEnd();
return 0;
}
let crs_obj = dataGlobal[crs_to_find];
console.info("Isolating TIPLOC from Object");
let tiploc = crs_obj['tiploc']; // crs_obj['tiploc']??
console.info("TIPLOC: " + tiploc);
console.info("Redirecting to " + tiploc + " departure board");
window.location = url + tiploc.toUpperCase() + view;
//If the code cannot be validated as CRS or TIPLOC then throw an alert and log
} else {
console.error("Please only enter:\n - A 3 character CRS code,\n - A 4-7 " +
"character TIPLOC code.\n\nPartial codes or station names cannot be looked "+
"up.");
alert("Please only enter:\n - A 3 character CRS code,\n - A 4-7 " +
"character TIPLOC code.\n\nPartial codes or station names cannot be looked "+
"up.");
}
// Lastly close the log group at the end of the Function and return.
console.groupEnd();
return 0;
}