-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpagenav.pm
208 lines (153 loc) · 5.77 KB
/
mpagenav.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package SRP::Plugin::mpagenav;
use Mojo::Base 'Mojolicious::Plugin';
use POSIX( qw/ceil/ );
use Mojo::ByteStream 'b';
our $VERSION = "0.13";
# Homer: Well basically, I just copied the plant we have now.
# Then, I added some fins to lower wind resistance.
# And this racing stripe here I feel is pretty sharp.
# Burns: Agreed. First prize!
sub register {
my ( $self, $app, $args ) = @_;
$args ||= {};
$app->helper( m_page_nav => sub {
my ( $self, $actual, $count, $opts ) = @_;
my $localize = ( $opts->{localize} || $args->{localize} ) ?
( $opts->{localize} || $args->{localize} ) : undef;
$count = ceil($count);
return "" unless $count > 1;
$opts = {} unless $opts;
my $round = $opts->{round} || $args->{round} || 4;
my $param = $opts->{param} || $args->{param} || "page";
my $class = $opts->{class} || $args->{class} || "";
if ($class ne ""){
$class = " " . $class;
}
my $outer = $opts->{outer} || $args->{outer} || 2;
my $query = exists $opts->{query} ? $opts->{query} : $args->{query} || "";
my $start = $opts->{start} // $args->{start} // 1;
my @current = ( $actual - $round .. $actual + $round );
my @first = ($start.. $start + $outer - 1);
my @tail = ( $count - $outer + 1 .. $count );
my @ret = ();
my $last = undef;
foreach my $number( sort { $a <=> $b } @current, @first, @tail) {
next if ( $last && $last == $number && $start > 0 ) || ( defined $last && $last == $number && $start == 0 );
next if ( $number <= 0 && $start > 0) || ( $number < 0 && $start == 0 );
last if ( $number > $count && $start > 0 ) || ( $number >= $count && $start == 0 );
push @ret, ".." if( $last && $last + 1 != $number );
push @ret, $number;
$last = $number;
}
my $html = "<ul class=\"pagination$class\">";
if ($actual == $start) {
$html .= "<li class=\"disabled\"><a href=\"#!\"><i class=\"material-icons\">chevron_left</i></a></li>";
} else {
$html .= "<li class=\"waves-effect\"><a href=\"".$self->url_with->query([$param => $actual - 1]).$query."\"><i class=\"material-icons\">chevron_left</i></a></li>";
}
my $last_num = -1;
foreach my $number ( @ret ) {
my $show_number = $start > 0 ? $number : ( $number =~ /\d+/ ? $number + 1 : $number );
if ( $localize ) {
$show_number = $localize->($self, $show_number);
}
if ( $number eq ".." && $last_num < $actual ){
my $offset = ceil( ( $actual - $round ) / 2 ) + 1 ;
$html .= "<li class=\"waves-effect\"><a href=\"".$self->url_with->query([$param => $start == 0 ? $offset + 1 : $offset]).$query."\" >…</a></li>";
}
elsif ( $number eq ".." && $last_num > $actual ) {
my $back = $count - $outer + 1;
my $forw = $round + $actual;
my $offset = ceil( ( ( $back - $forw ) / 2 ) + $forw );
$html .= "<li class=\"waves-effect\"><a href=\"".$self->url_with->query([$param => $start == 0 ? $offset + 1 : $offset]).$query."\" >…</a></li>";
} elsif ( $number == $actual ) {
$html .= "<li class=\"active\"><a href=\"#!\">$show_number</a></li>";
} else {
$html .= "<li class=\"waves-effect\"><a href=\"".$self->url_with->query([$param => $number]).$query."\">$show_number</a></li>";
}
$last_num = $number;
}
if( $actual == $count ){
$html .= "<li class=\"disabled\"><a href=\"#!\"><i class=\"material-icons\">chevron_right</i></a></li>";
} else {
$html .= "<li class=\"waves-effect\"><a href=\"".$self->url_with->query([$param => $actual + 1]).$query."\"><i class=\"material-icons\">chevron_right</i></a></li>";
}
$html .= "</ul>";
return b( $html );
} );
}
1;
__END__
=encoding utf-8
=head1 NAME
Mojolicious::Plugin::mpagenav - Page Navigator plugin for Mojolicious
This module has derived from L<Mojolicious::Plugin::PageNavigator>
=head1 SYNOPSIS
# Mojolicious::Lite
plugin 'mpagenav'
# Mojolicious
$self->plugin( 'mpagenav' );
=head1 DESCRIPTION
L<Mojolicious::Plugin::mpagenav> generates standard page navigation bar, like
<< 1 2 ... 11 12 13 14 15 ... 85 86 >>
=head1 HELPERS
=head2 mpagenav
%= m_page_nav( $current_page, $total_pages, $opts );
=head3 Options
Options is a optional ref hash.
%= m_page_nav( $current_page, $total_pages, {
round => 4,
outer => 2,
query => "&id=$id",
start => 1,
class => 'center-align',
param => 'page' } );
=over 1
=item round
Number of pages around the current page. Default: 4.
=item outer
Number of outer window pages (first and last pages). Default 2.
=item param
Name of param for query url. Default: 'page'
=item query
Additional query string to url. Optional.
=item start
Start number for query string. Default: 1. Optional.
=back
=head1 INTERNATIONALIZATION
If you want to use internationalization (I18N), you can pass a code reference via I<localize>.
plugin 'bootstrap_pagination' => {
localize => \&localize,
};
sub localize {
my ($number) = @_;
my %trans = (
1 => 'one',
2 => 'two',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
);
return $trans{$number};
}
This will print the words instead of the numbers.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>,L<Mojolicious::Plugin::PageNavigator>.
=head1 Repository
https://github.com/dokechin/Mojolicious-Plugin-BootstrapPagination
=head1 LICENSE
Copyright (C) dokechin.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
dokechin E<lt>E<gt>
=head1 CONTRIBUTORS
Andrey Chips Kuzmin <chipsoid@cpan.org>
=cut