From cb5a77efaef000c560ccb5d8b6534a97b863deff Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sun, 14 Apr 2019 09:52:00 +0000
Subject: [PATCH] Remove duplication vehicleTable/stopTable functions

---
 index.js |   69 ++++++++---------------
 map.js   |   58 ++++++-------------
 2 files changed, 43 insertions(+), 84 deletions(-)

diff --git a/index.js b/index.js
index e59ca07..3a07ed9 100644
--- a/index.js
+++ b/index.js
@@ -113,39 +113,25 @@
 			addParaWithText(times_alerts, data.generalAlerts[i].title);
 		}
 		
-		for(var i = 0, il = data.old.length; i < il; i++) {
+		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, data.old[i].patternText);
-			var dir_cell = addCellWithText(tr, data.old[i].direction);
-			var vehicle = parseVehicle(prefix + data.old[i].vehicleId);
+			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(data.old[i]);
-			addCellWithText(tr, status);
-			addCellWithText(tr, '');
-			
-			tr.className = 'active';
-			tr.addEventListener('click', function(tripId, vehicleInfo) {
-				return function(){ loadRoute(tripId, vehicleInfo); }
-			}(prefix + data.old[i].tripId, vehicle));
-			times_table.appendChild(tr);
-		}
-		
-		for(var i = 0, il = data.actual.length; i < il; i++) {
-			var tr = document.createElement('tr');
-			addCellWithText(tr, data.actual[i].patternText);
-			var dir_cell = addCellWithText(tr, data.actual[i].direction);
-			var vehicle = parseVehicle(prefix + data.actual[i].vehicleId);
-			dir_cell.appendChild(displayVehicle(vehicle));
-			addCellWithText(tr, (vehicle ? vehicle.num : '')).className = 'vehicleData';
-			var status = parseStatus(data.actual[i]);
+			var status = parseStatus(all_departures[i]);
 			var status_cell = addCellWithText(tr, status);
-			var delay = parseDelay(data.actual[i]);
+			var delay = parseDelay(all_departures[i]);
 			var delay_cell = addCellWithText(tr, delay);
 			
-			if(data.actual[i].status == 'STOPPING') {
+			if(i < data.old.length) {
+				tr.className = 'active';
+			} else if(all_departures[i].status === 'STOPPING') {
 				tr.className = 'success';
-				if (data.actual[i].actualRelativeTime <= 0) {
+				if (all_departures[i].actualRelativeTime <= 0) {
 					status_cell.className = 'status-boarding';
 				}
 			} else if(parseInt(delay) > 9) {
@@ -157,7 +143,7 @@
 			
 			tr.addEventListener('click', function(tripId, vehicleInfo) {
 				return function(){ loadRoute(tripId, vehicleInfo); }
-			}(prefix + data.actual[i].tripId, vehicle));
+			}(prefix + all_departures[i].tripId, vehicle));
 			times_table.appendChild(tr);
 		}
 		
@@ -189,14 +175,13 @@
 	
 	var prefix = tripId.substr(0, 1);
 	var trip = tripId.substr(1);
-	var url = ttss_urls[prefix];
 	
 	route_id = tripId;
 	route_vehicle_info = vehicleInfo;
 	
 	if(route_xhr) route_xhr.abort();
 	route_xhr = $.get(
-		url + '/services/tripInfo/tripPassages'
+		ttss_urls[prefix] + '/services/tripInfo/tripPassages'
 			+ '?tripId=' + encodeURIComponent(trip)
 			+ '&mode=departure'
 	).done(function(data) {
@@ -218,25 +203,19 @@
 		
 		deleteChildren(route_table);
 		
-		for(var i = 0, il = data.old.length; i < il; i++) {
-			var tr = document.createElement('tr');
-			addCellWithText(tr, data.old[i].actualTime || data.old[i].plannedTime);
-			addCellWithText(tr, data.old[i].stop_seq_num + '. ' + data.old[i].stop.name);
+		var all_departures = data.old.concat(data.actual);
+		var tr;
+		for(var i = 0, il = all_departures.length; i < il; i++) {
+			tr = document.createElement('tr');
+			addCellWithText(tr, all_departures[i].actualTime || all_departures[i].plannedTime);
+			addCellWithText(tr, all_departures[i].stop_seq_num + '. ' + all_departures[i].stop.name);
 			
-			tr.className = 'active';
-			tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(prefix + data.old[i].stop.shortName) );
-			route_table.appendChild(tr);
-		}
-		
-		for(var i = 0, il = data.actual.length; i < il; i++) {
-			var tr = document.createElement('tr');
-			addCellWithText(tr, data.actual[i].actualTime || data.actual[i].plannedTime);
-			addCellWithText(tr, data.actual[i].stop_seq_num + '. ' + data.actual[i].stop.name);
-			
-			if(data.actual[i].status == 'STOPPING') {
+			if(i < data.old.length) {
+				tr.className = 'active';
+			} else if(all_departures[i].status === 'STOPPING') {
 				tr.className = 'success';
 			}
-			tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(prefix + data.actual[i].stop.shortName) );
+			tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(prefix + all_departures[i].stop.shortName) );
 			route_table.appendChild(tr);
 		}
 	}).fail(fail_ajax);
