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