| 1 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Element::SimpleTable; | ||||||||||||||||||||||||||||
| 4 | 8 | $HTML::FormFu::Element::SimpleTable::VERSION = '2.06'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: simple table element | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use MooseX::Attribute::Chained; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | extends 'HTML::FormFu::Element::Block'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use HTML::FormFu::Util qw( append_xml_attribute ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Scalar::Util qw( reftype ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | has odd_class => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 16 | 8 | has even_class => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | after BUILD => sub { | ||||||||||||||||||||||||||||
| 19 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 7 | $self->tag('table'); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 7 | return; | |||||||||||||||||||||||||||
| 24 | 8 | }; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub headers { | ||||||||||||||||||||||||||||
| 27 | 4 | ▶ | my ( $self, $headers ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
headers: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | croak "headers must be passed as an array-ref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if reftype($headers) ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # save any elements already added | ||||||||||||||||||||||||||||
| 33 | 4 | my @original_rows = @{ $self->_elements }; | |||||||||||||||||||||||||||
| 34 | 4 | $self->_elements( [] ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | my $header_row = $self->element('Block'); | |||||||||||||||||||||||||||
| 37 | 4 | $header_row->tag('tr'); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | for my $text (@$headers) { | |||||||||||||||||||||||||||
| 40 | 7 | my $th = $header_row->element('Block'); | |||||||||||||||||||||||||||
| 41 | 7 | $th->tag('th'); | |||||||||||||||||||||||||||
| 42 | 7 | $th->content($text); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | ▶ | if (@original_rows) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 3 | push @{ $self->_elements }, @original_rows; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | return $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub rows { | ||||||||||||||||||||||||||||
| 53 | 6 | ▶ | my ( $self, $rows ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rows: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 6 | ▶ | croak "too many arguments" if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 6 | ▶ | croak "rows must be passed as an array-ref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | if reftype($rows) ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | for my $cells (@$rows) { | |||||||||||||||||||||||||||
| 61 | 10 | ▶ | croak "each row must be an array-ref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | if reftype($cells) ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 10 | my $row = $self->element('Block'); | |||||||||||||||||||||||||||
| 65 | 10 | $row->tag('tr'); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | for my $cell (@$cells) { | |||||||||||||||||||||||||||
| 68 | 15 | my $td = $row->element('Block'); | |||||||||||||||||||||||||||
| 69 | 15 | $td->tag('td'); | |||||||||||||||||||||||||||
| 70 | 15 | $td->element($cell); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | return $self; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub render_data { | ||||||||||||||||||||||||||||
| 78 | 4 | ▶ | return shift->render_data_non_recursive(@_); | ||||||||||||||||||||||||||
|
Subroutine
render_data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub render_data_non_recursive { # though it is really recursive | ||||||||||||||||||||||||||||
| 82 | 10 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
render_data_non_recursive: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 10 | my $odd = $self->odd_class; | |||||||||||||||||||||||||||
| 85 | 10 | my $even = $self->even_class; | |||||||||||||||||||||||||||
| 86 | 10 | my $i = 1; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 10 | for my $row ( @{ $self->get_elements } ) { | |||||||||||||||||||||||||||
| 89 | 19 | my $first_cell = $row->get_element; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 19 | ▶ | if ( $i == 1 && $first_cell->tag eq 'th' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i == 1 and $first_cell->tag eq "th"
MC/DC100%2/2
$i == 1 and $first_cell->tag eq "th"
$i == 1
$first_cell->tag eq "th"
Truth table100%3/3
A and B
A= $i == 1B= $first_cell->tag eq "th"
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # skip the header row | ||||||||||||||||||||||||||||
| 94 | 5 | next; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 14 | ▶ | if ( $i % 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 10 | ▶ | if ( defined $odd ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 2 | $row->attributes( { class => $odd } ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 4 | ▶ | if ( defined $even ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 1 | $row->attributes( { class => $even } ); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 14 | $i++; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my $render = $self->SUPER::render_data_non_recursive( | ||||||||||||||||||||||||||||
| 111 | 10 | ▶ | { elements => [ map { $_->render_data } @{ $self->_elements } ], | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | $args ? %$args : (), | ||||||||||||||||||||||||||||
| 113 | } ); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 10 | append_xml_attribute( $render->{attributes}, 'class', lc $self->type ); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 10 | return $render; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =pod | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 NAME | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | HTML::FormFu::Element::SimpleTable - simple table element | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 VERSION | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | version 2.06 | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | The following is yaml markup for a table consisting of a header row | ||||||||||||||||||||||||||||
| 141 | containing 2 C<th> cells, and a further 2 rows, each containing 2 C<td> | ||||||||||||||||||||||||||||
| 142 | cells. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | type: SimpleTable | ||||||||||||||||||||||||||||
| 145 | headers: | ||||||||||||||||||||||||||||
| 146 | - One | ||||||||||||||||||||||||||||
| 147 | - Two | ||||||||||||||||||||||||||||
| 148 | rows: | ||||||||||||||||||||||||||||
| 149 | - | ||||||||||||||||||||||||||||
| 150 | - type: Input | ||||||||||||||||||||||||||||
| 151 | name: one_a | ||||||||||||||||||||||||||||
| 152 | - type: Input | ||||||||||||||||||||||||||||
| 153 | name: two_a | ||||||||||||||||||||||||||||
| 154 | - | ||||||||||||||||||||||||||||
| 155 | - type: Input | ||||||||||||||||||||||||||||
| 156 | name: one_b | ||||||||||||||||||||||||||||
| 157 | - type: Input | ||||||||||||||||||||||||||||
| 158 | name: two_b | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Sometimes you just really need to use a table to display some fields in a | ||||||||||||||||||||||||||||
| 163 | grid format. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | As its name suggests, this is a compromise between power and simplicity. | ||||||||||||||||||||||||||||
| 166 | If you want more control of the markup, you'll probably just have to revert | ||||||||||||||||||||||||||||
| 167 | to using nested L<block's|HTML::FormFu::Element::_Block>, setting the tags | ||||||||||||||||||||||||||||
| 168 | to table, tr, td, etc. and adding the cell contents as elements. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 METHODS | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 headers | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Input Value: \@headers | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | L</headers> accepts an arrayref of strings. Each string is xml-escaped and | ||||||||||||||||||||||||||||
| 177 | inserted into a new header cell. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 rows | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Input Value: \@rows | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | L</rows> accepts an array-ref, each item representing a new row. Each row | ||||||||||||||||||||||||||||
| 184 | should be comprised of an array-ref, each item representing a table cell. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Each cell item should be appropriate for passing to L<HTML::FormFu/element>; | ||||||||||||||||||||||||||||
| 187 | so either a single element's definition, or an array-ref of element | ||||||||||||||||||||||||||||
| 188 | definitions. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head2 odd_class | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Input Value: $string | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | The supplied string will be used as the class-name for each odd-numbered row | ||||||||||||||||||||||||||||
| 195 | (not counting any header row). | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 even_class | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Input Value: $string | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | The supplied string will be used as the class-name for each even-numbered row | ||||||||||||||||||||||||||||
| 202 | (not counting any header row). | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Is a sub-class of, and inherits methods from | ||||||||||||||||||||||||||||
| 207 | L<HTML::FormFu::Element::Block>, | ||||||||||||||||||||||||||||
| 208 | L<HTML::FormFu::Element> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | L<HTML::FormFu> | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Carl Franks, C<cfranks@cpan.org> | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | This library is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 219 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Carl Franks <cpan@fireartist.com> | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | This software is copyright (c) 2018 by Carl Franks. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 230 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||