| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 15 |  |  | 15 |  | 9602 | use strict; | 
|  | 15 |  |  |  |  | 447 |  | 
|  | 15 |  |  |  |  | 417 |  | 
| 2 | 15 |  |  | 15 |  | 76 | use warnings; | 
|  | 15 |  |  |  |  | 26 |  | 
|  | 15 |  |  |  |  | 689 |  | 
| 3 |  |  |  |  |  |  | package HTML::Widget::Plugin::Radio; | 
| 4 |  |  |  |  |  |  | # ABSTRACT: a widget for sets of radio buttons | 
| 5 |  |  |  |  |  |  | $HTML::Widget::Plugin::Radio::VERSION = '0.204'; | 
| 6 | 15 |  |  | 15 |  | 70 | use parent 'HTML::Widget::Plugin'; | 
|  | 15 |  |  |  |  | 24 |  | 
|  | 15 |  |  |  |  | 80 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | #pod =head1 SYNOPSIS | 
| 9 |  |  |  |  |  |  | #pod | 
| 10 |  |  |  |  |  |  | #pod   $widget_factory->radio({ | 
| 11 |  |  |  |  |  |  | #pod     name    => 'radio', | 
| 12 |  |  |  |  |  |  | #pod     value   => 'value_1', | 
| 13 |  |  |  |  |  |  | #pod     options => [ | 
| 14 |  |  |  |  |  |  | #pod       [ value_1 => "Description 1" ], | 
| 15 |  |  |  |  |  |  | #pod       [ value_2 => "Description 2" ], | 
| 16 |  |  |  |  |  |  | #pod       [ value_2 => "Description 2", 'optional-elem-id' ], | 
| 17 |  |  |  |  |  |  | #pod     ], | 
| 18 |  |  |  |  |  |  | #pod   }); | 
| 19 |  |  |  |  |  |  | #pod | 
| 20 |  |  |  |  |  |  | #pod This will emit roughly: | 
| 21 |  |  |  |  |  |  | #pod | 
| 22 |  |  |  |  |  |  | #pod | 
| 23 |  |  |  |  |  |  | #pod   checked='checked'> | 
| 24 |  |  |  |  |  |  | #pod | 
| 25 |  |  |  |  |  |  | #pod | 
| 26 |  |  |  |  |  |  | #pod | 
| 27 |  |  |  |  |  |  | #pod | 
| 28 |  |  |  |  |  |  | #pod | 
| 29 |  |  |  |  |  |  | #pod | 
| 30 |  |  |  |  |  |  | #pod   id='optional-elem-id'> | 
| 31 |  |  |  |  |  |  | #pod | 
| 32 |  |  |  |  |  |  | #pod | 
| 33 |  |  |  |  |  |  | #pod =head1 DESCRIPTION | 
| 34 |  |  |  |  |  |  | #pod | 
| 35 |  |  |  |  |  |  | #pod This plugin provides a radio button-set widget | 
| 36 |  |  |  |  |  |  | #pod | 
| 37 |  |  |  |  |  |  | #pod =cut | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 15 |  |  | 15 |  | 951 | use HTML::Element; | 
|  | 15 |  |  |  |  | 34 |  | 
|  | 15 |  |  |  |  | 159 |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | #pod =head1 METHODS | 
| 42 |  |  |  |  |  |  | #pod | 
| 43 |  |  |  |  |  |  | #pod =head2 C< provided_widgets > | 
| 44 |  |  |  |  |  |  | #pod | 
| 45 |  |  |  |  |  |  | #pod This plugin provides the following widgets: radio | 
| 46 |  |  |  |  |  |  | #pod | 
| 47 |  |  |  |  |  |  | #pod =cut | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 16 |  |  | 16 | 1 | 46 | sub provided_widgets { qw(radio) } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | #pod =head2 C< radio > | 
| 52 |  |  |  |  |  |  | #pod | 
| 53 |  |  |  |  |  |  | #pod This method returns a set of radio buttons. | 
| 54 |  |  |  |  |  |  | #pod | 
| 55 |  |  |  |  |  |  | #pod In addition to the generic L attributes, the following | 
| 56 |  |  |  |  |  |  | #pod are valid arguments: | 
| 57 |  |  |  |  |  |  | #pod | 
| 58 |  |  |  |  |  |  | #pod =over | 
| 59 |  |  |  |  |  |  | #pod | 
| 60 |  |  |  |  |  |  | #pod =item disabled | 
| 61 |  |  |  |  |  |  | #pod | 
| 62 |  |  |  |  |  |  | #pod If true, this option indicates that the select widget can't be changed by the | 
| 63 |  |  |  |  |  |  | #pod user. | 
| 64 |  |  |  |  |  |  | #pod | 
| 65 |  |  |  |  |  |  | #pod =item ignore_invalid | 
| 66 |  |  |  |  |  |  | #pod | 
| 67 |  |  |  |  |  |  | #pod If this is given and true, an invalid value is ignored instead of throwing an | 
| 68 |  |  |  |  |  |  | #pod exception. | 
| 69 |  |  |  |  |  |  | #pod | 
| 70 |  |  |  |  |  |  | #pod =item options | 
| 71 |  |  |  |  |  |  | #pod | 
| 72 |  |  |  |  |  |  | #pod This option must be a reference to an array of allowed values, each of which | 
| 73 |  |  |  |  |  |  | #pod will get its own radio button. | 
| 74 |  |  |  |  |  |  | #pod | 
| 75 |  |  |  |  |  |  | #pod =item value | 
| 76 |  |  |  |  |  |  | #pod | 
| 77 |  |  |  |  |  |  | #pod If this argument is given, the option with this value will be pre-selected in | 
| 78 |  |  |  |  |  |  | #pod the widget's initial state. | 
| 79 |  |  |  |  |  |  | #pod | 
| 80 |  |  |  |  |  |  | #pod An exception will be thrown if more or less than one of the provided options | 
| 81 |  |  |  |  |  |  | #pod has this value. | 
| 82 |  |  |  |  |  |  | #pod | 
| 83 |  |  |  |  |  |  | #pod =back | 
| 84 |  |  |  |  |  |  | #pod | 
| 85 |  |  |  |  |  |  | #pod =cut | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 16 |  |  | 16 |  | 60 | sub _attribute_args { qw(disabled) } | 
| 88 | 32 |  |  | 32 |  | 99 | sub _boolean_args   { qw(disabled) } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub radio { | 
| 91 | 8 |  |  | 8 | 1 | 14 | my ($self, $factory, $arg) = @_; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 8 |  |  |  |  | 8 | my @widgets; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | $self->validate_value($arg->{value}, $arg->{options}) | 
| 96 | 8 | 100 |  |  |  | 31 | unless $arg->{ignore_invalid}; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 6 | 100 |  |  |  | 19 | if (my $id_attr = delete $arg->{attr}{id}) { | 
| 99 | 3 |  |  |  |  | 504 | Carp::cluck "id may not be used as a widget-level attribute for radio"; | 
| 100 | 3 | 100 |  |  |  | 363 | $arg->{attr}{name} = $id_attr if not defined $arg->{attr}{name}; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 6 |  |  |  |  | 8 | for my $option (@{ $arg->{options} }) { | 
|  | 6 |  |  |  |  | 15 |  | 
| 104 | 18 | 100 |  |  |  | 54 | my ($value, $text, $id) = (ref $option) ? (@$option) : (($option) x 2); | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 18 |  |  |  |  | 61 | my $widget = HTML::Element->new('input', type => 'radio'); | 
| 107 | 18 |  |  |  |  | 429 | $widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} }; | 
|  | 18 |  |  |  |  | 85 |  | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | $id = "$arg->{attr}{name}-$value" | 
| 110 | 18 | 100 | 66 |  |  | 282 | if ! defined $id and defined $arg->{attr}{name}; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 18 | 50 |  |  |  | 63 | $widget->attr(id => $id) if defined $id; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 18 |  |  |  |  | 206 | $widget->attr(value => $value); | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | $widget->attr(checked => 'checked') | 
| 117 | 18 | 100 | 100 |  |  | 282 | if defined $arg->{value} and $arg->{value} eq $value; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 18 |  |  |  |  | 64 | push @widgets, $widget; | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 18 |  |  |  |  | 48 | my $text_elem = HTML::Element->new('~literal', text => $text); | 
| 122 | 18 | 50 | 33 |  |  | 457 | if (! $arg->{parts} and defined $id) { | 
| 123 | 18 |  |  |  |  | 51 | my $label = HTML::Element->new(label => (for => $id)); | 
| 124 | 18 |  |  |  |  | 545 | $label->push_content($text_elem); | 
| 125 | 18 |  |  |  |  | 275 | push @widgets, $label; | 
| 126 |  |  |  |  |  |  | } else { | 
| 127 | 0 |  |  |  |  | 0 | push @widgets, $text_elem; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | # XXX document | 
| 132 | 6 | 0 | 33 |  |  | 15 | return @widgets if wantarray and $arg->{parts}; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 6 |  |  |  |  | 11 | return join q{}, map { $_->as_XML } @widgets; | 
|  | 36 |  |  |  |  | 6134 |  | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | #pod =head2 C< validate_value > | 
| 138 |  |  |  |  |  |  | #pod | 
| 139 |  |  |  |  |  |  | #pod This method checks whether the given value option is valid.  See C> | 
| 140 |  |  |  |  |  |  | #pod for an explanation of its default rules. | 
| 141 |  |  |  |  |  |  | #pod | 
| 142 |  |  |  |  |  |  | #pod =cut | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | sub validate_value { | 
| 145 | 7 |  |  | 7 | 1 | 12 | my ($class, $value, $options) = @_; | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 7 | 100 |  |  |  | 12 | my @options = map { ref $_ ? $_->[0] : $_ } @$options; | 
|  | 21 |  |  |  |  | 60 |  | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 7 | 100 |  |  |  | 20 | if (defined $value) { | 
| 150 | 6 |  |  |  |  | 9 | my $matches = grep { $value eq $_ } @options; | 
|  | 18 |  |  |  |  | 37 |  | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 6 | 100 |  |  |  | 24 | if (not $matches) { | 
|  |  | 100 |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | Carp::croak "provided value '$value' not in given options: " | 
| 154 | 1 |  |  |  |  | 4 | . join(q{ }, map { "'$_'" } @options); | 
|  | 3 |  |  |  |  | 153 |  | 
| 155 |  |  |  |  |  |  | } elsif ($matches > 1) { | 
| 156 | 1 |  |  |  |  | 111 | Carp::croak "provided value '$value' matches more than one option"; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | 1; | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | __END__ |