obtain autocompletion from database

This commit is contained in:
Birte Kristina Friesel 2023-08-26 09:17:28 +02:00
parent 559a1cedd7
commit f4d71b4d9f
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA
8 changed files with 34 additions and 9792 deletions

View file

@ -2397,6 +2397,7 @@ sub startup {
$r->get( '/p/:name/j/:id' => 'public_journey' )
->to('profile#journey_details');
$r->get('/.well-known/webfinger')->to('account#webfinger');
$r->get('/dyn/:av/autocomplete.js')->to('api#autocomplete');
$r->post('/api/v1/import')->to('api#import_v1');
$r->post('/api/v1/travel')->to('api#travel_v1');
$r->post('/action')->to('traveling#travel_action');

View file

@ -7,6 +7,7 @@ use Mojo::Base 'Mojolicious::Controller';
use DateTime;
use List::Util;
use Mojo::JSON qw(encode_json);
use UUID::Tiny qw(:std);
# Internal Helpers
@ -648,4 +649,21 @@ sub set_token {
$self->redirect_to('account');
}
sub autocomplete {
my ($self) = @_;
$self->res->headers->cache_control('max-age=86400, immutable');
my $output
= "document.addEventListener('DOMContentLoaded',function(){M.Autocomplete.init(document.querySelectorAll('.autocomplete'),{\n";
$output .= 'minLength:3,limit:50,data:';
$output .= encode_json( $self->stations->get_for_autocomplete );
$output .= "\n});});\n";
$self->render(
format => 'js',
data => $output
);
}
1;

View file

@ -50,6 +50,19 @@ sub add_or_update {
);
}
sub get_for_autocomplete {
my ($self) = @_;
my $res = $self->{pg}->db->select( 'stations', ['name'] );
my %ret;
while ( my $row = $res->hash ) {
$ret{ $row->{name} } = undef;
}
return \%ret;
}
# Fast
sub get_by_eva {
my ( $self, $eva, %opt ) = @_;

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -6,12 +6,10 @@
set -x
scripts/update-autocomplete
for theme in dark light; do
sassc -t compressed sass/src/${theme}/index.scss public/static/css/${theme}.min.css
done
for file in autocomplete geolocation travelynx-actions; do
for file in geolocation travelynx-actions; do
uglifyjs public/static/js/${file}.js -c -m > public/static/js/${file}.min.js
done

View file

@ -1,43 +0,0 @@
#!/usr/bin/env perl
use strict;
use warnings;
use 5.020;
use JSON;
use File::Slurp qw(write_file);
use List::Util qw(uniq);
use Travel::Status::DE::IRIS::Stations;
my @station_names
= uniq map { $_->[1] } Travel::Status::DE::IRIS::Stations::get_stations();
my $station_list = q{};
for my $station (@station_names) {
$station_list .= sprintf( "\t\t\t\"%s\": null,\n", $station );
}
my $autocomplete = <<"EOF";
/*
* Copyright (C) 2020 DB Station&Service AG, Europaplatz 1, 10557 Berlin
* Copyright (C) 2020 Birte Kristina Friesel
*
* SPDX-License-Identifier: CC-BY-4.0
*/
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.autocomplete');
M.Autocomplete.init(elems, {
minLength: 3,
limit: 50,
data: {
$station_list
}
});
});
EOF
write_file(
"public/static/js/autocomplete.js",
{ binmode => ':encoding(utf-8)' },
$autocomplete
);

View file

@ -68,7 +68,7 @@
%= javascript "/static/${av}/js/geolocation${min}.js"
% }
% if (stash('with_autocomplete')) {
%= javascript "/static/${av}/js/autocomplete${min}.js"
%= javascript "/dyn/${av}/autocomplete.js", defer => undef
% }
% if (stash('with_map')) {
%= javascript "/static/${av}/leaflet/leaflet.js"