Valiant/HTML/PagerBuilder.pm

filter: Statementstmt 16.9% 19 / 112 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 15.3% 4 / 26 Podpod 11.7% 2 / 17 totaltotal 11.7% 25 / 213 CC 30 SCAR 48.1
CC
30
Cov
11%
navigation_line
306.0
window_info
132.0
process_window_info
12.0
CRAP 122.6
↑ Summary Valiant/HTML/Util/Pager.pm »
1 package Valiant::HTML::PagerBuilder;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Valiant::Util 'process_template';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has model => (is=>'ro', required=>1);
9 1 has pager => (is=>'ro', required=>1, lazy=>1, builder=>1);
10 1 has uri_base => (is=>'ro', predicate=>'has_uri_base');
11  
12 0 ▶sub _build_pager {
Subroutine _build_pager: not called CC=1 SCAR=6.9
13 0 my ($self) = @_;
14 0 return $self->model->pager;
15 }
16  
17 1 has view => (is=>'ro', required=>1, builder=>1);
18  
19 0 ▶sub _build_view {
Subroutine _build_view: not called CC=1 SCAR=6.9
20 0 my ($self) = @_;
21 0 return my $view = Module::Runtime::use_module('Valiant::HTML::Util::View')->new;
22 }
23  
24 1 has name => (is=>'ro', required=>1, builder=>1);
25  
26 0 ▶sub _build_name {
Subroutine _build_name: not called CC=1 SCAR=6.9
27 0 my ($self) = @_;
28 0 return $self->model->result_class->model_name->param_key;
29 }
30  
31 1 has tag_builder => (
32 is => 'ro',
33 required => 1,
34 builder => 1,
35 lazy => 1,
36 );
37  
38 0 ▶ sub _build_tag_builder {
Subroutine _build_tag_builder: not called CC=1 SCAR=6.9
39 0 my ($self) = @_;
40 0 return Module::Runtime::use_module('Valiant::HTML::Util::TagBuilder')->new(view=>$self->view);
41 }
42  
43 0 ▶sub default_window_info_none {
Subroutine default_window_info_none: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my ($self) = @_;
45 0 return '';
46 }
47  
48 0 ▶sub default_window_info_one {
Subroutine default_window_info_one: not called CC=1 SCAR=6.9
Pod undocumented
49 0 my ($self) = @_;
50 0 return '{{total_entries}} {{model_name}}';
51 }
52  
53 0 ▶sub default_window_info_many {
Subroutine default_window_info_many: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my ($self) = @_;
55 0 return '{{first}} to {{last}} of {{total_entries}} {{model_name}}';
56 }
57  
58 0 ▶sub default_window_info_container {
Subroutine default_window_info_container: not called CC=1 SCAR=6.9
Pod undocumented
59 0 my ($self) = @_;
60 0 return '<div {{container_attrs}}>{{message}}</div>';
61 }
62  
63 0 ▶sub default_window_info_container_attrs {
Subroutine default_window_info_container_attrs: not called CC=1 SCAR=6.9
Pod undocumented
64 0 my ($self) = @_;
65 return +{
66 0 id => $self->name.'_pager_window_info',
67 class => 'pager_window_info',
68 };
69 }
70  
71 0 ▶sub process_window_info {
Subroutine process_window_info: not called CC=3 SCAR=24.8
Pod undocumented
72 0 my ($self, $arg) = @_;
73 0 ▶ return $arg->($self->view, $self, $self->pager, $self->model) if ref($arg) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg eq "CODE" 0 0 0
74  
75 0 ▶ my $model_name = $self->pager->total_entries > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$self->pager->total_entries > 1 ? : 0 0 0
76 $self->model->model_name->human(count=>2) :
77 $self->model->model_name->human(count=>1);
78  
79 0 return process_template $arg,
80 model_name => $model_name,
81 total_entries => $self->pager->total_entries,
82 entries_per_page => $self->pager->entries_per_page,
83 current_page => $self->pager->current_page,
84 entries_on_this_page => $self->pager->entries_on_this_page,
85 first => $self->pager->first,
86 last => $self->pager->last,
87 first_page => $self->pager->first_page,
88 last_page => $self->pager->last_page,
89 previous_page => $self->pager->previous_page,
90 next_page => $self->pager->next_page,
91 }
92  
93 0 ▶sub window_info {
Subroutine window_info: not called CC=11 SCAR=48.8
Pod documented
94 0 my $self = shift;
95 0 ▶ return $_[0]->($self->view, $self, $self->pager, $self->model) if ref($_[0]) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "CODE" 0 0 0
96 0 ▶ my %extra_container_args = (ref($_[0]||'') eq 'HASH') ? %{shift(@_)} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref($_[0] || '') eq 'HASH' ? : 0 0 0
Condition0%0/2
$_[0] || ''
l!l
00
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
97  
98 0 my (%args) = @_;
99 0 ▶ my $none = exists($args{none}) ? $args{none} : $self->default_window_info_none;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'none'} ? : 0 0 0
100 0 ▶ my $one = exists($args{one}) ? $args{one} : $self->default_window_info_one;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'one'} ? : 0 0 0
101 0 ▶ my $many = exists($args{many}) ? $args{many} : $self->default_window_info_many;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'many'} ? : 0 0 0
102
103 0 ▶ return $self->process_window_info($none) unless $self->pager->total_entries > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pager->total_entries > 0 0 0 0
104  
105 0 ▶ my $message = $self->pager->last_page == 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$self->pager->last_page == 1 ? : 0 0 0
106 $self->process_window_info($one) :
107 $self->process_window_info($many);
108  
109 0 ▶ my $container = exists($args{container}) ? $args{container} : $self->default_window_info_container;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'container'} ? : 0 0 0
110 0 ▶ my $container_attrs = exists($args{container_attrs}) ? $args{container_attrs} : $self->default_window_info_container_attrs;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'container_attrs'} ? : 0 0 0
111 0 $container_attrs = $self->tag_builder->_tag_options(%$container_attrs, %extra_container_args);
112  
113 0 my $window_info = process_template $container,
114 message => $message,
115 container_attrs => $container_attrs;
116  
117 0 return $self->view->raw($window_info);
118 }
119  
120 0 ▶sub default_navigation_line_none {
Subroutine default_navigation_line_none: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self) = @_;
122 0 return '';
123 }
124  
125 0 ▶sub default_navigation_line_page {
Subroutine default_navigation_line_page: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my ($self) = @_;
127 0 return '<a {{page_attrs}}>{{page}}</a>';
128 }
129  
130 0 ▶sub default_navigation_line_page_attrs {
Subroutine default_navigation_line_page_attrs: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my ($self) = @_;
132 return +{
133 0 class => "pager_page @{[ $self->name ]}_pager_page",
134 };
135 }
136  
137 0 ▶sub default_navigation_line_current_page {
Subroutine default_navigation_line_current_page: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self) = @_;
139 0 return '<a {{current_page_attrs}}>{{current_page}}</a>';
140 }
141  
142 0 ▶sub default_navigation_line_current_page_attrs {
Subroutine default_navigation_line_current_page_attrs: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my ($self) = @_;
144 return +{
145 0 id => $self->name.'_pager_current_page',
146 class => 'pager_page pager_current_page',
147 };
148 }
149  
150 0 ▶sub process_navigation_line {
Subroutine process_navigation_line: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my ($self, $arg) = @_;
152 0 ▶ return $arg->($self->view, $self, $self->pager, $self->model) if ref($arg) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg eq "CODE" 0 0 0
153 0 return process_template $arg,
154 total_entries => $self->pager->total_entries,
155 entries_per_page => $self->pager->entries_per_page,
156 current_page => $self->pager->current_page,
157 entries_on_this_page => $self->pager->entries_on_this_page,
158 first => $self->pager->first,
159 last => $self->pager->last,
160 first_page => $self->pager->first_page,
161 last_page => $self->pager->last_page,
162 previous_page => $self->pager->previous_page,
163 next_page => $self->pager->next_page,
164 }
165  
166 0 ▶sub default_navigation_line_container {
Subroutine default_navigation_line_container: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my ($self) = @_;
168 0 return '<div {{container_attrs}}>Page: {{message}}</div>';
169 }
170  
171 0 ▶sub default_navigation_line_container_attrs {
Subroutine default_navigation_line_container_attrs: not called CC=1 SCAR=6.9
Pod undocumented
172 0 my ($self) = @_;
173 return +{
174 0 id => $self->name.'_pager_navigation_line',
175 class => 'pager_navigation_line',
176 };
177 }
178  
179 0 ▶sub page_query_key {
Subroutine page_query_key: not called CC=1 SCAR=6.9
Pod undocumented
180 0 my ($self) = @_;
181 0 return $self->name.'.page';
182 }
183  
184 0 ▶sub navigation_line {
Subroutine navigation_line: not called CC=17 SCAR=57.2
Pod documented
185 0 my $self = shift;
186 0 ▶ my $uri_base = $self->has_uri_base ? $self->uri_base : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_uri_base ? : 0 0 0
187  
188 # If a link generator is passed, use it, otherwise create a default one
189 0 my $link_generator = undef;
190 0 ▶ $link_generator = shift if (ref($_[0])||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] || "") eq "CODE" 0 0 0
Condition0%0/2
ref $_[0] || ''
l!l
00
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
191 0 ▶ unless($link_generator) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link_generator) 0 0 0
192 0 ▶ croak "A Link generator must exist if there's no uri_base" unless $uri_base;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri_base 0 0 0
193 0 $uri_base->query_param_delete($self->page_query_key); # delete defaults
194 0 ▶ $link_generator = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 0 my ($view, $pb, $page_num, $pager, $model, $uri_base) = @_;
196 0 my $uri = $uri_base->clone;
197 0 $uri->query_param_append($pb->page_query_key=>$page_num);
198 0 return $uri;
199 0 };
200 }
201
202 0 my (%args) = @_;
203 0 ▶ my $none = exists($args{none}) ? $args{none} : $self->default_navigation_line_none;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'none'} ? : 0 0 0
204 0 ▶ my $page = exists($args{page}) ? $args{page} : $self->default_navigation_line_page;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'page'} ? : 0 0 0
205 0 ▶ my $page_attrs = exists($args{page_attrs}) ? $args{page_attrs} : $self->default_navigation_line_page_attrs;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'page_attrs'} ? : 0 0 0
206 0 ▶ my $current_page = exists($args{current_page}) ? $args{current_page} : $self->default_navigation_line_current_page;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'current_page'} ? : 0 0 0
207 0 ▶ my $current_page_attrs = exists($args{current_page_attrs}) ? $args{current_page_attrs} : $self->default_navigation_line_current_page_attrs;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'current_page_attrs'} ? : 0 0 0
208  
209 0 ▶ return $self->process_window_info($none) unless $self->pager->total_entries > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pager->total_entries > 0 0 0 0
210 0 ▶ return $self->process_window_info($none) unless $self->pager->last_page > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pager->last_page > 1 0 0 0
211  
212 0 my $html = '';
213  
214 0 foreach my $page_num (1..$self->pager->last_page) {
215 0 my $href = $link_generator->($self->view, $self, $page_num, $self->pager, $self->model, $uri_base);
216 0 ▶ if($page_num == $self->pager->current_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_num == $self->pager->current_page) { } 0 0 0
217 0 my $attrs = $self->tag_builder->_tag_options(%$current_page_attrs, href=>$href);
218 0 my $link = process_template $current_page, current_page=>$page_num, current_page_attrs=>$attrs;
219 0 $html .= $link;
220 } else {
221 0 my $attrs = $self->tag_builder->_tag_options(%$page_attrs, href=>$href);
222 0 my $link = process_template $page, page=>$page_num, page_attrs=>$attrs;
223 0 $html .= $link;
224 }
225 }
226 0 $link_generator = undef; # Needed because the coderef doesn't go out of scope for some reason...
227  
228 0 ▶ my $container = exists($args{container}) ? $args{container} : $self->default_navigation_line_container;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'container'} ? : 0 0 0
229 0 ▶ my $container_attrs = exists($args{container_attrs}) ? $args{container_attrs} : $self->default_navigation_line_container_attrs;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'container_attrs'} ? : 0 0 0
230 0 $container_attrs = $self->tag_builder->_tag_options(%$container_attrs);
231  
232 0 my $navigation_line = process_template $container,
233 message => $html,
234 container_attrs => $container_attrs;
235  
236 0 return $self->view->raw($navigation_line);
237 }
238  
239 1 1;
240  
241 =head1 NAME
242  
243 Valiant::HTML::PagerBuilder - A Perl module for building HTML paginators
244  
245 =head1 SYNOPSIS
246  
247 use Valiant::HTML::PagerBuilder;
248  
249 my $pager_builder = Valiant::HTML::PagerBuilder->new(
250 model => $model,
251 pager => $pager_object,
252 );
253  
254 my $window_info_html = $pager_builder->window_info(%args);
255  
256 my $navigation_line_html = $pager_builder->navigation_line(%args);
257  
258 =head1 DESCRIPTION
259  
260 The C<Valiant::HTML::PagerBuilder> module is designed for building HTML paginators for web applications. It provides methods for generating window information and navigation lines for paginated data.
261  
262 =head1 CONSTRUCTOR
263  
264 =over 4
265  
266 =item new
267  
268 Creates a new C<Valiant::HTML::PagerBuilder> object.
269  
270 my $pager_builder = Valiant::HTML::PagerBuilder->new(
271 model => $model,
272 pager => $pager_object,
273 );
274  
275 The constructor accepts the following parameters:
276  
277 =over 4
278  
279 =item model (required)
280  
281 The model object representing the data to be paginated.
282  
283 =item pager (required)
284  
285 The pager object used for pagination.
286  
287 =item uri_base (optional)
288  
289 The base URI for the paginator's navigation links. Should be a L<URI> object.
290  
291 =back
292  
293 =back
294  
295 =head1 METHODS
296  
297 This class supports the following public methods:
298  
299 =head2 window_info
300  
301 Generates window information HTML for the paginator.
302  
303 my $window_info_html = $pager_builder->window_info(%args);
304  
305 The C<window_info> method accepts a hash of arguments, which can include custom window
306 information templates. It generates HTML for the paginator's window information.
307  
308 Where args are:
309  
310 =over 4
311  
312 =item none
313  
314 The template for the window information when there are no entries to paginate.
315  
316 =item one
317  
318 The template for the window information when there is only one entry to paginate.
319  
320 =item many
321  
322 The template for the window information when there are multiple entries to paginate.
323  
324 =item container
325  
326 The template for the window information container.
327  
328 =item container_attrs
329  
330 The attributes for the window information container.
331  
332 =back
333  
334 =head2 navigation_line
335  
336 Generates navigation line HTML for the paginator.
337  
338 my $navigation_line_html = $pager_builder->navigation_line(%args);
339  
340 The C<navigation_line> method accepts a hash of arguments, which can include custom navigation
341 line templates. It generates HTML for the paginator's navigation line.
342  
343 Where args are:
344  
345 =over 4
346  
347 =item none
348  
349 The template for the navigation line when there are no entries to paginate.
350  
351 =item page
352  
353 The template for the navigation line when there are multiple pages to paginate.
354  
355 =item page_attrs
356  
357 The attributes for the navigation line pages.
358  
359 =item current_page
360  
361 The template for the navigation line when the current page is selected.
362  
363 =item current_page_attrs
364  
365 The attributes for the navigation line current page.
366  
367 =item container
368  
369 The template for the navigation line container.
370  
371 =item container_attrs
372  
373 The attributes for the navigation line container.
374  
375 =back
376  
377 =head1 SEE ALSO
378
379 L<Valiant>, L<Valiant::HTML::FormBuilder>, L<Valiant::HTML::Util::FormTags>
380  
381 =head1 AUTHOR
382
383 See L<Valiant>
384  
385 =head1 COPYRIGHT & LICENSE
386
387 See L<Valiant>
388  
389 =cut
↑ Summary Valiant/HTML/Util/Pager.pm »