Skip to content

Commit db4d970

Browse files
committed
allow toggling of relative and absolute time for stop departures
1 parent e5956cb commit db4d970

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080
<button id="refresh-virtual-board" class="btn btn-sm btn-outline-primary" onclick="update_stop_times(Number(this.dataset.code))"><i class="bi bi-arrow-clockwise"></i> Обнови</button>
8181
<span id="last-updated" class="text-muted" data-timestamp=""></span>
8282
</p>
83+
<p class="mb-0">
84+
<input class="form-check-input" type="checkbox" id="virtual_board_show_relative" onclick="refresh_stop_times(this.checked ? 'absolute' : 'relative')">
85+
<label class="form-check-label" for="virtual_board_show_relative">
86+
Показвай точен час на пристигане
87+
</label>
88+
</p>
8389
</div>
8490
<div id="settings-panel" class="panel position-absolute h-100 d-flex flex-column d-none p-2">
8591
<button class="btn btn-outline-secondary float-end fw-bolder fs-3 lh-1" onclick="this.parentElement.classList.add('d-none')">

src/js/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,8 @@ function init_settings() {
389389
}
390390
});
391391
});
392+
393+
const virtual_board_show_relative = get_setting('stop_time_style') === 'absolute';
394+
const check_el = document.querySelector('#virtual_board_show_relative');
395+
check_el.toggleAttribute('checked', virtual_board_show_relative);
392396
}

src/js/map_stops.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ async function update_stop_times(stop_code) {
9090
const old_tbody = panel.querySelector('tbody');
9191
if(old_tbody) {
9292
old_tbody.replaceWith(new_tbody);
93+
refresh_stop_times();
9394
}
9495
refresh_btn.disabled = false;
9596
last_updated_el.setAttribute('data-timestamp', (Date.now() / 1000).toString());
@@ -144,6 +145,32 @@ async function load_stop_times(stop_code) {
144145
return data.routes;
145146
}
146147

148+
function refresh_stop_times(style) {
149+
if(!style) {
150+
style = localStorage.getItem('livemap_stop_time_style') || 'relative';
151+
}
152+
localStorage.setItem('livemap_stop_time_style', style);
153+
const panel = document.querySelector('#virtual-board-panel');
154+
const elements = panel.querySelectorAll('span[data-stop-time]');
155+
const now1 = new Date();
156+
const now_hour = now1.getHours() * 60;
157+
const now_minute = now1.getMinutes();
158+
const current_time = now_hour + now_minute;
159+
for(const el of elements) {
160+
const stop_time = Number(el.getAttribute('data-stop-time'));
161+
if(style == 'relative') {
162+
const diff = stop_time - current_time;
163+
el.textContent = `${diff} мин.`;
164+
}
165+
else {
166+
const hour = Math.floor(stop_time / 60) % 24;
167+
const minute = (stop_time % 60).toString().padStart(2, '0');
168+
el.textContent = `${(hour % 24).toString().padStart(2, '0')}:${minute}`;
169+
}
170+
}
171+
}
172+
window.refresh_stop_times = refresh_stop_times;
173+
147174
function display_stop_times(stop_routes) {
148175
function display_hours(scheduled, actual) {
149176
if(scheduled === null) {
@@ -153,11 +180,8 @@ function display_stop_times(stop_routes) {
153180

154181
const diff_class = 3 < total_diff || total_diff < -1 ? 'text-danger fw-bold' : 'text-success';
155182
const diff_html = `<span class="${diff_class} text-nowrap">${total_diff > 0 ? '+' : ''}${total_diff == 0 ? 'навреме' : total_diff + ' мин.'}</span>`;
156-
157-
const hour = (Math.floor(actual / 60) % 24);
158-
const minute = (actual % 60).toString().padStart(2, '0');
159-
160-
const actual_formatted = `${(hour % 24).toString().padStart(2, '0')}:${minute}`;
183+
184+
const actual_formatted = `<span data-stop-time="${actual}"></span>`;
161185

162186
return [actual_formatted, total_diff === null ? null : diff_html];
163187
}

0 commit comments

Comments
 (0)