travelynx/public/static/js/geolocation.js
Birte Kristina Friesel 47f76da4f8
Multi-backend support
Squashed commit of the following:

commit 92518024ba295456358618c0e8180bd8e996fdf1
Author: Birte Kristina Friesel <birte.friesel@uos.de>
Date:   Fri Jul 26 18:39:46 2024 +0200

    add_or_update station: remove superfluos 'new backend id := old backend id'

commit df21c20c6e4c86454f8a9ac69121404415217f2a
Author: Birte Kristina Friesel <birte.friesel@uos.de>
Date:   Fri Jul 26 18:35:51 2024 +0200

    revert connection targets min_count to 3

commit be335cef07d0b42874f5fc1de4a1d13396e8e807
Author: Birte Kristina Friesel <birte.friesel@uos.de>
Date:   Fri Jul 26 18:20:05 2024 +0200

    mention backend selection in API documentation

commit 9f41828fb4f18fd707e0087def3032e8d4c8d7d8
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date:   Thu Jul 25 20:19:23 2024 +0200

    use_history: not all backends provide route data in departure monitor

commit 09714b4d89684b8331d0e96f564a4c7432318f70
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date:   Thu Jul 25 20:11:44 2024 +0200

    disambiguation: pass correct hafas parameter

commit 8cdf1120fc32155dc6525be64601b7c10a9c7f52
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date:   Thu Jul 25 20:11:28 2024 +0200

    _checked_in: hide Zuglauf link for non-db checkins

commit 7455653f541198e0e0a6d11aed421487ffdb6285
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date:   Thu Jul 25 20:01:47 2024 +0200

    debug output

commit b9cda07f85601a58ea32dbdacdd5399f302db52b
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date:   Thu Jul 25 19:09:07 2024 +0200

    fix remaining get_connection_targets / get_connecting_trains_p invocations

commit 2759d7258c37c7498905cfe19f6b4c4f6d16bd21
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date:   Wed Jul 24 20:50:12 2024 +0200

    support non-DB HAFAS backends (WiP)
2024-07-26 18:55:58 +02:00

106 lines
3.9 KiB
JavaScript

/*
* Copyright (C) 2020 Birte Kristina Friesel
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
$(document).ready(function() {
const getPlaceholder = function() {
return $('div.geolocation div.progress');
}
const showError = function(header, message, code) {
const errnode = $(document.createElement('div'));
errnode.attr('class', 'error');
errnode.text(message);
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(';');
const node = $('<a class="tablerow" href="/s/' + parts[0] + '?hafas=' + parts[2] + '"><span><i class="material-icons" aria-hidden="true">' + (parts[2] == '0' ? 'train' : 'directions') + '</i>' + parts[1] + '</span></a>');
node.click(function() {
$('nav .preloader-wrapper').addClass('active');
});
res.append(node);
});
$('p.geolocationhint').text('Letzte Ziele:');
getPlaceholder().replaceWith(res);
} else {
getPlaceholder().remove();
}
};
const processResult = function(data) {
if (data.error) {
showError('Backend-Fehler:', data.error, null);
} else if (data.candidates.length == 0) {
showError('Keine Bahnhöfe in 70km Umkreis gefunden', '', null);
} else {
const res = $(document.createElement('p'));
$.each(data.candidates, function(i, candidate) {
const eva = candidate.eva,
name = candidate.name,
hafas = candidate.hafas,
distance = candidate.distance.toFixed(1);
const node = $('<a class="tablerow" href="/s/' + eva + '?hafas=' + hafas + '"><span><i class="material-icons" aria-hidden="true">' + (hafas == '0' ? 'train' : 'directions') + '</i>' + name + '</span></a>');
node.click(function() {
$('nav .preloader-wrapper').addClass('active');
});
res.append(node);
});
getPlaceholder().replaceWith(res);
}
};
const processLocation = function(loc) {
const backend = $('div.geolocation > button').data('backend');
$.post('/geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude, backend: backend}, processResult);
};
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) {
showError('Standort konnte nicht ermittelt werden', '(Service nicht verfügbar)', 'geolocation.error.POSITION_UNAVAILABLE');
} else if (error.code == error.TIMEOUT) {
showError('Standort konnte nicht ermittelt werden', '(Timeout)', 'geolocation.error.TIMEOUT');
} else {
showError('Standort konnte nicht ermittelt werden', '(unbekannter Fehler)', 'unknown geolocation.error code');
}
};
const geoLocationButton = $('div.geolocation > button');
const recentStops = geoLocationButton.data('recent');
const getGeoLocation = function() {
geoLocationButton.replaceWith($('<p class="geolocationhint">Stationen in der Umgebung:</p><div class="progress"><div class="indeterminate"></div></div>'));
navigator.geolocation.getCurrentPosition(processLocation, processError);
}
if (geoLocationButton.length) {
if (navigator.geolocation) {
if (navigator.permissions) {
navigator.permissions.query({ name:'geolocation' }).then(function(value) {
if (value.state === 'prompt') {
geoLocationButton.on('click', getGeoLocation);
} else {
// User either rejected or granted permission. User wont get prompted and we can show stations/error
getGeoLocation();
}
});
} else {
geoLocationButton.on('click', getGeoLocation);
}
} else {
showError('Standortanfragen werden von diesem Browser nicht unterstützt', '', null);
}
}
});