File Coverage

blib/lib/XHTML/Instrumented/Form/Select.pm
Criterion Covered Total %
statement 21 110 19.0
branch 0 52 0.0
condition 0 20 0.0
subroutine 7 19 36.8
pod 12 12 100.0
total 40 213 18.7


line stmt bran cond sub pod time code
1 2     2   11 use strict;
  2         3  
  2         75  
2 2     2   11 use warnings;
  2         3  
  2         84  
3              
4             package
5             XHTML::Instrumented::Form::Select;
6              
7 2     2   10 use base 'XHTML::Instrumented::Form::ElementControl';
  2         4  
  2         1156  
8              
9 2     2   14 use Params::Validate qw( validate ARRAYREF );
  2         5  
  2         110  
10              
11 2     2   12 use Carp qw(croak);
  2         4  
  2         86  
12              
13 2     2   1359 use XHTML::Instrumented::Form::Option;
  2         5  
  2         518  
14              
15             sub name
16             {
17 0     0 1   my $self = shift;
18              
19 0           $self->{name};
20             }
21              
22             sub new
23             {
24 0     0 1   my $class = shift;
25 0           my %p = validate(@_, {
26             name => 1,
27             type => 1,
28             value => 0,
29             required => 0,
30             default => 0,
31             values => 0,
32             # multiple => 0,
33             onclick => 0,
34             onchange => 0,
35             class => 0,
36             data => {
37             optional => 1,
38             type => ARRAYREF,
39             },
40             });
41              
42 0 0         if ($p{type} eq 'select') {
43 0           my $data = $p{data};
44 0           for my $option (@$data) {
45 0 0         unless (defined $option->{value}) {
46 0           $option->{value} = $option->{text};
47             }
48 0           bless($option, 'XHTML::Instrumented::Form::Option');
49             }
50             }
51              
52 0           bless({ %p }, $class);
53             }
54              
55             sub set_select_data
56             {
57 0     0 1   my $self = shift;
58              
59 0           my @new;
60 0           for my $option (@_) {
61 0 0         unless (defined $option->{value}) {
62 0           $option->{value} = $option->{text};
63             }
64 0 0         unless (defined $option->{text}) {
65 0           $option->{text} = $option->{value};
66             }
67 2     2   12 use Data::Dumper;
  2         4  
  2         1952  
68 0 0         croak Dumper([@_]) . 'set_select_data' unless defined $option->{text};
69 0           push(@new, bless $option,'XHTML::Instrumented::Form::Option');
70             }
71 0           $self->{data} = \@new;
72             }
73              
74             sub set_default
75             {
76 0     0 1   my $self = shift;
77              
78 0           $self->{default} = shift;
79             }
80              
81             sub options
82             {
83 0     0 1   my $self = shift;
84              
85 0 0         map({ $_; } (@{$self->{data} || []}));
  0            
  0            
86             }
87              
88             sub elements
89             {
90 0     0 1   my $self = shift;
91              
92 0 0         map({ $_->{value} || $_->{text} } @{$self->{data} || []});
  0 0          
  0            
93             }
94              
95             sub values
96             {
97 0     0 1   my $self = shift;
98              
99 0 0         map({ $_->{value} || $_->{text} } @{$self->{values} || []});
  0 0          
  0            
100             }
101              
102             sub type
103             {
104 0     0 1   my $self = shift;
105              
106 0           $self->{type};
107             }
108              
109             sub exp_args
110             {
111 0     0 1   my $self = shift;
112 0 0         die caller if ref $_[0];
113 0           my @extra = ();
114              
115 0 0         if ($self->{multiple}) {
116 0           push(@extra, 'multiple', 'multiple');
117             }
118 0 0         if (my $data = $self->{onclick}) {
119 0           push(@extra, 'onclick', $data);
120             }
121              
122 0           my $ret = $self->SUPER::exp_args(@_, name => $self->name, @extra);
123              
124 0           $ret;
125             }
126              
127             sub expand_content
128             {
129 0     0 1   my $self = shift;
130              
131 0   0       $self->{value} ||= $self->{default};
132 0           require XHTML::Instrumented::Entry;
133 0           for my $option (@{$self->{data}}) {
  0            
134 0 0 0       if ($self->{value} && $option->{value}
      0        
135             && $self->{value} eq $option->{value}) {
136 0           $option->{selected} = 1;
137             }
138             }
139              
140 0   0       my $value = $self->{value} || $self->{default};
141              
142 0           my @ret;
143 0 0         if (@{$self->{data}||[]}) {
  0 0          
144 0           @ret = map({ XHTML::Instrumented::Entry->new(
  0            
145             tag => 'option',
146             flags => {},
147             args => { $_->as_args },
148             data => [ $_->{text} ],
149             ),
150 0           } @{$self->{data}}
151             );
152             } else {
153 0           for my $entry (@_) {
154 0 0         if (ref($entry)) {
155 0           my $entry_value = $entry->{args}{value};
156              
157 0 0 0       if ($value && $entry_value && $value eq $entry_value) {
      0        
158 0           $entry->{args}{selected} = 'selected';
159             }
160 0           push(@ret, $entry);
161             }
162             }
163             }
164 0           @ret;
165             }
166              
167             sub set_value
168             {
169 0     0 1   my $self = shift;
170 0           my $value = shift;
171 0 0         die if @_;
172              
173 0 0         if (my $type = ref($value)) {
174 0 0         die unless $type eq 'ARRAY';
175 0 0         if (@{$value} == 1) {
  0 0          
  0            
176 0           $self->{value} = $value->[0];
177             } elsif (@{$value} == 0) {
178 0           $self->{value} = '';
179             } else {
180 0           die 'bad data ' . "@{$value}";
  0            
181             }
182             } else {
183 0           $self->{value} = $value;
184             }
185 0           $value = $self->{value};
186 0 0         if (defined $value) {
187 0 0         for my $element (@{$self->{data} || []}) {
  0            
188 0           delete $element->{selected};
189 0 0 0       if ($value eq ($element->value || '')) {
190 0           $element->{selected} = 1;
191             }
192             }
193             }
194             }
195              
196             sub value
197             {
198 0     0 1   my $self = shift;
199              
200 0           $self->SUPER::value();
201             }
202              
203             1;
204             __END__