| 1 | package HTML::Widget::Element; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base qw/HTML::Widget::Accessor Class::Data::Accessor/; | ||
| 6 | use HTML::Element; | ||
| 7 | use HTML::Widget::Container; | ||
| 8 | use NEXT; | ||
| 9 | use Carp qw/croak/; | ||
| 10 | |||
| 11 | __PACKAGE__->mk_classaccessor( container_class => 'HTML::Widget::Container' ); | ||
| 12 | |||
| 13 | __PACKAGE__->mk_accessors(qw/name passive allow_filter allow_constraint/); | ||
| 14 | __PACKAGE__->mk_attr_accessors(qw/class/); | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | HTML::Widget::Element - Element Base Class | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | my $e = $widget->element( $type, $name, {disabled => 'disabled'} ); | ||
| 23 | $e->name('bar'); | ||
| 24 | $e->class('foo'); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Element Base Class. | ||
| 29 | |||
| 30 | =head1 METHODS | ||
| 31 | |||
| 32 | =head2 new | ||
| 33 | |||
| 34 | =head2 attributes | ||
| 35 | |||
| 36 | =head2 attrs | ||
| 37 | |||
| 38 | Arguments: %attributes | ||
| 39 | |||
| 40 | Arguments: \%attributes | ||
| 41 | |||
| 42 | Return Value: $element | ||
| 43 | |||
| 44 | Arguments: none | ||
| 45 | |||
| 46 | Return Value: \%attributes | ||
| 47 | |||
| 48 | Accepts either a list of key/value pairs, or a hash-ref. | ||
| 49 | |||
| 50 | $e->attributes( $key => $value ); | ||
| 51 | $e->attributes( { $key => $value } ); | ||
| 52 | |||
| 53 | Returns the C<$element> object, to allow method chaining. | ||
| 54 | |||
| 55 | As of v1.10, passing a hash-ref no longer deletes current | ||
| 56 | attributes, instead the attributes are added to the current attributes | ||
| 57 | hash. | ||
| 58 | |||
| 59 | This means the attributes hash-ref can no longer be emptied using | ||
| 60 | C<< $e->attributes( { } ); >>. Instead, you may use | ||
| 61 | C<< %{ $e->attributes } = (); >>. | ||
| 62 | |||
| 63 | As a special case, if no arguments are passed, the return value is a | ||
| 64 | hash-ref of attributes instead of the object reference. This provides | ||
| 65 | backwards compatability to support: | ||
| 66 | |||
| 67 | $e->attributes->{key} = $value; | ||
| 68 | |||
| 69 | L</attrs> is an alias for L</attributes>. | ||
| 70 | |||
| 71 | =head2 container | ||
| 72 | |||
| 73 | Arguments: \%attributes | ||
| 74 | |||
| 75 | Return Value: $container | ||
| 76 | |||
| 77 | Creates a new $container_class. Defaults to L<HTML::Widget::Container>. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub container { | ||
| 82 | my ( $self, $attributes ) = @_; | ||
| 83 | my $class = $self->container_class || 'HTML::Widget::Container'; | ||
| 84 | my $file = $class . ".pm"; | ||
| 85 | $file =~ s{::}{/}g; | ||
| 86 | eval { require $file }; | ||
| 87 | croak "Unable to load element container class $class: $@" if $@; | ||
| 88 | return $class->new( { passive => $self->passive, %$attributes } ); | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 id | ||
| 92 | |||
| 93 | Arguments: $widget | ||
| 94 | |||
| 95 | Return Value: $id | ||
| 96 | |||
| 97 | Creates a element id. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub id { | ||
| 102 | my ( $self, $w, $id ) = @_; | ||
| 103 | my $name = $self->name; | ||
| 104 | |||
| 105 | return unless defined($name) || defined($id); | ||
| 106 | |||
| 107 | return $w->name . '_' . ( $id || $self->name ); | ||
| 108 | } | ||
| 109 | |||
| 110 | =head2 init | ||
| 111 | |||
| 112 | Arguments: $widget | ||
| 113 | |||
| 114 | Called once when process() gets called for the first time. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub init { } | ||
| 119 | |||
| 120 | =head2 mk_error | ||
| 121 | |||
| 122 | Arguments: $widget, \@errors | ||
| 123 | |||
| 124 | Return Value: $error | ||
| 125 | |||
| 126 | Creates a new L<HTML::Widget::Error>. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub mk_error { | ||
| 131 | my ( $self, $w, $errors ) = @_; | ||
| 132 | |||
| 133 | return | ||
| 134 | if ( !$w->{empty_errors} | ||
| 135 | && ( !defined($errors) || !scalar(@$errors) ) ); | ||
| 136 | |||
| 137 | my $no_render_count = 0; | ||
| 138 | $no_render_count += $_->no_render ? 1 : 0 for @$errors; | ||
| 139 | return if !$w->{empty_errors} && $no_render_count == scalar @$errors; | ||
| 140 | |||
| 141 | my $id = $self->attributes->{id} || $self->id($w); | ||
| 142 | my $cont_id = $id . '_errors'; | ||
| 143 | my $container = HTML::Element->new( | ||
| 144 | 'span', | ||
| 145 | id => $cont_id, | ||
| 146 | class => 'error_messages' | ||
| 147 | ); | ||
| 148 | for my $error (@$errors) { | ||
| 149 | next if !$w->{empty_errors} && $error->no_render; | ||
| 150 | my $e_id = $id . '_error_' . lc( $error->{type} ); | ||
| 151 | my $e_class = lc( $error->{type} . '_errors' ); | ||
| 152 | my $e = HTML::Element->new( 'span', id => $e_id, class => $e_class ); | ||
| 153 | $e->push_content( $error->{message} ); | ||
| 154 | $container->push_content($e); | ||
| 155 | } | ||
| 156 | return $container; | ||
| 157 | } | ||
| 158 | |||
| 159 | =head2 mk_input | ||
| 160 | |||
| 161 | Arguments: $widget, \%attributes, \@errors | ||
| 162 | |||
| 163 | Return Value: $input_tag | ||
| 164 | |||
| 165 | Creates a new input tag. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub mk_input { | ||
| 170 | my ( $self, $w, $attrs, $errors ) = @_; | ||
| 171 | |||
| 172 | return $self->mk_tag( $w, 'input', $attrs, $errors ); | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 mk_tag | ||
| 176 | |||
| 177 | Arguments: $widget, $tagtype, \%attributes, \@errors | ||
| 178 | |||
| 179 | Return Value: $element_tag | ||
| 180 | |||
| 181 | Creates a new tag. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub mk_tag { | ||
| 186 | my ( $self, $w, $tag, $attrs, $errors ) = @_; | ||
| 187 | my $e = HTML::Element->new($tag); | ||
| 188 | my $id = $self->attributes->{id} || $self->id($w); | ||
| 189 | my $type = ref $self; | ||
| 190 | $type =~ s/^HTML::Widget::Element:://; | ||
| 191 | $type =~ s/::/_/g; | ||
| 192 | $self->attributes->{class} ||= lc($type); | ||
| 193 | $e->attr( id => $id ) unless $self->attributes->{id} || $w->{explicit_ids}; | ||
| 194 | $e->attr( name => $self->name ); | ||
| 195 | |||
| 196 | for my $key ( keys %$attrs ) { | ||
| 197 | my $value = $attrs->{$key}; | ||
| 198 | $e->attr( $key, $value ) if defined $value; | ||
| 199 | } | ||
| 200 | $e->attr( $_ => ${ $self->attributes }{$_} ) | ||
| 201 | for ( keys %{ $self->attributes } ); | ||
| 202 | |||
| 203 | return $e; | ||
| 204 | } | ||
| 205 | |||
| 206 | =head2 mk_label | ||
| 207 | |||
| 208 | Arguments: $widget, $name, $comment, \@errors | ||
| 209 | |||
| 210 | Return Value: $label_tag | ||
| 211 | |||
| 212 | Creates a new label tag. | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | sub mk_label { | ||
| 217 | my ( $self, $w, $name, $comment, $errors ) = @_; | ||
| 218 | return unless defined $name; | ||
| 219 | my $for = $self->attributes->{id} || $self->id($w); | ||
| 220 | my $id = $for . '_label'; | ||
| 221 | my $e = HTML::Element->new( 'label', for => $for, id => $id ); | ||
| 222 | if ($errors) { | ||
| 223 | $e->attr( 'class' => 'labels_with_errors' ); | ||
| 224 | } | ||
| 225 | $e->push_content($name); | ||
| 226 | if ($comment) { | ||
| 227 | my $c = HTML::Element->new( | ||
| 228 | 'span', | ||
| 229 | id => "$for\_comment", | ||
| 230 | class => 'label_comments' | ||
| 231 | ); | ||
| 232 | $c->push_content($comment); | ||
| 233 | $e->push_content($c); | ||
| 234 | } | ||
| 235 | return $e; | ||
| 236 | } | ||
| 237 | |||
| 238 | =head2 name | ||
| 239 | |||
| 240 | Arguments: $name | ||
| 241 | |||
| 242 | Return Value: $name | ||
| 243 | |||
| 244 | Contains the element name. | ||
| 245 | |||
| 246 | =head2 passive | ||
| 247 | |||
| 248 | Arguments: $bool | ||
| 249 | |||
| 250 | Return Value: $bool | ||
| 251 | |||
| 252 | Defines if element gets automatically rendered. | ||
| 253 | |||
| 254 | =head2 prepare | ||
| 255 | |||
| 256 | Arguments: $widget | ||
| 257 | |||
| 258 | Called whenever C<< $widget->process >> gets called, before | ||
| 259 | C<< $element->process >>. | ||
| 260 | |||
| 261 | =cut | ||
| 262 | |||
| 263 | sub prepare { } | ||
| 264 | |||
| 265 | =head2 process | ||
| 266 | |||
| 267 | Arguments: \%params, \@uploads | ||
| 268 | |||
| 269 | Return Value: \@errors | ||
| 270 | |||
| 271 | Called whenever C<< $widget->process >> is called. | ||
| 272 | |||
| 273 | Returns an arrayref of L<HTML::Widget::Error> objects. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub process { } | ||
| 278 | |||
| 279 | =head2 containerize | ||
| 280 | |||
| 281 | Arguments: $widget, $value, \@errors | ||
| 282 | |||
| 283 | Return Value: $container_tag | ||
| 284 | |||
| 285 | Containerize the element, label and error for later rendering. | ||
| 286 | Uses L<HTML::Widget::Container> by default, but this can be over-ridden on | ||
| 287 | a class or instance basis via L</container_class>. | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | sub containerize { } | ||
| 292 | |||
| 293 | =head2 container_class | ||
| 294 | |||
| 295 | Arguments: $class | ||
| 296 | |||
| 297 | Return Value: $class | ||
| 298 | |||
| 299 | Contains the class to use for contain the element which then get rendered. Defaults to L<HTML::Widget::Container>. C<container_class> can be set at a class or instance level: | ||
| 300 | |||
| 301 | HTML::Widget::Element->container_class('My::Container'); | ||
| 302 | # Override default to custom class | ||
| 303 | |||
| 304 | HTML::Widget::Element::Password->container_class(undef); | ||
| 305 | # Passwords use the default class | ||
| 306 | |||
| 307 | $w->element('Textfield')->name('foo')->container_class->('My::Other::Container'); | ||
| 308 | # This element only will use My::Other::Container to render | ||
| 309 | |||
| 310 | =cut | ||
| 311 | |||
| 312 | sub container_class { | ||
| 313 | my ($self) = shift; | ||
| 314 | |||
| 315 | if ( not $_[0] and @_ >= 1 ) { | ||
| 316 | delete $self->{container_class}; | ||
| 317 | } | ||
| 318 | |||
| 319 | return $self->_container_class_accessor(@_); | ||
| 320 | } | ||
| 321 | |||
| 322 | =head2 find_elements | ||
| 323 | |||
| 324 | Return Value: \@elements | ||
| 325 | |||
| 326 | For non-block-container elements, simply returns a one-element list | ||
| 327 | containing this element. | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | sub find_elements { return (shift); } | ||
| 332 | |||
| 333 | =head2 new | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub new { | ||
| 338 | return shift->NEXT::new(@_)->allow_filter(1)->allow_constraint(1); | ||
| 339 | } | ||
| 340 | |||
| 341 | =head2 allow_filter | ||
| 342 | |||
| 343 | Used by C<< $widget->filter_all >>. If false, the filter won't be added. | ||
| 344 | |||
| 345 | Default true. | ||
| 346 | |||
| 347 | =head2 allow_constraint | ||
| 348 | |||
| 349 | Used by C<< $widget->constraint_all >>. If false, the filter won't be added. | ||
| 350 | |||
| 351 | Default true. | ||
| 352 | |||
| 353 | =head1 AUTHOR | ||
| 354 | |||
| 355 | Sebastian Riedel, C<sri@oook.de> | ||
| 356 | |||
| 357 | =head1 LICENSE | ||
| 358 | |||
| 359 | This library is free software, you can redistribute it and/or modify it under | ||
| 360 | the same terms as Perl itself. | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | 1; |