/
Base.pm
92 lines (75 loc) · 2.71 KB
/
Base.pm
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
package HTML::FormHandler::Widget::Wrapper::Base;
# ABSTRACT: common methods for widget wrappers
use Moose::Role;
use HTML::FormHandler::Render::Util ('process_attrs');
=head1 NAME
HTML::FormHandler::Widget::Wrapper::Base
=head1 DESCRIPTION
Provides several common methods for wrapper widgets, including
'do_render_label' and 'wrap_checkbox'.
=cut
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 ) = @_;
return $rendered_widget
if( $self->get_tag('no_wrapped_label' ) );
my $label = $self->option_label || '';
if( $label eq '' && ! $self->do_label ) {
$label = $self->html_filter($self->loc_label);
}
elsif( $label ne '' ) {
$label = $self->html_filter($self->_localize($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;
}
# 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;