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.
athena.fb-infra.uk/scripts/tiploc-finder.js

126 lines
5.5 KiB
JavaScript
Raw Normal View History

2022-09-08 21:05:49 +01:00
const url = "https://tiger.worldline.global/";
const view = "/staff;scrollbar=true";
console.group("Athena - v0.2.1-alpha")
2022-09-08 21:05:49 +01:00
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;
}
/*This function provides the Lookup TIPLOC from CRS function from the menu*/
function tiplocFinder() {
console.group("Athena: function: tiplocFinder()");
let crs_to_find = document.getElementById("lookup-tiploc").value;
console.info("You are trying to lookup the TIPLOC for:" + crs_to_find.toUpperCase());
console.info("Checking if CRS code exists in look up 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];
let name = crs_obj['name'].toUpperCase();
let crs = crs_to_find.toUpperCase();
let stanme = crs_obj['stanme'].toUpperCase();
let stanox = crs_obj['stanox'];
let tiploc = crs_obj['tiploc'];
console.info("Found Data for " + crs +": Name: " + name + ", STANME: " + stanme +
", STANOX: " + stanox + ", TIPLOC: " + tiploc);
console.warn("Attempt writing to DOM, feature incomplete");
document.getElementById("resultCRS").innerHTML = "CRS: " + crs_to_find.toUpperCase();
document.getElementById("resultTIPLOC").innerHTML = "TIPLOC: " + tiploc.toUpperCase();
document.getElementById("resultSTANME").innerHTML = "STANME: " + stanme.toUpperCase();
document.getElementById("resultName").innerHTML = "Name: " + name.toUpperCase();
document.getElementById("resultSTANOX").innerHTML = "STANOX: " + stanox;
console.info("Writing to DOM Complete")
console.groupEnd();
}