File Coverage

blib/lib/Mojolicious/Plugin/BootstrapPagination.pm
Criterion Covered Total %
statement 64 65 98.4
branch 26 32 81.2
condition 42 56 75.0
subroutine 7 7 100.0
pod 1 1 100.0
total 140 161 86.9


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::BootstrapPagination;
2 2     2   2233 use Mojo::Base 'Mojolicious::Plugin';
  2         10989  
  2         16  
3 2     2   2167 use POSIX( qw/ceil/ );
  2         7871  
  2         17  
4 2     2   1651 use Mojo::ByteStream 'b';
  2         416019  
  2         114  
5              
6 2     2   18 use strict;
  2         4  
  2         68  
7 2     2   10 use warnings;
  2         3  
  2         1328  
8              
9             our $VERSION = "0.12";
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 34 my ( $self, $app, $args ) = @_;
17 1   50     3 $args ||= {};
18              
19             $app->helper( bootstrap_pagination => sub{
20 9     9   203192 my ( $self, $actual, $count, $opts ) = @_;
21 9         59 $count = ceil($count);
22 9 100       47 return "" unless $count > 1;
23 8 100       35 $opts = {} unless $opts;
24 8   100     39 my $round = $opts->{round} || 4;
25 8   100     52 my $param = $opts->{param} || "page";
26 8   100     40 my $class = $opts->{class} || "";
27 8 100       32 if ($class ne ""){
28 1         2 $class = " " . $class;
29             }
30 8   100     42 my $outer = $opts->{outer} || 2;
31 8   100     44 my $query = $opts->{query} || "";
32 8   100     36 my $start = $opts->{start} // 1;
33 8         44 my @current = ( $actual - $round .. $actual + $round );
34 8         28 my @first = ($start.. $start + $outer - 1);
35 8         24 my @tail = ( $count - $outer + 1 .. $count );
36 8         18 my @ret = ();
37 8         14 my $last = undef;
38 8         81 foreach my $number( sort { $a <=> $b } @current, @first, @tail ){
  146         167  
39 62 100 100     381 next if ( $last && $last == $number && $start > 0 ) || ( defined $last && $last == $number && $start == 0 );
      66        
      66        
      33        
      66        
40 53 100 66     225 next if ( $number <= 0 && $start > 0) || ( $number < 0 && $start == 0 );
      33        
      66        
41 45 100 66     192 last if ( $number > $count && $start > 0 ) || ( $number >= $count && $start == 0 );
      66        
      66        
42 43 100 100     146 push @ret, ".." if( $last && $last + 1 != $number );
43 43         49 push @ret, $number;
44 43         57 $last = $number;
45             }
46 8         25 my $html = "
    ";
47 8 100       26 if( $actual == $start ){
48 2         4 $html .= "
  • «
  • ";
    49             } else {
    50 6         54 $html .= "
  • url_with->query( [$param => $actual - 1] ) . $query . "\" >«
  • ";
    51             }
    52 8         9676 my $last_num = -1;
    53 8         25 foreach my $number( @ret ){
    54 54 0       141 my $show_number = $start > 0 ? $number : ( $number =~ /\d+/ ? $number + 1 : $number );
        50          
    55 54 100 100     334 if( $number eq ".." && $last_num < $actual ){
        100 66        
        100          
    56 6         45 my $offset = ceil( ( $actual - $round ) / 2 ) + 1 ;
    57 6 50       42 $html .= "
  • url_with->query( [$param => $start == 0 ? $offset + 1 : $offset] ) . $query ."\" >…
  • ";
    58             }
    59             elsif( $number eq ".." && $last_num > $actual ) {
    60 5         17 my $back = $count - $outer + 1;
    61 5         13 my $forw = $round + $actual;
    62 5         37 my $offset = ceil( ( ( $back - $forw ) / 2 ) + $forw );
    63 5 50       43 $html .= "
  • url_with->query( [$param => $start == 0 ? $offset + 1 : $offset] ) . $query ."\" >…
  • ";
    64             } elsif( $number == $actual ) {
    65 8         24 $html .= "
  • $show_number
  • ";
    66             } else {
    67 35         221 $html .= "
  • url_with->query( [$param => $number] ) . $query ."\">$show_number
  • ";
    68             }
    69 54         62772 $last_num = $number;
    70             }
    71 8 50       31 if( $actual == $count ){
    72 0         0 $html .= "
  • url_with->query( [$param => $actual + 1] ) . $query . "\" >»
  • ";
    73             } else {
    74 8         57 $html .= "
  • url_with->query( [$param => $actual + 1] ) . $query . "\" >»
  • ";
    75             }
    76 8         10930 $html .= "";
    77 8         50 return b( $html );
    78 1         12 } );
    79              
    80             }
    81              
    82             1;
    83             __END__