travelynx/public/static/js/geolocation.js

106 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(';');
2023-08-16 19:45:25 +00:00
const node = $('<a class="tablerow" href="/s/' + parts[0] + '?hafas=' + parts[2] + '"><span><i class="material-icons" aria-hidden="true">' + (parseInt(parts[2]) ? 'directions_bus' : 'train') + '</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
2023-08-16 19:45:25 +00:00
const node = $('<a class="tablerow" href="/s/' + eva + '?hafas=' + hafas + '"><span><i class="material-icons" aria-hidden="true">' + (parseInt(hafas) ? 'directions_bus' : 'train') + '</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) {
2019-03-07 17:36:11 +00:00
$.post('/geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, 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
}
});