File Coverage

blib/lib/Mojolicious/Plugin/PageNavigator.pm
Criterion Covered Total %
statement 51 53 96.2
branch 16 24 66.6
condition 11 16 68.7
subroutine 7 7 100.0
pod 1 1 100.0
total 86 101 85.1


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::PageNavigator;
2 1     1   962 use Mojo::Base 'Mojolicious::Plugin';
  1         2  
  1         8  
3 1     1   210 use POSIX( qw/ceil/ );
  1         2  
  1         9  
4 1     1   100 use Mojo::ByteStream 'b';
  1         2  
  1         53  
5              
6 1     1   5 use strict;
  1         2  
  1         41  
7 1     1   5 use warnings;
  1         2  
  1         756  
8              
9             our $VERSION = 0.01;
10              
11             # Homer: Well basically, I just copied the plant we have now.
12             # Then, I added some fins to lower wind resistance.
13             # And this racing stripe here I feel is pretty sharp.
14             # Burns: Agreed. First prize!
15             sub register{
16 1     1 1 41 my ( $self, $app, $args ) = @_;
17 1   50     5 $args ||= {};
18              
19             $app->helper( page_navigator => sub{
20 1     1   41905 my ( $self, $actual, $count, $opts ) = @_;
21 1         15 $count = ceil($count);
22 1 50       7 return "" unless $count > 1;
23 1 50       4 $opts = {} unless $opts;
24 1   50     7 my $round = $opts->{round} || 4;
25 1   50     15 my $class = $opts->{class} || "number";
26 1   50     8 my $param = $opts->{param} || "page";
27 1   50     6 my $outer = $opts->{outer} || 2;
28 1         6 my @current = ( $actual - $round .. $actual + $round );
29 1 50       7 my @first = ( $round > $actual ? (1..$round * 3 ) : (1..$outer) );
30 1 50       6 my @tail = ( $count - $round < $actual ? ( $count - $round * 2 + 1 .. $count ) :
31             ( $count - $outer + 1 .. $count ) );
32 1         2 my @ret = ();
33 1         2 my $last = undef;
34 1         10 foreach my $number( sort { $a <=> $b } @current, @first, @tail ){
  31         31  
35 13 100 100     55 next if $last && $last == $number;
36 12 50       24 next if $number <= 0 ;
37 12 50       18 last if $number > $count;
38 12 100 100     43 push @ret, ".." if( $last && $last + 1 != $number );
39 12         17 push @ret, $number;
40 12         28 $last = $number;
41             }
42            
43 1         3 my $html = "
";
44 1 50       10 if( $actual == 1 ){
45 0         0 $html .= "<<";
46             } else {
47 1         5 $html .= "url_for->query( $param => $actual - 1 ) . "\" class=\"$class\"><<";
48             }
49 1         1236 foreach my $number( @ret ){
50 13 100       10736 if( $number eq ".." ){
    100          
51 1         4 $html .= "..";
52             } elsif( $number == $actual ) {
53 1         4 $html .= "$number";
54             } else {
55 11         35 $html .= "url_for->query( $param => $number ) ."\" class=\"$class\">$number";
56             }
57             }
58 1 50       1203 if( $actual == $count ){
59 0         0 $html .= ">>";
60             } else {
61 1         6 $html .= "url_for->query( $param => $actual + 1 ) . "\" class=\"$class\">>>"
62             }
63 1         1015 $html .= " ";
64 1         8 return b( $html );
65 1         13 } );
66              
67             }
68              
69             1;
70             __END__