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