Improved www.ttss.krakow.pl
tsedor
2018-09-01 22ceb491d64d539150ec9a3a95f551677f1b314b
commit | author | age
68aeb4 1 //var ttss_base = 'http://www.ttss.krakow.pl/internetservice';
c25e91 2 var ttss_base = '/proxy.php';
JK 3 var ttss_refresh = 20000; // 20 seconds
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 + ')');
969115 88     stop_id = stopId;
9e6757 89     
5de24f 90     ignore_hashchange = true;
969115 91     window.location.hash = '#!' + language + stopId;
c25e91 92     refresh_button.removeAttribute('disabled');
JK 93     
ac721d 94     loading_start();
c25e91 95     times_xhr = $.get(
68aeb4 96         ttss_base + '/services/passageInfo/stopPassages/stop'
c25e91 97             + '?stop=' + encodeURIComponent(stopId)
JK 98             + '&mode=departure'
99     ).done(function(data) {
100         setText(times_stop_name, data.stopName);
9e6757 101         setText(page_title, lang.page_title_stop_name.replace('$stop', data.stopName));
c25e91 102         deleteChildren(times_alerts);
JK 103         deleteChildren(times_table);
68aeb4 104         //deleteChildren(times_lines);
c25e91 105         
JK 106         for(var i = 0, il = data.generalAlerts.length; i < il; i++) {
09944e 107             addParaWithText(times_alerts, data.generalAlerts[i].title);
c25e91 108         }
JK 109         
110         for(var i = 0, il = data.old.length; i < il; i++) {
111             var tr = document.createElement('tr');
112             addCellWithText(tr, data.old[i].patternText);
8fb34c 113             var dir_cell = addCellWithText(tr, data.old[i].direction);
ad39c7 114             var vehicle = parseVehicle(data.old[i].vehicleId);
4b0eda 115             dir_cell.appendChild(displayVehicle(vehicle));
218a38 116             addCellWithText(tr, (vehicle ? vehicle.num : '')).className = 'vehicleData';
c25e91 117             var status = parseStatus(data.old[i]);
JK 118             addCellWithText(tr, status);
119             addCellWithText(tr, '');
120             
121             tr.className = 'active';
0f11d0 122             tr.addEventListener('click', function(tripId, vehicleInfo) {
JK 123                 return function(){ loadRoute(tripId, vehicleInfo); }
ad39c7 124             }(data.old[i].tripId, vehicle));
c25e91 125             times_table.appendChild(tr);
JK 126         }
127         
128         for(var i = 0, il = data.actual.length; i < il; i++) {
129             var tr = document.createElement('tr');
130             addCellWithText(tr, data.actual[i].patternText);
8fb34c 131             var dir_cell = addCellWithText(tr, data.actual[i].direction);
4b0eda 132             var vehicle = parseVehicle(data.actual[i].vehicleId);
JK 133             dir_cell.appendChild(displayVehicle(vehicle));
218a38 134             addCellWithText(tr, (vehicle ? vehicle.num : '')).className = 'vehicleData';
c25e91 135             var status = parseStatus(data.actual[i]);
e2b283 136             var status_cell = addCellWithText(tr, status);
c25e91 137             var delay = parseDelay(data.actual[i]);
e2b283 138             var delay_cell = addCellWithText(tr, delay);
c25e91 139             
977871 140             if(data.actual[i].status == 'STOPPING') {
e2b283 141                 tr.className = 'success';
7a58f5 142                 if (data.actual[i].actualRelativeTime <= 0) {
P 143                     status_cell.className = 'status-boarding';
144                 }
e2b283 145             } else if(parseInt(delay) > 9) {
JK 146                 tr.className = 'danger';
147                 delay_cell.className = 'status-delayed';
148             } else if(parseInt(delay) > 3) {
149                 tr.className = 'warning';
150             }
68aeb4 151             
0f11d0 152             tr.addEventListener('click', function(tripId, vehicleInfo) {
JK 153                 return function(){ loadRoute(tripId, vehicleInfo); }
154             }(data.actual[i].tripId, vehicle));
c25e91 155             times_table.appendChild(tr);
JK 156         }
157         
68aeb4 158         /*
c25e91 159         for(var i = 0, il = data.routes.length; i < il; i++) {
JK 160             var tr = document.createElement('tr');
161             addCellWithText(tr, data.routes[i].name);
162             addCellWithText(tr, data.routes[i].directions.join(' - '));
163             addCellWithText(tr, data.routes[i].authority);
164             times_lines.appendChild(tr);
165         }
68aeb4 166         */
c25e91 167         
JK 168         startTimer(new Date());
e54544 169         fail_hide();
c25e91 170         
68aeb4 171         times_timer = setTimeout(function(){ loadTimes(); loadRoute(); }, ttss_refresh);
ac721d 172     }).fail(fail_ajax).always(loading_end);
68aeb4 173 }
JK 174
0f11d0 175 function loadRoute(tripId, vehicleInfo) {
68aeb4 176     if(!tripId) tripId = route_id;
JK 177     if(!tripId) return;
178     
765604 179     if(vehicleInfo === undefined) vehicleInfo = route_vehicle_info;
0f11d0 180     
68aeb4 181     console.log('loadRoute(' + tripId + ')');
JK 182     route_id = tripId;
0f11d0 183     route_vehicle_info = vehicleInfo;
68aeb4 184     
JK 185     if(route_xhr) route_xhr.abort();
186     route_xhr = $.get(
187         ttss_base + '/services/tripInfo/tripPassages'
188             + '?tripId=' + encodeURIComponent(tripId)
189             + '&mode=departure'
190     ).done(function(data) {
191         if(!data.routeName || !data.directionText || data.old.length + data.actual.length == 0) {
192             route_id = null;
193             return;
194         }
195         
196         setText(route_line, data.routeName + ' ' + data.directionText);
0f11d0 197         
JK 198         deleteChildren(route_vehicle);
199         if(vehicleInfo) {
200             var span = displayVehicle(vehicleInfo);
218a38 201             if(span) {
JK 202                 setText(route_vehicle, span.title);
203             }
0f11d0 204             route_vehicle.insertBefore(span, route_vehicle.firstChild);
JK 205         }
206         
68aeb4 207         deleteChildren(route_table);
JK 208         
209         for(var i = 0, il = data.old.length; i < il; i++) {
210             var tr = document.createElement('tr');
211             addCellWithText(tr, data.old[i].actualTime || data.old[i].plannedTime);
212             addCellWithText(tr, data.old[i].stop_seq_num + '. ' + data.old[i].stop.name);
213             
214             tr.className = 'active';
73da94 215             tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(data.old[i].stop.shortName) );
68aeb4 216             route_table.appendChild(tr);
JK 217         }
218         
219         for(var i = 0, il = data.actual.length; i < il; i++) {
220             var tr = document.createElement('tr');
221             addCellWithText(tr, data.actual[i].actualTime || data.actual[i].plannedTime);
222             addCellWithText(tr, data.actual[i].stop_seq_num + '. ' + data.actual[i].stop.name);
223             
224             if(data.actual[i].status == 'STOPPING') {
225                 tr.className = 'success';
226             }
73da94 227             tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(data.actual[i].stop.shortName) );
68aeb4 228             route_table.appendChild(tr);
JK 229         }
230     }).fail(fail_ajax);
c25e91 231 }
JK 232
233 function startTimer(date) {
234     if(date) {
9e6757 235         setText(refresh_text, lang.last_refreshed.replace('$time', lang.time_now));
c25e91 236         refresh_time = date;
JK 237     }
238     if(!refresh_time) return;
239     if(refresh_timer) clearInterval(refresh_timer);
240     
241     var now = new Date();
242     var ms = now.getTime() - refresh_time.getTime();
243     
244     var interval = 1000;
245     if(ms >= 120000) interval = 60000;
246     
247     refresh_timer = setInterval(function() {
248         var now = new Date();
249         var ms = now.getTime() - refresh_time.getTime();
250         
251         if(ms >= 120000) {
9e6757 252             setText(refresh_text, lang.last_refreshed.replace(
JK 253                 '$time',
254                 lang.time_minutes_ago_prefix + Math.floor(ms / 60000)
255                     + lang.time_minutes_ago_suffix
256             ));
c25e91 257             startTimer();
JK 258         } else {
9e6757 259             setText(refresh_text, lang.last_refreshed.replace(
JK 260                 '$time',
261                 lang.time_seconds_ago_prefix + Math.floor(ms / 1000)
262                     + lang.time_seconds_ago_suffix
263             ));
c25e91 264         }
JK 265     }, interval);
b50208 266 }
JK 267
9e6757 268 function translate() {
JK 269     var elements = document.querySelectorAll('*[data-translate]');
270     
271     var text_name;
272     for(var i = 0; i < elements.length; i++) {
273         text_name = elements[i].dataset.translate;
274         if(lang[text_name] == undefined) {
275             console.log('Missing translation: ' + text_name);
276             continue;
277         }
278         setText(elements[i], lang[text_name]);
279     }
280     
281     stop_name.setAttribute('placeholder', lang.stop_name_placeholder);
282     
283     if(stop_name_autocomplete.value) {
284         setText(refresh_text, lang.select_stop_click_go);
285     } else {
286         setText(refresh_text, lang.enter_stop_name_to_begin);
287     }
5de24f 288     
5ac72e 289     setText(page_title, lang.page_title);
JK 290     
960548 291     if(!stop_id) return;
JK 292     
5de24f 293     loadTimes();
JK 294     loadRoute();
9e6757 295 }
JK 296
297 function change_language(lang) {
a9e7f2 298     if(!lang || lang.length != 2) return false;
JK 299     if(lang == language) return false;
9e6757 300     lang_select.value = lang;
JK 301     if(!lang_select.value) {
302         lang_select.value = language;
303         return;
304     }
305     language = lang;
306     
307     var script = document.createElement('script');
308     script.type = 'text/javascript';
309     script.src = 'lang_' + lang + '.js';
310     script.id = 'lang_script';
311     script.onload = translate;
312     
313     document.body.removeChild(document.getElementById('lang_script'));
314     document.body.appendChild(script);
315     
5de24f 316     ignore_hashchange = true;
9e6757 317     window.location.hash = '#!' + language + stop_id;
a9e7f2 318     
JK 319     return true;
9e6757 320 }
JK 321
322 function hash() {
a9e7f2 323     if(ignore_hashchange) {
JK 324         ignore_hashchange = false;
325         return;
326     }
5de24f 327     
9e6757 328     if(window.location.hash.match(/^#![0-9]+$/)) {
JK 329         loadTimes(parseInt(window.location.hash.substr(2)));
330     } else if(window.location.hash.match(/^#![a-z]{2}[0-9]*$/)) {
331         var stop = parseInt(window.location.hash.substr(4));
332         if(stop) stop_id = stop;
a9e7f2 333         
JK 334         if(!change_language(window.location.hash.substr(2, 2))) {
335             loadTimes(parseInt(window.location.hash.substr(2)));
336         }
9e6757 337     }
JK 338 }
339
e95a73 340 function stop_autocomplete() {
JK 341     if(stop_name_autocomplete_xhr) stop_name_autocomplete_xhr.abort();
342     
343     stop_name_autocomplete_xhr = $.get(
ced309 344         'stops.php?query=' + encodeURIComponent(stop_name.value)
e95a73 345     ).done(function(data) {
JK 346         deleteChildren(stop_name_autocomplete);
ced309 347         for(var i = 0, il = data.length; i < il; i++) {
JK 348             if(data[i].type != 'stop') continue;
e95a73 349             if(data[i].id > 6000) continue;
JK 350             var opt = document.createElement('option');
351             opt.value = data[i].id;
9edf7f 352             setText(opt, data[i].name);
e95a73 353             stop_name_autocomplete.appendChild(opt);
JK 354         }
355         
356         if(!stop_id) setText(refresh_text, lang.select_stop_click_go);
357     }).fail(fail_ajax);
358 }
359
c25e91 360 function init() {
JK 361     if(!window.jQuery) {
9e6757 362         fail(lang.jquery_not_loaded);
c25e91 363         return;
JK 364     }
365     
366     $.ajaxSetup({
367         dataType: 'json',
12310b 368         timeout: 10000,
9e6757 369     });
JK 370     
371     lang_select.addEventListener('input', function(e) {
372         change_language(lang_select.value);
c25e91 373     });
JK 374     
375     stop_name.addEventListener('input', function(e) {
376         if(!stop_name.value) return;
e95a73 377         if(stop_name_autocomplete_timer) clearTimeout(stop_name_autocomplete_timer);
c25e91 378         
e95a73 379         stop_name_autocomplete_timer = setTimeout(stop_autocomplete, 100);
c25e91 380     });
JK 381     
9e6757 382     setText(refresh_text, lang.enter_stop_name_to_begin);
c25e91 383     
JK 384     stop_name_form.addEventListener('submit', function(e) {
385         e.preventDefault();
386         if(!stop_name_autocomplete.value) return;
969115 387         loadTimes(stop_name_autocomplete.value);
c25e91 388     });
JK 389     
4b0eda 390     refresh_button.addEventListener('click', function() {
68aeb4 391         loadTimes();
JK 392         loadRoute();
c25e91 393     });
JK 394     
4b0eda 395     alert_close.addEventListener('click', function() {
c25e91 396         alert.style.display = 'none';
JK 397     });
492fc5 398     
4b0eda 399     vehicle_data.addEventListener('click', function(e) {
JK 400         e.preventDefault();
401         vehicle_data.style.display = 'none';
402         setText(vehicle_data_style, '.vehicleData { display: table-cell; }')
403     });
404     
9e6757 405     hash();
JK 406     
407     window.addEventListener('hashchange', hash);
572390 408     
JK 409     checkVersionInit();
c25e91 410 }
JK 411
412 init();