line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Page::Nav; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
54642
|
use strict; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
49
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
43
|
|
5
|
2
|
|
|
2
|
|
9
|
use base 'Data::Page'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
809
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/number_of_pages/); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
2
|
|
|
2
|
1
|
76
|
my $class = shift; |
13
|
2
|
|
|
|
|
6
|
my $self = { |
14
|
|
|
|
|
|
|
_first_nav_page => undef, |
15
|
|
|
|
|
|
|
_last_nav_page => undef, |
16
|
|
|
|
|
|
|
}; |
17
|
2
|
|
|
|
|
5
|
bless( $self, $class ); |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
|
|
6
|
my ( $total_entries, $entries_per_page, $current_page, $number_of_pages ) = @_; |
20
|
2
|
|
50
|
|
|
19
|
$self->total_entries( $total_entries || 0 ); |
21
|
2
|
|
50
|
|
|
46
|
$self->entries_per_page( $entries_per_page || 10 ); |
22
|
2
|
|
50
|
|
|
35
|
$self->current_page( $current_page || 1 ); |
23
|
2
|
|
50
|
|
|
30
|
$self->number_of_pages( $number_of_pages || 10 ); |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
14
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub pages_nav { |
29
|
31
|
|
|
31
|
1
|
4623
|
my $self = shift; |
30
|
|
|
|
|
|
|
|
31
|
31
|
|
66
|
|
|
80
|
my $total = int(shift || $self->number_of_pages); |
32
|
31
|
|
|
|
|
110
|
my $rest = $total % 2; |
33
|
31
|
|
|
|
|
53
|
my $half = ($total - $rest) / 2; |
34
|
|
|
|
|
|
|
|
35
|
31
|
|
|
|
|
55
|
$self->{_first_nav_page} = $self->_start_nav($total, $half, $rest); |
36
|
31
|
|
|
|
|
901
|
$self->{_last_nav_page} = $self->_end_nav($total, $half); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
return wantarray |
39
|
31
|
100
|
|
|
|
809
|
? ($self->first_nav_page .. $self->last_nav_page) |
40
|
|
|
|
|
|
|
: [$self->first_nav_page .. $self->last_nav_page]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub first_nav_page { |
44
|
60
|
|
|
60
|
1
|
9131
|
my $self = shift; |
45
|
60
|
|
|
|
|
83
|
my $number_of_pages = shift; |
46
|
|
|
|
|
|
|
|
47
|
4
|
|
|
|
|
8
|
return shift @{$self->pages_nav($number_of_pages)} |
48
|
60
|
100
|
66
|
|
|
186
|
if $number_of_pages || !$self->{_first_nav_page}; |
49
|
|
|
|
|
|
|
|
50
|
56
|
50
|
|
|
|
157
|
return $self->{_first_nav_page} if $self->{_first_nav_page}; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
0
|
return; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub last_nav_page { |
56
|
60
|
|
|
60
|
1
|
89
|
my $self = shift; |
57
|
60
|
|
|
|
|
62
|
my $number_of_pages = shift; |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
8
|
return pop @{$self->pages_nav($number_of_pages)} |
60
|
60
|
100
|
66
|
|
|
167
|
if $number_of_pages || !$self->{_last_nav_page}; |
61
|
|
|
|
|
|
|
|
62
|
57
|
50
|
|
|
|
281
|
return $self->{_last_nav_page} if $self->{_last_nav_page}; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
return; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _start_nav { |
68
|
31
|
|
|
31
|
|
48
|
my ($self, $total, $half, $rest) = @_; |
69
|
|
|
|
|
|
|
|
70
|
31
|
100
|
|
|
|
55
|
if ($self->current_page > $half) { |
71
|
24
|
100
|
|
|
|
1079
|
if (($self->current_page + $half) > $self->last_page) { |
72
|
5
|
50
|
|
|
|
290
|
if ($self->last_page > $total) { |
73
|
5
|
|
|
|
|
103
|
return ($self->last_page - $total) + 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} else { |
76
|
19
|
100
|
|
|
|
1126
|
return ($self->current_page - $half) + ($rest ? 0 : 1); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
7
|
|
|
|
|
324
|
return 1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _end_nav { |
84
|
31
|
|
|
31
|
|
47
|
my ($self, $total, $half) = @_; |
85
|
|
|
|
|
|
|
|
86
|
31
|
100
|
|
|
|
44
|
if ($self->last_page > ($self->current_page + $half)) { |
87
|
22
|
100
|
|
|
|
1286
|
if ($self->current_page <= $half) { |
88
|
7
|
50
|
|
|
|
276
|
if ($self->last_page > $total) { |
89
|
7
|
|
|
|
|
146
|
return $total; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} else { |
92
|
15
|
|
|
|
|
586
|
return $self->current_page + $half; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
9
|
|
|
|
|
546
|
return $self->last_page; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |