site: extend extended_pager

Make it return a hash for page numbers and shortcuts. Move logic
handling for the shortcuts from the templates to the helper. Make the
pager length configurable. Add a helper for producing the shortcuts'
links.
This commit is contained in:
Lucas Gabriel Vuotto 2025-04-26 13:29:29 +00:00
parent dd492db92e
commit 1a0a224edc
2 changed files with 36 additions and 21 deletions

View file

@ -3,23 +3,43 @@ use Mojo::Base "Mojolicious", -signatures;
use Pooru::Storage::Static;
use constant {
EXTENDED_PAGER_LENGTH => 7,
};
sub extended_pager ($self, $pager)
{
my ($start, $end);
my (%pages, $start, $end, $mid);
if ($pager->{current_page} < $pager->{first_page} + 2) {
$mid = int(EXTENDED_PAGER_LENGTH / 2);
if ($pager->{current_page} < $pager->{first_page} + $mid) {
$start = $pager->{first_page};
$end = $start + 4;
} elsif ($pager->{current_page} > $pager->{last_page} - 2) {
$end = $start + $mid * 2;
} elsif ($pager->{current_page} > $pager->{last_page} - $mid) {
$end = $pager->{last_page};
$start = $end - 4;
$start = $end - $mid * 2;
} else {
$start = $pager->{current_page} - 2;
$end = $pager->{current_page} + 2;
$start = $pager->{current_page} - $mid;
$end = $pager->{current_page} + $mid;
}
$pages{pages} = [grep {$_ >= $pager->{first_page} &&
$_ <= $pager->{last_page}} ($start .. $end)];
return grep {$_ >= $pager->{first_page} && $_ <= $pager->{last_page}}
($start .. $end);
$pages{shortcuts}{first_page} = $pager->{first_page} if
$pager->{first_page} != $pager->{current_page};
$pages{shortcuts}{previous_page} = $pager->{previous_page} if
defined($pager->{previous_page});
$pages{shortcuts}{next_page} = $pager->{next_page} if
defined($pager->{next_page});
$pages{shortcuts}{last_page} = $pager->{last_page} if
$pager->{last_page} != $pager->{current_page};
return %pages;
}
sub tag_for_pager_shortcut ($self, $content, $page) {
return (defined($page) and $self->link_to($content,
$self->url_with->query({page => $page})));
}
sub link_for_tag ($self, $tag)
@ -68,6 +88,7 @@ sub startup ($self)
return Mojo::URL->new($self->config("pooru_api")->{v0});
});
$self->helper(extended_pager => \&extended_pager);
$self->helper(tag_for_pager_shortcut => \&tag_for_pager_shortcut);
$self->helper(link_for_tag => \&link_for_tag);
$self->helper(tags_by_kind => \&tags_by_kind);