Debian Codegraph @

lib/HTML/FormHandler/Widget/Wrapper/Bootstrap3.pm

raw
1 package HTML::FormHandler::Widget::Wrapper::Bootstrap3;
2 # ABSTRACT: Twitter Bootstrap 3.0 field wrapper
3 $HTML::FormHandler::Widget::Wrapper::Bootstrap3::VERSION = '0.410002';
4 use Moose::Role;
5 use namespace::autoclean;
6 use HTML::FormHandler::Render::Util ('process_attrs');
7 use List::Util 1.33 ('any');
8 use HTML::Entities qw( encode_entities );
9
10 with 'HTML::FormHandler::Widget::Wrapper::Base';
11
12
13 sub is_b3 {1}
14
15 sub build_wrapper_tags {
16 {
17 radio_element_wrapper => 1,
18 checkbox_element_wrapper => 1,
19 }
20 }
21
22 sub wrap_field {
23 my ( $self, $result, $rendered_widget ) = @_;
24
25 my $output;
26
27 # create attribute string for wrapper
28 if ( $self->do_wrapper ) {
29 my $attr = $self->wrapper_attributes($result);
30 # no 'control-group' class for Hidden fields, 'form-actions' for submit/reset
31 my $div_class = 'form-group';
32 unshift @{$attr->{class}}, $div_class;
33 my $attr_str = process_attrs( $attr );
34 # wrapper is always a div
35 $output .= $self->get_tag('before_wrapper');
36 $output .= qq{\n<div$attr_str>};
37 }
38 # render the label
39 $output .= "\n" . $self->do_render_label($result, undef, ['control-label'] )
40 if $self->do_label;
41 $output .= $self->get_tag('before_element');
42
43 # the controls div; used to have 'controls' class. Now it comes from
44 # the 'element_wrapper_class'. Used for column layout.
45 my $ew_attr = $self->element_wrapper_attributes($result);
46 my $element_wrapper_attrs = process_attrs( $ew_attr );
47 $output .= qq{\n<div$element_wrapper_attrs>}
48 unless !$self->do_wrapper;
49
50 # yet another tag
51 $output .= $self->get_tag('before_element_inside_div');
52 # handle input-prepend and input-append
53 if( $self->get_tag('input_prepend') || $self->get_tag('input_append') ||
54 $self->get_tag('input_append_button') ) {
55 $rendered_widget = $self->do_prepend_append($rendered_widget);
56 }
57 elsif( lc $self->widget eq 'checkbox' ) {
58 $rendered_widget = $self->wrap_checkbox($result, $rendered_widget, 'b3_label_left')
59 }
60
61 $output .= "\n$rendered_widget";
62 # various 'help-inline' bits: errors, warnings
63 unless( $self->get_tag('no_errors') ) {
64 $output .= qq{\n<span class="help-block">$_</span>}
65 for map { encode_entities($_) } $result->all_errors;
66 $output .= qq{\n<span class="help-block">$_</span>} for $result->all_warnings;
67 }
68 # extra after element stuff
69 $output .= $self->get_tag('after_element');
70 # close element_wrapper 'control' div
71 $output .= '</div>' unless !$self->do_wrapper;
72 # close wrapper
73 if ( $self->do_wrapper ) {
74 $output .= "\n</div>";
75 $output .= $self->get_tag('after_wrapper');
76 }
77 return "$output";
78 }
79
80 # don't render label for checkboxes
81 sub do_render_label {
82 my ( $self ) = @_;
83
84 return '' if $self->type_attr eq 'checkbox';
85 HTML::FormHandler::Widget::Wrapper::Base::do_render_label(@_);
86 }
87
88 sub add_standard_element_classes {
89 my ( $self, $result, $class ) = @_;
90 push @$class, 'has-error' if $result->has_errors;
91 push @$class, 'has-warning' if $result->has_warnings;
92 push @$class, 'disabled' if $self->disabled;
93 push @$class, 'form-control'
94 if $self->html_element eq 'select' || $self->html_element eq 'textarea' ||
95 $self->type_attr eq 'text' || $self->type_attr eq 'password';
96 }
97
98 sub add_standard_wrapper_classes {
99 my ( $self, $result, $class ) = @_;
100 push @$class, 'has-error' if ( $result->has_error_results || $result->has_errors );
101 push @$class, 'has-warning' if $result->has_warnings;
102 # TODO: has-success?
103 }
104
105 sub add_standard_label_classes {
106 my ( $self, $result, $class ) = @_;
107 if ( my $classes = $self->form->get_tag('layout_classes') ) {
108 my $label_class = $classes->{label_class};
109 if ( $label_class && not any { $_ =~ /^col\-/ } @$class ) {
110 push @$class, @{$classes->{label_class}};
111 }
112 }
113 }
114
115 sub add_standard_element_wrapper_classes {
116 my ( $self, $result, $class ) = @_;
117 if ( my $classes = $self->form->get_tag('layout_classes') ) {
118 if ( exists $classes->{element_wrapper_class} &&
119 not any { $_ =~ /^col\-/ } @$class ) {
120 push @$class, @{$classes->{element_wrapper_class}};
121 }
122 if ( exists $classes->{no_label_element_wrapper_class} &&
123 ( ! $self->do_label || $self->type_attr eq 'checkbox' ) &&
124 not any { $_ =~ /^col\-.*offset/ } @$class ) {
125 push @$class, @{$classes->{no_label_element_wrapper_class}};
126 }
127 }
128 }
129
130 sub wrap_checkbox {
131 my ( $self, $result, $rendered_widget ) = @_;
132
133 # use the regular label
134 my $label = $self->option_label || $self->label;
135 $label = $self->get_tag('label_no_filter') ? $self->_localize($label) : $self->html_filter($self->_localize($label));
136 my $id = $self->id;
137 my $for = qq{ for="$id"};
138
139 # return unwrapped checkbox with 'checkbox-inline'
140 return qq{<label class="checkbox-inline" $for>$rendered_widget\n$label\n</label>}
141 if( $self->get_tag('inline') );
142
143 # return wrapped checkbox, either on left or right
144 return qq{<div class="checkbox"><label$for>\n$label\n$rendered_widget</label></div>}
145 if( $self->get_tag('label_left') );
146 return qq{<div class="checkbox"><label$for>$rendered_widget\n$label\n</label></div>};
147 }
148
149 sub do_prepend_append {
150 my ( $self, $rendered_widget ) = @_;
151
152 my @class;
153 if( my $ip_tag = $self->get_tag('input_prepend' ) ) {
154 $rendered_widget = qq{<span class="input-group-addon">$ip_tag</span>$rendered_widget};
155 push @class, 'input-group';
156 }
157 if ( my $ia_tag = $self->get_tag('input_append' ) ) {
158 $rendered_widget = qq{$rendered_widget<span class="input-group-addon">$ia_tag</span>};
159 push @class, 'input-group';
160 }
161 if ( my $iab_tag = $self->get_tag('input_append_button') ) {
162 my $iab_element_attr_tag = $self->get_tag('input_append_button_element_attr');
163
164 my ($btn_class, $attr);
165 if (ref $iab_element_attr_tag eq 'HASH') {
166 $btn_class = ref $iab_element_attr_tag->{class} eq 'ARRAY' ? shift @{$iab_element_attr_tag->{class}} : $iab_element_attr_tag->{class};
167 $attr = process_attrs( $iab_element_attr_tag );
168 }
169
170 my @buttons = ref $iab_tag eq 'ARRAY' ? @$iab_tag : ($iab_tag);
171 my $group = qq{<span class="input-group-btn">};
172 foreach my $btn ( @buttons ) {
173 $group .= qq{<button type="button" class="btn $btn_class"$attr>$btn</button>};
174 }
175 $group .= qq{</span>};
176 $rendered_widget = qq{$rendered_widget$group};
177 push @class, 'input-group';
178 }
179 my $attr = process_attrs( { class => \@class } );
180 $rendered_widget =
181 qq{<div$attr>
182 $rendered_widget
183 </div>};
184 return $rendered_widget;
185 }
186
187 1;
188
189 __END__
190
191 =pod
192
193 =encoding UTF-8
194
195 =head1 NAME
196
197 HTML::FormHandler::Widget::Wrapper::Bootstrap3 - Twitter Bootstrap 3.0 field wrapper
198
199 =head1 VERSION
200
201 version 0.410002
202
203 =head1 SYNOPSIS
204
205 Wrapper to implement Bootstrap 3.0 style form element rendering.
206
207 =head1 DESCRIPTION
208
209 Differences from the Bootstrap 2.0 wrapper:
210
211 The wrapper class is 'form-group' instead of 'control-group'
212 The div wrapping the form element does not
213 have the 'controls' class. Used for sizing css classes.
214 Input prepend & append use different classes
215 The input elements have a 'form-control' class
216
217 Tags supported:
218
219 label_no_filter -- don't html filter the label
220 label_after -- useful for putting a colon, or other trailing formatting
221 before_element -- insert tag before input, outside element's control div
222 before_element_inside_div -- insert tag before input element, inside control div
223 input_prepend -- for Bootstrap 'input-prepend' class
224 input_append -- for Bootstrap 'input-append' class
225 input_append_button -- 'input-append' with button instead of span
226 no_errors -- don't append error to field rendering
227 after_element -- insert tag after input element
228
229 =head1 AUTHOR
230
231 FormHandler Contributors - see HTML::FormHandler
232
233 =head1 COPYRIGHT AND LICENSE
234
235 This software is copyright (c) 2017 by Gerda Shank.
236
237 This is free software; you can redistribute it and/or modify it under
238 the same terms as the Perl 5 programming language system itself.
239
240 =cut