Comment client side JS

This commit is contained in:
Fred Boniface 2023-04-05 16:10:21 +01:00
parent d6806bf3b5
commit 632031d453
4 changed files with 33 additions and 27 deletions

View File

@ -20,7 +20,7 @@ async function storageAvailable(type) { // Currently not used
}
}
async function versionDisplay() {
async function versionDisplay() { // Outputs version string on to any page with a tag with id="ver_str"
localStorage.setItem("version", version)
document.getElementById('ver_str').textContent = version
return;
@ -127,7 +127,7 @@ async function getQuery(param) {
}
}
async function vibe(type) {
async function vibe(type) { // Offers three standard vibration patterns for consistency
let canVibrate = "vibrate" in navigator || "mozVibrate" in navigator
if (canVibrate && !("vibrate" in navigator)){
navigator.vibrate = navigator.mozVibrate
@ -144,7 +144,7 @@ async function vibe(type) {
}
}
async function convertUnixLocal(unix) {
async function convertUnixLocal(unix) { // Convert unix time string to local
var jsTime = unix*1000
var dt = new Date(jsTime)
return dt.toLocaleString()

View File

@ -1,11 +1,16 @@
//
// Init:
//
// Setup quick links
const ql = ["ql0","ql1","ql2","ql3","ql4","ql5","ql6","ql7","ql8","ql9","ql10","ql11"]
storageAvailable("localStorage");
getQl();
hideLoading();
// Check if already registered
ifAlreadyRegistered();
// Hide loading
hideLoading();
async function ifAlreadyRegistered() {
async function ifAlreadyRegistered() { // If already registered, show this on the page
if (! await isRegistered()) { return } else {
document.getElementsByName("eml")[0].placeholder = "Registered";
document.getElementById("reg_text").textContent = "You are already registered";
@ -13,7 +18,7 @@ async function ifAlreadyRegistered() {
}
}
async function getQl(){
async function getQl(){ // Fetch Quick Links from localstorage
var qlOpt = await getQuickLinks()
if (qlOpt){
var i = 0
@ -26,8 +31,8 @@ async function getQl(){
}
}
async function setQl(){
await showLoading();
async function setQl(){ // Fetch Quick Links from text input and save to localstorage
await showLoading();// called as an onclick function
var qlSet = []
for (i in ql) {
var opt = document.getElementById(`ql${i}`).value
@ -45,7 +50,7 @@ async function setQl(){
hideDone();
}
async function clearQl(){
async function clearQl(){ // Clear Quick Links from localstorage
showLoading();
localStorage.removeItem("qlOpt")
log(`settings.setQl: User settings reset to default`, "INFO")
@ -57,7 +62,7 @@ async function clearQl(){
hideDone();
}
async function isRegistered() {
async function isRegistered() { // Check if a device is registered, returns BOOL
if (localStorage.getItem("uuid")) {
return true
// Also need an API Call here to check if auth is working.
@ -66,7 +71,7 @@ async function isRegistered() {
return false
}
async function register() {
async function register() { // Registers a device by sending POST request to API Server
if (! await isRegistered()) {
showLoading()
let url = `${window.location.origin}/api/v1/auth/request`;
@ -92,16 +97,16 @@ async function register() {
}
}
async function logout() {
async function logout() { // Simply removed the UUID from localstorage
localStorage.removeItem("uuid");
location.reload();
return
}
async function showDone() {
async function showDone() { // Diaplays the 'Done' dialogue.
document.getElementById("done").style = "opacity: 1";
}
async function hideDone() {
async function hideDone() { // Hides the 'Done' dialogue.
document.getElementById("done").style = "opacity: 0";
}

View File

@ -1,8 +1,9 @@
/* Page Init: */
// Run the init function at page load.
init()
/* Init function */
async function init() {
async function init() { // Gets query string and then fetch API response and pass to parsing function
console.time("Time: Init to Complete")
setLoadingDesc(`Loading\nservices`)
var stn = await getQuery("stn");
@ -87,7 +88,7 @@ async function buildPage(data) {
}
async function displayTrains(data) {
async function displayTrains(data) { // Iterated through train services and passes API sections to other functions
log(`simple-board.displayTrains: Inserting data in DOM`)
for(var i = 0; i < data.length; i++) {
// Reset Vars
@ -99,20 +100,20 @@ async function displayTrains(data) {
log(`simple-board.displayTrains: Insertion complete`)
}
async function displayFerry(ferrySvc) {
async function displayFerry(ferrySvc) { // Iterates through each ferry service and passes to another function
for(var i = 0; i < ferrySvc.length; i++) {
displayFerryService(ferrySvc[i])
}
}
async function displayBus(busSvc) {
async function displayBus(busSvc) { // Iterates through each bus service and passes to other functions.
for(var i = 0; i < busSvc.length; i++) {
displayBusService(busSvc[i])
buildCallLists(busSvc[i])
}
}
async function displayService(svc) {
async function displayService(svc) { // Creates a table row from each train service.
var table = document.getElementById("output");
// Determine Time Message
@ -138,17 +139,17 @@ async function displayService(svc) {
</table>`
// Put Table Row
table.insertAdjacentHTML("beforeend", row)
// Display Operator
// Display Operator where provided (it always will be, I think)
if (svc.operator) {
var opRow = `<p class="msg op">A ${svc.operator} service`
if (svc.length) {
if (svc.length) { // Displays number of carriages where provided
opRow += ` with ${svc.length} carriages</p>`;
} else {
opRow += `</p>`
}
table.insertAdjacentHTML("beforeend", opRow);
}
// Parse cancelReason & delayReason
// Parse cancelReason and then delayReason
if (svc.cancelReason) {
var cancelRow = `<p class="msg">${svc.cancelReason}</p>`
table.insertAdjacentHTML("beforeend", cancelRow);
@ -159,7 +160,7 @@ async function displayService(svc) {
}
}
async function displayFerryService(svc) {
async function displayFerryService(svc) { // Creates a table for for each ferry service
var table = document.getElementById("ferry");
log(JSON.stringify(svc))
// Determine Time Message
@ -196,7 +197,7 @@ async function displayFerryService(svc) {
document.getElementById("ferry").style = "display:block"
}
async function displayBusService(svc) {
async function displayBusService(svc) { // Creates a table row for each bus service.
var table = document.getElementById("bus");
log(JSON.stringify(svc))
// Determine Time Message

View File

@ -1,16 +1,16 @@
init();
async function init() {
async function init() { // The page init function
display(await get())
}
async function get() {
async function get() { // Fetch data from API
var url = `${window.location.origin}/api/v1/stats`;
var resp = await fetch(url);
return await resp.json();
}
async function display(data) {
async function display(data) { // Parses and displays data from API
document.getElementById('server_host').textContent = data.host;
document.getElementById('server_mode').textContent = data.mode || "Unknown";
document.getElementById('ver-bkend').textContent = data.verBkend || "Unknown";