File Coverage

blib/lib/Mojolicious/Plugin/SemanticUIPageNavigator.pm
Criterion Covered Total %
statement 55 55 100.0
branch 16 22 72.7
condition 10 14 71.4
subroutine 7 7 100.0
pod 1 1 100.0
total 89 99 89.9


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::SemanticUIPageNavigator;
2 3     3   3142 use Mojo::Base 'Mojolicious::Plugin';
  3         3494  
  3         17  
3 3     3   2837 use POSIX( qw/ceil/ );
  3         10070  
  3         18  
4 3     3   2886 use Mojo::DOM;
  3         293068  
  3         108  
5 3     3   27 use Mojo::ByteStream 'b';
  3         4  
  3         173  
6 3     3   458 use Modern::Perl;
  3         1336  
  3         30  
7              
8             our $VERSION = 0.03;
9             # ABSTRACT: Mojolicious::Plugin::SemanticUIPageNavigator
10              
11              
12             sub register{
13 1     1 1 52 my ($self, $app, $args) = @_;
14 1   50     4 $args ||={};
15              
16             $app->helper( page_navigator => sub{
17 1     1   8941 my ( $self, $actual, $count, $opts ) = @_;
18 1         10 $count = ceil($count);
19 1 50       5 return "" unless $count > 1;
20 1 50       6 $opts = {} unless $opts;
21 1   50     3 my $round = $opts->{round} || 3;
22 1   50     7 my $param = $opts->{param} || 'p';
23 1   50     4 my $outer = $opts->{outer} || 2;
24 1         4 my @current = ($actual - $round .. $actual + $round );
25 1 50       6 my @first = ( $round > $actual ? (1 .. $round * 3) : (1..$outer) );
26 1 50       5 my @tail = ( $count - $round < $actual
27             ? ($count - $round * 2 + 1 .. $count)
28             : ($count - $outer + 1 .. $count)
29             );
30 1         2 my @ret = ();
31 1         2 my $last = undef;
32 1         7 foreach my $number( sort { $a <=> $b} @current, @first, @tail ){
  30         26  
33 13 100 100     32 next if $last && $last == $number;
34 10 100       22 next if $number <= 0 ;
35 8 50       11 last if $number > $count;
36 8 100 100     21 push @ret, ".." if( $last && $last + 1 != $number );
37 8         9 push @ret, $number;
38 8         10 $last = $number;
39             }
40 1         9 my $dom = Mojo::DOM->new('
');
41 1         450 $dom->at('.pagination_outer')->attr({style => "margin: 10px auto; text-align: center"});
42 1         377 $dom->at('.pagination_inner')->append_content('首页上一页');
43 1         400 $dom->at('.pagination_inner')->attr({class => 'ui pagination menu'});
44 1         161 $dom->at(".semantic_pagination_1")->attr( {class => 'item', href => $self->url_for->clone->query($param => 1)} );
45 1         647 $dom->at(".semantic_pagination_2")->attr( {class => 'item', href => $self->url_for->clone->query($param => $actual - 1)} );
46 1         572 for my $number ( @ret ){
47 9 100       4868 if( $number eq '..'){
48 1         3 $dom->at('.pagination')->append_content('..');
49             }else {
50 8         12 my $tmp_class = "se-pa-a$number";
51 8         16 $dom->at(".pagination")->append_content("$number");
52 8 100       2413 my $real_class = $number eq $actual ? 'active teal item' : 'item';
53 8         21 $dom->at(".$tmp_class")->attr( {class => $real_class, href => $self->url_for->clone->query($param => $number) } );
54             }
55             }
56 1         739 $dom->at('.pagination')->append_content('下一页末页');
57 1 50       397 $dom->at(".last1")->attr( {class => 'item', href => $self->url_for->clone->query($param => $actual + 1 > $count ? $count : $actual + 1 ) } );
58 1         796 $dom->at(".last2")->attr( {class => 'item', href => $self->url_for->clone->query($param => $count)} );
59 1         825 return b($dom);
60 1         13 });
61             }
62              
63             1;
64              
65             __END__