line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::BootstrapPagination; |
2
|
5
|
|
|
5
|
|
11850
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
5
|
|
|
|
|
10988
|
|
|
5
|
|
|
|
|
35
|
|
3
|
5
|
|
|
5
|
|
2745
|
use POSIX( qw/ceil/ ); |
|
5
|
|
|
|
|
7805
|
|
|
5
|
|
|
|
|
43
|
|
4
|
5
|
|
|
5
|
|
1974
|
use Mojo::ByteStream 'b'; |
|
5
|
|
|
|
|
108865
|
|
|
5
|
|
|
|
|
293
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
31
|
use strict; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
304
|
|
7
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
5095
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "0.13"; |
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
|
4
|
|
|
4
|
1
|
162
|
my ( $self, $app, $args ) = @_; |
17
|
4
|
|
50
|
|
|
16
|
$args ||= {}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->helper( bootstrap_pagination => sub{ |
20
|
20
|
|
|
20
|
|
415179
|
my ( $self, $actual, $count, $opts ) = @_; |
21
|
|
|
|
|
|
|
|
22
|
20
|
100
|
66
|
|
|
243
|
my $localize = ( $opts->{localize} || $args->{localize} ) ? |
|
|
|
33
|
|
|
|
|
23
|
|
|
|
|
|
|
( $opts->{localize} || $args->{localize} ) : undef; |
24
|
|
|
|
|
|
|
|
25
|
20
|
|
|
|
|
140
|
$count = ceil($count); |
26
|
20
|
100
|
|
|
|
93
|
return "" unless $count > 1; |
27
|
18
|
50
|
|
|
|
55
|
$opts = {} unless $opts; |
28
|
18
|
|
100
|
|
|
97
|
my $round = $opts->{round} || $args->{round} || 4; |
29
|
18
|
|
100
|
|
|
125
|
my $param = $opts->{param} || $args->{param} || "page"; |
30
|
18
|
|
100
|
|
|
119
|
my $class = $opts->{class} || $args->{class} || ""; |
31
|
18
|
100
|
|
|
|
55
|
if ($class ne ""){ |
32
|
9
|
|
|
|
|
21
|
$class = " " . $class; |
33
|
|
|
|
|
|
|
} |
34
|
18
|
|
100
|
|
|
98
|
my $outer = $opts->{outer} || $args->{outer} || 2; |
35
|
18
|
100
|
100
|
|
|
91
|
my $query = exists $opts->{query} ? $opts->{query} : $args->{query} || ""; |
36
|
18
|
|
66
|
|
|
117
|
my $start = $opts->{start} // $args->{start} // 1; |
|
|
|
100
|
|
|
|
|
37
|
18
|
|
|
|
|
85
|
my @current = ( $actual - $round .. $actual + $round ); |
38
|
18
|
|
|
|
|
59
|
my @first = ($start.. $start + $outer - 1); |
39
|
18
|
|
|
|
|
54
|
my @tail = ( $count - $outer + 1 .. $count ); |
40
|
18
|
|
|
|
|
38
|
my @ret = (); |
41
|
18
|
|
|
|
|
32
|
my $last = undef; |
42
|
18
|
|
|
|
|
107
|
foreach my $number( sort { $a <=> $b } @current, @first, @tail ){ |
|
319
|
|
|
|
|
278
|
|
43
|
142
|
100
|
100
|
|
|
818
|
next if ( $last && $last == $number && $start > 0 ) || ( defined $last && $last == $number && $start == 0 ); |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
44
|
123
|
100
|
66
|
|
|
462
|
next if ( $number <= 0 && $start > 0) || ( $number < 0 && $start == 0 ); |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
45
|
111
|
100
|
66
|
|
|
457
|
last if ( $number > $count && $start > 0 ) || ( $number >= $count && $start == 0 ); |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
46
|
107
|
100
|
100
|
|
|
331
|
push @ret, ".." if( $last && $last + 1 != $number ); |
47
|
107
|
|
|
|
|
103
|
push @ret, $number; |
48
|
107
|
|
|
|
|
106
|
$last = $number; |
49
|
|
|
|
|
|
|
} |
50
|
18
|
|
|
|
|
53
|
my $html = " |
51
|
18
|
100
|
|
|
|
53
|
if( $actual == $start ){ |
52
|
4
|
|
|
|
|
9
|
$html .= "«"; |
53
|
|
|
|
|
|
|
} else { |
54
|
14
|
|
|
|
|
122
|
$html .= "url_with->query( [$param => $actual - 1] ) . $query . "\" >«"; |
55
|
|
|
|
|
|
|
} |
56
|
18
|
|
|
|
|
17300
|
my $last_num = -1; |
57
|
18
|
|
|
|
|
51
|
foreach my $number( @ret ){ |
58
|
132
|
0
|
|
|
|
302
|
my $show_number = $start > 0 ? $number : ( $number =~ /\d+/ ? $number + 1 : $number ); |
|
|
50
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
132
|
100
|
|
|
|
235
|
if ( $localize ) { |
61
|
26
|
|
|
|
|
61
|
$show_number = $localize->($self, $show_number); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
132
|
100
|
100
|
|
|
2428
|
if( $number eq ".." && $last_num < $actual ){ |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
|
|
|
|
|
65
|
14
|
|
|
|
|
95
|
my $offset = ceil( ( $actual - $round ) / 2 ) + 1 ; |
66
|
14
|
50
|
|
|
|
87
|
$html .= "url_with->query( [$param => $start == 0 ? $offset + 1 : $offset] ) . $query ."\" >…"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
elsif( $number eq ".." && $last_num > $actual ) { |
69
|
11
|
|
|
|
|
34
|
my $back = $count - $outer + 1; |
70
|
11
|
|
|
|
|
19
|
my $forw = $round + $actual; |
71
|
11
|
|
|
|
|
58
|
my $offset = ceil( ( ( $back - $forw ) / 2 ) + $forw ); |
72
|
11
|
50
|
|
|
|
68
|
$html .= "url_with->query( [$param => $start == 0 ? $offset + 1 : $offset] ) . $query ."\" >…"; |
73
|
|
|
|
|
|
|
} elsif( $number == $actual ) { |
74
|
18
|
|
|
|
|
129
|
$html .= "$show_number"; |
75
|
|
|
|
|
|
|
} else { |
76
|
89
|
|
|
|
|
448
|
$html .= "url_with->query( [$param => $number] ) . $query ."\">$show_number"; |
77
|
|
|
|
|
|
|
} |
78
|
132
|
|
|
|
|
119743
|
$last_num = $number; |
79
|
|
|
|
|
|
|
} |
80
|
18
|
50
|
|
|
|
103
|
if( $actual == $count ){ |
81
|
0
|
|
|
|
|
0
|
$html .= "url_with->query( [$param => $actual + 1] ) . $query . "\" >»"; |
82
|
|
|
|
|
|
|
} else { |
83
|
18
|
|
|
|
|
105
|
$html .= "url_with->query( [$param => $actual + 1] ) . $query . "\" >»"; |
84
|
|
|
|
|
|
|
} |
85
|
18
|
|
|
|
|
18810
|
$html .= ""; |
86
|
18
|
|
|
|
|
94
|
return b( $html ); |
87
|
4
|
|
|
|
|
42
|
} ); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
__END__ |