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