blib/lib/Tags/HTML/Pager.pm

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