From e9373246175a0853fb56aff4a740f4db7dda7215 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 18 Jan 2023 18:13:35 +0100 Subject: landing page: show past stops if geolocation is unavailable --- public/static/js/geolocation.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'public') diff --git a/public/static/js/geolocation.js b/public/static/js/geolocation.js index f186d2e..5ef0a5c 100644 --- a/public/static/js/geolocation.js +++ b/public/static/js/geolocation.js @@ -4,23 +4,36 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ $(document).ready(function() { - function getPlaceholder() { + const getPlaceholder = function() { return $('div.geolocation div.progress'); } - var showError = function(header, message, code) { - getPlaceholder().remove(); - var errnode = $(document.createElement('div')); + const showError = function(header, message, code) { + const errnode = $(document.createElement('div')); errnode.attr('class', 'error'); errnode.text(message); - var headnode = $(document.createElement('strong')); - headnode.text(header); + const headnode = $(document.createElement('strong')); + headnode.text(header + ' '); errnode.prepend(headnode); $('div.geolocation').append(errnode); + + const recent = $('div.geolocation').data('recent'); + if (recent) { + const stops = recent.split('|'); + const res = $(document.createElement('p')); + $.each(stops, function(i, stop) { + const parts = stop.split(';'); + res.append($('' + parts[1] + '')); + }); + $('p.geolocationhint').text('Letzte Ziele:'); + getPlaceholder().replaceWith(res); + } else { + getPlaceholder().remove(); + } }; - var processResult = function(data) { + const processResult = function(data) { if (data.error) { showError('Backend-Fehler:', data.error, null); } else if (data.candidates.length == 0) { @@ -39,11 +52,11 @@ $(document).ready(function() { } }; - var processLocation = function(loc) { + const processLocation = function(loc) { $.post('/geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, processResult); }; - var processError = function(error) { + const processError = function(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) { @@ -55,8 +68,9 @@ $(document).ready(function() { } }; - var geoLocationButton = $('div.geolocation > button'); - var getGeoLocation = function() { + const geoLocationButton = $('div.geolocation > button'); + const recentStops = geoLocationButton.data('recent'); + const getGeoLocation = function() { geoLocationButton.replaceWith($('

Stationen in der Umgebung:

')); navigator.geolocation.getCurrentPosition(processLocation, processError); } -- cgit v1.2.3