avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 1 | var scheduleController = { |
| 2 | maxSchedules: 6, |
| 3 | recommendedSchedulesPerLine: 2, |
| 4 | fetchInterval: 8*60*1000, |
| 5 | _times: [], |
| 6 | _queueTimes: [], |
| 7 | _lastTime: -1, |
| 8 | _lastDestinations: [], |
| 9 | _numRoutes: -1, |
| 10 | _stop: -1, |
| 11 | init: function() { |
| 12 | scheduleController.setStop(); |
| 13 | scheduleController.removeDummy(); |
| 14 | scheduleController.fetchTimes(); |
| 15 | scheduleController.timer(); |
| 16 | }, |
| 17 | setStop: function() { |
| 18 | var params = new URLSearchParams(window.location.search); |
| 19 | scheduleController._stop = params.get("station"); |
| 20 | }, |
| 21 | isOldView: function() { |
| 22 | return document.body.classList.contains("view-old"); |
| 23 | }, |
| 24 | numSchedules: function() { |
| 25 | return Math.min(scheduleController.maxSchedules, 2 * scheduleController.recommendedSchedulesPerLine * scheduleController._numRoutes); |
| 26 | }, |
| 27 | numSchedulesDetailed: function() { |
| 28 | return Math.ceil(scheduleController.numSchedules() / 2); |
| 29 | }, |
| 30 | isDetailed: function(i) { |
| 31 | return (i < scheduleController.numSchedulesDetailed()); |
| 32 | }, |
| 33 | removeElement: function(el) { |
| 34 | el.parentNode.removeChild(el); |
| 35 | }, |
| 36 | removeDummy: function() { |
| 37 | document.getElementById("trains").innerHTML = ""; |
| 38 | }, |
avm99963 | 0c4c7d0 | 2020-01-29 00:25:12 +0100 | [diff] [blame] | 39 | getTime: function() { |
| 40 | return Math.floor((new Date()).getTime()/1000); |
| 41 | }, |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 42 | prettyTime: function(seconds, detailed = true) { |
| 43 | if (detailed) return (Math.floor(seconds/60)).toString().padStart(2, '0')+":"+(seconds % 60).toString().padStart(2, '0'); |
| 44 | return Math.floor(seconds/60)+"min"; |
| 45 | }, |
| 46 | addCurrentTime: function(data) { |
| 47 | var trains = document.getElementById("trains"); |
| 48 | |
| 49 | var train = document.createElement("div"); |
| 50 | train.setAttribute("class", "train"); |
| 51 | |
| 52 | var logo = document.createElement("div"); |
| 53 | logo.setAttribute("class", "logo"); |
| 54 | logo.style.backgroundColor = "#"+data.color; |
| 55 | logo.style.color = "#"+data.textColor; |
| 56 | logo.style.fontSize = ("calc("+((scheduleController.isOldView() ? 20 : 18)/data.route.length)+"*var(--unit-size))"); |
| 57 | logo.textContent = data.route; |
| 58 | train.appendChild(logo); |
| 59 | |
| 60 | var destination = document.createElement("div"); |
| 61 | destination.setAttribute("class", "destination"); |
| 62 | destination.textContent = data.destination; |
| 63 | train.appendChild(destination); |
| 64 | |
| 65 | var time = document.createElement("div"); |
| 66 | time.setAttribute("class", "time"); |
| 67 | train.appendChild(time); |
| 68 | |
| 69 | trains.appendChild(train); |
| 70 | |
| 71 | data._element = time; |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 72 | scheduleController._times.push(data); |
| 73 | }, |
| 74 | addTime: function(data) { |
| 75 | if (data.departureTime > scheduleController._lastTime) { |
| 76 | scheduleController._lastTime = data.departureTime; |
| 77 | scheduleController._lastDestinations = [data.destination]; |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame] | 78 | } else if (data.departureTime == scheduleController._lastTime) { |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 79 | var flag = false; |
| 80 | scheduleController._lastDestinations.forEach(dest => { |
| 81 | if (data.destination == dest) flag = true; |
| 82 | }); |
| 83 | |
| 84 | if (flag) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | scheduleController._lastDestinations.push(dest); |
| 89 | } else return; |
| 90 | |
| 91 | if (scheduleController._times.length < scheduleController.numSchedules()) { |
| 92 | scheduleController.addCurrentTime(data); |
| 93 | } else { |
| 94 | scheduleController._queueTimes.push(data); |
| 95 | } |
| 96 | }, |
| 97 | fetchTimes: function() { |
| 98 | fetch("ajax/api.php?action=getTimes&stop="+scheduleController._stop).then(res => { |
| 99 | return res.json(); |
| 100 | }).then(json => { |
| 101 | if (json.status != "ok") throw Error("Not ok"); |
| 102 | |
| 103 | scheduleController._numRoutes = json.data.numRoutes; |
| 104 | |
| 105 | json.data.schedules.forEach(data => { |
| 106 | scheduleController.addTime(data); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | setTimeout(scheduleController.fetchTimes, scheduleController.fetchInterval); |
| 111 | }, |
| 112 | timer: function() { |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame] | 113 | var now = scheduleController.getTime(); |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 114 | |
| 115 | var removed = 0; |
| 116 | for (var i = 0;; ++i) { |
| 117 | if (scheduleController._times[i - removed] === undefined) break; |
| 118 | |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame] | 119 | if (scheduleController._times[i - removed].departureTime < now) { |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 120 | scheduleController.removeElement(scheduleController._times[i - removed]._element.parentNode); |
| 121 | scheduleController._times.shift(); |
| 122 | ++removed; |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | for (var i = 0; i < removed; ++i) { |
| 127 | if (scheduleController._queueTimes.length > 0) { |
| 128 | scheduleController.addCurrentTime(scheduleController._queueTimes.shift()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | var i = 0; |
| 133 | scheduleController._times.forEach(time => { |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame] | 134 | var diff = time.departureTime - now; |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 135 | |
| 136 | time._element.textContent = scheduleController.prettyTime(diff, scheduleController.isDetailed(i)); |
| 137 | |
| 138 | ++i; |
| 139 | }); |
| 140 | |
| 141 | setTimeout(scheduleController.timer, (1000 - (new Date().getTime()) % 1000)); |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | window.addEventListener("load", _ => { |
| 146 | scheduleController.init(); |
| 147 | }); |