diff options
Diffstat (limited to 'public/static/js/travelynx-actions.js')
-rw-r--r-- | public/static/js/travelynx-actions.js | 86 |
1 files changed, 77 insertions, 9 deletions
diff --git a/public/static/js/travelynx-actions.js b/public/static/js/travelynx-actions.js index 7558e69..48e878f 100644 --- a/public/static/js/travelynx-actions.js +++ b/public/static/js/travelynx-actions.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Daniel Friesel + * Copyright (C) 2020 Birte Kristina Friesel * * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -8,8 +8,22 @@ var j_duration = 0; var j_arrival = 0; var j_dest = ''; var j_stops = []; +var j_token = ''; + +function setTheme(theme) { + localStorage.setItem('theme', theme); + if (!otherTheme.hasOwnProperty(theme)) { + theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; + } + addStyleSheet(theme, 'theme'); +} + function upd_journey_data() { $('.countdown').each(function() { + const journey_token = $(this).data('token'); + if (journey_token) { + j_token = journey_token; + } var journey_data = $(this).data('journey'); if (journey_data) { journey_data = journey_data.split(';'); @@ -36,12 +50,19 @@ function upd_journey_data() { }); } function upd_countdown() { - var now = Date.now() / 1000; + const now = Date.now() / 1000; if (j_departure > now) { $('.countdown').text('Abfahrt in ' + Math.round((j_departure - now)/60) + ' Minuten'); } else if (j_arrival > 0) { if (j_arrival > now) { - $('.countdown').text('Ankunft in ' + Math.round((j_arrival - now)/60) + ' Minuten'); + var diff = Math.round((j_arrival - now)/60); + if (diff >= 120) { + $('.countdown').text('Ankunft in ' + Math.floor(diff/60) + ' Stunden und ' + (diff%60) + ' Minuten'); + } else if (diff >= 60) { + $('.countdown').text('Ankunft in 1 Stunde und ' + (diff%60) + ' Minuten'); + } else { + $('.countdown').text('Ankunft in ' + diff + ' Minuten'); + } } else { $('.countdown').text('Ziel erreicht'); } @@ -65,7 +86,13 @@ function odelay(sched, rt) { function tvly_run(link, req, err_callback) { var error_icon = '<i class="material-icons">error</i>'; - var progressbar = $('<div class="progress"><div class="indeterminate"></div></div>'); + var progressbar; + if (link.data('tr')) { + progressbar = $('<tr><td colspan="' + link.data('tr') + '"><div class="progress"><div class="indeterminate"></div></div></td></tr>'); + } + else { + progressbar = $('<div class="progress"><div class="indeterminate"></div></div>'); + } link.hide(); link.after(progressbar); $.post('/action', req, function(data) { @@ -96,10 +123,12 @@ function tvly_update() { } function tvly_update_public() { var user_name; + var profile_status = 0; $('.publicstatuscol').each(function() { user_name = $(this).data('user'); + profile_status = $(this).data('profile'); }); - $.get('/ajax/status/' + user_name + '.html', function(data) { + $.get('/ajax/status/' + user_name + '.html', {token: j_token, profile: profile_status}, function(data) { $('.publicstatuscol').html(data); upd_journey_data(); setTimeout(tvly_update_public, 40000); @@ -109,6 +138,15 @@ function tvly_update_public() { setTimeout(tvly_update_public, 5000); }); } +function tvly_update_timeline() { + $.get('/timeline/in-transit', {ajax: 1}, function(data) { + $('.timeline-in-transit').html(data); + setTimeout(tvly_update_timeline, 60000); + }).fail(function() { + $('.sync-failed-marker').css('display', 'block'); + setTimeout(tvly_update_timeline, 10000); + }); +} function tvly_journey_progress() { var now = Date.now() / 1000; var progress = 0; @@ -137,7 +175,11 @@ function tvly_journey_progress() { break; } if ((rt_dep != 0) && (rt_dep - now > 0)) { - $('.next-stop').html(stop_name + '<br/>' + hhmm(rt_arr) + ' → ' + hhmm(rt_dep) + odelay(sched_dep, rt_dep)); + if (rt_arr != 0) { + $('.next-stop').html(stop_name + '<br/>' + hhmm(rt_arr) + ' → ' + hhmm(rt_dep) + odelay(sched_dep, rt_dep)); + } else { + $('.next-stop').html(stop_name + '<br/>' + hhmm(rt_dep) + odelay(sched_dep, rt_dep)); + } break; } } @@ -152,6 +194,7 @@ function tvly_reg_handlers() { station: link.data('station'), train: link.data('train'), dest: link.data('dest'), + ts: link.data('ts'), }; tvly_run(link, req); }); @@ -163,8 +206,10 @@ function tvly_reg_handlers() { force: link.data('force'), }; tvly_run(link, req, function() { - link.append(' – Ohne Echtzeitdaten auschecken?') - link.data('force', true); + if (!link.data('force')) { + link.append(' – Ohne Echtzeitdaten auschecken?') + link.data('force', true); + } }); }); $('.action-undo').click(function() { @@ -188,6 +233,7 @@ function tvly_reg_handlers() { var req = { action: 'cancelled_from', station: link.data('station'), + ts: link.data('ts'), train: link.data('train'), }; tvly_run(link, req); @@ -209,7 +255,7 @@ function tvly_reg_handlers() { checkin: link.data('checkin'), checkout: link.data('checkout'), }; - var really_delete = confirm("Diese Zugfahrt wirklich löschen? Der Eintrag wird sofort aus der Datenbank entfernt und kann nicht wiederhergestellt werden."); + var really_delete = confirm("Diese Fahrt wirklich löschen? Der Eintrag wird sofort aus der Datenbank entfernt und kann nicht wiederhergestellt werden."); if (really_delete) { tvly_run(link, req); } @@ -255,7 +301,29 @@ $(document).ready(function() { setTimeout(tvly_update_public, 40000); setTimeout(tvly_journey_progress, 5000); } + if ($('.timeline-in-transit .autorefresh').length) { + setTimeout(tvly_update_timeline, 60000); + } $('a[href]').click(function() { $('nav .preloader-wrapper').addClass('active'); }); + $('a[href="#now"]').keydown(function(event) { + // also trigger click handler on keyboard enter + if (event.keyCode == 13) { + event.preventDefault(); + event.target.click(); + } + }); + $('a[href="#now"]').click(function(event) { + event.preventDefault(); + $('nav .preloader-wrapper').removeClass('active'); + now_el = $('#now')[0]; + now_el.previousElementSibling.querySelector(".dep-time").focus(); + now_el.scrollIntoView({behavior: "smooth", block: "center"}); + }); + const elems = document.querySelectorAll('.carousel'); + const instances = M.Carousel.init(elems, { + fullWidth: true, + indicators: true} + ); }); |