File Coverage

blib/lib/Mojolicious/Plugin/SemanticUIPageNavigator.pm
Criterion Covered Total %
statement 56 56 100.0
branch 16 22 72.7
condition 10 14 71.4
subroutine 7 7 100.0
pod 1 1 100.0
total 90 100 90.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::SemanticUIPageNavigator;
2 3     3   3213 use Mojo::Base 'Mojolicious::Plugin';
  3         3361  
  3         18  
3 3     3   2832 use POSIX( qw/ceil/ );
  3         9941  
  3         19  
4 3     3   2869 use Mojo::DOM;
  3         132827  
  3         114  
5 3     3   26 use Mojo::ByteStream 'b';
  3         5  
  3         168  
6 3     3   700 use Modern::Perl;
  3         2089  
  3         26  
7              
8             our $VERSION = 0.01;
9             # ABSTRACT: Mojolicious::Plugin::SemanticUIPageNavigator
10              
11              
12             sub register{
13 1     1 1 68 my ($self, $app, $args) = @_;
14 1   50     6 $args ||={};
15              
16             $app->helper( page_navigator => sub{
17 1     1   13305 my ( $self, $actual, $count, $opts ) = @_;
18 1         13 $count = ceil($count);
19 1 50       6 return "" unless $count > 1;
20 1 50       8 $opts = {} unless $opts;
21 1   50     4 my $round = $opts->{round} || 3;
22 1   50     8 my $param = $opts->{param} || 'p';
23 1   50     9 my $outer = $opts->{outer} || 2;
24 1         6 my @current = ($actual - $round .. $actual + $round );
25 1 50       8 my @first = ( $round > $actual ? (1 .. $round * 3) : (1..$outer) );
26 1 50       6 my @tail = ( $count - $round < $actual
27             ? ($count - $round * 2 + 1 .. $count)
28             : ($count - $outer + 1 .. $count)
29             );
30 1         4 my @ret = ();
31 1         2 my $last = undef;
32 1         9 foreach my $number( sort { $a <=> $b} @current, @first, @tail ){
  30         33  
33 13 100 100     47 next if $last && $last == $number;
34 10 100       26 next if $number <= 0 ;
35 8 50       15 last if $number > $count;
36 8 100 100     36 push @ret, ".." if( $last && $last + 1 != $number );
37 8         10 push @ret, $number;
38 8         10 $last = $number;
39             }
40 1         11 my $dom = Mojo::DOM->new('
');
41 1         556 $dom->at('#pagination_outer')->attr({style => "margin: 10px auto; text-align: center"});
42 1         862 $dom->at('#pagination_inner')->append_content('首页上一页');
43 1         609 $dom->at('#pagination_inner')->attr({class => 'ui pagination menu'});
44 1         253 $dom->at("#semantic_pagination_1")->attr( {class => 'item', href => $self->url_for->clone->query($param => 1)} );
45 1         925 $dom->at("#semantic_pagination_2")->attr( {class => 'item', href => $self->url_for->clone->query($param => $actual - 1)} );
46 1         979 for my $number ( @ret ){
47 9 100       8214 if( $number eq '..'){
48 1         7 $dom->at('#pagination_inner')->append_content('..');
49 1         496 $dom->at("#se-pa-a")->attr( {class => 'item'} );
50             }else {
51 8         22 my $myid = "se-pa-a$number";
52 8         25 $dom->at("#pagination_inner")->append_content("$number");
53 8 100       3733 my $class = $number eq $actual ? 'active teal item' : 'item';
54 8         34 $dom->at("#$myid")->attr( {class => $class, href => $self->url_for->clone->query($param => $number) } );
55             }
56             }
57 1         1287 $dom->at('#pagination_inner')->append_content('下一页末页');
58 1 50       627 $dom->at("#last1")->attr( {class => 'item', href => $self->url_for->clone->query($param => $actual + 1 > $count ? $count : $actual + 1 ) } );
59 1         1357 $dom->at("#last2")->attr( {class => 'item', href => $self->url_for->clone->query($param => $count)} );
60 1         1426 return b($dom);
61 1         15 });
62             }
63              
64             1;
65              
66             __END__