| 1 | package HTML::FormHandler::Widget::Block; | ||||||||||||||||||||
| 2 | # ABSTRACT: base block renderer | ||||||||||||||||||||
| 3 | 147 | $HTML::FormHandler::Widget::Block::VERSION = '0.410002'; | |||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 147 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 147 | with 'HTML::FormHandler::TraitFor::Types'; | |||||||||||||||||||
| 7 | 147 | ▶ | use HTML::FormHandler::Render::Util ('process_attrs'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 147 | ▶ | use HTML::FormHandler::Field; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 147 | has 'name' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||
| 11 | 147 | has 'form' => ( is => 'ro', isa => 'HTML::FormHandler', required => 1, | |||||||||||||||||||
| 12 | weak_ref => 1, | ||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 147 | has 'class' => ( | |||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||
| 17 | isa => 'HFH::ArrayRefStr', | ||||||||||||||||||||
| 18 | traits => ['Array'], | ||||||||||||||||||||
| 19 | builder => 'build_class', | ||||||||||||||||||||
| 20 | handles => { | ||||||||||||||||||||
| 21 | has_class => 'count', | ||||||||||||||||||||
| 22 | add_class => 'push', | ||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||
| 25 | 18 | ▶ | sub build_class { [] } | ||||||||||||||||||
|
Subroutine
build_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 26 | 147 | has 'attr' => ( | |||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||
| 28 | traits => ['Hash'], | ||||||||||||||||||||
| 29 | builder => 'build_attr', | ||||||||||||||||||||
| 30 | handles => { | ||||||||||||||||||||
| 31 | has_attr => 'count', | ||||||||||||||||||||
| 32 | set__attr => 'set', | ||||||||||||||||||||
| 33 | delete__attr => 'delete' | ||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||
| 36 | 21 | ▶ | sub build_attr { {} } | ||||||||||||||||||
|
Subroutine
build_attr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 37 | 147 | has 'wrapper' => ( is => 'rw', isa => 'Bool', default => 1 ); | |||||||||||||||||||
| 38 | 147 | has 'tag' => ( is => 'rw', isa => 'Str', default => 'div' ); | |||||||||||||||||||
| 39 | 147 | has 'label' => ( is => 'rw', isa => 'Str', predicate => 'has_label' ); | |||||||||||||||||||
| 40 | 147 | has 'label_tag' => ( is => 'rw', isa => 'Str' ); | |||||||||||||||||||
| 41 | 147 | has 'label_class' => ( | |||||||||||||||||||
| 42 | is => 'rw', | ||||||||||||||||||||
| 43 | isa => 'HFH::ArrayRefStr', | ||||||||||||||||||||
| 44 | traits => ['Array'], | ||||||||||||||||||||
| 45 | builder => 'build_label_class', | ||||||||||||||||||||
| 46 | handles => { | ||||||||||||||||||||
| 47 | has_label_class => 'count', | ||||||||||||||||||||
| 48 | add_label_class => 'push', | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||
| 51 | 20 | ▶ | sub build_label_class { [] } | ||||||||||||||||||
|
Subroutine
build_label_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 147 | has 'render_list' => ( | |||||||||||||||||||
| 54 | is => 'rw', | ||||||||||||||||||||
| 55 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||
| 56 | lazy => 1, | ||||||||||||||||||||
| 57 | traits => ['Array'], | ||||||||||||||||||||
| 58 | builder => 'build_render_list', | ||||||||||||||||||||
| 59 | handles => { | ||||||||||||||||||||
| 60 | has_render_list => 'count', | ||||||||||||||||||||
| 61 | add_to_render_list => 'push', | ||||||||||||||||||||
| 62 | all_render_list => 'elements', | ||||||||||||||||||||
| 63 | get_render_list => 'get', | ||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | has 'build_render_list_method' => ( | ||||||||||||||||||||
| 68 | is => 'rw', | ||||||||||||||||||||
| 69 | isa => 'CodeRef', | ||||||||||||||||||||
| 70 | traits => ['Code'], | ||||||||||||||||||||
| 71 | handles => {'build_render_list' => 'execute_method'}, | ||||||||||||||||||||
| 72 | 20 | ▶ | default => sub { \&default_build_render_list }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 73 | 147 | ); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | ▶ | sub default_build_render_list { [] } | ||||||||||||||||||
|
Subroutine
default_build_render_list: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 147 | has 'content' => ( is => 'rw' ); | |||||||||||||||||||
| 78 | 147 | has 'after_plist' => ( is => 'rw' ); | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 19 | ▶ | sub render { | ||||||||||||||||||
|
Subroutine
render: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 81 | 19 | my ( $self, $result ) = @_; | |||||||||||||||||||
| 82 | 19 | ▶ | $result ||= $self->form->result; | ||||||||||||||||||
|
Condition66%2/3
$result ||= $self->form->result
MC/DC0%0/2
$result ||= $self->form->result
$result
$self->form->result
Truth table66%2/3
A ||= B
A= $resultB= $self->form->result
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 19 | my $start_wrapper = $self->render_start; | |||||||||||||||||||
| 85 | 19 | my $end_wrapper = $self->render_end; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # get rendering of contained fields, if any | ||||||||||||||||||||
| 88 | 19 | my $rendered_fb = $self->render_from_list($result); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 19 | ▶ | my $content = $self->content || ''; | ||||||||||||||||||
|
Condition100%2/2
$self->content || ''
MC/DC100%1/1
$self->content || ''
$self->content
Truth table100%2/2
A
A= $self->content
| |||||||||||||||||||||
| 91 | 19 | ▶ | my $after_plist = $self->after_plist || ''; | ||||||||||||||||||
|
Condition50%1/2
$self->after_plist || ''
MC/DC0%0/1
$self->after_plist || ''
$self->after_plist
Truth table50%1/2
A
A= $self->after_plist
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # create label | ||||||||||||||||||||
| 94 | 19 | my $label = $self->render_label; | |||||||||||||||||||
| 95 | 19 | my $block = qq{\n$start_wrapper$label$content$rendered_fb$after_plist$end_wrapper}; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 19 | ▶ | sub render_start { | ||||||||||||||||||
|
Subroutine
render_start: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 100 | 19 | my $self = shift; | |||||||||||||||||||
| 101 | 19 | ▶ | if( $self->wrapper ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 18 | my $tag = $self->tag; | |||||||||||||||||||
| 103 | # create attribute string | ||||||||||||||||||||
| 104 | 18 | my $attr_str = $self->render_attribute_string; | |||||||||||||||||||
| 105 | 18 | return qq{<$tag$attr_str>}; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | 1 | return ''; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 19 | ▶ | sub render_end { | ||||||||||||||||||
|
Subroutine
render_end: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 111 | 19 | my $self = shift; | |||||||||||||||||||
| 112 | 19 | ▶ | if( $self->wrapper ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 18 | my $tag = $self->tag; | |||||||||||||||||||
| 114 | 18 | return qq{</$tag>}; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | 1 | return ''; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 18 | ▶ | sub render_attribute_string { | ||||||||||||||||||
|
Subroutine
render_attribute_string: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 120 | 18 | my $self = shift; | |||||||||||||||||||
| 121 | 18 | my $attr = { %{ $self->attr } }; | |||||||||||||||||||
| 122 | 18 | ▶ | $attr->{class} = $self->class if $self->has_class; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 18 | my $attr_str = process_attrs($attr); | |||||||||||||||||||
| 124 | 18 | return $attr_str; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 19 | ▶ | sub render_label { | ||||||||||||||||||
|
Subroutine
render_label: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 128 | 19 | my $self = shift; | |||||||||||||||||||
| 129 | 19 | my $label = ''; | |||||||||||||||||||
| 130 | 19 | ▶ | if ( $self->has_label ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 10 | ▶ | my $label_tag = $self->label_tag || 'span'; | ||||||||||||||||||
|
Condition100%2/2
$self->label_tag || 'span'
MC/DC100%1/1
$self->label_tag || 'span'
$self->label_tag
Truth table100%2/2
A
A= $self->label_tag
| |||||||||||||||||||||
| 132 | 10 | ▶ | $label_tag = 'legend' if lc $self->tag eq 'fieldset'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 10 | my $label_str = $self->form->_localize( $self->label ); | |||||||||||||||||||
| 134 | 10 | my $attr_str = ''; | |||||||||||||||||||
| 135 | 10 | ▶ | $attr_str = process_attrs( { class => $self->label_class } ) if $self->has_label_class; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 10 | $label = qq{<$label_tag$attr_str>$label_str</$label_tag>}; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | 19 | return $label; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 19 | ▶ | sub render_from_list { | ||||||||||||||||||
|
Subroutine
render_from_list: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||
| 142 | 19 | my ( $self, $result ) = @_; | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 19 | my $output = ''; | |||||||||||||||||||
| 145 | 19 | ▶ | if ( $self->has_render_list ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 18 | foreach my $fb ( @{ $self->render_list } ) { | |||||||||||||||||||
| 147 | # it's a Field | ||||||||||||||||||||
| 148 | 36 | ▶ | if ( $self->form->field_in_index($fb) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | # find field result and use that | ||||||||||||||||||||
| 150 | 34 | my $fld_result = $result->get_result($fb); | |||||||||||||||||||
| 151 | # if no result, then we shouldn't be rendering this field | ||||||||||||||||||||
| 152 | 34 | ▶ | next unless $fld_result; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 34 | $output .= $fld_result->render; | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | # it's a Block | ||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||
| 157 | # block can't be called from a field, so this should | ||||||||||||||||||||
| 158 | # always be the for result | ||||||||||||||||||||
| 159 | 2 | my $block = $self->form->block($fb); | |||||||||||||||||||
| 160 | 2 | ▶ | die "found no form field or block named '$fb'\n" unless $block; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 2 | $output .= $block->render($result); | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | # else nothing to render from render_list | ||||||||||||||||||||
| 166 | 19 | return $output; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 147 | 1; | |||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||