line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
15
|
|
|
15
|
|
11223
|
use strict; |
|
15
|
|
|
|
|
32
|
|
|
15
|
|
|
|
|
529
|
|
2
|
15
|
|
|
15
|
|
84
|
use warnings; |
|
15
|
|
|
|
|
26
|
|
|
15
|
|
|
|
|
745
|
|
3
|
|
|
|
|
|
|
package HTML::Widget::Plugin::Radio; |
4
|
|
|
|
|
|
|
# ABSTRACT: a widget for sets of radio buttons |
5
|
|
|
|
|
|
|
$HTML::Widget::Plugin::Radio::VERSION = '0.202'; |
6
|
15
|
|
|
15
|
|
79
|
use parent 'HTML::Widget::Plugin'; |
|
15
|
|
|
|
|
24
|
|
|
15
|
|
|
|
|
78
|
|
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
|
|
1085
|
use HTML::Element; |
|
15
|
|
|
|
|
24
|
|
|
15
|
|
|
|
|
235
|
|
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
|
56
|
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
|
|
58
|
sub _attribute_args { qw(disabled) } |
88
|
32
|
|
|
32
|
|
118
|
sub _boolean_args { qw(disabled) } |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub radio { |
91
|
8
|
|
|
8
|
1
|
16
|
my ($self, $factory, $arg) = @_; |
92
|
|
|
|
|
|
|
|
93
|
8
|
|
|
|
|
14
|
my @widgets; |
94
|
|
|
|
|
|
|
|
95
|
8
|
100
|
|
|
|
44
|
$self->validate_value($arg->{value}, $arg->{options}) |
96
|
|
|
|
|
|
|
unless $arg->{ignore_invalid}; |
97
|
|
|
|
|
|
|
|
98
|
6
|
100
|
|
|
|
22
|
if (my $id_attr = delete $arg->{attr}{id}) { |
99
|
3
|
|
|
|
|
662
|
Carp::cluck "id may not be used as a widget-level attribute for radio"; |
100
|
3
|
100
|
|
|
|
448
|
$arg->{attr}{name} = $id_attr if not defined $arg->{attr}{name}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
6
|
|
|
|
|
12
|
for my $option (@{ $arg->{options} }) { |
|
6
|
|
|
|
|
16
|
|
104
|
18
|
100
|
|
|
|
58
|
my ($value, $text, $id) = (ref $option) ? (@$option) : (($option) x 2); |
105
|
|
|
|
|
|
|
|
106
|
18
|
|
|
|
|
77
|
my $widget = HTML::Element->new('input', type => 'radio'); |
107
|
18
|
|
|
|
|
515
|
$widget->attr($_ => $arg->{attr}{$_}) for keys %{ $arg->{attr} }; |
|
18
|
|
|
|
|
113
|
|
108
|
|
|
|
|
|
|
|
109
|
18
|
100
|
66
|
|
|
336
|
$id = "$arg->{attr}{name}-$value" |
110
|
|
|
|
|
|
|
if ! defined $id and defined $arg->{attr}{name}; |
111
|
|
|
|
|
|
|
|
112
|
18
|
50
|
|
|
|
68
|
$widget->attr(id => $id) if defined $id; |
113
|
|
|
|
|
|
|
|
114
|
18
|
|
|
|
|
220
|
$widget->attr(value => $value); |
115
|
|
|
|
|
|
|
|
116
|
18
|
100
|
100
|
|
|
281
|
$widget->attr(checked => 'checked') |
117
|
|
|
|
|
|
|
if defined $arg->{value} and $arg->{value} eq $value; |
118
|
|
|
|
|
|
|
|
119
|
18
|
|
|
|
|
67
|
push @widgets, $widget; |
120
|
|
|
|
|
|
|
|
121
|
18
|
|
|
|
|
55
|
my $text_elem = HTML::Element->new('~literal', text => $text); |
122
|
18
|
50
|
33
|
|
|
508
|
if (! $arg->{parts} and defined $id) { |
123
|
18
|
|
|
|
|
58
|
my $label = HTML::Element->new(label => (for => $id)); |
124
|
18
|
|
|
|
|
457
|
$label->push_content($text_elem); |
125
|
18
|
|
|
|
|
311
|
push @widgets, $label; |
126
|
|
|
|
|
|
|
} else { |
127
|
0
|
|
|
|
|
0
|
push @widgets, $text_elem; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# XXX document |
132
|
6
|
50
|
33
|
|
|
21
|
return @widgets if wantarray and $arg->{parts}; |
133
|
|
|
|
|
|
|
|
134
|
6
|
|
|
|
|
17
|
return join q{}, map { $_->as_XML } @widgets; |
|
36
|
|
|
|
|
6586
|
|
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
|
17
|
my ($class, $value, $options) = @_; |
146
|
|
|
|
|
|
|
|
147
|
7
|
100
|
|
|
|
18
|
my @options = map { ref $_ ? $_->[0] : $_ } @$options; |
|
21
|
|
|
|
|
78
|
|
148
|
|
|
|
|
|
|
|
149
|
7
|
100
|
|
|
|
25
|
if (defined $value) { |
150
|
6
|
|
|
|
|
15
|
my $matches = grep { $value eq $_ } @options; |
|
18
|
|
|
|
|
39
|
|
151
|
|
|
|
|
|
|
|
152
|
6
|
100
|
|
|
|
31
|
if (not $matches) { |
|
|
100
|
|
|
|
|
|
153
|
3
|
|
|
|
|
222
|
Carp::croak "provided value '$value' not in given options: " |
154
|
1
|
|
|
|
|
4
|
. join(q{ }, map { "'$_'" } @options); |
155
|
|
|
|
|
|
|
} elsif ($matches > 1) { |
156
|
1
|
|
|
|
|
152
|
Carp::croak "provided value '$value' matches more than one option"; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
1; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
__END__ |