Skip to content

Commit 0f487a3

Browse files
committed
refactor stop times
1 parent 919500b commit 0f487a3

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

src/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@
7676
<table class="table table-bordered">
7777
<tbody></tbody>
7878
</table>
79-
<p id="last-updated" class="text-muted text-end" data-timestamp=""></p>
79+
<p class="d-flex flex-row justify-content-between mb-0">
80+
<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>
81+
<span id="last-updated" class="text-muted" data-timestamp=""></span>
82+
</p>
8083
</div>
8184
<div id="settings-panel" class="panel position-absolute h-100 d-flex flex-column d-none p-2">
8285
<button class="btn btn-outline-secondary float-end fw-bolder fs-3 lh-1" onclick="this.parentElement.classList.add('d-none')">

src/js/map_stops.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,7 @@ function get_stop_marker(stop) {
5353
}
5454
})
5555
.on('click', async (e) => {
56-
const panel = document.querySelector('#virtual-board-panel');
57-
{
58-
const old_tbody = panel.querySelector('tbody');
59-
const loading_tbody = document.createElement('tbody');
60-
const loading_row = document.createElement('tr');
61-
const loading_td = document.createElement('td');
62-
loading_td.innerHTML = '<i class="bi bi-arrow-clockwise loading-icon"></i> Зареждане...';
63-
loading_row.appendChild(loading_td);
64-
loading_tbody.appendChild(loading_row);
65-
66-
old_tbody.replaceWith(loading_tbody);
67-
}
68-
panel.querySelector('#stop_name').textContent = `[${stop.code.toString().padStart(4, '0')}] ${stop.names.bg}`;
69-
panel.classList.remove('d-none');
70-
const times = await load_stop_times(stop.code);
71-
const new_tbody = display_stop_times(times);
72-
const old_tbody = panel.querySelector('tbody');
73-
if(old_tbody) {
74-
old_tbody.replaceWith(new_tbody);
75-
}
76-
const last_updated_el = panel.querySelector('#last-updated');
77-
last_updated_el.setAttribute('data-timestamp', (Date.now() / 1000).toString());
78-
last_updated_el.textContent = determine_time_ago(Date.now() / 1000);
79-
56+
await update_stop_times(stop.code);
8057
})
8158
.on('popupclose', (e) => {
8259
e.target.unbindPopup();
@@ -85,6 +62,41 @@ function get_stop_marker(stop) {
8562
return marker;
8663
}
8764

65+
async function update_stop_times(stop_code) {
66+
const panel = document.querySelector('#virtual-board-panel');
67+
const refresh_btn = panel.querySelector('#refresh-virtual-board');
68+
const last_updated_el = panel.querySelector('#last-updated');
69+
last_updated_el.setAttribute('data-timestamp', '');
70+
last_updated_el.textContent = '';
71+
refresh_btn.dataset.code = stop_code;
72+
refresh_btn.disabled = true;
73+
{
74+
const old_tbody = panel.querySelector('tbody');
75+
const loading_tbody = document.createElement('tbody');
76+
const loading_row = document.createElement('tr');
77+
const loading_td = document.createElement('td');
78+
loading_td.classList.add('text-center', 'py-3');
79+
loading_td.innerHTML = '<div class="spinner-border spinner-border-sm" role="status"></div> Зареждане...';
80+
loading_row.appendChild(loading_td);
81+
loading_tbody.appendChild(loading_row);
82+
83+
old_tbody.replaceWith(loading_tbody);
84+
}
85+
const stop = stops.get(stop_code);
86+
panel.querySelector('#stop_name').textContent = `[${stop.code.toString().padStart(4, '0')}] ${stop.names.bg}`;
87+
panel.classList.remove('d-none');
88+
const times = await load_stop_times(stop.code);
89+
const new_tbody = display_stop_times(times);
90+
const old_tbody = panel.querySelector('tbody');
91+
if(old_tbody) {
92+
old_tbody.replaceWith(new_tbody);
93+
}
94+
refresh_btn.disabled = false;
95+
last_updated_el.setAttribute('data-timestamp', (Date.now() / 1000).toString());
96+
last_updated_el.textContent = determine_time_ago(Date.now() / 1000);
97+
}
98+
window.update_stop_times = update_stop_times;
99+
88100
function are_stops_shown(map) {
89101
const zoom = map.getZoom();
90102
return zoom >= 17;

0 commit comments

Comments
 (0)