| 1 |
package HTML::FormHandler::Widget::Block; |
| 2 |
# ABSTRACT: base block renderer |
| 3 |
$HTML::FormHandler::Widget::Block::VERSION = '0.410002'; |
| 4 |
|
| 5 |
use Moose; |
| 6 |
with 'HTML::FormHandler::TraitFor::Types'; |
| 7 |
use HTML::FormHandler::Render::Util ('process_attrs'); |
| 8 |
use HTML::FormHandler::Field; |
| 9 |
|
| 10 |
has 'name' => ( is => 'ro', isa => 'Str', required => 1 ); |
| 11 |
has 'form' => ( is => 'ro', isa => 'HTML::FormHandler', required => 1, |
| 12 |
weak_ref => 1, |
| 13 |
); |
| 14 |
|
| 15 |
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 |
sub build_class { [] } |
| 26 |
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 |
sub build_attr { {} } |
| 37 |
has 'wrapper' => ( is => 'rw', isa => 'Bool', default => 1 ); |
| 38 |
has 'tag' => ( is => 'rw', isa => 'Str', default => 'div' ); |
| 39 |
has 'label' => ( is => 'rw', isa => 'Str', predicate => 'has_label' ); |
| 40 |
has 'label_tag' => ( is => 'rw', isa => 'Str' ); |
| 41 |
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 |
sub build_label_class { [] } |
| 52 |
|
| 53 |
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 |
default => sub { \&default_build_render_list }, |
| 73 |
); |
| 74 |
|
| 75 |
sub default_build_render_list { [] } |
| 76 |
|
| 77 |
has 'content' => ( is => 'rw' ); |
| 78 |
has 'after_plist' => ( is => 'rw' ); |
| 79 |
|
| 80 |
sub render { |
| 81 |
my ( $self, $result ) = @_; |
| 82 |
$result ||= $self->form->result; |
| 83 |
|
| 84 |
my $start_wrapper = $self->render_start; |
| 85 |
my $end_wrapper = $self->render_end; |
| 86 |
|
| 87 |
# get rendering of contained fields, if any |
| 88 |
my $rendered_fb = $self->render_from_list($result); |
| 89 |
|
| 90 |
my $content = $self->content || ''; |
| 91 |
my $after_plist = $self->after_plist || ''; |
| 92 |
|
| 93 |
# create label |
| 94 |
my $label = $self->render_label; |
| 95 |
my $block = qq{\n$start_wrapper$label$content$rendered_fb$after_plist$end_wrapper}; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
sub render_start { |
| 100 |
my $self = shift; |
| 101 |
if( $self->wrapper ) { |
| 102 |
my $tag = $self->tag; |
| 103 |
# create attribute string |
| 104 |
my $attr_str = $self->render_attribute_string; |
| 105 |
return qq{<$tag$attr_str>}; |
| 106 |
} |
| 107 |
return ''; |
| 108 |
} |
| 109 |
|
| 110 |
sub render_end { |
| 111 |
my $self = shift; |
| 112 |
if( $self->wrapper ) { |
| 113 |
my $tag = $self->tag; |
| 114 |
return qq{</$tag>}; |
| 115 |
} |
| 116 |
return ''; |
| 117 |
} |
| 118 |
|
| 119 |
sub render_attribute_string { |
| 120 |
my $self = shift; |
| 121 |
my $attr = { %{ $self->attr } }; |
| 122 |
$attr->{class} = $self->class if $self->has_class; |
| 123 |
my $attr_str = process_attrs($attr); |
| 124 |
return $attr_str; |
| 125 |
} |
| 126 |
|
| 127 |
sub render_label { |
| 128 |
my $self = shift; |
| 129 |
my $label = ''; |
| 130 |
if ( $self->has_label ) { |
| 131 |
my $label_tag = $self->label_tag || 'span'; |
| 132 |
$label_tag = 'legend' if lc $self->tag eq 'fieldset'; |
| 133 |
my $label_str = $self->form->_localize( $self->label ); |
| 134 |
my $attr_str = ''; |
| 135 |
$attr_str = process_attrs( { class => $self->label_class } ) if $self->has_label_class; |
| 136 |
$label = qq{<$label_tag$attr_str>$label_str</$label_tag>}; |
| 137 |
} |
| 138 |
return $label; |
| 139 |
} |
| 140 |
|
| 141 |
sub render_from_list { |
| 142 |
my ( $self, $result ) = @_; |
| 143 |
|
| 144 |
my $output = ''; |
| 145 |
if ( $self->has_render_list ) { |
| 146 |
foreach my $fb ( @{ $self->render_list } ) { |
| 147 |
# it's a Field |
| 148 |
if ( $self->form->field_in_index($fb) ) { |
| 149 |
# find field result and use that |
| 150 |
my $fld_result = $result->get_result($fb); |
| 151 |
# if no result, then we shouldn't be rendering this field |
| 152 |
next unless $fld_result; |
| 153 |
$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 |
my $block = $self->form->block($fb); |
| 160 |
die "found no form field or block named '$fb'\n" unless $block; |
| 161 |
$output .= $block->render($result); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
# else nothing to render from render_list |
| 166 |
return $output; |
| 167 |
} |
| 168 |
|
| 169 |
1; |
| 170 |
|
| 171 |
__END__ |
| 172 |
|
| 173 |
=pod |
| 174 |
|
| 175 |
=encoding UTF-8 |
| 176 |
|
| 177 |
=head1 NAME |
| 178 |
|
| 179 |
HTML::FormHandler::Widget::Block - base block renderer |
| 180 |
|
| 181 |
=head1 VERSION |
| 182 |
|
| 183 |
version 0.410002 |
| 184 |
|
| 185 |
=head1 SYNOPSIS |
| 186 |
|
| 187 |
Base block renderer to be used with L<HTML::FormHandler::Blocks>. |
| 188 |
|
| 189 |
has_block 'my_fieldset' => ( tag => 'fieldset', |
| 190 |
render_list => ['bar'], label => 'My Special Bar' ); |
| 191 |
|
| 192 |
=head1 ATTRIBUTES |
| 193 |
|
| 194 |
=over 4 |
| 195 |
|
| 196 |
=item render_list |
| 197 |
|
| 198 |
List of names of objects to render (fields and blocks) |
| 199 |
|
| 200 |
=item build_render_list_method |
| 201 |
|
| 202 |
Coderef for constructing the list of names of objects to render (fields and blocks) |
| 203 |
|
| 204 |
=item tag |
| 205 |
|
| 206 |
HTML tag for this block. Default 'div'. |
| 207 |
|
| 208 |
=item class |
| 209 |
|
| 210 |
Arrayref of classes for the HTML element. |
| 211 |
|
| 212 |
=item attr |
| 213 |
|
| 214 |
Other attributes for the HTML element. |
| 215 |
|
| 216 |
=item label_tag |
| 217 |
|
| 218 |
Tag to use for the label. Default: 'span'; default for 'fieldset' is 'legend'. |
| 219 |
|
| 220 |
=item label_class |
| 221 |
|
| 222 |
Classes for the label. |
| 223 |
|
| 224 |
=item label |
| 225 |
|
| 226 |
Label string. Will be localized. |
| 227 |
|
| 228 |
=back |
| 229 |
|
| 230 |
=head1 AUTHOR |
| 231 |
|
| 232 |
FormHandler Contributors - see HTML::FormHandler |
| 233 |
|
| 234 |
=head1 COPYRIGHT AND LICENSE |
| 235 |
|
| 236 |
This software is copyright (c) 2017 by Gerda Shank. |
| 237 |
|
| 238 |
This is free software; you can redistribute it and/or modify it under |
| 239 |
the same terms as the Perl 5 programming language system itself. |
| 240 |
|
| 241 |
=cut |