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 ( ) {
2023-01-18 17:13:35 +00:00
const getPlaceholder = function ( ) {
2019-05-11 10:43:04 +00:00
return $ ( 'div.geolocation div.progress' ) ;
}
2023-01-18 17:13:35 +00:00
const showError = function ( header , message , code ) {
const errnode = $ ( document . createElement ( 'div' ) ) ;
2019-03-08 18:40:57 +00:00
errnode . attr ( 'class' , 'error' ) ;
2019-03-16 20:33:19 +00:00
errnode . text ( message ) ;
2019-03-08 18:40:57 +00:00
2023-01-18 17:13:35 +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 ) ;
2023-01-18 17:13:35 +00:00
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>' ) ;
2023-05-07 07:55:38 +00:00
node . click ( function ( ) {
$ ( 'nav .preloader-wrapper' ) . addClass ( 'active' ) ;
} ) ;
res . append ( node ) ;
2023-01-18 17:13:35 +00:00
} ) ;
$ ( 'p.geolocationhint' ) . text ( 'Letzte Ziele:' ) ;
getPlaceholder ( ) . replaceWith ( res ) ;
} else {
getPlaceholder ( ) . remove ( ) ;
}
2018-10-07 09:35:47 +00:00
} ;
2023-01-18 17:13:35 +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 {
2022-07-14 16:26:23 +00:00
const res = $ ( document . createElement ( 'p' ) ) ;
2018-10-07 09:35:47 +00:00
$ . each ( data . candidates , function ( i , candidate ) {
2023-08-16 16:54:32 +00:00
const eva = candidate . eva ,
2018-10-07 09:35:47 +00:00
name = candidate . name ,
2023-08-16 16:54:32 +00:00
hafas = candidate . hafas ,
2022-07-14 16:26:23 +00:00
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>' ) ;
2023-05-07 07:55:38 +00:00
node . click ( function ( ) {
$ ( 'nav .preloader-wrapper' ) . addClass ( 'active' ) ;
} ) ;
res . append ( node ) ;
2018-10-07 09:35:47 +00:00
} ) ;
2022-07-14 16:26:23 +00:00
getPlaceholder ( ) . replaceWith ( res ) ;
2018-10-07 09:35:47 +00:00
}
} ;
2023-01-18 17:13:35 +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
} ;
2023-01-18 17:13:35 +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' ) ;
}
} ;
2023-01-18 17:13:35 +00:00
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 ) {
2019-05-05 10:30:34 +00:00
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 ) ;
}
2019-05-05 10:30:34 +00:00
} else {
showError ( 'Standortanfragen werden von diesem Browser nicht unterstützt' , '' , null ) ;
}
2018-10-07 09:35:47 +00:00
}
} ) ;