[wellwell-devel] [wellwell] Various enhancements for custom paging subroutine.
Stefan Hornburg
wellwell-devel at rt.icdevgroup.org
Tue Mar 20 11:51:39 UTC 2012
commit 61c8c4e576f483c4c075e87a3506aeb37b92657f
Author: Stefan Hornburg (Racke) <racke at linuxia.de>
Date: Tue Mar 20 12:44:14 2012 +0100
Various enhancements for custom paging subroutine.
Add support for [more], [matches], [match-count] and any [-anchor] tag inside [more-list].
Store current position into paging_first_match scratch variable.
Fix calculation of page count.
code/paging.sub | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 102 insertions(+), 9 deletions(-)
---
diff --git a/code/paging.sub b/code/paging.sub
index 040575a..85b5057 100644
--- a/code/paging.sub
+++ b/code/paging.sub
@@ -1,6 +1,9 @@
Sub paging <<EOS
sub {
my ($next, $prev, $page, $border, $border_selected, $opt, $r) = @_;
+ my ($q, $pages, $first, $curpage, $url, @links, @labels, @more, $base_url,
+ $prefix, $suffix, $session, $form_arg, $nav, $ml, $matches, $replace,
+ $out, $link_prefix, $link_suffix);
my $q;
@@ -8,8 +11,6 @@ sub {
return '' unless $q->{matches} > $q->{mv_matchlimit}
and $q->{mv_matchlimit} > 0;
- my ($pages, $first, $curpage, $url, @links, @more, $base_url, $prefix, $suffix, $session, $form_arg, $nav, $ml, $matches);
-
# turn form parameter into a hash
my %form;
@@ -26,17 +27,66 @@ sub {
$ml = $q->{mv_matchlimit};
$matches = $q->{matches};
- $pages = $matches / $ml;
+ $pages = int($matches / $ml);
if ($matches % $ml) {
$pages++;
}
+ $curpage = $opt->{paging_page} || $CGI->{category_page};
+
+ if ($curpage) {
+ $first = ($curpage - 1) * $ml;
+ }
+ else {
+ $first = $q->{mv_first_match} || 1;
+ }
+
+ if ($r =~ /\[more\]/) {
+ # check for [more] replacement
+ $replace = 1;
+ }
+
+ if ($r =~ /\[match/) {
+ my ($range_start, $range_end);
+
+ $range_start = $first;
+ $range_end = $first + $ml - 1;
+
+ if ($range_end > $matches) {
+ $range_end = $matches;
+ }
+
+ $r =~ s/\[matches]/$range_start-$range_end/;
+ $r =~ s/\[match-count]/$matches/;
+
+ return $r unless $replace;
+ }
+
+ # extract anchor labels
+ my %anchor_labels;
+
+ for my $anchor qw(first prev next last) {
+ if ($r =~ s:\[$anchor[-_]anchor\](.*?)\[/$anchor[-_]anchor\]::i) {
+ $anchor_labels{$anchor} = $1;
+ }
+ }
+
+ # link prefix / link suffix
+ $link_prefix = (exists $opt->{link_prefix}) ? $opt->{link_prefix} : '<li>';
+ $link_suffix = (exists $opt->{link_suffix}) ? $opt->{link_suffix} : '</li>';
+
$first = $q->{mv_first_match} || 0;
+ $Scratch->{paging_first_match} = $first;
$session = $q->{mv_cache_key};
- $prefix = q{<div id="pagenumbers"><ul>};
- $suffix = q{</ul></div>};
+ if ($replace) {
+ $prefix = $suffix = '';
+ }
+ else {
+ $prefix = q{<div id="pagenumbers"><ul>};
+ $suffix = q{</ul></div>};
+ }
$search_page = $q->{mv_search_page} || $Tag->var('MV_PAGE',1);
@@ -68,22 +118,65 @@ sub {
$url = $Tag->area({href => "scan/MM=$nav", form => $form_arg});
}
$links[$i] = $url;
+ $labels[$i] = $i;
}
+ # current page
+ $links[$curpage] = '';
+ $labels[$curpage] = $curpage;
+
for (my $i = 1; $i <= $pages; $i++) {
if ($links[$i]) {
- push(@more, qq{<li><a href="$links[$i]">$i</a></li>});
+ push(@more, qq{$link_prefix<a href="$links[$i]">$labels[$i]</a>$link_suffix});
}
else {
- push(@more, qq{<li class="down">$i</li>});
+ push(@more, qq{$link_prefix$labels[$i]$link_suffix});
}
}
+ unless ($curpage == 1) {
+ $url = $links[$curpage-1];
+
+ if ($anchor_labels{first}) {
+ unshift(@more, qq{$link_prefix<a href="$url">$anchor_labels{first}</a>$link_suffix});
+ }
+ else {
+ unshift(@more, qq{$link_prefix<a href="$url">first</a>$link_suffix});
+ }
+
+ if ($anchor_labels{prev}) {
+ unshift(@more, qq{$link_prefix<a href="$url">$anchor_labels{prev}</a>$link_suffix});
+ }
+ else {
+ unshift(@more, qq{$link_prefix<a href="$url">previous <</a>$link_suffix});
+ }
+ }
+
unless ($curpage == int($pages)) {
$url = $links[$curpage+1];
- push(@more, qq{<li><a href="$url">next ></a></li>});
+
+ if ($anchor_labels{next}) {
+ push(@more, qq{$link_prefix<a href="$url">$anchor_labels{next}</a>$link_suffix});
+ }
+ else {
+ push(@more, qq{$link_prefix<a href="$url">next ></a>$link_suffix});
+ }
+
+ if ($anchor_labels{last}) {
+ push(@more, qq{$link_prefix<a href="$url">$anchor_labels{last}</a>$link_suffix});
+ }
+ else {
+ push(@more, qq{$link_prefix<a href="$url">$more</a>$link_suffix});
+ }
+ }
+
+ $out = $prefix . join(' ', @more) . $suffix;
+
+ if ($replace) {
+ $r =~ s/\[more\]/$out/g;
+ return $r;
}
- return $prefix . join(' ', @more) . $suffix;
+ return $out;
}
EOS
More information about the wellwell-devel
mailing list