1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/*
* 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 [ eva, name, dbris, efa, hafas, motis ] = parts;
const node = $('<a class="tablerow" href="/s/' + eva + '?dbris=' + (dbris||0) + '&efa=' + (efa||0) + '&hafas=' + (hafas||0) + '&motis=' + (motis||0) + '"><span><i class="material-icons" aria-hidden="true">' + (!(dbris||efa||hafas||motis) ? 'train' : 'directions') + '</i>' + name + '</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) {
let node;
if (candidate.dbris) {
const eva = candidate.eva,
name = candidate.name,
dbris = candidate.dbris,
distance = candidate.distance.toFixed(1);
node = $('<a class="tablerow" href="/s/' + id + '?dbris=' + dbris + '"><span><i class="material-icons" aria-hidden="true">directions</i>' + name + '</span></a>');
} else if (candidate.efa) {
const eva = candidate.eva,
name = candidate.name,
efa = candidate.efa,
distance = candidate.distance.toFixed(1);
node = $('<a class="tablerow" href="/s/' + eva + '?efa=' + efa + '"><span><i class="material-icons" aria-hidden="true">directions</i>' + name + '</span></a>');
} else if (candidate.hafas) {
const eva = candidate.eva,
name = candidate.name,
hafas = candidate.hafas,
distance = candidate.distance.toFixed(1);
node = $('<a class="tablerow" href="/s/' + eva + '?hafas=' + hafas + '"><span><i class="material-icons" aria-hidden="true">directions</i>' + name + '</span></a>');
} else if (candidate.motis) {
const { id, name, motis } = candidate;
node = $('<a class="tablerow" href="/s/' + id + '?motis=' + motis + '"><span><i class="material-icons" aria-hidden="true">directions</i>' + name + '</span></a>');
} else {
const eva = candidate.eva,
name = candidate.name,
distance = candidate.distance.toFixed(1);
node = $('<a class="tablerow" href="/s/' + eva + '"><span><i class="material-icons" aria-hidden="true">train</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').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 > .request');
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);
}
}
});
|