travelynx/public/static/js/geolocation.js

107 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-11-27 21:12:56 +00:00
/*
2023-07-03 15:59:25 +00:00
* Copyright (C) 2020 Birte Kristina Friesel
2020-11-27 21:12:56 +00:00
*
2021-01-29 17:32:13 +00:00
* SPDX-License-Identifier: AGPL-3.0-or-later
2020-11-27 21:12:56 +00:00
*/
2018-10-07 09:35:47 +00:00
$(document).ready(function() {
const getPlaceholder = function() {
2019-05-11 10:43:04 +00:00
return $('div.geolocation div.progress');
}
const showError = function(header, message, code) {
const errnode = $(document.createElement('div'));
2019-03-08 18:40:57 +00:00
errnode.attr('class', 'error');
errnode.text(message);
2019-03-08 18:40:57 +00:00
const headnode = $(document.createElement('strong'));
headnode.text(header + ' ');
2019-03-08 18:40:57 +00:00
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(';');
2024-06-21 19:12:35 +00:00
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();
}
2018-10-07 09:35:47 +00:00
};
const processResult = function(data) {
2018-10-07 09:35:47 +00:00
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'));
2018-10-07 09:35:47 +00:00
$.each(data.candidates, function(i, candidate) {
const eva = candidate.eva,
2018-10-07 09:35:47 +00:00
name = candidate.name,
hafas = candidate.hafas,
distance = candidate.distance.toFixed(1);
2018-10-07 09:35:47 +00:00
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);
2018-10-07 09:35:47 +00:00
});
getPlaceholder().replaceWith(res);
2018-10-07 09:35:47 +00:00
}
};
const processLocation = function(loc) {
2024-07-26 19:12:32 +00:00
const backend = $('div.geolocation').data('backend');
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 16:55:58 +00:00
$.post('/geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude, backend: backend}, processResult);
2018-10-07 09:35:47 +00:00
};
const processError = function(error) {
2018-10-07 09:35:47 +00:00
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() {
2019-05-11 10:43:04 +00:00
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) {
2019-05-11 10:43:04 +00:00
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);
}
2018-10-07 09:35:47 +00:00
}
});