From b161fcc96a318fac315d583a9974bc05f611afc9 Mon Sep 17 00:00:00 2001 From: Akron Date: Mon, 14 Oct 2024 16:26:12 +0200 Subject: [PATCH] Establish navi->list helper Change-Id: I0bab00500aac7c0398e8e4ab60792e0f0b567bb7 --- Changes | 3 ++- lib/Kalamar/Plugin/KalamarPages.pm | 27 +++++++++++++++++++++++++++ t/navigation.t | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Changes b/Changes index 82705c1b9..cdd34ff4d 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ -0.58 2024-10-08 +0.58 2024-10-14 - Fix meta table view for key value pairs (diewald) - Fix warning on OAuth public clients (diewald) + - Introduce navi->list helper (diewald) 0.57 2024-10-08 - Support VCs via URL without queries (diewald) diff --git a/lib/Kalamar/Plugin/KalamarPages.pm b/lib/Kalamar/Plugin/KalamarPages.pm index 244973fc3..514b1936f 100644 --- a/lib/Kalamar/Plugin/KalamarPages.pm +++ b/lib/Kalamar/Plugin/KalamarPages.pm @@ -268,6 +268,33 @@ sub register { return 1; } ); + + # List navi entries on top level + $mojo->helper( + 'navi.list' => sub { + my $c = shift; + my $realm = shift; + my @list = (); + + # Iterate over items + foreach (@{$navi->{$realm} // []}) { + # Fragments are not allowed on the top level - so ignore this! + + # Generate url with query parameter inheritance + my $url = $c->url_with($realm, page => $_->{id}); + + # Canonicalize (for empty scopes) + $url->path->canonicalize; + $url->fragment('page-top'); + + push @list, { + url => $url, + title => $c->loc('Nav_' . $_->{id}, $_->{title}) + }; + }; + return @list; + } + ); } 1; diff --git a/t/navigation.t b/t/navigation.t index bcb9c93df..1f56c9943 100644 --- a/t/navigation.t +++ b/t/navigation.t @@ -12,6 +12,8 @@ push(@{$app->plugins->namespaces}, 'Kalamar::Plugin'); # Establish test route $app->routes->get('/doc/:scope/:page')->to(cb => sub {}, scope => undef)->name('doc'); +$app->routes->get('/settings/:scope/:page')->to(cb => sub {}, scope => undef)->name('settings'); + # Load plugin to test $app->plugin('KalamarPages'); @@ -273,7 +275,27 @@ like($render, qr!Krill!, like($render, qr!FAQ!, 'Path matches FAQ'); +# Create settings realm +$app->navi->add(settings => ( + 'OAuth', 'oauth' +)); + +# Create settings realm +$app->navi->add(settings => ( + 'Marketplace', 'marketplace' +)); + +$render = $app->navigation('settings'); + +like($render, qr!/settings/oauth#page-top!); +like($render, qr!/settings/marketplace#page-top!); + +my @list = $app->navi->list('settings'); +is($list[0]->{url}, '/settings/oauth#page-top'); +is($list[0]->{title}, 'OAuth'); +is($list[1]->{url}, '/settings/marketplace#page-top'); +is($list[1]->{title}, 'Marketplace'); done_testing;