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   3116 use Mojo::Base 'Mojolicious::Plugin';
  2         9988  
  2         14  
3 2     2   2248 use POSIX( qw/ceil/ );
  2         6559  
  2         16  
4 2     2   1732 use Mojo::ByteStream 'b';
  2         478953  
  2         125  
5              
6 2     2   17 use strict;
  2         3  
  2         62  
7 2     2   12 use warnings;
  2         4  
  2         2189  
8              
9             our $VERSION = "0.11";
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 52 my ( $self, $app, $args ) = @_;
17 1   50     4 $args ||= {};
18              
19             $app->helper( bootstrap_pagination => sub{
20 8     8   192224 my ( $self, $actual, $count, $opts ) = @_;
21 8         64 $count = ceil($count);
22 8 100       58 return "" unless $count > 1;
23 7 100       29 $opts = {} unless $opts;
24 7   100     39 my $round = $opts->{round} || 4;
25 7   100     42 my $param = $opts->{param} || "page";
26 7   100     36 my $class = $opts->{class} || "";
27 7 100       24 if ($class ne ""){
28 1         4 $class = " " . $class;
29             }
30 7   100     47 my $outer = $opts->{outer} || 2;
31 7   100     34 my $query = $opts->{query} || "";
32 7   100     38 my $start = $opts->{start} // 1;
33 7         36 my @current = ( $actual - $round .. $actual + $round );
34 7         29 my @first = ($start.. $start + $outer - 1);
35 7         21 my @tail = ( $count - $outer + 1 .. $count );
36 7         20 my @ret = ();
37 7         11 my $last = undef;
38 7         53 foreach my $number( sort { $a <=> $b } @current, @first, @tail ){
  137         155  
39 57 100 100     397 next if ( $last && $last == $number && $start > 0 ) || ( defined $last && $last == $number && $start == 0 );
      66        
      66        
      33        
      66        
40 48 100 66     328 next if ( $number <= 0 && $start > 0) || ( $number < 0 && $start == 0 );
      33        
      66        
41 40 100 66     196 last if ( $number > $count && $start > 0 ) || ( $number >= $count && $start == 0 );
      66        
      66        
42 38 100 100     137 push @ret, ".." if( $last && $last + 1 != $number );
43 38         45 push @ret, $number;
44 38         56 $last = $number;
45             }
46 7         22 my $html = "
    ";
47 7 100       18 if( $actual == $start ){
48 2         5 $html .= "
  • «
  • ";
    49             } else {
    50 5         24 $html .= "
  • url_for->query( $param => $actual - 1 ) . $query . "\" >«
  • ";
    51             }
    52 7         5862 my $last_num = -1;
    53 7         19 foreach my $number( @ret ){
    54 47 0       104 my $show_number = $start > 0 ? $number : ( $number =~ /\d+/ ? $number + 1 : $number );
        50          
    55 47 100 100     291 if( $number eq ".." && $last_num < $actual ){
        100 66        
        100          
    56 5         24 my $offset = ceil( ( $actual - $round ) / 2 ) + 1 ;
    57 5 50       23 $html .= "
  • url_for->query( $param => $start == 0 ? $offset + 1 : $offset ) . $query ."\" >..
  • ";
    58             }
    59             elsif( $number eq ".." && $last_num > $actual ) {
    60 4         12 my $back = $count - $outer + 1;
    61 4         8 my $forw = $round + $actual;
    62 4         18 my $offset = ceil( ( ( $back - $forw ) / 2 ) + $forw );
    63 4 50       18 $html .= "
  • url_for->query( $param => $start == 0 ? $offset + 1 : $offset ) . $query ."\" >..
  • ";
    64             } elsif( $number == $actual ) {
    65 7         20 $html .= "
  • $show_number
  • ";
    66             } else {
    67 31         106 $html .= "
  • url_for->query( $param => $number ) . $query ."\">$show_number
  • ";
    68             }
    69 47         39242 $last_num = $number;
    70             }
    71 7 50       29 if( $actual == $count ){
    72 0         0 $html .= "
  • url_for->query( $param => $actual + 1 ) . $query . "\" >»
  • ";
    73             } else {
    74 7         29 $html .= "
  • url_for->query( $param => $actual + 1 ) . $query . "\" >»
  • ";
    75             }
    76 7         7305 $html .= "";
    77 7         43 return b( $html );
    78 1         15 } );
    79              
    80             }
    81              
    82             1;
    83             __END__