From adaa1f628f04a6af247e142edbea679578b90fef Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sat, 15 Apr 2017 11:43:18 +0000
Subject: [PATCH] Add layers with stops and stop points
---
index.js | 275 +++++++++++++++---------------------------------------
1 files changed, 77 insertions(+), 198 deletions(-)
diff --git a/index.js b/index.js
index dfbdf7c..04e285b 100644
--- a/index.js
+++ b/index.js
@@ -1,13 +1,14 @@
-//var ttss_base = 'http://www.ttss.krakow.pl/internetservice/services';
+//var ttss_base = 'http://www.ttss.krakow.pl/internetservice';
var ttss_base = '/proxy.php';
var ttss_refresh = 20000; // 20 seconds
var page_title = document.getElementsByTagName('title')[0];
+var ignore_hashchange = false;
var language = 'en';
var lang_select = document.getElementById('lang-select');
-var stop_id = '';
+var stop_id;
var stop_name = document.getElementById('stop-name');
var stop_name_form = stop_name.form;
var stop_name_autocomplete = document.getElementById('stop-name-autocomplete');
@@ -19,13 +20,12 @@
var times_stop_name = document.getElementById('times-stop-name');
var times_alerts = document.getElementById('times-alerts');
var times_table = document.getElementById('times-table');
-var times_lines = document.getElementById('times-lines');
+//var times_lines = document.getElementById('times-lines');
-/*
+var route_id;
var route_xhr;
var route_line = document.getElementById('route-line');
var route_table = document.getElementById('route-table');
-*/
var refresh_button = document.getElementById('refresh');
var refresh_text = document.getElementById('refresh-text');
@@ -83,109 +83,6 @@
return lang.time_minutes_prefix + ((actual.getTime() - planned.getTime()) / 1000 / 60) + lang.time_minutes_suffix;
}
-function parseVehicle(vehicleId) {
- if(!vehicleId) return;
- if(vehicleId.substr(0, 15) != '635218529567218') {
- console.log('Unknown vehicle, vehicleId=' + vehicleId);
- return;
- }
-
- var id = parseInt(vehicleId.substr(15)) - 736;
- var prefix;
- var type;
- var low; // low floor: 0 = no, 1 - semi, 2 - full
-
- // Single exception - old id used in one case
- if(id == 831) {
- id = 216;
- }
-
- if(101 <= id && id <= 173) {
- prefix = 'HW';
- type = 'E1';
- low = 0;
-
- if((108 <= id && id <= 113) || id == 127 || id == 131 || id == 132 || id == 134 || (137 <= id && id <= 139) || (148 <= id && id <= 150) || (153 <= id && id <= 166) || id == 161) {
- prefix = 'RW';
- }
- } else if(201 <= id && id <= 293) {
- prefix = 'RZ';
- type = '105Na';
- low = 0;
-
- if(246 <= id) {
- prefix = 'HZ';
- }
- if(id == 290) {
- type = '105Nb';
- }
- } else if(301 <= id && id <= 328) {
- prefix = 'RF';
- type = 'GT8S';
- low = 0;
-
- if(id == 313) {
- type = 'GT8C'
- low = 1;
- }
- } else if(401 <= id && id <= 440) {
- prefix = 'HL';
- type = 'EU8N';
- low = 1;
- } else if(451 <= id && id <= 462) {
- prefix = 'HK';
- type = 'N8S-NF';
- low = 0;
-
- if((451 <= id && id <= 453) || id == 462) {
- type = 'N8C-NF';
- low = 1;
- }
- } else if(601 <= id && id <= 650) {
- prefix = 'RP';
- type = 'NGT6 (3)';
- low = 2;
-
- if(id <= 613) {
- type = 'NGT6 (1)';
- } else if (id <= 626) {
- type = 'NGT6 (2)';
- }
- } else if(801 <= id && id <= 824) {
- prefix = 'RY';
- type = 'NGT8';
- low = 2;
- } else if(id == 899) {
- prefix = 'RY';
- type = '126N';
- low = 2;
- } else if(901 <= id && id <= 936) {
- prefix = 'RG';
- type = '2014N';
- low = 2;
-
- if(915 <= id) {
- prefix = 'HG';
- }
- } else if(id === 999) {
- prefix = 'HX';
- type = '405N-Kr';
- low = 1;
- } else {
- console.log('Unknown vehicle, vehicleId=' + vehicleId + ', id=' + id);
- return;
- }
-
- return {
- vehicleId: vehicleId,
- prefix: prefix,
- id: id,
- num: prefix + id,
- type: type,
- low: low
- };
-}
-
function displayVehicle(vehicleInfo) {
if(!vehicleInfo) return document.createTextNode('');
@@ -210,30 +107,6 @@
.replace('$floor', floor_type);
return span;
-}
-
-function deleteChildren(element) {
- while(element.lastChild) element.removeChild(element.lastChild);
-}
-
-function addElementWithText(parent, element, text) {
- var elem = document.createElement(element);
- elem.appendChild(document.createTextNode(text));
- parent.appendChild(elem);
- return elem;
-}
-
-function addCellWithText(parent, text) {
- return addElementWithText(parent, 'td', text);
-}
-
-function addParaWithText(parent, text) {
- return addElementWithText(parent, 'p', text);
-}
-
-function setText(element, text) {
- deleteChildren(element);
- element.appendChild(document.createTextNode(text));
}
function fail(message, more) {
@@ -271,21 +144,24 @@
nav.className = nav.className.replace(' loading', '');
}
-function loadTimes(stopId = null, clearRoute = false) {
+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 + ', ' + clearRoute + ')');
+ console.log('loadTimes(' + stopId + ')');
+ stop_id = stopId;
- window.location.hash = '#!' + language + stop_id;
+ ignore_hashchange = true;
+ window.location.hash = '#!' + language + stopId;
+ ignore_hashchange = false;
refresh_button.removeAttribute('disabled');
loading_start();
times_xhr = $.get(
- ttss_base + '/passageInfo/stopPassages/stop'
+ ttss_base + '/services/passageInfo/stopPassages/stop'
+ '?stop=' + encodeURIComponent(stopId)
+ '&mode=departure'
).done(function(data) {
@@ -293,13 +169,7 @@
setText(page_title, lang.page_title_stop_name.replace('$stop', data.stopName));
deleteChildren(times_alerts);
deleteChildren(times_table);
- deleteChildren(times_lines);
- /*
- if(clearRoute) {
- deleteChildren(route_line);
- deleteChildren(route_table);
- }
- */
+ //deleteChildren(times_lines);
for(var i = 0, il = data.generalAlerts.length; i < il; i++) {
addParaWithText(times_alerts, data.generalAlerts[i].title);
@@ -315,6 +185,7 @@
addCellWithText(tr, '');
tr.className = 'active';
+ tr.addEventListener('click', function(tripId){ return function(){ loadRoute(tripId); } }(data.old[i].tripId) );
times_table.appendChild(tr);
}
@@ -337,65 +208,71 @@
} else if(parseInt(delay) > 3) {
tr.className = 'warning';
}
+
+ tr.addEventListener('click', function(tripId){ return function(){ loadRoute(tripId); } }(data.actual[i].tripId) );
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);
-
- /*
- tr.addEventListener('click', function(routeId, routeTr){ return function(e) {
- var trs = tr.parentNode;
- for(var i = 0; i < trs.childNodes.length; i++) {
- trs.childNodes[i].removeAttribute('class');
- }
- routeTr.className = 'warning';
-
- if(route_xhr) route_xhr.abort();
- route_xhr = $.get(
- ttss_base + '/routeInfo/routeStops'
- + '?routeId=' + encodeURIComponent(routeId)
- ).done(function(data) {
- setText(route_line, data.route.name + ': '
- + data.route.directions.join(' - '));
- deleteChildren(route_table);
-
- routeTr.className = 'success';
-
- for(var i = 0, il = data.stops.length; i < il; i++) {
- var tr = document.createElement('tr');
- addCellWithText(tr, data.stops[i].name);
- route_table.appendChild(tr);
- }
- }).fail(fail_ajax);
- }}(data.routes[i].id, tr));
- */
-
times_lines.appendChild(tr);
-
- for(var j = 0, jl = data.routes[i].alerts.length; j < jl; j++) {
- addParaWithText(
- times_alerts,
- lang.line_alert_pattern
- .replace('$line', data.routes[i].name)
- .replace('$alert', data.routes[i].alerts[j].title)
- );
- }
}
+ */
startTimer(new Date());
fail_hide();
- times_timer = setTimeout(function(){ loadTimes(); }, ttss_refresh);
+ times_timer = setTimeout(function(){ loadTimes(); loadRoute(); }, ttss_refresh);
}).fail(fail_ajax).always(loading_end);
}
-function declinate(num, singular, plural) {
- if(num == 1) return num + ' ' + singular;
- return num + ' ' + plural;
+function loadRoute(tripId) {
+ if(!tripId) tripId = route_id;
+ if(!tripId) return;
+
+ console.log('loadRoute(' + tripId + ')');
+ route_id = tripId;
+
+ if(route_xhr) route_xhr.abort();
+ route_xhr = $.get(
+ ttss_base + '/services/tripInfo/tripPassages'
+ + '?tripId=' + encodeURIComponent(tripId)
+ + '&mode=departure'
+ ).done(function(data) {
+ if(!data.routeName || !data.directionText || data.old.length + data.actual.length == 0) {
+ route_id = null;
+ return;
+ }
+
+ setText(route_line, data.routeName + ' ' + data.directionText);
+ 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);
+
+ tr.className = 'active';
+ tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(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') {
+ tr.className = 'success';
+ }
+ tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(data.actual[i].stop.shortName) );
+ route_table.appendChild(tr);
+ }
+ }).fail(fail_ajax);
}
function startTimer(date) {
@@ -433,12 +310,6 @@
}, interval);
}
-var decodeEntitiesTextArea = document.createElement('textarea');
-function decodeEntities(text) {
- decodeEntitiesTextArea.innerHTML = text;
- return decodeEntitiesTextArea.value;
-}
-
function translate() {
var elements = document.querySelectorAll('*[data-translate]');
@@ -456,11 +327,16 @@
if(stop_id) return;
+ setText(page_title, lang.page_title);
+
if(stop_name_autocomplete.value) {
setText(refresh_text, lang.select_stop_click_go);
} else {
setText(refresh_text, lang.enter_stop_name_to_begin);
}
+
+ loadTimes();
+ loadRoute();
}
function change_language(lang) {
@@ -482,17 +358,20 @@
document.body.removeChild(document.getElementById('lang_script'));
document.body.appendChild(script);
+ ignore_hashchange = true;
window.location.hash = '#!' + language + stop_id;
+ ignore_hashchange = false;
}
function hash() {
+ if(ignore_hashchange) 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));
- loadTimes(stop_id);
}
}
@@ -500,11 +379,11 @@
if(stop_name_autocomplete_xhr) stop_name_autocomplete_xhr.abort();
stop_name_autocomplete_xhr = $.get(
- ttss_base + '/lookup/autocomplete/json'
- + '?query=' + encodeURIComponent(stop_name.value)
+ 'stops.php?query=' + encodeURIComponent(stop_name.value)
).done(function(data) {
deleteChildren(stop_name_autocomplete);
- for(var i = 1, il = data.length; i < il; i++) {
+ for(var i = 0, il = data.length; i < il; i++) {
+ if(data[i].type != 'stop') continue;
if(data[i].id > 6000) continue;
var opt = document.createElement('option');
opt.value = data[i].id;
@@ -543,12 +422,12 @@
stop_name_form.addEventListener('submit', function(e) {
e.preventDefault();
if(!stop_name_autocomplete.value) return;
- stop_id = stop_name_autocomplete.value;
- loadTimes(stop_id, true);
+ loadTimes(stop_name_autocomplete.value);
});
refresh_button.addEventListener('click', function(e) {
- loadTimes(stop_id);
+ loadTimes();
+ loadRoute();
});
alert_close.addEventListener('click', function(e) {
--
Gitblit v1.9.1