Improved www.ttss.krakow.pl
Jacek Kowalski
2019-05-04 d87cf076349d354b829bfe896ef11247b1182dd9
commit | author | age
f4a54f 1 "use strict";
JK 2
c25e91 3 var ttss_refresh = 20000; // 20 seconds
JK 4
f50bae 5 var page_title = document.getElementsByTagName('title')[0];
5de24f 6 var ignore_hashchange = false;
f50bae 7
9e9b25 8 var language = 'pl';
9e6757 9 var lang_select = document.getElementById('lang-select');
JK 10
8f89f0 11 var stop_id = '';
c25e91 12 var stop_name = document.getElementById('stop-name');
JK 13 var stop_name_form = stop_name.form;
14 var stop_name_autocomplete = document.getElementById('stop-name-autocomplete');
15 var stop_name_autocomplete_xhr;
e95a73 16 var stop_name_autocomplete_timer;
c25e91 17
JK 18 var times_xhr;
19 var times_timer;
448c31 20 var times_stop_type = document.getElementById('times-stop-type');
c25e91 21 var times_stop_name = document.getElementById('times-stop-name');
JK 22 var times_alerts = document.getElementById('times-alerts');
23 var times_table = document.getElementById('times-table');
68aeb4 24 //var times_lines = document.getElementById('times-lines');
c25e91 25
68aeb4 26 var route_id;
c25e91 27 var route_xhr;
JK 28 var route_line = document.getElementById('route-line');
29 var route_table = document.getElementById('route-table');
0f11d0 30 var route_vehicle = document.getElementById('route-vehicle');
765604 31 var route_vehicle_info;
c25e91 32
JK 33 var refresh_button = document.getElementById('refresh');
34 var refresh_text = document.getElementById('refresh-text');
35 var refresh_time;
36 var refresh_timer;
37
38 var alert = document.getElementById('alert');
39 var alert_text = document.getElementById('alert-text');
40 var alert_close = document.getElementById('alert-close');
41
0f9bcf 42 var nav = document.getElementsByTagName('nav')[0];
4b0eda 43 var vehicle_data = document.getElementById('vehicle-data');
JK 44 var vehicle_data_style = document.getElementById('vehicle-data-style');
ac721d 45
c25e91 46 function fail(message, more) {
JK 47     if(times_timer) clearTimeout(times_timer);
48     
49     setText(alert_text, message);
50     alert.style.display = 'block';
51     
572390 52     console.log(message + (more ? ' More details follow.' : ''));
c25e91 53     if(more) console.log(more);
JK 54 }
55
56 function fail_ajax(data) {
57     // abort() is not a failure
faad2a 58     if(data.readyState === 0) return;
c25e91 59     
faad2a 60     if(data.status === 0) {
9e6757 61         fail(lang.error_request_failed_connectivity, data);
c25e91 62     } else if (data.statusText) {
9e6757 63         fail(lang.error_request_failed_status.replace('$status', data.statusText), data);
c25e91 64     } else {
9e6757 65         fail(lang.error_request_failed, data);
c25e91 66     }
JK 67 }
68
e54544 69 function fail_hide() {
JK 70     alert.style.display = 'none';
71 }
72
ac721d 73 function loading_start() {
0f9bcf 74     nav.className += ' loading';
ac721d 75 }
JK 76
77 function loading_end() {
0f9bcf 78     nav.className = nav.className.replace(' loading', '');
ac721d 79 }
JK 80
68aeb4 81 function loadTimes(stopId) {
c25e91 82     if(!stopId) stopId = stop_id;
JK 83     if(!stopId) return;
84     
85     if(times_timer) clearTimeout(times_timer);
86     if(times_xhr) times_xhr.abort();
87     
68aeb4 88     console.log('loadTimes(' + stopId + ')');
cbce8b 89     
JK 90     loading_start();
91     
4bfa36 92     var prefix = stopId.substr(0, 1);
cbce8b 93     var stop = stopId.substr(1);
4bfa36 94     var url = ttss_urls[prefix];
cbce8b 95     
969115 96     stop_id = stopId;
9e6757 97     
5de24f 98     ignore_hashchange = true;
969115 99     window.location.hash = '#!' + language + stopId;
c25e91 100     refresh_button.removeAttribute('disabled');
JK 101     
3dd66f 102     var alternative_stop = null;
c206d1 103     var candidate;
3dd66f 104     for(var i = 0; i < stop_name_autocomplete.options.length; i++) {
JK 105         candidate = stop_name_autocomplete.options[i].value;
106         if(candidate.substr(0, 1) != prefix && candidate.substr(1) == stop) {
107             alternative_stop = candidate;
108             break;
109         }
110     }
111     
c25e91 112     times_xhr = $.get(
cbce8b 113         url + '/services/passageInfo/stopPassages/stop'
JK 114             + '?stop=' + encodeURIComponent(stop)
c25e91 115             + '&mode=departure'
JK 116     ).done(function(data) {
448c31 117         setText(times_stop_type, lang.types['s' + prefix]);
ca42d3 118         setText(times_stop_name, normalizeName(data.stopName));
JK 119         setText(page_title, lang.page_title_stop_name.replace('$stop', normalizeName(data.stopName)));
c25e91 120         deleteChildren(times_alerts);
JK 121         deleteChildren(times_table);
68aeb4 122         //deleteChildren(times_lines);
c25e91 123         
3dd66f 124         if(alternative_stop !== null) {
JK 125             var a = addParaWithText(times_alerts, '');
126             a = addElementWithText(a, 'a', (prefix == 'b' ? lang.departures_for_trams : lang.departures_for_buses));
127             a.href = '';
128             a.onclick = function(e) {
129                 e.preventDefault();
130                 loadTimes(alternative_stop);
131             };
132             
133         }
134         
c206d1 135         var i, il;
JK 136         for(i = 0, il = data.generalAlerts.length; i < il; i++) {
09944e 137             addParaWithText(times_alerts, data.generalAlerts[i].title);
c25e91 138         }
JK 139         
cb5a77 140         var all_departures = data.old.concat(data.actual);
JK 141         var tr, dir_cell, vehicle, status, status_cell, delay, delay_cell;
c206d1 142         for(i = 0, il = all_departures.length; i < il; i++) {
JK 143             tr = document.createElement('tr');
cb5a77 144             addCellWithText(tr, all_departures[i].patternText);
c206d1 145             dir_cell = addCellWithText(tr, all_departures[i].direction);
JK 146             vehicle = parseVehicle(prefix + all_departures[i].vehicleId);
4b0eda 147             dir_cell.appendChild(displayVehicle(vehicle));
218a38 148             addCellWithText(tr, (vehicle ? vehicle.num : '')).className = 'vehicleData';
c206d1 149             status = parseStatus(all_departures[i]);
JK 150             status_cell = addCellWithText(tr, status);
151             delay = parseDelay(all_departures[i]);
152             delay_cell = addCellWithText(tr, delay);
c25e91 153             
cb5a77 154             if(i < data.old.length) {
JK 155                 tr.className = 'active';
156             } else if(all_departures[i].status === 'STOPPING') {
e2b283 157                 tr.className = 'success';
cb5a77 158                 if (all_departures[i].actualRelativeTime <= 0) {
7a58f5 159                     status_cell.className = 'status-boarding';
P 160                 }
e2b283 161             } else if(parseInt(delay) > 9) {
JK 162                 tr.className = 'danger';
163                 delay_cell.className = 'status-delayed';
164             } else if(parseInt(delay) > 3) {
165                 tr.className = 'warning';
166             }
68aeb4 167             
0f11d0 168             tr.addEventListener('click', function(tripId, vehicleInfo) {
JK 169                 return function(){ loadRoute(tripId, vehicleInfo); }
cb5a77 170             }(prefix + all_departures[i].tripId, vehicle));
c25e91 171             times_table.appendChild(tr);
JK 172         }
173         
68aeb4 174         /*
c25e91 175         for(var i = 0, il = data.routes.length; i < il; i++) {
JK 176             var tr = document.createElement('tr');
177             addCellWithText(tr, data.routes[i].name);
178             addCellWithText(tr, data.routes[i].directions.join(' - '));
179             addCellWithText(tr, data.routes[i].authority);
180             times_lines.appendChild(tr);
181         }
68aeb4 182         */
c25e91 183         
JK 184         startTimer(new Date());
e54544 185         fail_hide();
c25e91 186         
68aeb4 187         times_timer = setTimeout(function(){ loadTimes(); loadRoute(); }, ttss_refresh);
ac721d 188     }).fail(fail_ajax).always(loading_end);
2b6454 189     return times_xhr;
68aeb4 190 }
JK 191
0f11d0 192 function loadRoute(tripId, vehicleInfo) {
68aeb4 193     if(!tripId) tripId = route_id;
JK 194     if(!tripId) return;
195     
765604 196     if(vehicleInfo === undefined) vehicleInfo = route_vehicle_info;
0f11d0 197     
68aeb4 198     console.log('loadRoute(' + tripId + ')');
cbce8b 199     
4bfa36 200     var prefix = tripId.substr(0, 1);
cbce8b 201     var trip = tripId.substr(1);
JK 202     
68aeb4 203     route_id = tripId;
0f11d0 204     route_vehicle_info = vehicleInfo;
68aeb4 205     
JK 206     if(route_xhr) route_xhr.abort();
207     route_xhr = $.get(
cb5a77 208         ttss_urls[prefix] + '/services/tripInfo/tripPassages'
cbce8b 209             + '?tripId=' + encodeURIComponent(trip)
68aeb4 210             + '&mode=departure'
JK 211     ).done(function(data) {
212         if(!data.routeName || !data.directionText || data.old.length + data.actual.length == 0) {
213             route_id = null;
214             return;
215         }
216         
217         setText(route_line, data.routeName + ' ' + data.directionText);
0f11d0 218         
JK 219         deleteChildren(route_vehicle);
220         if(vehicleInfo) {
221             var span = displayVehicle(vehicleInfo);
218a38 222             if(span) {
JK 223                 setText(route_vehicle, span.title);
224             }
0f11d0 225             route_vehicle.insertBefore(span, route_vehicle.firstChild);
JK 226         }
227         
68aeb4 228         deleteChildren(route_table);
JK 229         
cb5a77 230         var all_departures = data.old.concat(data.actual);
JK 231         var tr;
232         for(var i = 0, il = all_departures.length; i < il; i++) {
233             tr = document.createElement('tr');
234             addCellWithText(tr, all_departures[i].actualTime || all_departures[i].plannedTime);
235             addCellWithText(tr, all_departures[i].stop_seq_num + '. ' + all_departures[i].stop.name);
68aeb4 236             
cb5a77 237             if(i < data.old.length) {
JK 238                 tr.className = 'active';
239             } else if(all_departures[i].status === 'STOPPING') {
68aeb4 240                 tr.className = 'success';
JK 241             }
cb5a77 242             tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(prefix + all_departures[i].stop.shortName) );
68aeb4 243             route_table.appendChild(tr);
JK 244         }
245     }).fail(fail_ajax);
2b6454 246     return route_xhr;
c25e91 247 }
JK 248
249 function startTimer(date) {
250     if(date) {
9e6757 251         setText(refresh_text, lang.last_refreshed.replace('$time', lang.time_now));
c25e91 252         refresh_time = date;
JK 253     }
254     if(!refresh_time) return;
255     if(refresh_timer) clearInterval(refresh_timer);
256     
257     var now = new Date();
258     var ms = now.getTime() - refresh_time.getTime();
259     
260     var interval = 1000;
261     if(ms >= 120000) interval = 60000;
262     
263     refresh_timer = setInterval(function() {
264         var now = new Date();
265         var ms = now.getTime() - refresh_time.getTime();
266         
267         if(ms >= 120000) {
9e6757 268             setText(refresh_text, lang.last_refreshed.replace(
JK 269                 '$time',
270                 lang.time_minutes_ago_prefix + Math.floor(ms / 60000)
271                     + lang.time_minutes_ago_suffix
272             ));
c25e91 273             startTimer();
JK 274         } else {
9e6757 275             setText(refresh_text, lang.last_refreshed.replace(
JK 276                 '$time',
277                 lang.time_seconds_ago_prefix + Math.floor(ms / 1000)
278                     + lang.time_seconds_ago_suffix
279             ));
c25e91 280         }
JK 281     }, interval);
b50208 282 }
JK 283
9e6757 284 function translate() {
JK 285     var elements = document.querySelectorAll('*[data-translate]');
286     
287     var text_name;
288     for(var i = 0; i < elements.length; i++) {
289         text_name = elements[i].dataset.translate;
f4dd75 290         if(typeof lang[text_name] === 'undefined') {
9e6757 291             console.log('Missing translation: ' + text_name);
JK 292             continue;
293         }
294         setText(elements[i], lang[text_name]);
295     }
296     
297     stop_name.setAttribute('placeholder', lang.stop_name_placeholder);
298     
299     if(stop_name_autocomplete.value) {
300         setText(refresh_text, lang.select_stop_click_go);
301     } else {
302         setText(refresh_text, lang.enter_stop_name_to_begin);
303     }
5de24f 304     
5ac72e 305     setText(page_title, lang.page_title);
JK 306     
960548 307     if(!stop_id) return;
JK 308     
5de24f 309     loadTimes();
JK 310     loadRoute();
9e6757 311 }
JK 312
313 function change_language(lang) {
a9e7f2 314     if(!lang || lang.length != 2) return false;
JK 315     if(lang == language) return false;
9e6757 316     lang_select.value = lang;
JK 317     if(!lang_select.value) {
318         lang_select.value = language;
319         return;
320     }
321     language = lang;
322     
1eea9a 323     var old_script = document.getElementById('lang_script');
JK 324     var old_version = old_script.src.match(/\?v[0-9]+/)[0];
9e6757 325     var script = document.createElement('script');
JK 326     script.type = 'text/javascript';
1eea9a 327     script.src = 'lang_' + lang + '.js' + (old_version ? old_version : '');
9e6757 328     script.id = 'lang_script';
JK 329     script.onload = translate;
330     
1eea9a 331     document.body.removeChild(old_script);
9e6757 332     document.body.appendChild(script);
JK 333     
5de24f 334     ignore_hashchange = true;
9e6757 335     window.location.hash = '#!' + language + stop_id;
a9e7f2 336     
JK 337     return true;
9e6757 338 }
JK 339
340 function hash() {
a9e7f2 341     if(ignore_hashchange) {
JK 342         ignore_hashchange = false;
343         return;
344     }
5de24f 345     
9e6757 346     if(window.location.hash.match(/^#![0-9]+$/)) {
cbce8b 347         loadTimes('t' + window.location.hash.substr(2));
JK 348     } else if(window.location.hash.match(/^#![bt][0-9]+$/)) {
349         loadTimes(window.location.hash.substr(2));
9e6757 350     } else if(window.location.hash.match(/^#![a-z]{2}[0-9]*$/)) {
cbce8b 351         var stop = 't' + window.location.hash.substr(4);
9e6757 352         if(stop) stop_id = stop;
a9e7f2 353         
JK 354         if(!change_language(window.location.hash.substr(2, 2))) {
cbce8b 355             loadTimes(stop);
JK 356         }
357     } else if(window.location.hash.match(/^#![a-z]{2}[bt][0-9]*$/)) {
358         var stop = window.location.hash.substr(4);
359         if(stop) stop_id = stop;
360         
361         if(!change_language(window.location.hash.substr(2, 2))) {
362             loadTimes(stop);
a9e7f2 363         }
9e6757 364     }
JK 365 }
366
e95a73 367 function stop_autocomplete() {
JK 368     if(stop_name_autocomplete_xhr) stop_name_autocomplete_xhr.abort();
369     
370     stop_name_autocomplete_xhr = $.get(
cbce8b 371         'stops/?query=' + encodeURIComponent(stop_name.value)
e95a73 372     ).done(function(data) {
JK 373         deleteChildren(stop_name_autocomplete);
ced309 374         for(var i = 0, il = data.length; i < il; i++) {
e95a73 375             var opt = document.createElement('option');
JK 376             opt.value = data[i].id;
76f5c4 377             setText(opt, lang.select_stop_type[data[i].id.substr(0,1)].replace('$stop', data[i].name));
e95a73 378             stop_name_autocomplete.appendChild(opt);
JK 379         }
380         
381         if(!stop_id) setText(refresh_text, lang.select_stop_click_go);
382     }).fail(fail_ajax);
2b6454 383     return stop_name_autocomplete_xhr;
e95a73 384 }
JK 385
c25e91 386 function init() {
9e6757 387     lang_select.addEventListener('input', function(e) {
JK 388         change_language(lang_select.value);
c25e91 389     });
JK 390     
391     stop_name.addEventListener('input', function(e) {
392         if(!stop_name.value) return;
e95a73 393         if(stop_name_autocomplete_timer) clearTimeout(stop_name_autocomplete_timer);
c25e91 394         
e95a73 395         stop_name_autocomplete_timer = setTimeout(stop_autocomplete, 100);
c25e91 396     });
JK 397     
9e6757 398     setText(refresh_text, lang.enter_stop_name_to_begin);
c25e91 399     
JK 400     stop_name_form.addEventListener('submit', function(e) {
401         e.preventDefault();
402         if(!stop_name_autocomplete.value) return;
969115 403         loadTimes(stop_name_autocomplete.value);
c25e91 404     });
JK 405     
4b0eda 406     refresh_button.addEventListener('click', function() {
68aeb4 407         loadTimes();
JK 408         loadRoute();
c25e91 409     });
JK 410     
4b0eda 411     alert_close.addEventListener('click', function() {
c25e91 412         alert.style.display = 'none';
JK 413     });
492fc5 414     
4b0eda 415     vehicle_data.addEventListener('click', function(e) {
JK 416         e.preventDefault();
417         vehicle_data.style.display = 'none';
418         setText(vehicle_data_style, '.vehicleData { display: table-cell; }')
419     });
420     
f0bae0 421     updateVehicleInfo()
JK 422     
9e6757 423     hash();
JK 424     
425     window.addEventListener('hashchange', hash);
572390 426     
JK 427     checkVersionInit();
c25e91 428 }
JK 429
430 init();