From 218a38496d8907175346b81ce8a4afbe2943d1fd Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 17 Apr 2017 19:04:42 +0000
Subject: [PATCH] Fix problem with departures not loading when unknown vehicle is present on list
---
map.js | 119 +++++++++++++++++++++++++++++++++++++++++++++++------------
1 files changed, 94 insertions(+), 25 deletions(-)
diff --git a/map.js b/map.js
index 34493f9..e7b821d 100644
--- a/map.js
+++ b/map.js
@@ -1,6 +1,6 @@
//var ttss_base = 'http://www.ttss.krakow.pl/internetservice';
var ttss_base = '/proxy.php';
-var ttss_refresh = 5000; // 5 seconds
+var ttss_refresh = 10000; // 10 seconds
var vehicles_xhr = null;
var vehicles_timer = null;
@@ -19,6 +19,8 @@
var popup_element = document.getElementById('popup');
var popup = null;
var fail_element = document.getElementById('fail');
+
+var ignore_hashchange = false;
function fail(msg) {
console.log(msg);
@@ -142,6 +144,8 @@
updateVehicles();
}, ttss_refresh);
}).fail(fail_ajax);
+
+ return vehicles_xhr;
}
function updateStopSource(stops, prefix, source) {
@@ -149,6 +153,9 @@
for(var i = 0; i < stops.length; i++) {
var stop = stops[i];
+
+ if(stop.category == 'other') continue;
+
stop.geometry = getGeometry(stop);
var stop_feature = new ol.Feature(stop);
@@ -166,7 +173,7 @@
}
function updateStops() {
- $.get(
+ return $.get(
ttss_base + '/geoserviceDispatcher/services/stopinfo/stops'
+ '?left=-648000000'
+ '&bottom=-324000000'
@@ -178,7 +185,7 @@
}
function updateStopPoints() {
- $.get(
+ return $.get(
ttss_base + '/geoserviceDispatcher/services/stopinfo/stopPoints'
+ '?left=-648000000'
+ '&bottom=-324000000'
@@ -187,6 +194,80 @@
).done(function(data) {
updateStopSource(data.stopPoints, 'p', stop_points_source);
}).fail(fail_ajax);
+}
+
+function featureClicked(feature) {
+ if(!feature) {
+ popupHide();
+
+ ignore_hashchange = true;
+ window.location.hash = '';
+
+ return;
+ }
+
+ var coordinates = feature.getGeometry().getCoordinates();
+
+ deleteChildren(popup_element);
+
+ addParaWithText(popup_element, feature.get('name')).className = 'bold';
+ switch(feature.getId().substr(0, 1)) {
+ case 'v':
+ var vehicle_type = parseVehicle(feature.get('id'));
+ if(vehicle_type) {
+ addParaWithText(popup_element, vehicle_type.num + ' ' + vehicle_type.type);
+ }
+ break;
+ }
+
+ ignore_hashchange = true;
+ window.location.hash = '#!' + feature.getId();
+
+ popupShow(coordinates, feature.getId());
+}
+
+function hash() {
+ if(ignore_hashchange) {
+ ignore_hashchange = false;
+ return;
+ }
+
+ var tramId = null;
+
+ var vehicleId = null;
+ var stopId = null;
+ var stopPointId = null;
+
+ var feature = null;
+
+ if(window.location.hash.match(/^#!t[0-9]{3}$/)) {
+ tramId = parseInt(window.location.hash.substr(3));
+ } else if(window.location.hash.match(/^#![A-Za-z]{2}[0-9]{3}$/)) {
+ tramId = parseInt(window.location.hash.substr(4));
+ } else if(window.location.hash.match(/^#!v[0-9]+$/)) {
+ vehicleId = window.location.hash.substr(3);
+ } else if(window.location.hash.match(/^#!s[0-9]+$/)) {
+ stopId = window.location.hash.substr(3);
+ } else if(window.location.hash.match(/^#!p[0-9]+$/)) {
+ stopPointId = window.location.hash.substr(3);
+ }
+
+ if(tramId) {
+ vehicleId = tramIdToVehicleId(tramId);
+ }
+
+ if(vehicleId) {
+ feature = vehicles_source.getFeatureById('v' + vehicleId);
+ } else if(stopId) {
+ feature = stops_source.getFeatureById('s' + stopId);
+ } else if(stopPointId) {
+ feature = stop_points_source.getFeatureById('p' + stopPointId);
+ }
+
+ featureClicked(feature);
+ if(feature) {
+ map.getView().setCenter(feature.getGeometry().getCoordinates());
+ }
}
function init() {
@@ -261,25 +342,7 @@
// Display popup on click
map.on('singleclick', function(e) {
var feature = map.forEachFeatureAtPixel(e.pixel, function(feature) { return feature; });
- if(feature) {
- var coordinates = feature.getGeometry().getCoordinates();
-
- deleteChildren(popup_element);
-
- addParaWithText(popup_element, feature.get('name')).className = 'bold';
- switch(feature.getId().substr(0, 1)) {
- case 'v':
- var vehicle_type = parseVehicle(feature.get('id'));
- if(vehicle_type) {
- addParaWithText(popup_element, vehicle_type.num + ' ' + vehicle_type.type);
- }
- break;
- }
-
- popupShow(coordinates, feature.getId());
- } else {
- popupHide();
- }
+ featureClicked(feature);
});
// Change mouse cursor when over marker
@@ -300,9 +363,15 @@
}
});
- updateVehicles();
- updateStops();
- updateStopPoints();
+ $.when(
+ updateVehicles(),
+ updateStops(),
+ updateStopPoints()
+ ).done(function() {
+ hash();
+ });
+
+ window.addEventListener('hashchange', hash);
setTimeout(function() {
if(vehicles_xhr) vehicles_xhr.abort();
--
Gitblit v1.9.1