| 1 | package HTML::Widget::Container; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'Class::Accessor::Fast'; | ||
| 6 | |||
| 7 | __PACKAGE__->mk_accessors(qw/element label error javascript passive name/); | ||
| 8 | |||
| 9 | use overload '""' => sub { return shift->as_xml }, fallback => 1; | ||
| 10 | |||
| 11 | *js = \&javascript; | ||
| 12 | *js_xml = \&javascript_xml; | ||
| 13 | *field = \&element; | ||
| 14 | *field_xml = \&element_xml; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | HTML::Widget::Container - Container | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | my $container = $form->element('foo'); | ||
| 23 | |||
| 24 | my $field = $container->field; | ||
| 25 | my $error = $container->error; | ||
| 26 | my $label = $container->label; | ||
| 27 | |||
| 28 | my $field_xml = $container->field_xml; | ||
| 29 | my $error_xml = $container->error_xml; | ||
| 30 | my $javascript_xml = $container->javascript_xml; | ||
| 31 | |||
| 32 | my $xml = $container->as_xml; | ||
| 33 | # $xml eq "$container" | ||
| 34 | |||
| 35 | my $javascript = $container->javascript; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Container. | ||
| 40 | |||
| 41 | =head1 METHODS | ||
| 42 | |||
| 43 | =head2 as_xml | ||
| 44 | |||
| 45 | Return Value: $xml | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub as_xml { | ||
| 50 | my $self = shift; | ||
| 51 | my $xml = ''; | ||
| 52 | $xml .= $self->element_xml if $self->element; | ||
| 53 | $xml .= $self->javascript_xml if $self->javascript; | ||
| 54 | $xml .= $self->error_xml if $self->error; | ||
| 55 | return $xml; | ||
| 56 | } | ||
| 57 | |||
| 58 | =head2 _build_element | ||
| 59 | |||
| 60 | Arguments: $element | ||
| 61 | |||
| 62 | Return Value: @elements | ||
| 63 | |||
| 64 | Convert $element to L<HTML::Element> object. Accepts arrayref. | ||
| 65 | |||
| 66 | If you wish to change the rendering behaviour of HTML::Widget; specifically, | ||
| 67 | the handling of elements which are array-refs, you can specify | ||
| 68 | L<HTML::Widget::Element/container_class> to a custom class which just | ||
| 69 | overrides this function. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub _build_element { | ||
| 74 | my ( $self, $element ) = @_; | ||
| 75 | |||
| 76 | return () unless $element; | ||
| 77 | |||
| 78 | if ( ref $element eq 'ARRAY' ) { | ||
| 79 | return map { $self->_build_element($_) } @{$element}; | ||
| 80 | } | ||
| 81 | |||
| 82 | return $self->build_single_element( $element->clone ); | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 build_single_element | ||
| 86 | |||
| 87 | Arguments: $element | ||
| 88 | |||
| 89 | Return Value: $element | ||
| 90 | |||
| 91 | Convert $element to L<HTML::Element> object. | ||
| 92 | |||
| 93 | Called by L</_build_element>. | ||
| 94 | |||
| 95 | If you wish to change the rendering behaviour of HTML::Widget; specifically, | ||
| 96 | the handling of an individual element, you can override this function. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub build_single_element { | ||
| 101 | my ( $self, $element ) = @_; | ||
| 102 | |||
| 103 | my $class = $element->attr('class') || ''; | ||
| 104 | |||
| 105 | $element = $self->build_element_error($element); | ||
| 106 | |||
| 107 | $element = $self->build_element_label( $element, $class ); | ||
| 108 | |||
| 109 | return $element; | ||
| 110 | } | ||
| 111 | |||
| 112 | =head2 build_element_error | ||
| 113 | |||
| 114 | Arguments: $element | ||
| 115 | |||
| 116 | Return Value: $element | ||
| 117 | |||
| 118 | Called by L</build_single_element>. | ||
| 119 | |||
| 120 | If you wish to change how an error is rendered, override this function. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub build_element_error { | ||
| 125 | my ( $self, $element ) = @_; | ||
| 126 | |||
| 127 | if ( $self->error && $element->tag eq 'input' ) { | ||
| 128 | $element = HTML::Element->new( 'span', class => 'fields_with_errors' ) | ||
| 129 | ->push_content($element); | ||
| 130 | } | ||
| 131 | |||
| 132 | return $element; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 build_element_label | ||
| 136 | |||
| 137 | Arguments: $element, $class | ||
| 138 | |||
| 139 | Return Value: $element | ||
| 140 | |||
| 141 | Called by L</build_single_element>. | ||
| 142 | |||
| 143 | If you wish to change how an element's label is rendered, override this | ||
| 144 | function. | ||
| 145 | |||
| 146 | The $class argument is the original class of the element, before | ||
| 147 | L</build_element_error> was called. | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub build_element_label { | ||
| 152 | my ( $self, $element, $class ) = @_; | ||
| 153 | |||
| 154 | return $element unless defined $self->label; | ||
| 155 | |||
| 156 | my $l = $self->label->clone; | ||
| 157 | my $radiogroup; | ||
| 158 | |||
| 159 | if ( $class eq 'radiogroup_fieldset' ) { | ||
| 160 | $element->unshift_content($l); | ||
| 161 | $radiogroup = 1; | ||
| 162 | } | ||
| 163 | elsif ( $self->error && $element->tag eq 'span' ) { | ||
| 164 | |||
| 165 | # it might still be a radiogroup wrapped in an error span | ||
| 166 | for my $elem ( $element->content_refs_list ) { | ||
| 167 | next unless ref $$elem; | ||
| 168 | if ( $$elem->attr('class') eq 'radiogroup_fieldset' ) { | ||
| 169 | $$elem->unshift_content($l); | ||
| 170 | $radiogroup = 1; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | if ( !$radiogroup ) { | ||
| 176 | |||
| 177 | # Do we prepend or append input to label? | ||
| 178 | $element = | ||
| 179 | ( $class eq 'checkbox' or $class eq 'radio' ) | ||
| 180 | ? $l->unshift_content($element) | ||
| 181 | : $l->push_content($element); | ||
| 182 | } | ||
| 183 | |||
| 184 | return $element; | ||
| 185 | } | ||
| 186 | |||
| 187 | =head2 as_list | ||
| 188 | |||
| 189 | Return Value: @elements | ||
| 190 | |||
| 191 | Returns a list of L<HTML::Element> objects. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub as_list { | ||
| 196 | my $self = shift; | ||
| 197 | my @list; | ||
| 198 | push @list, $self->_build_element( $self->element ); | ||
| 199 | push @list, $self->javascript_element if $self->javascript; | ||
| 200 | push @list, $self->error if $self->error; | ||
| 201 | return @list; | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 element | ||
| 205 | |||
| 206 | =head2 field | ||
| 207 | |||
| 208 | Arguments: $element | ||
| 209 | |||
| 210 | L</field> is an alias for L</element>. | ||
| 211 | |||
| 212 | =head2 element_xml | ||
| 213 | |||
| 214 | =head2 field_xml | ||
| 215 | |||
| 216 | Return Value: $xml | ||
| 217 | |||
| 218 | L</field_xml> is an alias for L</element_xml>. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub element_xml { | ||
| 223 | my $self = shift; | ||
| 224 | my @e = $self->_build_element; | ||
| 225 | return join( '', | ||
| 226 | map( { $_->as_XML } $self->_build_element( $self->element ) ) ) | ||
| 227 | || ''; | ||
| 228 | } | ||
| 229 | |||
| 230 | =head2 error | ||
| 231 | |||
| 232 | Arguments: $error | ||
| 233 | |||
| 234 | Return Value: $error | ||
| 235 | |||
| 236 | =head2 error_xml | ||
| 237 | |||
| 238 | Return Value: $xml | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | sub error_xml { | ||
| 243 | my $self = shift; | ||
| 244 | return $self->error ? $self->error->as_XML : ''; | ||
| 245 | } | ||
| 246 | |||
| 247 | =head2 javascript | ||
| 248 | |||
| 249 | =head2 js | ||
| 250 | |||
| 251 | Arguments: $javascript | ||
| 252 | |||
| 253 | Return Value: $javascript | ||
| 254 | |||
| 255 | L</js> is an alias for L</javascript>. | ||
| 256 | |||
| 257 | =head2 javascript_element | ||
| 258 | |||
| 259 | Return Value: $javascript_element | ||
| 260 | |||
| 261 | Returns javascript in a script L<HTML::Element>. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub javascript_element { | ||
| 266 | my $self = shift; | ||
| 267 | my $script = HTML::Element->new( 'script', type => 'text/javascript' ); | ||
| 268 | my $content = "\n<!--\n" . $self->javascript . "\n//-->\n"; | ||
| 269 | my $literal = HTML::Element->new( '~literal', text => $content ); | ||
| 270 | $script->push_content($literal); | ||
| 271 | return $script; | ||
| 272 | } | ||
| 273 | |||
| 274 | =head2 javascript_xml | ||
| 275 | |||
| 276 | =head2 js_xml | ||
| 277 | |||
| 278 | Return Value: $javascript_xml | ||
| 279 | |||
| 280 | Returns javascript in a script block. | ||
| 281 | |||
| 282 | L</js_xml> is an alias for L</javascript_xml>. | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub javascript_xml { | ||
| 287 | my $self = shift; | ||
| 288 | return $self->javascript_element->as_HTML('<>&'); | ||
| 289 | } | ||
| 290 | |||
| 291 | =head1 AUTHOR | ||
| 292 | |||
| 293 | Sebastian Riedel, C<sri@oook.de> | ||
| 294 | |||
| 295 | =head1 LICENSE | ||
| 296 | |||
| 297 | This library is free software, you can redistribute it and/or modify it under | ||
| 298 | the same terms as Perl itself. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | 1; |