diff --git a/map.js b/map.js
index 11c7aa0..c34431e 100644
--- a/map.js
+++ b/map.js
@@ -395,27 +395,21 @@
 		
 		deleteChildren(table);
 		
-		var i, il;
+		var all_departures = data.old.concat(data.actual);
 		var tr;
-		for(i = 0, il = data.old.length; i < il; i++) {
-			tr = document.createElement('tr');
-			addCellWithText(tr, data.old[i].actualTime || data.old[i].plannedTime);
-			addCellWithText(tr, data.old[i].stop_seq_num + '. ' + data.old[i].stop.name);
-			
-			tr.className = 'active';
-			table.appendChild(tr);
-		}
-		
 		var stopsToMark = [];
-		
-		for(i = 0, il = data.actual.length; i < il; i++) {
+		for(var i = 0, il = all_departures.length; i < il; i++) {
 			tr = document.createElement('tr');
-			addCellWithText(tr, data.actual[i].actualTime || data.actual[i].plannedTime);
-			addCellWithText(tr, data.actual[i].stop_seq_num + '. ' + data.actual[i].stop.name);
+			addCellWithText(tr, all_departures[i].actualTime || all_departures[i].plannedTime);
+			addCellWithText(tr, all_departures[i].stop_seq_num + '. ' + all_departures[i].stop.name);
 			
-			stopsToMark.push('s' + ttss_type + data.actual[i].stop.id);
+			if(i >= data.old.length) {
+				stopsToMark.push('s' + ttss_type + all_departures[i].stop.id);
+			}
 			
-			if(data.actual[i].status == 'STOPPING') {
+			if(i < data.old.length) {
+				tr.className = 'active';
+			} else if(all_departures[i].status === 'STOPPING') {
 				tr.className = 'success';
 			}
 			table.appendChild(tr);
@@ -439,36 +433,22 @@
 	).done(function(data) {
 		deleteChildren(table);
 		
-		var i, il;
+		var all_departures = data.old.concat(data.actual);
 		var tr, dir_cell, vehicle, status, status_cell, delay, delay_cell;
-		for(i = 0, il = data.old.length; i < il; i++) {
+		for(var i = 0, il = all_departures.length; i < il; i++) {
 			tr = document.createElement('tr');
-			addCellWithText(tr, data.old[i].patternText);
-			dir_cell = addCellWithText(tr, data.old[i].direction);
-			vehicle = parseVehicle(data.old[i].vehicleId);
+			addCellWithText(tr, all_departures[i].patternText);
+			dir_cell = addCellWithText(tr, all_departures[i].direction);
+			vehicle = parseVehicle(all_departures[i].vehicleId);
 			dir_cell.appendChild(displayVehicle(vehicle));
-			status = parseStatus(data.old[i]);
+			status = parseStatus(all_departures[i]);
 			status_cell = addCellWithText(tr, status);
-			delay_cell = addCellWithText(tr, '');
-			
-			tr.className = 'active';
-			table.appendChild(tr);
-		}
-		
-		for(i = 0, il = data.actual.length; i < il; i++) {
-			tr = document.createElement('tr');
-			addCellWithText(tr, data.actual[i].patternText);
-			dir_cell = addCellWithText(tr, data.actual[i].direction);
-			vehicle = parseVehicle(data.actual[i].vehicleId);
-			dir_cell.appendChild(displayVehicle(vehicle));
-			status = parseStatus(data.actual[i]);
-			status_cell = addCellWithText(tr, status);
-			delay = parseDelay(data.actual[i]);
+			delay = parseDelay(all_departures[i]);
 			delay_cell = addCellWithText(tr, delay);
 			
-			if(data.actual[i].status === 'DEPARTED') {
+			if(i < data.old.length) {
 				tr.className = 'active';
-			} else if(status == lang.boarding_sign) {
+			} else if(status === lang.boarding_sign) {
 				tr.className = 'success';
 				status_cell.className = 'status-boarding';
 			} else if(parseInt(delay) > 9) {

--
Gitblit v1.9.1