From 5bd38819682d40421166d6ebc15e7346e9ade690 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Thu, 29 Jul 2021 22:41:53 +0000
Subject: [PATCH] [map] Allow zero-prefixed stop identifiers (fix for bus stops)

---
 index.js |  286 ++++++++++++++++++++++++++++----------------------------
 1 files changed, 144 insertions(+), 142 deletions(-)

diff --git a/index.js b/index.js
index 469a6ab..2927c12 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,4 @@
-"use strict";
+'use strict';
 
 var ttss_refresh = 20000; // 20 seconds
 
@@ -78,114 +78,39 @@
 	nav.className = nav.className.replace(' loading', '');
 }
 
-function loadTimes(stopId) {
-	if(!stopId) stopId = stop_id;
-	if(!stopId) return;
-	
-	if(times_timer) clearTimeout(times_timer);
-	if(times_xhr) times_xhr.abort();
-	
-	console.log('loadTimes(' + stopId + ')');
-	
-	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 = null;
-	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;
-		}
+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);
 	
-	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, data.stopName);
-		setText(page_title, lang.page_title_stop_name.replace('$stop', data.stopName));
-		deleteChildren(times_alerts);
-		deleteChildren(times_table);
-		//deleteChildren(times_lines);
+	var now = new Date();
+	var ms = now.getTime() - refresh_time.getTime();
+	
+	var interval = 1000;
+	if(ms >= 120000) interval = 60000;
+	
+	refresh_timer = setInterval(function() {
+		var now = new Date();
+		var ms = now.getTime() - refresh_time.getTime();
 		
-		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);
-			};
-			
+		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
+			));
 		}
-		
-		for(var 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(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) {
@@ -245,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() {
@@ -342,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();
 		}
 	}
 }
@@ -383,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);
 		
@@ -417,7 +419,7 @@
 		setText(vehicle_data_style, '.vehicleData { display: table-cell; }')
 	});
 	
-	updateVehicleInfo()
+	vehicles_info.update();
 	
 	hash();
 	

--
Gitblit v1.9.1