From a9e7f2ed9cec6e9363f5be8e4371689c02237364 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 17 Apr 2017 19:16:47 +0000
Subject: [PATCH] Fix hashchange event handling due to various problems: - initial stop data not loading, - doubled loadTimes() invocations.

---
 index.js |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/index.js b/index.js
index a26a7a1..551d035 100644
--- a/index.js
+++ b/index.js
@@ -160,7 +160,6 @@
 	
 	ignore_hashchange = true;
 	window.location.hash = '#!' + language + stopId;
-	ignore_hashchange = false;
 	refresh_button.removeAttribute('disabled');
 	
 	loading_start();
@@ -365,8 +364,8 @@
 }
 
 function change_language(lang) {
-	if(!lang || lang.length != 2) return;
-	if(lang == language) return;
+	if(!lang || lang.length != 2) return false;
+	if(lang == language) return false;
 	lang_select.value = lang;
 	if(!lang_select.value) {
 		lang_select.value = language;
@@ -385,18 +384,25 @@
 	
 	ignore_hashchange = true;
 	window.location.hash = '#!' + language + stop_id;
-	ignore_hashchange = false;
+	
+	return true;
 }
 
 function hash() {
-	if(ignore_hashchange) return;
+	if(ignore_hashchange) {
+		ignore_hashchange = false;
+		return;
+	}
 	
 	if(window.location.hash.match(/^#![0-9]+$/)) {
 		loadTimes(parseInt(window.location.hash.substr(2)));
 	} else if(window.location.hash.match(/^#![a-z]{2}[0-9]*$/)) {
 		var stop = parseInt(window.location.hash.substr(4));
 		if(stop) stop_id = stop;
-		change_language(window.location.hash.substr(2, 2));
+		
+		if(!change_language(window.location.hash.substr(2, 2))) {
+			loadTimes(parseInt(window.location.hash.substr(2)));
+		}
 	}
 }
 

--
Gitblit v1.9.1