| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::Web::Form::Field::Collection; | ||
| 5 | |||
| 6 | use base qw/Jifty::Web::Form::Field/; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Jifty::Web::Form::Field::Collection - render a whole collection of available values | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | Renders multiple text inputs with the same name, available_values used as defaults. | ||
| 15 | |||
| 16 | =head1 METHODS | ||
| 17 | |||
| 18 | =head2 render_widget | ||
| 19 | |||
| 20 | Renders the whole collection of available values. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub render_widget { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | for my $opt ($self->available_values) { | ||
| 28 | $self->render_option($opt); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | =head2 render_label | ||
| 33 | |||
| 34 | Render a label for our own. We need to output the label as a span | ||
| 35 | instead since the labels are associated with the individual options. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub render_label { | ||
| 40 | my $self = shift; | ||
| 41 | Jifty->web->out( | ||
| 42 | qq{<span class="label @{[$self->classes]}">} | ||
| 43 | . Jifty->web->escape(_($self->label)) | ||
| 44 | . qq{</span>\n} | ||
| 45 | ); | ||
| 46 | |||
| 47 | return ''; | ||
| 48 | } | ||
| 49 | |||
| 50 | =head2 render_option option | ||
| 51 | |||
| 52 | Renders a normal input field. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub render_option { | ||
| 57 | my $self = shift; | ||
| 58 | my $opt = shift; | ||
| 59 | my $display = ref($opt) ? $opt->{'display'} : $opt; | ||
| 60 | my $value = ref($opt) ? $opt->{'value'} : $opt; | ||
| 61 | $value = "0" unless defined $value; | ||
| 62 | |||
| 63 | my $id = $self->element_id . "-" . $value; | ||
| 64 | $id =~ s/\s+/_/; | ||
| 65 | |||
| 66 | my $wclass = $self->_widget_class; | ||
| 67 | |||
| 68 | my $field = qq! <input type="text" !; | ||
| 69 | $field .= qq! name="@{[ Jifty->web->escape( $self->input_name ) ]}"!; | ||
| 70 | $field .= qq! title="@{[ Jifty->web->escape( $self->title ) ]}"! if ($self->title); | ||
| 71 | $field .= qq! id="@{[ Jifty->web->escape( $id ) ]}"!; | ||
| 72 | $field .= qq! value="@{[ Jifty->web->escape( $value ) ]}""!; | ||
| 73 | $field .= $wclass; | ||
| 74 | $field .= qq{ />}; | ||
| 75 | |||
| 76 | $field .= qq{<label for="@{[ Jifty->web->escape( $id ) ]}"}; | ||
| 77 | $field .= $wclass; | ||
| 78 | $field .= qq{ >@{[ Jifty->web->escape( $display ) ]}</label>\n }; | ||
| 79 | |||
| 80 | $field = qq{<span class="multitext">$field</span>}; | ||
| 81 | |||
| 82 | Jifty->web->out($field); | ||
| 83 | ''; | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; |