commit | author | age
|
7e7221
|
1 |
'use strict'; |
f4a54f
|
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', ''); |
7e7221
|
79 |
} |
JK |
80 |
|
|
81 |
function startTimer(date) { |
|
82 |
if(date) { |
|
83 |
setText(refresh_text, lang.last_refreshed.replace('$time', lang.time_now)); |
|
84 |
refresh_time = date; |
|
85 |
} |
|
86 |
if(!refresh_time) return; |
|
87 |
if(refresh_timer) clearInterval(refresh_timer); |
|
88 |
|
|
89 |
var now = new Date(); |
|
90 |
var ms = now.getTime() - refresh_time.getTime(); |
|
91 |
|
|
92 |
var interval = 1000; |
|
93 |
if(ms >= 120000) interval = 60000; |
|
94 |
|
|
95 |
refresh_timer = setInterval(function() { |
|
96 |
var now = new Date(); |
|
97 |
var ms = now.getTime() - refresh_time.getTime(); |
|
98 |
|
|
99 |
if(ms >= 120000) { |
|
100 |
setText(refresh_text, lang.last_refreshed.replace( |
|
101 |
'$time', |
|
102 |
lang.time_minutes_ago_prefix + Math.floor(ms / 60000) |
|
103 |
+ lang.time_minutes_ago_suffix |
|
104 |
)); |
|
105 |
startTimer(); |
|
106 |
} else { |
|
107 |
setText(refresh_text, lang.last_refreshed.replace( |
|
108 |
'$time', |
|
109 |
lang.time_seconds_ago_prefix + Math.floor(ms / 1000) |
|
110 |
+ lang.time_seconds_ago_suffix |
|
111 |
)); |
|
112 |
} |
|
113 |
}, interval); |
|
114 |
} |
|
115 |
|
|
116 |
function loadRoute(tripId, vehicleInfo) { |
|
117 |
if(!tripId) tripId = route_id; |
|
118 |
if(!tripId) return; |
|
119 |
|
|
120 |
if(vehicleInfo === undefined) vehicleInfo = route_vehicle_info; |
|
121 |
|
|
122 |
console.log('loadRoute(' + tripId + ')'); |
|
123 |
|
|
124 |
var prefix = tripId.substr(0, 1); |
|
125 |
var trip = tripId.substr(1); |
|
126 |
|
|
127 |
route_id = tripId; |
|
128 |
route_vehicle_info = vehicleInfo; |
|
129 |
|
|
130 |
if(route_xhr) route_xhr.abort(); |
|
131 |
route_xhr = $.get( |
|
132 |
ttss_urls[prefix] + '/services/tripInfo/tripPassages' |
|
133 |
+ '?tripId=' + encodeURIComponent(trip) |
|
134 |
+ '&mode=departure' |
|
135 |
).done(function(data) { |
|
136 |
if(!data.routeName || !data.directionText || data.old.length + data.actual.length == 0) { |
|
137 |
route_id = null; |
|
138 |
return; |
|
139 |
} |
|
140 |
|
|
141 |
setText(route_line, data.routeName + ' ' + data.directionText); |
|
142 |
|
|
143 |
deleteChildren(route_vehicle); |
|
144 |
if(vehicleInfo) { |
|
145 |
var span = displayVehicle(vehicleInfo); |
|
146 |
if(span) { |
|
147 |
setText(route_vehicle, span.title); |
|
148 |
} |
|
149 |
route_vehicle.insertBefore(span, route_vehicle.firstChild); |
|
150 |
} |
|
151 |
|
|
152 |
deleteChildren(route_table); |
|
153 |
|
|
154 |
var all_departures = data.old.concat(data.actual); |
|
155 |
var tr; |
|
156 |
for(var i = 0, il = all_departures.length; i < il; i++) { |
|
157 |
tr = document.createElement('tr'); |
|
158 |
addCellWithText(tr, all_departures[i].actualTime || all_departures[i].plannedTime); |
|
159 |
addCellWithText(tr, all_departures[i].stop_seq_num + '. ' + all_departures[i].stop.name); |
|
160 |
|
|
161 |
if(i < data.old.length) { |
|
162 |
tr.className = 'active'; |
|
163 |
} else if(all_departures[i].status === 'STOPPING') { |
|
164 |
tr.className = 'success'; |
|
165 |
} |
|
166 |
tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(prefix + all_departures[i].stop.shortName) ); |
|
167 |
route_table.appendChild(tr); |
|
168 |
} |
|
169 |
}).fail(fail_ajax); |
|
170 |
return route_xhr; |
ac721d
|
171 |
} |
JK |
172 |
|
68aeb4
|
173 |
function loadTimes(stopId) { |
c25e91
|
174 |
if(!stopId) stopId = stop_id; |
JK |
175 |
if(!stopId) return; |
|
176 |
|
|
177 |
if(times_timer) clearTimeout(times_timer); |
|
178 |
if(times_xhr) times_xhr.abort(); |
|
179 |
|
68aeb4
|
180 |
console.log('loadTimes(' + stopId + ')'); |
cbce8b
|
181 |
|
JK |
182 |
loading_start(); |
|
183 |
|
4bfa36
|
184 |
var prefix = stopId.substr(0, 1); |
cbce8b
|
185 |
var stop = stopId.substr(1); |
4bfa36
|
186 |
var url = ttss_urls[prefix]; |
cbce8b
|
187 |
|
969115
|
188 |
stop_id = stopId; |
9e6757
|
189 |
|
5de24f
|
190 |
ignore_hashchange = true; |
969115
|
191 |
window.location.hash = '#!' + language + stopId; |
c25e91
|
192 |
refresh_button.removeAttribute('disabled'); |
JK |
193 |
|
3dd66f
|
194 |
var alternative_stop = null; |
c206d1
|
195 |
var candidate; |
3dd66f
|
196 |
for(var i = 0; i < stop_name_autocomplete.options.length; i++) { |
JK |
197 |
candidate = stop_name_autocomplete.options[i].value; |
2bc9da
|
198 |
if(candidate.substr(0, 1) !== prefix && candidate.substr(1) == stop) { |
3dd66f
|
199 |
alternative_stop = candidate; |
JK |
200 |
break; |
|
201 |
} |
|
202 |
} |
|
203 |
|
c25e91
|
204 |
times_xhr = $.get( |
cbce8b
|
205 |
url + '/services/passageInfo/stopPassages/stop' |
JK |
206 |
+ '?stop=' + encodeURIComponent(stop) |
c25e91
|
207 |
+ '&mode=departure' |
JK |
208 |
).done(function(data) { |
448c31
|
209 |
setText(times_stop_type, lang.types['s' + prefix]); |
ca42d3
|
210 |
setText(times_stop_name, normalizeName(data.stopName)); |
JK |
211 |
setText(page_title, lang.page_title_stop_name.replace('$stop', normalizeName(data.stopName))); |
c25e91
|
212 |
deleteChildren(times_alerts); |
JK |
213 |
deleteChildren(times_table); |
68aeb4
|
214 |
//deleteChildren(times_lines); |
c25e91
|
215 |
|
3dd66f
|
216 |
if(alternative_stop !== null) { |
JK |
217 |
var a = addParaWithText(times_alerts, ''); |
2bc9da
|
218 |
a = addElementWithText(a, 'a', (prefix === 'b' ? lang.departures_for_trams : lang.departures_for_buses)); |
3dd66f
|
219 |
a.href = ''; |
JK |
220 |
a.onclick = function(e) { |
|
221 |
e.preventDefault(); |
|
222 |
loadTimes(alternative_stop); |
|
223 |
}; |
|
224 |
|
|
225 |
} |
|
226 |
|
c206d1
|
227 |
var i, il; |
JK |
228 |
for(i = 0, il = data.generalAlerts.length; i < il; i++) { |
09944e
|
229 |
addParaWithText(times_alerts, data.generalAlerts[i].title); |
c25e91
|
230 |
} |
JK |
231 |
|
cb5a77
|
232 |
var all_departures = data.old.concat(data.actual); |
JK |
233 |
var tr, dir_cell, vehicle, status, status_cell, delay, delay_cell; |
c206d1
|
234 |
for(i = 0, il = all_departures.length; i < il; i++) { |
JK |
235 |
tr = document.createElement('tr'); |
cb5a77
|
236 |
addCellWithText(tr, all_departures[i].patternText); |
c206d1
|
237 |
dir_cell = addCellWithText(tr, all_departures[i].direction); |
2bc9da
|
238 |
vehicle = vehicles_info.getParsed(prefix + all_departures[i].vehicleId); |
4b0eda
|
239 |
dir_cell.appendChild(displayVehicle(vehicle)); |
218a38
|
240 |
addCellWithText(tr, (vehicle ? vehicle.num : '')).className = 'vehicleData'; |
c206d1
|
241 |
status = parseStatus(all_departures[i]); |
JK |
242 |
status_cell = addCellWithText(tr, status); |
|
243 |
delay = parseDelay(all_departures[i]); |
|
244 |
delay_cell = addCellWithText(tr, delay); |
c25e91
|
245 |
|
cb5a77
|
246 |
if(i < data.old.length) { |
JK |
247 |
tr.className = 'active'; |
|
248 |
} else if(all_departures[i].status === 'STOPPING') { |
e2b283
|
249 |
tr.className = 'success'; |
cb5a77
|
250 |
if (all_departures[i].actualRelativeTime <= 0) { |
7a58f5
|
251 |
status_cell.className = 'status-boarding'; |
P |
252 |
} |
e2b283
|
253 |
} else if(parseInt(delay) > 9) { |
JK |
254 |
tr.className = 'danger'; |
|
255 |
delay_cell.className = 'status-delayed'; |
|
256 |
} else if(parseInt(delay) > 3) { |
|
257 |
tr.className = 'warning'; |
|
258 |
} |
68aeb4
|
259 |
|
0f11d0
|
260 |
tr.addEventListener('click', function(tripId, vehicleInfo) { |
JK |
261 |
return function(){ loadRoute(tripId, vehicleInfo); } |
cb5a77
|
262 |
}(prefix + all_departures[i].tripId, vehicle)); |
c25e91
|
263 |
times_table.appendChild(tr); |
JK |
264 |
} |
|
265 |
|
68aeb4
|
266 |
/* |
c25e91
|
267 |
for(var i = 0, il = data.routes.length; i < il; i++) { |
JK |
268 |
var tr = document.createElement('tr'); |
|
269 |
addCellWithText(tr, data.routes[i].name); |
|
270 |
addCellWithText(tr, data.routes[i].directions.join(' - ')); |
|
271 |
addCellWithText(tr, data.routes[i].authority); |
|
272 |
times_lines.appendChild(tr); |
|
273 |
} |
68aeb4
|
274 |
*/ |
c25e91
|
275 |
|
JK |
276 |
startTimer(new Date()); |
e54544
|
277 |
fail_hide(); |
c25e91
|
278 |
|
68aeb4
|
279 |
times_timer = setTimeout(function(){ loadTimes(); loadRoute(); }, ttss_refresh); |
ac721d
|
280 |
}).fail(fail_ajax).always(loading_end); |
2b6454
|
281 |
return times_xhr; |
68aeb4
|
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 |
|
7e7221
|
346 |
var stop; |
9e6757
|
347 |
if(window.location.hash.match(/^#![0-9]+$/)) { |
cbce8b
|
348 |
loadTimes('t' + window.location.hash.substr(2)); |
JK |
349 |
} else if(window.location.hash.match(/^#![bt][0-9]+$/)) { |
|
350 |
loadTimes(window.location.hash.substr(2)); |
9e6757
|
351 |
} else if(window.location.hash.match(/^#![a-z]{2}[0-9]*$/)) { |
7e7221
|
352 |
stop = window.location.hash.substr(4); |
JK |
353 |
if(stop) stop_id = 't' + stop; |
a9e7f2
|
354 |
|
JK |
355 |
if(!change_language(window.location.hash.substr(2, 2))) { |
7e7221
|
356 |
loadTimes(); |
cbce8b
|
357 |
} |
JK |
358 |
} else if(window.location.hash.match(/^#![a-z]{2}[bt][0-9]*$/)) { |
7e7221
|
359 |
stop = window.location.hash.substr(4); |
cbce8b
|
360 |
if(stop) stop_id = stop; |
JK |
361 |
|
|
362 |
if(!change_language(window.location.hash.substr(2, 2))) { |
7e7221
|
363 |
loadTimes(); |
a9e7f2
|
364 |
} |
9e6757
|
365 |
} |
JK |
366 |
} |
|
367 |
|
e95a73
|
368 |
function stop_autocomplete() { |
JK |
369 |
if(stop_name_autocomplete_xhr) stop_name_autocomplete_xhr.abort(); |
|
370 |
|
|
371 |
stop_name_autocomplete_xhr = $.get( |
cbce8b
|
372 |
'stops/?query=' + encodeURIComponent(stop_name.value) |
e95a73
|
373 |
).done(function(data) { |
JK |
374 |
deleteChildren(stop_name_autocomplete); |
ced309
|
375 |
for(var i = 0, il = data.length; i < il; i++) { |
e95a73
|
376 |
var opt = document.createElement('option'); |
JK |
377 |
opt.value = data[i].id; |
76f5c4
|
378 |
setText(opt, lang.select_stop_type[data[i].id.substr(0,1)].replace('$stop', data[i].name)); |
e95a73
|
379 |
stop_name_autocomplete.appendChild(opt); |
JK |
380 |
} |
|
381 |
|
|
382 |
if(!stop_id) setText(refresh_text, lang.select_stop_click_go); |
|
383 |
}).fail(fail_ajax); |
2b6454
|
384 |
return stop_name_autocomplete_xhr; |
e95a73
|
385 |
} |
JK |
386 |
|
c25e91
|
387 |
function init() { |
7e7221
|
388 |
lang_select.addEventListener('input', function() { |
9e6757
|
389 |
change_language(lang_select.value); |
c25e91
|
390 |
}); |
JK |
391 |
|
7e7221
|
392 |
stop_name.addEventListener('input', function() { |
c25e91
|
393 |
if(!stop_name.value) return; |
e95a73
|
394 |
if(stop_name_autocomplete_timer) clearTimeout(stop_name_autocomplete_timer); |
c25e91
|
395 |
|
e95a73
|
396 |
stop_name_autocomplete_timer = setTimeout(stop_autocomplete, 100); |
c25e91
|
397 |
}); |
JK |
398 |
|
9e6757
|
399 |
setText(refresh_text, lang.enter_stop_name_to_begin); |
c25e91
|
400 |
|
JK |
401 |
stop_name_form.addEventListener('submit', function(e) { |
|
402 |
e.preventDefault(); |
|
403 |
if(!stop_name_autocomplete.value) return; |
969115
|
404 |
loadTimes(stop_name_autocomplete.value); |
c25e91
|
405 |
}); |
JK |
406 |
|
4b0eda
|
407 |
refresh_button.addEventListener('click', function() { |
68aeb4
|
408 |
loadTimes(); |
JK |
409 |
loadRoute(); |
c25e91
|
410 |
}); |
JK |
411 |
|
4b0eda
|
412 |
alert_close.addEventListener('click', function() { |
c25e91
|
413 |
alert.style.display = 'none'; |
JK |
414 |
}); |
492fc5
|
415 |
|
4b0eda
|
416 |
vehicle_data.addEventListener('click', function(e) { |
JK |
417 |
e.preventDefault(); |
|
418 |
vehicle_data.style.display = 'none'; |
|
419 |
setText(vehicle_data_style, '.vehicleData { display: table-cell; }') |
|
420 |
}); |
|
421 |
|
2bc9da
|
422 |
vehicles_info.update(); |
f0bae0
|
423 |
|
9e6757
|
424 |
hash(); |
JK |
425 |
|
|
426 |
window.addEventListener('hashchange', hash); |
572390
|
427 |
|
JK |
428 |
checkVersionInit(); |
c25e91
|
429 |
} |
JK |
430 |
|
|
431 |
init(); |