From d80109132d362199e6d9e370d5f54bd0d6554126 Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Thu, 29 Jul 2021 22:40:29 +0000 Subject: [PATCH] [map] Allow zero-prefixed stop identifiers (fix for bus stops) --- index.js | 263 ++++++++++++++++++++++++++++----------------------- 1 files changed, 144 insertions(+), 119 deletions(-) diff --git a/index.js b/index.js index fd60868..2927c12 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; var ttss_refresh = 20000; // 20 seconds @@ -17,6 +17,7 @@ var times_xhr; var times_timer; +var times_stop_type = document.getElementById('times-stop-type'); var times_stop_name = document.getElementById('times-stop-name'); var times_alerts = document.getElementById('times-alerts'); var times_table = document.getElementById('times-table'); @@ -77,92 +78,39 @@ nav.className = nav.className.replace(' loading', ''); } -function loadTimes(stopId) { - if(!stopId) stopId = stop_id; - if(!stopId) return; +function startTimer(date) { + if(date) { + setText(refresh_text, lang.last_refreshed.replace('$time', lang.time_now)); + refresh_time = date; + } + if(!refresh_time) return; + if(refresh_timer) clearInterval(refresh_timer); - if(times_timer) clearTimeout(times_timer); - if(times_xhr) times_xhr.abort(); + var now = new Date(); + var ms = now.getTime() - refresh_time.getTime(); - console.log('loadTimes(' + stopId + ')'); + var interval = 1000; + if(ms >= 120000) interval = 60000; - loading_start(); - - var prefix = stopId.substr(0, 1); - var stop = stopId.substr(1); - var url = ttss_urls[prefix]; - - stop_id = stopId; - - ignore_hashchange = true; - window.location.hash = '#!' + language + stopId; - refresh_button.removeAttribute('disabled'); - - times_xhr = $.get( - url + '/services/passageInfo/stopPassages/stop' - + '?stop=' + encodeURIComponent(stop) - + '&mode=departure' - ).done(function(data) { - setText(times_stop_name, data.stopName); - setText(page_title, lang.page_title_stop_name.replace('$stop', data.stopName)); - deleteChildren(times_alerts); - deleteChildren(times_table); - //deleteChildren(times_lines); + refresh_timer = setInterval(function() { + var now = new Date(); + var ms = now.getTime() - refresh_time.getTime(); - for(var i = 0, il = data.generalAlerts.length; i < il; i++) { - addParaWithText(times_alerts, data.generalAlerts[i].title); + if(ms >= 120000) { + setText(refresh_text, lang.last_refreshed.replace( + '$time', + lang.time_minutes_ago_prefix + Math.floor(ms / 60000) + + lang.time_minutes_ago_suffix + )); + startTimer(); + } else { + setText(refresh_text, lang.last_refreshed.replace( + '$time', + lang.time_seconds_ago_prefix + Math.floor(ms / 1000) + + lang.time_seconds_ago_suffix + )); } - - var all_departures = data.old.concat(data.actual); - var tr, dir_cell, vehicle, status, status_cell, delay, delay_cell; - for(var i = 0, il = all_departures.length; i < il; i++) { - var tr = document.createElement('tr'); - addCellWithText(tr, all_departures[i].patternText); - var dir_cell = addCellWithText(tr, all_departures[i].direction); - var vehicle = parseVehicle(prefix + all_departures[i].vehicleId); - dir_cell.appendChild(displayVehicle(vehicle)); - addCellWithText(tr, (vehicle ? vehicle.num : '')).className = 'vehicleData'; - var status = parseStatus(all_departures[i]); - var status_cell = addCellWithText(tr, status); - var delay = parseDelay(all_departures[i]); - var delay_cell = addCellWithText(tr, delay); - - if(i < data.old.length) { - tr.className = 'active'; - } else if(all_departures[i].status === 'STOPPING') { - tr.className = 'success'; - if (all_departures[i].actualRelativeTime <= 0) { - status_cell.className = 'status-boarding'; - } - } else if(parseInt(delay) > 9) { - tr.className = 'danger'; - delay_cell.className = 'status-delayed'; - } else if(parseInt(delay) > 3) { - tr.className = 'warning'; - } - - tr.addEventListener('click', function(tripId, vehicleInfo) { - return function(){ loadRoute(tripId, vehicleInfo); } - }(prefix + all_departures[i].tripId, vehicle)); - times_table.appendChild(tr); - } - - /* - for(var i = 0, il = data.routes.length; i < il; i++) { - var tr = document.createElement('tr'); - addCellWithText(tr, data.routes[i].name); - addCellWithText(tr, data.routes[i].directions.join(' - ')); - addCellWithText(tr, data.routes[i].authority); - times_lines.appendChild(tr); - } - */ - - startTimer(new Date()); - fail_hide(); - - times_timer = setTimeout(function(){ loadTimes(); loadRoute(); }, ttss_refresh); - }).fail(fail_ajax).always(loading_end); - return times_xhr; + }, interval); } function loadRoute(tripId, vehicleInfo) { @@ -222,39 +170,115 @@ return route_xhr; } -function startTimer(date) { - if(date) { - setText(refresh_text, lang.last_refreshed.replace('$time', lang.time_now)); - refresh_time = date; - } - if(!refresh_time) return; - if(refresh_timer) clearInterval(refresh_timer); +function loadTimes(stopId) { + if(!stopId) stopId = stop_id; + if(!stopId) return; - var now = new Date(); - var ms = now.getTime() - refresh_time.getTime(); + if(times_timer) clearTimeout(times_timer); + if(times_xhr) times_xhr.abort(); - var interval = 1000; - if(ms >= 120000) interval = 60000; + console.log('loadTimes(' + stopId + ')'); - refresh_timer = setInterval(function() { - var now = new Date(); - var ms = now.getTime() - refresh_time.getTime(); - - if(ms >= 120000) { - setText(refresh_text, lang.last_refreshed.replace( - '$time', - lang.time_minutes_ago_prefix + Math.floor(ms / 60000) - + lang.time_minutes_ago_suffix - )); - startTimer(); - } else { - setText(refresh_text, lang.last_refreshed.replace( - '$time', - lang.time_seconds_ago_prefix + Math.floor(ms / 1000) - + lang.time_seconds_ago_suffix - )); + loading_start(); + + var prefix = stopId.substr(0, 1); + var stop = stopId.substr(1); + var url = ttss_urls[prefix]; + + stop_id = stopId; + + ignore_hashchange = true; + window.location.hash = '#!' + language + stopId; + refresh_button.removeAttribute('disabled'); + + var alternative_stop = null; + var candidate; + for(var i = 0; i < stop_name_autocomplete.options.length; i++) { + candidate = stop_name_autocomplete.options[i].value; + if(candidate.substr(0, 1) !== prefix && candidate.substr(1) == stop) { + alternative_stop = candidate; + break; } - }, interval); + } + + times_xhr = $.get( + url + '/services/passageInfo/stopPassages/stop' + + '?stop=' + encodeURIComponent(stop) + + '&mode=departure' + ).done(function(data) { + setText(times_stop_type, lang.types['s' + prefix]); + setText(times_stop_name, normalizeName(data.stopName)); + setText(page_title, lang.page_title_stop_name.replace('$stop', normalizeName(data.stopName))); + deleteChildren(times_alerts); + deleteChildren(times_table); + //deleteChildren(times_lines); + + if(alternative_stop !== null) { + var a = addParaWithText(times_alerts, ''); + a = addElementWithText(a, 'a', (prefix === 'b' ? lang.departures_for_trams : lang.departures_for_buses)); + a.href = ''; + a.onclick = function(e) { + e.preventDefault(); + loadTimes(alternative_stop); + }; + + } + + var i, il; + for(i = 0, il = data.generalAlerts.length; i < il; i++) { + addParaWithText(times_alerts, data.generalAlerts[i].title); + } + + var all_departures = data.old.concat(data.actual); + var tr, dir_cell, vehicle, status, status_cell, delay, delay_cell; + for(i = 0, il = all_departures.length; i < il; i++) { + tr = document.createElement('tr'); + addCellWithText(tr, all_departures[i].patternText); + dir_cell = addCellWithText(tr, all_departures[i].direction); + vehicle = vehicles_info.getParsed(prefix + all_departures[i].vehicleId); + dir_cell.appendChild(displayVehicle(vehicle)); + addCellWithText(tr, (vehicle ? vehicle.num : '')).className = 'vehicleData'; + status = parseStatus(all_departures[i]); + status_cell = addCellWithText(tr, status); + delay = parseDelay(all_departures[i]); + delay_cell = addCellWithText(tr, delay); + + if(i < data.old.length) { + tr.className = 'active'; + } else if(all_departures[i].status === 'STOPPING') { + tr.className = 'success'; + if (all_departures[i].actualRelativeTime <= 0) { + status_cell.className = 'status-boarding'; + } + } else if(parseInt(delay) > 9) { + tr.className = 'danger'; + delay_cell.className = 'status-delayed'; + } else if(parseInt(delay) > 3) { + tr.className = 'warning'; + } + + tr.addEventListener('click', function(tripId, vehicleInfo) { + return function(){ loadRoute(tripId, vehicleInfo); } + }(prefix + all_departures[i].tripId, vehicle)); + times_table.appendChild(tr); + } + + /* + for(var i = 0, il = data.routes.length; i < il; i++) { + var tr = document.createElement('tr'); + addCellWithText(tr, data.routes[i].name); + addCellWithText(tr, data.routes[i].directions.join(' - ')); + addCellWithText(tr, data.routes[i].authority); + times_lines.appendChild(tr); + } + */ + + startTimer(new Date()); + fail_hide(); + + times_timer = setTimeout(function(){ loadTimes(); loadRoute(); }, ttss_refresh); + }).fail(fail_ajax).always(loading_end); + return times_xhr; } function translate() { @@ -319,23 +343,24 @@ return; } + var stop; if(window.location.hash.match(/^#![0-9]+$/)) { loadTimes('t' + window.location.hash.substr(2)); } else if(window.location.hash.match(/^#![bt][0-9]+$/)) { loadTimes(window.location.hash.substr(2)); } else if(window.location.hash.match(/^#![a-z]{2}[0-9]*$/)) { - var stop = 't' + window.location.hash.substr(4); - if(stop) stop_id = stop; + stop = window.location.hash.substr(4); + if(stop) stop_id = 't' + stop; if(!change_language(window.location.hash.substr(2, 2))) { - loadTimes(stop); + loadTimes(); } } else if(window.location.hash.match(/^#![a-z]{2}[bt][0-9]*$/)) { - var stop = window.location.hash.substr(4); + stop = window.location.hash.substr(4); if(stop) stop_id = stop; if(!change_language(window.location.hash.substr(2, 2))) { - loadTimes(stop); + loadTimes(); } } } @@ -360,11 +385,11 @@ } function init() { - lang_select.addEventListener('input', function(e) { + lang_select.addEventListener('input', function() { change_language(lang_select.value); }); - stop_name.addEventListener('input', function(e) { + stop_name.addEventListener('input', function() { if(!stop_name.value) return; if(stop_name_autocomplete_timer) clearTimeout(stop_name_autocomplete_timer); @@ -394,7 +419,7 @@ setText(vehicle_data_style, '.vehicleData { display: table-cell; }') }); - updateVehicleInfo() + vehicles_info.update(); hash(); -- Gitblit v1.9.1