+
+
+
+ Route
From
diff --git a/view-train.js b/view-train.js index d8865e4..cd0c7ca 100644 --- a/view-train.js +++ b/view-train.js @@ -1,5 +1,6 @@ var trainNumber var date +var groupIndex = null var showKm = false @@ -7,7 +8,7 @@ var trainData = null var lastSuccessfulFetch = null function onTrainData(data) { - var title = document.getElementsByTagName('h1')[0] + var title = document.getElementById('title') title.textContent = '' title.appendChild(document.createTextNode('Train ')) var rankSpan = document.createElement('span') @@ -19,10 +20,80 @@ function onTrainData(data) { document.getElementById('company').textContent = data.operator document.getElementById('date').textContent = data.date - document.getElementById('route-from').textContent = data.route.from - document.getElementById('route-to').textContent = data.route.to + var group = null; + if (data.groups.length > 1 && groupIndex == null) { + document.getElementById('group-choice').classList.remove('hidden') + document.getElementById('group-choice').focus() + document.getElementById('train-info').classList.add('hidden') - if (data.status) { + document.getElementsByClassName('rsk')[0].textContent = '' + document.getElementsByClassName('csk')[0].textContent = 'Select' + + title.textContent = `Select Group for ${data.rank} ${data.number}` + + var gc = document.getElementById('group-choice') + while (gc.childNodes.length > 0) { + gc.childNodes[0].remove() + } + + for (var i = 0; i < data.groups.length; i++) { + var g = data.groups[i] + + var groupLi = document.createElement('li') + gc.append(groupLi) + groupLi.tabIndex = i + groupLi.classList.add('items') + if (i === currentIndex) { + groupLi.focus() + } + + (function (i) { + function onAction(e) { + var url = new URL(window.location.toString()) + groupIndex = i + url.searchParams.append('groupIndex', groupIndex) + window.history.pushState({'groupIndex': groupIndex}, '', url.toString( )) + onTrainData(data) + } + groupLi.addEventListener('click', onAction) + groupLi.addEventListener('keypress', function (e) { + if (e.key == 'Enter') { + onAction(e) + } + }) + })(i) + + var routeP = document.createElement('p') + groupLi.append(routeP) + routeP.classList.add('pri') + routeP.textContent = `${g.route.from} ➔ ${g.route.to}` + + var groupP = document.createElement('p') + groupLi.append(groupP) + groupP.classList.add('thi') + groupP.textContent = i === 0 ? 'Main train' : `Group ${i}` + + } + + return + } + else if (data.groups.length === 1) { + group = data.groups[0] + } + else { + group = data.groups[groupIndex] + } + document.getElementById('group-choice').classList.add('hidden') + document.getElementById('train-info').classList.remove('hidden') + document.getElementById('train-info').focus() + + document.getElementsByClassName('rsk')[0].textContent = 'Refresh' + document.getElementsByClassName('csk')[0].textContent = '' + + document.getElementById('route-from').textContent = group.route.from + document.getElementById('route-to').textContent = group.route.to + + if (group.status) { document.getElementById('status').classList.remove('hidden') var statusDelay = document.getElementById('status-delay') @@ -30,7 +101,7 @@ function onTrainData(data) { statusDelay.childNodes[0].remove() } var delayString = '' - var delayMinutes = data.status.delay + var delayMinutes = group.status.delay if (delayMinutes === 0) { delayString = 'On time' statusDelay.appendChild(document.createTextNode(delayString)) @@ -81,19 +152,19 @@ function onTrainData(data) { statusLocation.childNodes[0].remove() } var stateString = '' - if (data.status.state === 'arrival') { + if (group.status.state === 'arrival') { stateString += 'when arriving at ' } - else if (data.status.state === 'departure') { + else if (group.status.state === 'departure') { stateString += 'when departing from ' } - else if (data.status.state === 'passing') { + else if (group.status.state === 'passing') { stateString += 'while passing through ' } statusLocation.appendChild(document.createTextNode(stateString)) var stationSpan = document.createElement('span') statusLocation.appendChild(stationSpan) - stationSpan.textContent = data.status.station + stationSpan.textContent = group.status.station stationSpan.classList.add('station') } else { @@ -112,7 +183,7 @@ function onTrainData(data) { var stationsList = document.createElement('ul') stationsDiv.appendChild(stationsList) - data.stations.forEach(function (station) { + group.stations.forEach(function (station) { var stationItem = document.createElement('li') stationsList.appendChild(stationItem) stationItem.classList.add('stationItem') @@ -202,7 +273,7 @@ function refresh() { }, timeout || 60000) } return fetch( - `https://scraper.infotren.dcdev.ro/v2/train/${trainNumber}?date=${date.getFullYear().toString()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`, + `https://scraper.infotren.dcdev.ro/v3/trains/${trainNumber}?date=${date.getFullYear().toString()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`, { cache: 'no-store', }, @@ -235,6 +306,11 @@ function rsk() { refresh() } +window.addEventListener('popstate', function (e) { + groupIndex = null + refresh() +}) + window.addEventListener('load', function (e) { if (!new URL(window.location.href).searchParams.has('train')) { window.history.back() @@ -249,6 +325,7 @@ window.addEventListener('load', function (e) { trainNumber = sp.get('train') date = sp.has('date') ? new Date(sp.get('date')) : new Date() + groupIndex = sp.has('groupIndex') ? parseInt(sp.get('groupIndex')) : null document.querySelectorAll('.rsk').forEach(function (rskElem) { rskElem.addEventListener('click', function (e) { diff --git a/worker.js b/worker.js index 882fb19..f3abdec 100644 --- a/worker.js +++ b/worker.js @@ -20,4 +20,6 @@ const registerServiceWorker = async () => { } }; -registerServiceWorker(); +if (!/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) { + registerServiceWorker(); +}