File Coverage

blib/lib/Moonshine/Bootstrap/Component/Pagination.pm
Criterion Covered Total %
statement 19 24 79.1
branch 5 8 62.5
condition 1 2 50.0
subroutine 3 3 100.0
pod n/a
total 28 37 75.6


line stmt bran cond sub pod time code
1             package Moonshine::Bootstrap::Component::Pagination;
2              
3 6     6   473804 use Moonshine::Magic;
  6         305435  
  6         59  
4 6     6   3633 use Params::Validate qw/ARRAYREF HASHREF/;
  6         19  
  6         529  
5              
6             lazy_components qw/li/;
7              
8             extends(
9             'Moonshine::Bootstrap::Component',
10             'Moonshine::Bootstrap::Component::LinkedLi',
11             'Moonshine::Bootstrap::Component::LinkedLiSpan',
12             );
13              
14             has(
15             pagination_spec => sub {
16             {
17             tag => { default => 'ul' },
18             class_base => { default => 'pagination' },
19             items => { type => ARRAYREF, optional => 1 },
20             sizing_base => { default => 'pagination-' },
21             count => 0,
22             previous => {
23             default => {
24             span => { data => '«', aria_hidden => 'true' },
25             link => { href => "#", aria_label => 'Previous' },
26             },
27             type => HASHREF
28             },
29             next => {
30             default => {
31             span => { data => '»', aria_hidden => 'true' },
32             link => { href => "#", aria_label => "Next" }
33             },
34             type => HASHREF,
35             build => 1,
36             },
37             nav => 0,
38             nav_args => { default => { tag => 'nav' } },
39             };
40             }
41             );
42              
43             sub pagination {
44 16     16   142559 my ($self) = shift;
45              
46 16   50     196 my ( $base_args, $build_args ) = $self->validate_build(
47             {
48             params => $_[0] // {},
49             spec => $self->pagination_spec,
50             }
51             );
52              
53 16         312 my $base_element = Moonshine::Element->new($base_args);
54              
55             $base_element->add_child(
56 16         45002 $self->linked_li_span( $build_args->{previous} ) );
57              
58 16 50       621 if ( defined $build_args->{items} ) {
    100          
59 0         0 for ( @{ $build_args->{items} } ) {
  0         0  
60 0 0       0 if ( $_->{active} ) {
61 0         0 $base_element->add_child( $self->li($_) );
62             }
63             else {
64 0         0 $base_element->add_child( $self->linked_li($_) );
65             }
66             }
67             }
68             elsif ( defined $build_args->{count} ) {
69 7         44 for ( 1 .. $build_args->{count} ) {
70 35         1255 $base_element->add_child(
71             $self->linked_li( { data => $_, link => '#' } ) );
72             }
73             }
74              
75 16         363 $base_element->add_child( $self->linked_li_span( $build_args->{next} ) );
76              
77 16 100       565 if ( defined $build_args->{nav} ) {
78 6         43 my $nav = Moonshine::Element->new( $build_args->{nav_args} );
79 6         15483 $nav->add_child($base_element);
80 6         355 return $nav;
81             }
82              
83 10         215 return $base_element;
84             }
85              
86             1;
87              
88             __END__