/* eslint-disable no-unused-vars */ /* Fetch Functions */ async function publicLdb(stn) { var url = `${apiEndpoint}/v1/ldb/${stn}` console.time('Time: Fetch LDB Data') var resp = await fetch(url) console.timeEnd('Time: Fetch LDB Data') 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() sessionStorage.setItem('board_location', title) } /* Display No Trains Message */ async function displayNoTrains() { log('No Trains', 'WARN') document.getElementById('no_services').style = 'display: block;' hideLoading() } /* Parse the value of `platform` to account for unknown platforms */ async function parsePlatform(svc){ let platform, changed if (svc.platform != undefined) { platform = svc.platform } else { platform = '-' } if (svc.platformChanged) { // Not present in public API, ready for staff version. changed = 'changed' } else { changed = '' } return {num: platform, change: changed} } /* Change value of time strings to fit well on small screens */ async function parseTime(string){ let output let change switch (string) { case 'Delayed': output = 'LATE' change = 'changed' break case 'Cancelled': output = 'CANC' change = 'cancelled' break case 'On time': output = 'RT' change = '' break case '': output = '-' change = '' break case undefined: output = '-' change = '' break case 'No report': output = '-' change = '' break case 'undefined': output = false change = '' break default: output = string 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 } } // 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]}

` } if (counter > 0) { document.getElementById('alerts_msg').insertAdjacentHTML('beforeend', messages) document.getElementById('alerts').style = 'display:block' document.getElementById('alerts_bar').style = 'display:block' 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` } return true } return false } /* 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()') } /*//// SERVICE DETAIL LISTS ////*/ // Build calling list: -- This outputs calling point data to sessionStorage in the format: key{pre: [{PREVIOUS_Stops}], post: [{POST_STOPS}]} async function buildCallLists(svc) { var sSvcId = svc.serviceID var oSvcData = { plat: svc.platform, sta: svc.sta, eta: svc.eta, std: svc.std, etd: svc.etd } try { if (typeof svc.previousCallingPoints.callingPointList.callingPoint != 'undefined') { let array = await makeArray(svc.previousCallingPoints.callingPointList.callingPoint) oSvcData.pre = array } } catch (err) { /* Do nothing if ERR */ } try { if (typeof svc.subsequentCallingPoints.callingPointList.callingPoint != 'undefined') { let array = await makeArray(svc.subsequentCallingPoints.callingPointList.callingPoint) oSvcData.post = array } } catch (err) { /* Do nothing if ERR */ } sessionStorage.setItem(sSvcId, JSON.stringify(oSvcData)) } /* Display calling list: - Read data from sessionStorage and write to DOM. */ async function showCalls(id) { log(`Showing details for service ${id}`, 'INFO') var svcDetail = await JSON.parse(sessionStorage.getItem(id)) var pre = '' var post = '' if (typeof svcDetail.pre != 'undefined') { for(var preCall = 0; preCall < svcDetail.pre.length; preCall++) { pre += await singleCall(svcDetail.pre[preCall]) } } if (typeof svcDetail.post != 'undefined') { for(var postCall = 0; postCall < svcDetail.post.length; postCall++) { post += await singleCall(svcDetail.post[postCall]) } } /* Run retreived data through parsers */ var thisStd = await parseTime(svcDetail.std) var thisEtd = await parseTime(svcDetail.etd) var thisSta = await parseTime(svcDetail.sta) var thisEta = await parseTime(svcDetail.eta) /* Prepare data for this station */ let sTime let eTime let change if (thisStd.data != '-') { sTime = `${thisStd.data}` eTime = `${thisEtd.data}` change = thisEtd.changed } else { sTime = `${thisSta.data}` eTime = `${thisEta.data}` change = thisEta.changed } let here = ` ${sessionStorage.getItem('board_location')} ${sTime} ${eTime} ` /* Prepare then insert DOM Data */ let dom = `

X

Click on a stations name to go to it's departure board

${pre} ${here} ${post}
Location Schedule Act/Est
` document.body.insertAdjacentHTML('beforeend', dom) document.getElementById(id).style = 'display: block;' return } async function hideCalls(id) { let element = document.getElementById(id) element.style = 'display: none;' element.remove() return } /* Builds the train data information in to a table row */ async function singleCall(data) { let link = `./board.html?stn=${data.crs}` let time if (typeof data.et != 'undefined') { time = await parseTime(data.et) } else if (typeof data.at != 'undefined') { time = await parseTime(data.at) } return ` ${data.locationName} ${data.st} ${time.data} ` } /* Error Handler */ async function errorHandler() { let errCount if (sessionStorage.getItem('failcount')) { errCount = parseInt(sessionStorage.getItem('failcount')) } else { errCount = 0 } errCount += 1 sessionStorage.setItem('failcount', errCount.toString()) if (errCount < 10){ await delay(3000) vibe('err') location.reload() } else { sessionStorage.removeItem('failcount') window.location.assign('conn-err.html') } }