Pager.pm

filter: Statementstmt 88.8% 80 / 90 Branchbran 70.4% 31 / 44 Conditioncond 61.9% 13 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 78.4% 142 / 181 CC 31 SCAR 34.4
CC
31
Cov
80%
_process
48.8
new
5.0
_css_colors_optional
2.0
CRAP 31.1
↑ Summary Pager/Utils.pm »
1 package Tags::HTML::Pager;
2  
3 5 ▶use base qw(Tags::HTML);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Class::Utils qw(set_params split_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Unicode::UTF8 qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 Readonly::Scalar our $NUMBER_OF_BOXES => 7;
13  
14 5 our $VERSION = 0.05;
15  
16 # Constructor.
17 16 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
18 16 my ($class, @params) = @_;
19  
20 # Create object.
21 16 my ($object_params_ar, $other_params_ar) = split_params(
22 ['css_colors', 'css_pager', 'flag_prev_next', 'flag_paginator',
23 'url_page_cb'], @params);
24 16 my $self = $class->SUPER::new(@{$other_params_ar});
25  
26 # CSS colors.
27 $self->{'css_colors'} = {
28 'actual_background' => 'black',
29 'actual_color' => 'white',
30 'border' => 'black',
31 'hover_background' => 'black',
32 'hover_color' => 'white',
33 'other_background' => undef,
34 'other_color' => 'black',
35 },
36  
37 # CSS class.
38 14 $self->{'css_pager'} = 'pager';
39  
40 # Flag for prev/next buttons.
41 14 $self->{'flag_prev_next'} = 0;
42  
43 # Flag for paginator.
44 14 $self->{'flag_paginator'} = 1;
45  
46 # URL of page.
47 14 $self->{'url_page_cb'} = undef;
48  
49 # Process params.
50 14 set_params($self, @{$object_params_ar});
51  
52 14 ▶ if (! defined $self->{'css_pager'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"css_pager"}) 1 13 14
53 1 err "Parameter 'css_pager' is required.";
54 }
55  
56 13 ▶ if (! defined $self->{'url_page_cb'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"url_page_cb"}) 1 12 13
57 1 err "Missing 'url_page_cb' parameter.";
58 }
59  
60 12 ▶ if (! $self->{'flag_paginator'} && ! $self->{'flag_prev_next'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"flag_paginator"} or $self->{"flag_prev_next"}) 1 11 12
Condition100%3/3
$self->{"flag_paginator"} or $self->{"flag_prev_next"}
l!l&&r!l&&!r
921
MC/DC100%2/2
$self->{"flag_paginator"} or $self->{"flag_prev_next"}
$self->{"flag_paginator"} $self->{"flag_prev_next"}
Truth table100%3/3
A or B
A= $self->{"flag_paginator"}B= $self->{"flag_prev_next"}
ABresult
1X1
011
000
61 1 err 'Both paginator styles disabled.';
62 }
63  
64 # Object.
65 11 return $self;
66 }
67  
68 20 ▶sub _css_class {
Subroutine _css_class: called CC=1 SCAR=0.0
69 20 my ($self, $suffix) = @_;
70  
71 20 return $self->{'css_pager'}.'-'.$suffix;
72 }
73  
74 4 ▶sub _css_colors_optional {
Subroutine _css_colors_optional: called CC=2 SCAR=6.9
75 4 my ($self, $css_color, $css_key) = @_;
76  
77 return defined $self->{'css_colors'}->{$css_color}
78 4 ▶ ? (['d', $css_key, $self->{'css_colors'}->{$css_color}])
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'css_colors'}{$css_color} ? : 3 1 4
79 : ();
80 }
81  
82 7 ▶sub _process {
Subroutine _process: called CC=26 SCAR=38.9
83 7 my ($self, $pages_hr) = @_;
84  
85 7 ▶ if (! $pages_hr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pages_hr) 1 6 7
86 1 err 'Pages data structure is missing.';
87 }
88 6 ▶ if (! exists $pages_hr->{'pages_num'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pages_hr->{"pages_num"}) 1 5 6
89 1 err "Missing 'pages_num' parameter in pages data structure.";
90 }
91 5 ▶ if (! exists $pages_hr->{'actual_page'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pages_hr->{"actual_page"}) 1 4 5
92 1 err "Missing 'actual_page' parameter in pages data structure.";
93 }
94 4 ▶ if ($pages_hr->{'actual_page'} > $pages_hr->{'pages_num'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{"actual_page"} > $pages_hr->{"pages_num"}) 1 3 4
95 err "Parameter 'actual_page' is greater than parameter 'pages_num'.",
96 'actual_page', $pages_hr->{'actual_page'},
97 1 'pages_num', $pages_hr->{'pages_num'},
98 ;
99 }
100  
101 # No code.
102 3 ▶ if (! $self->{'flag_paginator'}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"flag_paginator"} and $self->{"flag_prev_next"} and $pages_hr->{"pages_num"} == 1) 1 2 3
Condition83%5/6
not $self->{"flag_paginator"} and $self->{"flag_prev_next"} and $pages_hr->{"pages_num"} == 1
!ll&&!rl&&r
111
not $self->{"flag_paginator"} and $self->{"flag_prev_next"}
!ll&&!rl&&r
102
MC/DC66%2/3
not $self->{"flag_paginator"} and $self->{"flag_prev_next"} and $pages_hr->{"pages_num"} == 1
not $self->{"flag_paginator"} $self->{"flag_prev_next"} $pages_hr->{"pages_num"} == 1
Truth table75%3/4
not(A) and B and C
A= not $self->{"flag_paginator"}B= $self->{"flag_prev_next"}C= $pages_hr->{"pages_num"} == 1
ABCresult
0XX0
10X0
1100
1111
103 && $self->{'flag_prev_next'} && $pages_hr->{'pages_num'} == 1) {
104  
105 1 return;
106 }
107  
108 $self->{'tags'}->put(
109 ['b', 'div'],
110 2 ['a', 'class', $self->{'css_pager'}],
111 );
112  
113 # Paginator
114 2 ▶ if ($self->{'flag_paginator'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flag_paginator"}) 1 1 2
115 1 $self->{'tags'}->put(
116 ['b', 'p'],
117 ['a', 'class', $self->_css_class('paginator')],
118 );
119 1 my $buttons_from = 1;
120 1 my $buttons_to = $pages_hr->{'pages_num'};
121 1 ▶ if ($pages_hr->{'actual_page'} > 4
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{"actual_page"} > 4 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES) 0 1 1
Condition33%1/3
$pages_hr->{"actual_page"} > 4 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
!ll&&!rl&&r
100
MC/DC0%0/2
$pages_hr->{"actual_page"} > 4 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
$pages_hr->{"actual_page"} > 4 $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
Truth table33%1/3
A and B
A= $pages_hr->{"actual_page"} > 4B= $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
ABresult
0X0
100
111
122 && $pages_hr->{'pages_num'} > $NUMBER_OF_BOXES) {
123  
124 $self->{'tags'}->put(
125 ['b', 'a'],
126 0 ['a', 'href', $self->{'url_page_cb'}->(1, $pages_hr)],
127 ['d', 1],
128 ['e', 'a'],
129  
130 ['b', 'span'],
131 ['d', decode_utf8('…')],
132 ['e', 'span'],
133 );
134 0 ▶ if ($pages_hr->{'actual_page'} < $pages_hr->{'pages_num'} - 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{'actual_page'} < $pages_hr->{'pages_num'} - 3) { } 0 0 0
135 0 $buttons_from = $pages_hr->{'actual_page'} - 1;
136 } else {
137 0 $buttons_from = $pages_hr->{'pages_num'} - 4;
138 }
139 }
140 1 ▶ if ($pages_hr->{'actual_page'} < $pages_hr->{'pages_num'} - 3
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES) 0 1 1
Condition33%1/3
$pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
!ll&&!rl&&r
100
MC/DC0%0/2
$pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
$pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
Truth table33%1/3
A and B
A= $pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3B= $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
ABresult
0X0
100
111
141 && $pages_hr->{'pages_num'} > $NUMBER_OF_BOXES) {
142  
143 0 ▶ if ($pages_hr->{'actual_page'} > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{'actual_page'} > 4) { } 0 0 0
144 0 $buttons_to = $pages_hr->{'actual_page'} + 1;
145 } else {
146 0 $buttons_to = 5;
147 }
148 }
149 1 foreach my $button_num ($buttons_from .. $buttons_to) {
150 1 ▶ if ($pages_hr->{'actual_page'} eq $button_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{'actual_page'} eq $button_num) { } 1 0 1
151 1 $self->{'tags'}->put(
152 ['b', 'strong'],
153 ['a', 'class', $self->_css_class('paginator-selected')],
154 ['d', $button_num],
155 ['e', 'strong'],
156 );
157 } else {
158 $self->{'tags'}->put(
159 ['b', 'a'],
160 0 ['a', 'href', $self->{'url_page_cb'}->($button_num, $pages_hr)],
161 ['d', $button_num],
162 ['e', 'a'],
163 );
164 }
165 }
166 1 ▶ if ($pages_hr->{'actual_page'} < $pages_hr->{'pages_num'} - 3
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES) 0 1 1
Condition33%1/3
$pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
!ll&&!rl&&r
100
MC/DC0%0/2
$pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 and $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
$pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3 $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
Truth table33%1/3
A and B
A= $pages_hr->{"actual_page"} < $pages_hr->{"pages_num"} - 3B= $pages_hr->{"pages_num"} > $Tags::HTML::Pager::NUMBER_OF_BOXES
ABresult
0X0
100
111
167 && $pages_hr->{'pages_num'} > $NUMBER_OF_BOXES) {
168  
169 $self->{'tags'}->put(
170 ['b', 'span'],
171 ['d', decode_utf8('…')],
172 ['e', 'span'],
173  
174 ['b', 'a'],
175 ['a', 'href', $self->{'url_page_cb'}->($pages_hr->{'pages_num'}, $pages_hr)],
176 0 ['d', $pages_hr->{'pages_num'}],
177 ['e', 'a'],
178 );
179 }
180 1 $self->{'tags'}->put(
181 ['e', 'p'],
182 );
183 }
184  
185 # Paging.
186 2 ▶ if ($self->{'flag_prev_next'} && $pages_hr->{'pages_num'} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flag_prev_next"} and $pages_hr->{"pages_num"} > 1) 1 1 2
Condition66%2/3
$self->{"flag_prev_next"} and $pages_hr->{"pages_num"} > 1
!ll&&!rl&&r
101
MC/DC50%1/2
$self->{"flag_prev_next"} and $pages_hr->{"pages_num"} > 1
$self->{"flag_prev_next"} $pages_hr->{"pages_num"} > 1
Truth table66%2/3
A and B
A= $self->{"flag_prev_next"}B= $pages_hr->{"pages_num"} > 1
ABresult
0X0
100
111
187 1 my ($prev, $next);
188 1 ▶ if ($pages_hr->{'pages_num'} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{"pages_num"} > 1) 1 0 1
189 1 ▶ if ($pages_hr->{'actual_page'} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{"actual_page"} > 1) 0 1 1
190 0 $prev = $pages_hr->{'actual_page'} - 1;
191 }
192 1 ▶ if ($pages_hr->{'actual_page'} < $pages_hr->{'pages_num'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages_hr->{"actual_page"} < $pages_hr->{"pages_num"}) 1 0 1
193 1 $next = $pages_hr->{'actual_page'} + 1;
194 }
195 }
196
197 $self->{'tags'}->put(
198 ['b', 'p'],
199 ['a', 'class', $self->_css_class('prev_next')],
200  
201 # Previous page.
202 $prev ? (
203 ['b', 'a'],
204 ['a', 'class', $self->_css_class('prev')],
205 ['a', 'href', $self->{'url_page_cb'}->($prev, $pages_hr)],
206 ['d', decode_utf8('←')],
207 ['e', 'a'],
208 ) : (
209 ['b', 'span'],
210 ['a', 'class', $self->_css_class('prev-disabled')],
211 ['d', decode_utf8('←')],
212 ['e', 'span'],
213 ),
214  
215 # Next page.
216 $next ? (
217 ['b', 'a'],
218 ['a', 'class', $self->_css_class('next')],
219 1 ▶ ['a', 'href', $self->{'url_page_cb'}->($next, $pages_hr)],
Branch: 2 branches
BranchTrueFalseTotal
$prev ? : 0 1 1
$next ? : 1 0 1
220 ['d', decode_utf8('→')],
221 ['e', 'a'],
222 ) : (
223 ['b', 'span'],
224 ['a', 'class', $self->_css_class('next-disabled')],
225 ['d', decode_utf8('→')],
226 ['e', 'span'],
227 ),
228  
229 ['e', 'p'],
230 );
231 }
232  
233 2 $self->{'tags'}->put(
234 ['e', 'div'],
235 );
236  
237 2 return;
238 }
239  
240 1 ▶sub _process_css {
Subroutine _process_css: called CC=1 SCAR=0.0
241 1 my $self = shift;
242  
243 $self->{'css'}->put(
244 ['s', '.'.$self->{'css_pager'}.' a'],
245 ['d', 'text-decoration', 'none'],
246 ['e'],
247  
248 ['s', '.'.$self->_css_class('paginator')],
249 ['d', 'display', 'flex'],
250 ['d', 'flex-wrap', 'wrap'],
251 ['d', 'justify-content', 'center'],
252 ['d', 'padding-left', '130px'],
253 ['d', 'padding-right', '130px'],
254 ['d', 'float', 'both'],
255 ['e'],
256  
257 ['s', '.'.$self->_css_class('prev_next')],
258 ['d', 'display', 'flex'],
259 ['e'],
260  
261 ['s', '.'.$self->_css_class('paginator').' a'],
262 ['s', '.'.$self->_css_class('paginator').' strong'],
263 ['s', '.'.$self->_css_class('paginator').' span'],
264 ['s', '.'.$self->_css_class('next')],
265 ['s', '.'.$self->_css_class('next-disabled')],
266 ['s', '.'.$self->_css_class('prev')],
267 ['s', '.'.$self->_css_class('prev-disabled')],
268 ['d', 'display', 'flex'],
269 ['d', 'height', '55px'],
270 ['d', 'width', '55px'],
271 ['d', 'justify-content', 'center'],
272 ['d', 'align-items', 'center'],
273 ['d', 'border', '1px solid '.$self->{'css_colors'}->{'border'}],
274 ['d', 'margin-left', '-1px'],
275 ['e'],
276  
277  
278 ['s', '.'.$self->_css_class('prev')],
279 ['s', '.'.$self->_css_class('next')],
280 ['d', 'display', 'inline-flex'],
281 ['d', 'align-items', 'center'],
282 ['d', 'justify-content', 'center'],
283 ['e'],
284  
285 ['s', '.'.$self->_css_class('paginator').' a:hover'],
286 ['s', '.'.$self->_css_class('prev_next').' a:hover'],
287 $self->_css_colors_optional('hover_color', 'color'),
288 $self->_css_colors_optional('hover_background', 'background-color'),
289 ['e'],
290  
291 ['s', '.'.$self->_css_class('paginator').' a'],
292 $self->_css_colors_optional('other_color', 'color'),
293 $self->_css_colors_optional('other_background', 'background-color'),
294 ['e'],
295  
296 ['s', '.'.$self->_css_class('paginator-selected')],
297 ['d', 'background-color', $self->{'css_colors'}->{'actual_background'}],
298 1 ['d', 'color', $self->{'css_colors'}->{'actual_color'}],
299 ['e'],
300 );
301  
302 1 return;
303 }
304  
305 5 1;
306  
307 __END__
↑ Summary Pager/Utils.pm »