-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_out.js
More file actions
340 lines (300 loc) · 13.2 KB
/
script_out.js
File metadata and controls
340 lines (300 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('loading').textContent = 'Loading...';
// Fetch all stops from our server relay and build a code-to-name lookup
fetch('/api/stops')
.then(res => res.json())
.then(stopsData => {
// Build a lookup: code (uppercased) => name
const codeToName = {};
if (stopsData && stopsData.Stations && Array.isArray(stopsData.Stations.Station)) {
stopsData.Stations.Station.forEach(station => {
// Use two-letter code if possible, else LocationCode
// We'll try to match OA, UN, etc. to LocationName
if (station.LocationName && station.LocationCode) {
// Some LocationCodes are numbers, but for GO stations, the two-letter code is often in the name (e.g., "Oakville GO")
// We'll map by uppercase code if possible
// You may want to enhance this mapping if you have a better code source
codeToName[station.LocationCode.toUpperCase()] = station.LocationName;
}
});
}
// Function to fetch journeys starting from the current time and render
function getYYYYMMDD(d) {
const yyyy = d.getFullYear();
const mm = String(d.getMonth() + 1).padStart(2, '0');
const dd = String(d.getDate()).padStart(2, '0');
return `${yyyy}${mm}${dd}`;
}
function getHHMM(d) {
return String(d.getHours()).padStart(2, '0') + String(d.getMinutes()).padStart(2, '0');
}
function parseTimeString(t) {
if (!t) return NaN;
if (typeof t !== 'string') return NaN;
if (t.includes(':')) {
// assume HH:MM or H:MM
const parts = t.split(':');
const hh = parseInt(parts[0], 10);
const mm = parseInt(parts[1], 10);
return hh * 60 + mm;
}
// assume HHMM
if (t.length === 4 && /^\d{4}$/.test(t)) {
const hh = parseInt(t.slice(0, 2), 10);
const mm = parseInt(t.slice(2), 10);
return hh * 60 + mm;
}
return NaN;
}
function computeDelayMinutes(scheduledStr, actualStr) {
const s = parseTimeString(scheduledStr);
const a = parseTimeString(actualStr);
if (isNaN(s) || isNaN(a)) return null;
return a - s; // minutes (positive = delayed)
}
// track currently selected station code (default to 'union')
let selectedStationCode = 'union';
// helper to update selected station (called from button clicks)
function setSelectedStation(code) {
selectedStationCode = code;
// update button styles
document.querySelectorAll('.station-btn').forEach(btn => {
btn.classList.toggle('selected', btn.dataset.code === code);
});
// update SVG circle visuals (fill white for selected, restore others)
try {
const svg = document.getElementById('inlineLineSvg');
if (svg) {
svg.querySelectorAll('circle').forEach(c => {
if (c.id === code) {
c.setAttribute('data-selected', 'true');
c.setAttribute('fill', 'white');
} else {
c.removeAttribute('data-selected');
// restore default fill for unselected circles
// default colour used in the SVG is #2A4D28 for most circles,
// and one circle uses white with stroke — we won't change stroke here.
if (c.id === 'port_credit') {
// port_credit originally had white fill and a stroke
c.setAttribute('fill', 'white');
} else {
c.setAttribute('fill', '#2A4D28');
}
}
});
}
} catch (e) {
console.warn('Error updating inline SVG selection', e);
}
}
// wire station buttons after DOM ready
function wireStationButtons() {
document.querySelectorAll('.station-btn').forEach(btn => {
btn.addEventListener('click', () => {
const code = btn.dataset.code;
setSelectedStation(code);
// after selecting, refresh journeys to use new destination
fetchAndRenderJourneys();
});
});
}
// Inline the SVG so we can modify its elements. Replace <img id="lineSvgImg"> with inline SVG contents
async function inlineSvg() {
const img = document.getElementById('lineSvgImg');
if (!img) return;
try {
const res = await fetch(img.src);
const text = await res.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'image/svg+xml');
const svg = doc.querySelector('svg');
if (!svg) return;
// give the svg an id for easier selection
svg.id = 'inlineLineSvg';
// replace image with inline svg
img.replaceWith(svg);
} catch (e) {
console.error('Failed to inline SVG', e);
}
}
// initially wire buttons and inline svg
inlineSvg().then(() => {
wireStationButtons();
// set initial selection to union
setSelectedStation(selectedStationCode);
});
// map from our data-code to the two-letter Metrolinx API code
const twoLetterOverrides = {
// explicit overrides if the simple 'first two letters' rule doesn't match API codes
// 'port_credit': 'pc', // example if needed
};
function toApiCode(stationCode) {
if (!stationCode) return stationCode;
if (twoLetterOverrides[stationCode]) return twoLetterOverrides[stationCode];
// derive from the station name token before underscore (e.g., port_credit -> port)
const token = stationCode.split('_')[0];
// take first two letters
return token.slice(0, 2).toLowerCase();
}
async function fetchAndRenderJourneys() {
const now = new Date();
const dateStr = getYYYYMMDD(now);
const start = getHHMM(now);
const max = 12; // show next 12 departures
// convert our selectedStationCode to the API's two-letter code
const toCode = toApiCode(selectedStationCode);
const url = `/api/journeys?date=${dateStr}&from=oa&to=${toCode}&start=${start}&max=${max}`;
try {
const res = await fetch(url);
const data = await res.json();
document.getElementById('loading').style.display = 'none';
const departuresDiv = document.getElementById('departures');
departuresDiv.innerHTML = '';
if (
!data ||
!data.SchJourneys ||
!Array.isArray(data.SchJourneys) ||
data.SchJourneys.length === 0 ||
!data.SchJourneys[0].Services ||
!Array.isArray(data.SchJourneys[0].Services) ||
data.SchJourneys[0].Services.length === 0
) {
departuresDiv.innerHTML = '<p>No departures found.</p>';
return;
}
// table
const table = document.createElement('table');
table.id = 'departuresTable';
const thead = table.createTHead();
const headerRow = thead.insertRow();
['Destination', 'Departure', 'Status', 'Arrival', 'To', 'Duration'].forEach(h => {
const th = document.createElement('th');
th.textContent = h;
headerRow.appendChild(th);
});
const tbody = document.createElement('tbody');
table.appendChild(tbody);
data.SchJourneys[0].Services.forEach(service => {
const trips = service.Trips && service.Trips.Trip ? service.Trips.Trip : [];
trips.forEach(trip => {
const stops = trip.Stops && trip.Stops.Stop ? trip.Stops.Stop : [];
const departStop = stops[0];
const arriveStop = stops[stops.length - 1];
const departName = departStop && departStop.Code ? (codeToName[departStop.Code.toUpperCase()] || departStop.Code) : 'N/A';
const arriveName = arriveStop && arriveStop.Code ? (codeToName[arriveStop.Code.toUpperCase()] || arriveStop.Code) : 'N/A';
const tr = document.createElement('tr');
const tdTrip = document.createElement('td');
tdTrip.innerHTML = `<strong>${trip.Display || ('Trip ' + (trip.Number || ''))}</strong>`;
tr.appendChild(tdTrip);
const tdDep = document.createElement('td');
const depSpan = document.createElement('span');
depSpan.className = 'time-depart';
depSpan.textContent = departStop?.Time || 'N/A';
tdDep.appendChild(depSpan);
tr.appendChild(tdDep);
// status/delay detection
const tdStatus = document.createElement('td');
let statusText = 'On time';
let statusClass = 'status-on';
// Detect cancellation
if (trip?.Cancelled || trip?.IsCancelled || service?.Cancelled) {
statusText = 'Cancelled';
statusClass = 'status-cancelled';
} else {
// try to find actual/estimated time fields
const actual = departStop?.AdjustedTime || departStop?.EstimatedTime || departStop?.ActualTime || departStop?.TimeRT || departStop?.Realtime || departStop?.RealtimeEstimated;
const scheduled = departStop?.Time;
const delay = computeDelayMinutes(scheduled, actual);
if (delay !== null) {
if (delay > 0) {
statusText = `Delayed ${delay}m`;
statusClass = 'status-delayed';
} else if (delay < 0) {
statusText = `Early ${Math.abs(delay)}m`;
statusClass = 'status-early';
} else {
statusText = 'On time';
statusClass = 'status-on';
}
} else if (trip?.Status) {
statusText = trip.Status;
}
}
tdStatus.textContent = statusText;
tdStatus.className = statusClass;
tr.appendChild(tdStatus);
// const tdFrom = document.createElement('td');
// tdFrom.textContent = departName;
// tr.appendChild(tdFrom);
const tdArr = document.createElement('td');
const arrSpan = document.createElement('span');
arrSpan.className = 'time-arrive';
arrSpan.textContent = arriveStop?.Time || 'N/A';
tdArr.appendChild(arrSpan);
tr.appendChild(tdArr);
const tdTo = document.createElement('td');
tdTo.textContent = arriveName;
tr.appendChild(tdTo);
const tdDur = document.createElement('td');
tdDur.textContent = service.Duration || '';
tr.appendChild(tdDur);
tbody.appendChild(tr);
});
});
departuresDiv.appendChild(table);
} catch (err) {
document.getElementById('loading').textContent = 'Error loading departures.';
console.error('Error fetching journey data:', err);
}
}
// initial fetch and poll every 60 seconds
fetchAndRenderJourneys();
setInterval(fetchAndRenderJourneys, 60 * 1000);
})
.catch(err => {
document.getElementById('loading').textContent = 'Error loading stops.';
console.error('Error fetching stops data:', err);
});
// Live clock: update #liveClock every second with user's local time
const liveClockEl = document.getElementById('liveClock');
const clockToggle = document.getElementById('clockToggle');
// Load saved preference ("24" or "12"). Default to 12h.
let clockFormat = localStorage.getItem('clockFormat') || '12';
function applyToggleState() {
if (!clockToggle) return;
// use class-based state instead of aria-pressed
clockToggle.classList.toggle('pressed', clockFormat === '24');
clockToggle.textContent = clockFormat === '24' ? '24H' : '12H';
}
function formatTime(date) {
if (!date) return '';
if (clockFormat === '24') {
// 24-hour, ensure zero-padded hours: 00-23
const hh = String(date.getHours()).padStart(2, '0');
const mm = String(date.getMinutes()).padStart(2, '0');
const ss = String(date.getSeconds()).padStart(2, '0');
return `${hh}:${mm}:${ss}`;
} else {
// 12-hour w/ AM/PM
const opts = { hour: 'numeric', minute: '2-digit', second: '2-digit' };
return date.toLocaleTimeString([], opts);
}
}
if (clockToggle) {
clockToggle.addEventListener('click', () => {
clockFormat = clockFormat === '24' ? '12' : '24';
localStorage.setItem('clockFormat', clockFormat);
applyToggleState();
// update immediately
if (liveClockEl) liveClockEl.textContent = formatTime(new Date());
});
}
applyToggleState();
function updateClock() {
if (!liveClockEl) return;
liveClockEl.textContent = formatTime(new Date());
}
updateClock();
setInterval(updateClock, 1000);
});
// End of script: all fetching handled inside DOMContentLoaded handler above