From f5a6a5927bfcc4a2b82c34db5041bfe3a49b95e6 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 21 Aug 2021 12:11:35 +0200 Subject: refresh departure list every minute --- bin/nvm | 6 +++- static/js/geolocation.js | 12 +++---- static/js/reload.js | 13 +++++++ templates/departure_list.html | 81 +++++++++++++----------------------------- templates/departures_page.html | 35 ++++++++++++++++++ 5 files changed, 83 insertions(+), 64 deletions(-) create mode 100644 static/js/reload.js create mode 100644 templates/departures_page.html diff --git a/bin/nvm b/bin/nvm index f923b91..3df9013 100755 --- a/bin/nvm +++ b/bin/nvm @@ -350,7 +350,11 @@ async def show_departure_board(request, eva=None): departures = sorted(departures, key=lambda departure: departure.sort_by) - departure_board = env.get_template("departure_list.html") + if request.query.get("ajax", False): + departure_board = env.get_template("departure_list.html") + else: + departure_board = env.get_template("departures_page.html") + return web.Response( body=departure_board.render( title=station_name, diff --git a/static/js/geolocation.js b/static/js/geolocation.js index aba8b10..eeb535f 100644 --- a/static/js/geolocation.js +++ b/static/js/geolocation.js @@ -1,14 +1,14 @@ document.addEventListener("DOMContentLoaded", function() { const geoLocationButton = document.getElementById('geolocationsearch'); - const mkTextNode = function(className, textContent) { + const mkTextNode = (className, textContent) => { const node = document.createElement("span"); node.className = className; node.textContent = textContent; return node } - const processResult = function(results) { + const processResult = (results) => { const list = document.createElement("ul"); list.className = "stops"; @@ -73,7 +73,7 @@ document.addEventListener("DOMContentLoaded", function() { geoLocationButton.replaceWith(list); }; - const processLocation = function(loc) { + const processLocation = (loc) => { fetch('/geolocation', { method: 'POST', headers: { @@ -86,7 +86,7 @@ document.addEventListener("DOMContentLoaded", function() { }).then(response => response.json()).then(processResult); }; - const showError = function(header, text, code) { + const showError = (header, text, code) => { const errnode = document.createElement("div"); const errhead = document.createElement("strong"); const errtext = document.createTextNode(text); @@ -105,7 +105,7 @@ document.addEventListener("DOMContentLoaded", function() { geoLocationButton.replaceWith(errnode); } - const processError = function(error) { + const processError = (error) => { if (error.code == error.PERMISSION_DENIED) { showError('Standortanfrage nicht möglich.', 'Vermutlich fehlen die Rechte im Browser oder der Android Location Service ist deaktiviert.', 'geolocation.error.PERMISSION_DENIED'); } else if (error.code == error.POSITION_UNAVAILABLE) { @@ -117,7 +117,7 @@ document.addEventListener("DOMContentLoaded", function() { } }; - const getGeoLocation = function() { + const getGeoLocation = () => { geoLocationButton.textContent = "Suche Haltestellen ..."; geoLocationButton.disabled = true; navigator.geolocation.getCurrentPosition(processLocation, processError); diff --git a/static/js/reload.js b/static/js/reload.js new file mode 100644 index 0000000..9bdc7c0 --- /dev/null +++ b/static/js/reload.js @@ -0,0 +1,13 @@ +document.addEventListener("DOMContentLoaded", function() { + const departureList = document.getElementById('departurelist'); + + const showDepartures = (departureText) => { + departureList.innerHTML = departureText; + }; + + const fetchDepartures = () => { + fetch(window.location.href + '?ajax=1').then(response => response.text()).then(showDepartures); + }; + + setInterval(fetchDepartures, 60000); +}); diff --git a/templates/departure_list.html b/templates/departure_list.html index a9cda01..66e2900 100644 --- a/templates/departure_list.html +++ b/templates/departure_list.html @@ -1,59 +1,26 @@ - - - - {{ title }} - {% include 'header.html' %} - - - -{% include 'navbar.html' %} - -
-
- {% if warning %} -
-
- {{ warning["lead"] }} - {{ warning["body"] }} -
{{ warning["code"] }}
-
-
+{% for departure in departures %} +
  • + {{ departure.line.name }} + {% if departure.suffix %} + {{ departure.suffix }} {% endif %} - {% if not departures %} -
    Keine Abfahrten innerhalb der nächsten zwei Stunden
    + {{ departure.direction }} + + {% if departure.cancelled and departure.plannedWhen %} + + fällt aus + {{ departure.plannedWhen.strftime("%H:%M") }} + + {% else %} + + {% if departure.delay %} + ({{ departure.delay }}) + {% endif %} + {{ departure.relativeWhen }} + {% endif %} -
      - {% for departure in departures %} -
    • - {{ departure.line.name }} - {% if departure.suffix %} - {{ departure.suffix }} - {% endif %} - {{ departure.direction }} - - {% if departure.cancelled and departure.plannedWhen %} - - fällt aus - {{ departure.plannedWhen.strftime("%H:%M") }} - - {% else %} - - {% if departure.delay %} - ({{ departure.delay }}) - {% endif %} - {{ departure.relativeWhen }} - - {% endif %} - {% if departure.platform %} - {{ departure.platform }} - {% endif %} -
    • - {% endfor %} -
    -
  • -
    - -{% include 'footer.html' %} - - - + {% if departure.platform %} + {{ departure.platform }} + {% endif %} + +{% endfor %} diff --git a/templates/departures_page.html b/templates/departures_page.html new file mode 100644 index 0000000..52733c4 --- /dev/null +++ b/templates/departures_page.html @@ -0,0 +1,35 @@ + + + + {{ title }} + {% include 'header.html' %} + + + + +{% include 'navbar.html' %} + +
    +
    + {% if warning %} +
    +
    + {{ warning["lead"] }} + {{ warning["body"] }} +
    {{ warning["code"] }}
    +
    +
    + {% endif %} + {% if not departures %} +
    Keine Abfahrten innerhalb der nächsten zwei Stunden
    + {% endif %} +
      + {% include 'departure_list.html' %} +
    +
    +
    + +{% include 'footer.html' %} + + + -- cgit v1.2.3