From ac721dc18e5bf997afc016a486fa4788f094b148 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Thu, 08 Dec 2016 22:47:26 +0000
Subject: [PATCH] Add request in progress indicator

---
 index.js |   58 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/index.js b/index.js
index 54b901a..e522ba4 100644
--- a/index.js
+++ b/index.js
@@ -30,13 +30,16 @@
 var alert_text = document.getElementById('alert-text');
 var alert_close = document.getElementById('alert-close');
 
+var progress = document.getElementById('progress');
+
+var parseStatusBoarding = '>>>';
 function parseStatus(status) {
 	switch(status.status) {
 		case 'STOPPING':
-			return '<<<';
+			return parseStatusBoarding;
 		case 'PREDICTED':
 			if(status.actualRelativeTime <= 0)
-				return '<<<';
+				return parseStatusBoarding;
 			if(status.actualRelativeTime >= 60)
 				return Math.floor(status.actualRelativeTime / 60) + ' min';
 			return status.actualRelativeTime + ' s';
@@ -87,11 +90,11 @@
 }
 
 function addCellWithText(parent, text) {
-	addElementWithText(parent, 'td', text);
+	return addElementWithText(parent, 'td', text);
 }
 
 function addParaWithText(parent, text) {
-	addElementWithText(parent, 'p', text);
+	return addElementWithText(parent, 'p', text);
 }
 
 function setText(element, text) {
@@ -100,7 +103,6 @@
 }
 
 function fail(message, more) {
-	if(refresh_timer) clearInterval(refresh_timer);
 	if(times_timer) clearTimeout(times_timer);
 	
 	setText(alert_text, message);
@@ -123,6 +125,18 @@
 	}
 }
 
+function fail_hide() {
+	alert.style.display = 'none';
+}
+
+function loading_start() {
+	progress.style.display = 'block';
+}
+
+function loading_end() {
+	progress.style.display = 'none';
+}
+
 function loadTimes(stopId = null, clearRoute = false) {
 	if(!stopId) stopId = stop_id;
 	if(!stopId) return;
@@ -132,6 +146,7 @@
 	
 	refresh_button.removeAttribute('disabled');
 	
+	loading_start();
 	times_xhr = $.get(
 		ttss_base + '/passageInfo/stopPassages/stop' 
 			+ '?stop=' + encodeURIComponent(stopId)
@@ -169,13 +184,19 @@
 			addCellWithText(tr, data.actual[i].patternText);
 			addCellWithText(tr, data.actual[i].direction);
 			var status = parseStatus(data.actual[i]);
-			addCellWithText(tr, status);
+			var status_cell = addCellWithText(tr, status);
 			var delay = parseDelay(data.actual[i]);
-			addCellWithText(tr, delay);
+			var delay_cell = addCellWithText(tr, delay);
 			
-			if(status == '<<<') tr.className = 'success';
-			else if(parseInt(delay) > 9) tr.className = 'danger';
-			else if(parseInt(delay) > 3) tr.className = 'warning';
+			if(status == parseStatusBoarding) {
+				tr.className = 'success';
+				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';
+			}
 			times_table.appendChild(tr);
 		}
 		
@@ -225,9 +246,10 @@
 		}
 		
 		startTimer(new Date());
+		fail_hide();
 		
 		times_timer = setTimeout(function(){ loadTimes(); }, ttss_refresh);
-	}).fail(fail_ajax);
+	}).fail(fail_ajax).always(loading_end);
 }
 
 function declinate(num, singular, plural) {
@@ -264,6 +286,12 @@
 	}, interval);
 }
 
+var decodeEntitiesTextArea = document.createElement('textarea');
+function decodeEntities(text) {
+	decodeEntitiesTextArea.innerHTML = text;
+	return decodeEntitiesTextArea.value;
+}
+
 function init() {
 	if(!window.jQuery) {
 		fail('Required JavaScript jQuery library failed to load. You may try refreshing the page.');
@@ -287,7 +315,7 @@
 			for(var i = 1, il = data.length; i < il; i++) {
 				var opt = document.createElement('option');
 				opt.value = data[i].id;
-				opt.appendChild(document.createTextNode(data[i].name));
+				setText(opt, decodeEntities(data[i].name));
 				stop_name_autocomplete.appendChild(opt);
 			}
 			
@@ -301,6 +329,7 @@
 		e.preventDefault();
 		if(!stop_name_autocomplete.value) return;
 		stop_id = stop_name_autocomplete.value;
+		window.location.hash = '#!' + stop_id;
 		loadTimes(stop_id, true);
 	});
 	
@@ -311,6 +340,11 @@
 	alert_close.addEventListener('click', function(e) {
 		alert.style.display = 'none';
 	});
+	
+	if(window.location.hash.match(/^#![0-9]+$/)) {
+		stop_id = parseInt(window.location.hash.slice(2));
+		loadTimes(stop_id);
+	}
 }
 
 init();

--
Gitblit v1.9.1