diff options
Diffstat (limited to 'public/static/js/collapse.js')
-rw-r--r-- | public/static/js/collapse.js | 185 |
1 files changed, 149 insertions, 36 deletions
diff --git a/public/static/js/collapse.js b/public/static/js/collapse.js index 1f835c5..e861169 100644 --- a/public/static/js/collapse.js +++ b/public/static/js/collapse.js @@ -1,26 +1,48 @@ +/* + * Copyright (C) 2020-2023 Birte Kristina Friesel + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +function setLang(lang) { + document.cookie = 'lang=' + lang + ';SameSite=None;Secure'; + location.reload(); +} + +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 reload_app() { - $.get(window.location.href, {ajax: 1}, function(data) { - $('div.app > ul').html(data); - dbf_reg_handlers(); - setTimeout(reload_app, 60000); - }).fail(function() { - setTimeout(reload_app, 10000); - }); + // TODO use a variable instead of window.location.href, as + // window.location.href may be /z/... + // Until then, we guard it by only reloading whin moreinfo is not expanded. + if ($('.expanded-moreinfo').length == 0) { + $.get(window.location.href, {ajax: 1}, function(data) { + // TODO check expanded-moreinfo again here (until the issue itself has been resolved) + $('div.app > ul').html(data); + dbf_reg_handlers(); + setTimeout(reload_app, 60000); + }).fail(function() { + setTimeout(reload_app, 10000); + }); + } else { + setTimeout(reload_app, 30000); + } } -function dbf_reg_handlers() { - $('div.app > ul > li').click(function() { - const trainElem = $(this); - const routeprev = trainElem.data('routeprev').split('|'); - const routenext = trainElem.data('routenext').split('|'); - const moreinfo = trainElem.data('moreinfo').split('|'); - const this_href = window.location.href; - const station = $('div.app').data('station'); - // TODO get station name... - history.pushState({'page':'traindetail','train':trainElem.data('no')}, 'test', '/z/' + trainElem.data('train') + '/' + station); - $('.moreinfo').each(function() { - var infoElem = $(this); - $('.moreinfo .train-line').removeClass('bahn sbahn fern ext').addClass(trainElem.data('linetype')); +function dbf_show_moreinfo(trainElem, keep_old) { + const routeprev = trainElem.data('routeprev').split('|'); + const routenext = trainElem.data('routenext').split('|'); + const moreinfo = trainElem.data('moreinfo').split('|'); + $('.moreinfo').each(function() { + const infoElem = $(this); + if (!keep_old) { + $('.moreinfo .train-line').removeClass('sbahn fern ext ubahn bus tram').addClass(trainElem.data('linetype')); $('.moreinfo .train-line').text(trainElem.data('line')); $('.moreinfo .train-no').text(trainElem.data('no')); $('.moreinfo .train-origin').text(trainElem.data('from')); @@ -70,15 +92,86 @@ function dbf_reg_handlers() { } } $('.moreinfo .mfooter').append('Fahrtverlauf: <ul class="mroute">' + routebuf + '</ul>'); - $.get(this_href, {train: trainElem.data('train'), ajax: 1}, function(data) { - $('.moreinfo').html(data); - }).fail(function() { - $('.moreinfo .mfooter').append('Der Zug ist abgefahren (Zug nicht gefunden)'); - }); - infoElem.removeClass('collapsed-moreinfo'); - infoElem.addClass('expanded-moreinfo'); + } + $.get(window.location.href, {train: trainElem.data('train'), jid: trainElem.data('jid'), ajax: 1}, function(data) { + $('.moreinfo').html(data); + }).fail(function() { + $('.moreinfo .mfooter').append('Keine weiteren Details verfügbar'); + $('.moreinfo .loading').remove(); }); + infoElem.removeClass('collapsed-moreinfo'); + infoElem.addClass('expanded-moreinfo'); + }); +} + +function dbf_reg_handlers() { + $('div.app > ul > li').click(function(event) { + const trainElem = $(this); + const station = $('div.app').data('station'); + const param = new URLSearchParams(window.location.search); + event.preventDefault(); + var suffix = '?'; + if (param.get('detailed')) { + suffix += '&detailed=1'; + } + if (param.get('dbris') && param.get('dbris') != '0') { + suffix += '&dbris=' + param.get('dbris') + '&highlight=' + trainElem.data('station'); + } + if (param.get('efa') && param.get('efa') != '0') { + suffix += '&efa=' + param.get('efa') + '&highlight=' + trainElem.data('station'); + } + if (param.get('hafas') && param.get('hafas') != '0') { + suffix += '&hafas=' + param.get('hafas') + '&highlight=' + trainElem.data('station'); + } + if (param.get('past')) { + suffix += '&past=1'; + } + if (param.get('rt') || param.get('show_realtime')) { + suffix += '&rt=1'; + } + if (param.get('hafas') && param.get('hafas') != '0') { + history.pushState({'page':'traindetail','jid':trainElem.data('jid')}, 'test', '/z/' + trainElem.data('jid') + suffix); + } else if (param.get('efa') && param.get('efa') != '0') { + history.pushState({'page':'traindetail','jid':trainElem.data('jid')}, 'test', '/z/' + trainElem.data('jid') + suffix); + } else if (param.get('dbris') && param.get('dbris') != '0') { + history.pushState({'page':'traindetail','jid':trainElem.data('jid')}, 'test', '/z/' + trainElem.data('jid') + suffix); + } else { + history.pushState({'page':'traindetail','station':station,'train':trainElem.data('no')}, 'test', '/z/' + trainElem.data('train') + '/' + trainElem.data('station') + suffix); + } + dbf_show_moreinfo(trainElem, false); }); + const trainid = $(location).attr('hash').substr(1); + if (trainid) { + var found = false; + $('div.app > ul > li').each(function(index) { + if (found) { + return; + } + $(this).find('.anchor').each(function() { + if ($(this).attr('id') == trainid) { + found = true; + } + }); + }); + if (found) { + found = false; + $('div.app > ul > li').each(function(index) { + if (found) { + return; + } + $(this).find('.anchor').each(function() { + if ($(this).attr('id') == trainid) { + found = true; + } + }); + if (found) { + $(this).addClass('selected'); + } else { + $(this).addClass('past'); + } + }); + } + } } $(function() { @@ -122,16 +215,36 @@ $(function() { history.replaceState({'page':'station'}, document.title, ''); } window.onpopstate = function(event) { - console.log('pop ' + document.location + ' ' + JSON.stringify(event.state)); - if ((event.state != null) && (event.state['page'] == 'station')) { - $('.moreinfo').each(function() { - $(this).removeClass('expanded-moreinfo'); - $(this).addClass('collapsed-moreinfo'); - }); - if (!$('div.app > ul').length) { - $('div.app').append('<ul></ul>'); - reload_app(); + if (event.state != null) { + if ((event.state['page'] == 'station')) { + $('.moreinfo').each(function() { + $(this).removeClass('expanded-moreinfo'); + $(this).addClass('collapsed-moreinfo'); + }); + if (!$('div.app > ul').length) { + $('div.app').append('<ul></ul>'); + reload_app(); + } + } else if ((event.state['page'] == 'traindetail')) { + var success = false; + $('div.app > ul > li').each(function() { + const trainElem = $(this); + if (trainElem.data('no') == event.state['train']) { + dbf_show_moreinfo(trainElem, true); + success = true; + return; + } + }); + if (!success) { + $('.moreinfo').each(function() { + $(this).removeClass('collapsed-moreinfo'); + $(this).addClass('expanded-moreinfo'); + }); + $('.moreinfo .mfooter').append('Der Zug ist abgefahren (Zug nicht gefunden)'); + } } + } else { + console.log("unhandled popstate! " + document.location); } }; }); |