From 6824e6bcf881c83091f844f55fcfed0c5f4de826 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Sat, 26 Aug 2023 08:32:42 +0200 Subject: generate autocomplete list dynamically from installed IRIS module --- lib/DBInfoscreen.pm | 2 ++ lib/DBInfoscreen/Controller/Stationboard.pm | 22 +++++++++++++++++++++- scripts/asset-rebuild | 4 +--- scripts/update-autocomplete | 29 ----------------------------- templates/layouts/app.html.ep | 3 +++ templates/layouts/legacy.html.ep | 3 +++ 6 files changed, 30 insertions(+), 33 deletions(-) delete mode 100755 scripts/update-autocomplete diff --git a/lib/DBInfoscreen.pm b/lib/DBInfoscreen.pm index fd3ed0b..4618059 100644 --- a/lib/DBInfoscreen.pm +++ b/lib/DBInfoscreen.pm @@ -305,6 +305,8 @@ sub startup { $r->get('/_impressum')->to('static#imprint'); + $r->get('/dyn/:av/autocomplete.js')->to('stationboard#autocomplete'); + $r->get('/_wr/:train/:departure')->to('wagenreihung#wagenreihung'); $r->get('/wr/:train')->to('wagenreihung#zugbildung_db'); $r->get('/w/*wagon')->to('wagenreihung#wagen'); diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index 8fb57a2..eb2badb 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -12,7 +12,7 @@ use Encode qw(decode encode); use File::Slurp qw(read_file write_file); use List::Util qw(max uniq); use List::MoreUtils qw(); -use Mojo::JSON qw(decode_json); +use Mojo::JSON qw(decode_json encode_json); use Mojo::Promise; use Mojo::UserAgent; use Travel::Status::DE::HAFAS; @@ -1836,4 +1836,24 @@ sub stations_by_coordinates { } } +sub autocomplete { + my $self = shift; + + $self->res->headers->cache_control('max-age=31536000, immutable'); + + my $output = '$(function(){const stations='; + $output + .= encode_json( + [ map { $_->[1] } Travel::Status::DE::IRIS::Stations::get_stations() ] + ); + $output .= ";\n"; + $output + .= "\$('input.station').autocomplete({delay:0,minLength:3,source:stations});});\n"; + + $self->render( + format => 'js', + data => $output + ); +} + 1; diff --git a/scripts/asset-rebuild b/scripts/asset-rebuild index 302cc66..98e5841 100755 --- a/scripts/asset-rebuild +++ b/scripts/asset-rebuild @@ -8,9 +8,7 @@ set -ex sassc -t compressed sass/dark.scss public/static/css/dark.min.css sassc -t compressed sass/light.scss public/static/css/light.min.css -scripts/update-autocomplete - -uglifyjs public/static/js/autocomplete.js public/static/js/collapse.js -c -m > public/static/js/dbf.min.js +uglifyjs public/static/js/collapse.js -c -m > public/static/js/dbf.min.js uglifyjs public/static/js/geostop.js -c -m > public/static/js/geostop.min.js uglifyjs public/static/js/geotrain.js -c -m > public/static/js/geotrain.min.js uglifyjs public/static/js/map-refresh.js -c -m > public/static/js/map-refresh.min.js diff --git a/scripts/update-autocomplete b/scripts/update-autocomplete deleted file mode 100755 index 22aab91..0000000 --- a/scripts/update-autocomplete +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env perl -# Copyright (C) 2020 Birte Kristina Friesel -# -# SPDX-License-Identifier: CC0-1.0 - -use strict; -use warnings; -use 5.020; - -use JSON; -use File::Slurp qw(write_file); -use Travel::Status::DE::IRIS::Stations; - -my @station_names = map { $_->[1] } Travel::Status::DE::IRIS::Stations::get_stations(); - -my $station_json = JSON->new->utf8->encode([@station_names]); - -my $autocomplete = <<"EOF"; -\$(function() { - var stations = $station_json; - \$('input.station').autocomplete({ - delay: 0, - minLength: 3, - source: stations - }); -}); -EOF - -write_file("public/static/js/autocomplete.js", $autocomplete); diff --git a/templates/layouts/app.html.ep b/templates/layouts/app.html.ep index 3727340..f5d0577 100644 --- a/templates/layouts/app.html.ep +++ b/templates/layouts/app.html.ep @@ -55,6 +55,9 @@ %= javascript '/static/js/jquery-3.4.1.min.js', defer => undef %= javascript "/static/${av}/js/jquery-ui.min.js", defer => undef %= javascript "/static/${av}/js/dbf.min.js", defer => undef + % if (not stash('hide_opts')) { + %= javascript "/dyn/${av}/autocomplete.js", defer => undef + % } % if (stash('with_geostop')) { %= javascript "/static/${av}/js/geostop.min.js", defer => undef % } diff --git a/templates/layouts/legacy.html.ep b/templates/layouts/legacy.html.ep index 7be30f5..60e8fb9 100644 --- a/templates/layouts/legacy.html.ep +++ b/templates/layouts/legacy.html.ep @@ -29,6 +29,9 @@ %= javascript '/static/js/jquery-3.4.1.min.js' %= javascript "/static/${av}/js/jquery-ui.min.js" %= javascript "/static/${av}/js/dbf.min.js" + % if (not stash('hide_opts')) { + %= javascript "/dyn/${av}/autocomplete.js", defer => undef + % } %= javascript "/static/${av}/js/marquee.min.js" %= javascript begin $(function () { $('marquee').marquee() }); -- cgit v1.2.3