Unverified Commit 6a671d45 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

geostop: always show hafas and iris stops

parent fab688d0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ requires 'GIS::Distance';
requires 'GIS::Distance::Fast';
requires 'JSON';
requires 'JSON::XS';
requires 'List::UtilsBy';
requires 'LWP::UserAgent';
requires 'LWP::Protocol::https';
requires 'Mojolicious';
+0 −15
Original line number Diff line number Diff line
@@ -42,23 +42,8 @@ sub redirect {
sub geostop {
	my ($self) = @_;

	my ( $api_link, $api_text, $api_icon );
	if ( $self->param('hafas') ) {
		$api_link = '/_autostop';
		$api_text = 'Auf Bahnverkehr wechseln';
		$api_icon = 'directions_bus';
	}
	else {
		$api_link = '/_autostop?hafas=1';
		$api_text = 'Auf Nahverkehr wechseln';
		$api_icon = 'train';
	}

	$self->render(
		'geostop',
		api_link     => $api_link,
		api_text     => $api_text,
		api_icon     => $api_icon,
		with_geostop => 1,
		hide_opts    => 1
	);
+61 −52
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ use DateTime::Format::Strptime;
use Encode          qw(decode encode);
use File::Slurp     qw(read_file write_file);
use List::Util      qw(max uniq);
use List::UtilsBy   qw(uniq_by);
use List::MoreUtils qw();
use Mojo::JSON      qw(decode_json encode_json);
use Mojo::Promise;
@@ -1781,9 +1782,26 @@ sub stations_by_coordinates {

	if ( not $lon or not $lat ) {
		$self->render( json => { error => 'Invalid lon/lat received' } );
		return;
	}
	if ($hafas) {

	$self->render_later;

	my @iris = map {
		{
			ds100    => $_->[0][0],
			name     => $_->[0][1],
			eva      => $_->[0][2],
			lon      => $_->[0][3],
			lat      => $_->[0][4],
			distance => $_->[1],
			hafas    => 0,
		}
	} Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon,
		$lat, 10 );

	@iris = uniq_by { $_->{name} } @iris;

	Travel::Status::DE::HAFAS->new_p(
		promise    => 'Mojo::Promise',
		user_agent => $self->ua,
@@ -1794,7 +1812,7 @@ sub stations_by_coordinates {
	)->then(
		sub {
			my ($hafas) = @_;
				my @candidates = map {
			my @hafas = map {
				{
					name     => $_->name,
					eva      => $_->eva,
@@ -1802,42 +1820,33 @@ sub stations_by_coordinates {
					hafas    => 1
				}
			} $hafas->results;
			if ( @hafas > 10 ) {
				@hafas = @hafas[ 0 .. 9 ];
			}
			my @results = map { $_->[0] }
			  sort { $a->[1] <=> $b->[1] }
			  map { [ $_, $_->{distance} ] } ( @iris, @hafas );
			$self->render(
				json => {
						candidates => [@candidates],
					candidates => [@results],
				}
			);
		}
	)->catch(
		sub {
			my ($err) = @_;
				$self->render( json => { error => $err } );
			}
		)->wait;
	}
	else {
		my @candidates = map {
			{
				ds100    => $_->[0][0],
				name     => $_->[0][1],
				eva      => $_->[0][2],
				lon      => $_->[0][3],
				lat      => $_->[0][4],
				distance => $_->[1],
				hafas    => 0,
			}
		} Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon,
			$lat, 10 );
			$self->render(
				json => {
				candidates => [@candidates],
					candidates => [@iris],
					warning    => $err,
				}
			);
		}
	)->wait;
}

sub autocomplete {
	my $self = shift;
	my ($self) = @_;

	$self->res->headers->cache_control('max-age=31536000, immutable');

+8 −3
Original line number Diff line number Diff line
@@ -43,20 +43,25 @@ $(function() {

				const stationlink = $(document.createElement('a'));
				stationlink.attr('href', eva + '?hafas=' + hafas);
				stationlink.text(name);
				stationlink.text(name + ' ');

				const distancenode = $(document.createElement('div'));
				distancenode.attr('class', 'distance');
				distancenode.text(distance);

				const icon = $(document.createElement('i'));
				icon.attr('class', 'material-icons');
				icon.text(hafas ? 'directions' : 'train');

				stationlink.append(icon);
				stationlink.append(distancenode);
				$('div.candidatelist').append(stationlink);
			});
		}
	};

	var processLocation = function(loc) {
		$.post('/_geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude, hafas: window.location.href.match('hafas=1') ? 1 : 0}, processResult).fail(function(jqXHR, textStatus, errorThrown) {
	const processLocation = function(loc) {
		$.post('/_geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, processResult).fail(function(jqXHR, textStatus, errorThrown) {
			removeStatus();
			showError("Netzwerkfehler: ", textStatus, errorThrown);
		});