| 1 |
package HTML::FormHandler::Widget::Wrapper::Base; |
| 2 |
# ABSTRACT: common methods for widget wrappers |
| 3 |
$HTML::FormHandler::Widget::Wrapper::Base::VERSION = '0.410002'; |
| 4 |
use Moose::Role; |
| 5 |
use HTML::FormHandler::Render::Util ('process_attrs'); |
| 6 |
|
| 7 |
|
| 8 |
sub do_render_label { |
| 9 |
my ( $self, $result, $label_tag, $class ) = @_; |
| 10 |
|
| 11 |
$label_tag ||= $self->get_tag('label_tag') || 'label'; |
| 12 |
my $attr = $self->label_attributes( $result ); |
| 13 |
push @{ $attr->{class} }, @$class if $class; |
| 14 |
my $attrs = process_attrs($attr); |
| 15 |
my $label; |
| 16 |
if( $self->does_wrap_label ) { |
| 17 |
$label = $self->wrap_label( $self->label ); |
| 18 |
} |
| 19 |
else { |
| 20 |
$label = $self->get_tag('label_no_filter') ? $self->loc_label : $self->html_filter($self->loc_label); |
| 21 |
} |
| 22 |
$label .= $self->get_tag('label_after') if $label_tag ne 'legend'; |
| 23 |
my $id = $self->id; |
| 24 |
my $for = $label_tag eq 'label' ? qq{ for="$id"} : ''; |
| 25 |
return qq{<$label_tag$attrs$for>$label</$label_tag>}; |
| 26 |
} |
| 27 |
|
| 28 |
sub wrap_checkbox { |
| 29 |
my ( $self, $result, $rendered_widget, $default_wrapper ) = @_; |
| 30 |
|
| 31 |
my $option_wrapper = $self->option_wrapper || $default_wrapper; |
| 32 |
if ( $option_wrapper && $option_wrapper ne 'standard' && |
| 33 |
$option_wrapper ne 'label' ) { |
| 34 |
unless ( $self->can($option_wrapper) ) { |
| 35 |
die "HFH: no option_wrapper method '$option_wrapper'"; |
| 36 |
} |
| 37 |
return $self->$option_wrapper($result, $rendered_widget); |
| 38 |
} |
| 39 |
else { |
| 40 |
return $self->standard_wrap_checkbox($result, $rendered_widget); |
| 41 |
} |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
sub standard_wrap_checkbox { |
| 46 |
my ( $self, $result, $rendered_widget ) = @_; |
| 47 |
|
| 48 |
return $rendered_widget |
| 49 |
if( $self->get_tag('no_wrapped_label' ) ); |
| 50 |
|
| 51 |
my $label = $self->get_checkbox_label; |
| 52 |
my $id = $self->id; |
| 53 |
my $for = qq{ for="$id"}; |
| 54 |
|
| 55 |
# use "simple" label attributes for inner label |
| 56 |
my @label_class = ('checkbox'); |
| 57 |
push @label_class, 'inline' if $self->get_tag('inline'); |
| 58 |
my $lattrs = process_attrs( { class => \@label_class } ); |
| 59 |
|
| 60 |
# return wrapped checkbox, either on left or right |
| 61 |
my $output = ''; |
| 62 |
if ( $self->get_tag('label_left') ) { |
| 63 |
$output = qq{<label$lattrs$for>\n$label\n$rendered_widget</label>}; |
| 64 |
} |
| 65 |
else { |
| 66 |
$output = qq{<label$lattrs$for>$rendered_widget\n$label\n</label>}; |
| 67 |
} |
| 68 |
if ( $self->get_tag('checkbox_element_wrapper') ) { |
| 69 |
$output = qq{<div class="checkbox">$output</div>}; |
| 70 |
} |
| 71 |
return $output; |
| 72 |
} |
| 73 |
|
| 74 |
sub get_checkbox_label { |
| 75 |
my $self = shift; |
| 76 |
|
| 77 |
my $label = $self->option_label || ''; |
| 78 |
if( $label eq '' && ! $self->do_label ) { |
| 79 |
$label = $self->get_tag('label_no_filter') ? $self->loc_label : $self->html_filter($self->loc_label); |
| 80 |
} |
| 81 |
elsif( $label ne '' ) { |
| 82 |
$label = $self->get_tag('label_no_filter') ? $self->_localize($label) : $self->html_filter($self->_localize($label)); |
| 83 |
} |
| 84 |
return $label; |
| 85 |
} |
| 86 |
|
| 87 |
sub b3_label_left { |
| 88 |
my ( $self, $result, $rendered_widget ) = @_; |
| 89 |
|
| 90 |
my $label = $self->get_checkbox_label; |
| 91 |
my $id = $self->id; |
| 92 |
my $output = qq{<div class="checkbox">}; |
| 93 |
$output .= qq{<label for="$id">\n$label\n$rendered_widget</label>}; |
| 94 |
$output .= qq{</div>}; |
| 95 |
return $output; |
| 96 |
} |
| 97 |
|
| 98 |
sub b3_label_left_inline { |
| 99 |
my ( $self, $result, $rendered_widget ) = @_; |
| 100 |
|
| 101 |
my $label = $self->get_checkbox_label; |
| 102 |
my $id = $self->id; |
| 103 |
my $output = qq{<label class="checkbox-inline" for="$id">\n$label\n$rendered_widget</label>}; |
| 104 |
return $output; |
| 105 |
} |
| 106 |
|
| 107 |
sub b3_label_right { |
| 108 |
my ( $self, $result, $rendered_widget ) = @_; |
| 109 |
|
| 110 |
my $label = $self->get_checkbox_label; |
| 111 |
my $id = $self->id; |
| 112 |
my $output = qq{<div class="checkbox">}; |
| 113 |
$output .= qq{<label for="$id">$rendered_widget\n$label\n</label>}; |
| 114 |
$output .= qq{</div>}; |
| 115 |
return $output; |
| 116 |
} |
| 117 |
|
| 118 |
sub label_left { |
| 119 |
my ( $self, $result, $rendered_widget ) = @_; |
| 120 |
|
| 121 |
my $label = $self->get_checkbox_label; |
| 122 |
my $id = $self->id; |
| 123 |
my $output = qq{<label class="checkbox" for="$id">\n$label\n$rendered_widget</label>}; |
| 124 |
return $output; |
| 125 |
} |
| 126 |
|
| 127 |
sub label_right { |
| 128 |
my ( $self, $result, $rendered_widget ) = @_; |
| 129 |
|
| 130 |
my $label = $self->get_checkbox_label; |
| 131 |
my $id = $self->id; |
| 132 |
my $output = qq{<label class="checkbox" for="$id">$rendered_widget\n$label\n</label>}; |
| 133 |
return $output; |
| 134 |
} |
| 135 |
|
| 136 |
sub no_wrapped_label { |
| 137 |
my ( $self, $result, $rendered_widget ) = @_; |
| 138 |
return $rendered_widget; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
# for compatibility with older code |
| 143 |
sub render_label { |
| 144 |
my $self = shift; |
| 145 |
my $attrs = process_attrs($self->label_attributes); |
| 146 |
my $label = $self->html_filter($self->loc_label); |
| 147 |
$label .= ": " unless $self->get_tag('label_no_colon'); |
| 148 |
return qq{<label$attrs for="} . $self->id . qq{">$label</label>}; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
# this is not actually used any more, but is left here for compatibility |
| 153 |
# with user created widgets |
| 154 |
sub render_class { |
| 155 |
my ( $self, $result ) = @_; |
| 156 |
$result ||= $self->result; |
| 157 |
return process_attrs($self->wrapper_attributes($result)); |
| 158 |
} |
| 159 |
|
| 160 |
sub render_elementx { |
| 161 |
my ($self, %args) = @_; |
| 162 |
my $result ||= $self->result; |
| 163 |
|
| 164 |
if ( keys %args > 0 ) { |
| 165 |
while ( my ( $key, $value ) = each %args ) { |
| 166 |
confess "invalid attribute '$key' passed to render_elementx" |
| 167 |
unless $self->can($key); |
| 168 |
$self->$key($value); |
| 169 |
} |
| 170 |
} |
| 171 |
$self->render_element($result); |
| 172 |
} |
| 173 |
|
| 174 |
sub renderx { |
| 175 |
my ($self, %args) = @_; |
| 176 |
my $result ||= $self->result; |
| 177 |
|
| 178 |
if ( keys %args > 0 ) { |
| 179 |
while ( my ( $key, $value ) = each %args ) { |
| 180 |
confess "invalid attribute '$key' passed to renderx" |
| 181 |
unless $self->can($key); |
| 182 |
$self->$key($value); |
| 183 |
} |
| 184 |
} |
| 185 |
$self->render($result); |
| 186 |
} |
| 187 |
|
| 188 |
use namespace::autoclean; |
| 189 |
1; |
| 190 |
|
| 191 |
__END__ |
| 192 |
|
| 193 |
=pod |
| 194 |
|
| 195 |
=encoding UTF-8 |
| 196 |
|
| 197 |
=head1 NAME |
| 198 |
|
| 199 |
HTML::FormHandler::Widget::Wrapper::Base - common methods for widget wrappers |
| 200 |
|
| 201 |
=head1 VERSION |
| 202 |
|
| 203 |
version 0.410002 |
| 204 |
|
| 205 |
=head1 DESCRIPTION |
| 206 |
|
| 207 |
Provides several common methods for wrapper widgets, including |
| 208 |
'do_render_label' and 'wrap_checkbox'. |
| 209 |
|
| 210 |
Implements the checkbox 'option_wrapper' rendering: |
| 211 |
|
| 212 |
b3_label_left |
| 213 |
b3_label_right |
| 214 |
b3_label_left_inline |
| 215 |
label_left |
| 216 |
label_right |
| 217 |
no_wrapped_label |
| 218 |
|
| 219 |
=head1 NAME |
| 220 |
|
| 221 |
HTML::FormHandler::Widget::Wrapper::Base |
| 222 |
|
| 223 |
=head1 AUTHOR |
| 224 |
|
| 225 |
FormHandler Contributors - see HTML::FormHandler |
| 226 |
|
| 227 |
=head1 COPYRIGHT AND LICENSE |
| 228 |
|
| 229 |
This software is copyright (c) 2017 by Gerda Shank. |
| 230 |
|
| 231 |
This is free software; you can redistribute it and/or modify it under |
| 232 |
the same terms as the Perl 5 programming language system itself. |
| 233 |
|
| 234 |
=cut |