| 1 | package HTML::Widget::Element::Button; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'HTML::Widget::Element'; | ||
| 6 | use NEXT; | ||
| 7 | |||
| 8 | __PACKAGE__->mk_accessors( | ||
| 9 | qw/value content type _src height width | ||
| 10 | retain_default/ | ||
| 11 | ); | ||
| 12 | |||
| 13 | # alias | ||
| 14 | *label = \&value; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | HTML::Widget::Element::Button - Button Element | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | $e = $widget->element( 'Button', 'foo' ); | ||
| 23 | $e->value('bar'); | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Button Element. | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | =head2 new | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | return shift->NEXT::new(@_)->type('button'); | ||
| 37 | } | ||
| 38 | |||
| 39 | =head2 value | ||
| 40 | |||
| 41 | =head2 label | ||
| 42 | |||
| 43 | The value of this Button element. Is also used by the browser as the | ||
| 44 | button label. | ||
| 45 | |||
| 46 | L</label> is an alias for L</value>. | ||
| 47 | |||
| 48 | =head2 content | ||
| 49 | |||
| 50 | If set, the element will use a C<button> tag rather than an C<input> | ||
| 51 | tag. | ||
| 52 | |||
| 53 | The value of C<content> will be used between the C<button> tags, unescaped. | ||
| 54 | This means that any html markup may be used to display the button. | ||
| 55 | |||
| 56 | =head2 type | ||
| 57 | |||
| 58 | Valid values are C<button>, C<submit>, C<reset> and C<image>. | ||
| 59 | |||
| 60 | =head2 src | ||
| 61 | |||
| 62 | If set, the element will be rendered as an image button, using this url as | ||
| 63 | the image. | ||
| 64 | |||
| 65 | Automatically sets L</type> to C<image>. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub src { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | $self->type('image') if @_; | ||
| 73 | |||
| 74 | return $self->_src(@_); | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 retain_default | ||
| 78 | |||
| 79 | If true, overrides the default behaviour, so that after a field is missing | ||
| 80 | from the form submission, the xml output will contain the default value, | ||
| 81 | rather than be empty. | ||
| 82 | |||
| 83 | =head2 render | ||
| 84 | |||
| 85 | =head2 containerize | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub containerize { | ||
| 90 | my ( $self, $w, $value, $errors, $args ) = @_; | ||
| 91 | |||
| 92 | $value = ref $value eq 'ARRAY' ? shift @$value : $value; | ||
| 93 | |||
| 94 | $value = $self->value | ||
| 95 | if ( not defined $value ) | ||
| 96 | and $self->retain_default || not $args->{submitted}; | ||
| 97 | |||
| 98 | my $i; | ||
| 99 | my %args = ( | ||
| 100 | type => $self->type, | ||
| 101 | value => $value, | ||
| 102 | ); | ||
| 103 | |||
| 104 | $args{src} = $self->src if defined $self->src; | ||
| 105 | $args{height} = $self->height if defined $self->height; | ||
| 106 | $args{width} = $self->width if defined $self->width; | ||
| 107 | |||
| 108 | if ( defined $self->content && length $self->content ) { | ||
| 109 | $i = $self->mk_tag( $w, 'button', \%args ); | ||
| 110 | $i->push_content( | ||
| 111 | HTML::Element->new( '~literal', text => $self->content ) ); | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | $i = $self->mk_input( $w, \%args ); | ||
| 115 | } | ||
| 116 | |||
| 117 | return $self->container( { element => $i } ); | ||
| 118 | } | ||
| 119 | |||
| 120 | =head1 SEE ALSO | ||
| 121 | |||
| 122 | L<HTML::Widget::Element> | ||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | Marcus Ramberg, C<mramberg@cpan.org> | ||
| 127 | |||
| 128 | =head1 LICENSE | ||
| 129 | |||
| 130 | This library is free software, you can redistribute it and/or modify it under | ||
| 131 | the same terms as Perl itself. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | 1; |