File Coverage

blib/lib/Rose/HTML/Form/Field/Option/Container.pm
Criterion Covered Total %
statement 67 87 77.0
branch 24 32 75.0
condition 17 22 77.2
subroutine 17 32 53.1
pod 17 22 77.2
total 142 195 72.8


line stmt bran cond sub pod time code
1             package Rose::HTML::Form::Field::Option::Container;
2              
3 11     11   86 use strict;
  11         25  
  11         322  
4              
5 11     11   54 use Carp();
  11         23  
  11         190  
6              
7 11     11   54 use base 'Rose::HTML::Form::Field::Group::OnOff';
  11         22  
  11         5107  
8              
9 11     11   88 use Rose::HTML::Form::Field::Group;
  11         29  
  11         104  
10              
11             require Rose::HTML::Form::Field::Option;
12             require Rose::HTML::Form::Field::OptionGroup;
13              
14             our $VERSION = '0.606';
15              
16 62     62   319 sub _item_class { shift->object_type_class_loaded('option') }
17 916     916   2772 sub _item_group_class { shift->object_type_class_loaded('option group') }
18 0     0   0 sub _item_name { 'option' }
19 0     0   0 sub _item_name_plural { 'options' }
20              
21             *options = \&Rose::HTML::Form::Field::Group::items;
22             *options_localized = \&Rose::HTML::Form::Field::Group::items_localized;
23             *option = \&Rose::HTML::Form::Field::Group::OnOff::item;
24             *option_group = \&Rose::HTML::Form::Field::Group::OnOff::item_group;
25             *visible_options = \&Rose::HTML::Form::Field::Group::visible_items;
26              
27             *add_options = \&Rose::HTML::Form::Field::Group::add_items;
28             *add_option = \&add_options;
29             *add_options_localized = \&Rose::HTML::Form::Field::Group::add_items_localized;
30             *add_option_localized = \&add_options_localized;
31              
32             *add_options_localized = \&Rose::HTML::Form::Field::Group::add_items_localized;
33             *add_option_localized = \&Rose::HTML::Form::Field::Group::add_item_localized;
34              
35             *choices = \&options;
36             *choices_localized = \&options_localized;
37              
38             *_args_to_items = \&Rose::HTML::Form::Field::Group::_args_to_items;
39              
40             *show_all_options = \&Rose::HTML::Form::Field::Group::show_all_items;
41             *hide_all_options = \&Rose::HTML::Form::Field::Group::hide_all_items;
42              
43             *delete_option = \&Rose::HTML::Form::Field::Group::delete_item;
44             *delete_options = \&Rose::HTML::Form::Field::Group::delete_items;
45              
46             *delete_option_group = \&Rose::HTML::Form::Field::Group::delete_item_group;
47             *delete_option_groups = \&Rose::HTML::Form::Field::Group::delete_item_groups;
48              
49 0     0 1 0 sub html_element { 'select' }
50 0     0 1 0 sub xhtml_element { 'select' }
51              
52             #sub name { shift->html_attr('name', @_) }
53              
54 0     0 0 0 sub is_flat_group { 0 }
55              
56             sub is_empty
57             {
58 11     11   100 no warnings 'uninitialized';
  11         32  
  11         4408  
59 503 100   503 1 1447 return (grep { /\S/ } shift->internal_value) ? 0 : 1;
  503         2688  
60             }
61              
62             sub children
63             {
64 12     12 1 2594 my($self) = shift;
65 12 50       41 Carp::croak "Cannot set children() for an option container ($_[0]). Use options() instead." if(@_);
66 12         44 return $self->options;
67             }
68              
69 0     0 1 0 sub push_children { shift->add_items(@_) }
70 0     0 1 0 sub push_child { shift->add_item(@_) }
71              
72 0     0 1 0 sub pop_children { shift->pop_items(@_) }
73 0     0 1 0 sub pop_child { shift->pop_item(@_) }
74              
75 0     0 1 0 sub shift_children { shift->add_items(@_) }
76 0     0 1 0 sub shift_child { shift->add_item(@_) }
77              
78 0     0 1 0 sub unshift_children { shift->unshift_items(@_) }
79 0     0 1 0 sub unshift_child { shift->unshift_item(@_) }
80              
81             sub child
82             {
83 0     0 1 0 my($self, $index) = @_;
84 0   0     0 my $items = $self->items || [];
85 0         0 return $items->[$index];
86             }
87              
88             sub delete_child_at_index
89             {
90 0     0 0 0 my($self) = shift;
91 0 0       0 Carp::croak "Missing array index" unless(@_);
92 0   0     0 my $items = $self->items || [];
93 11     11   119 no warnings;
  11         37  
  11         8953  
94 0         0 splice(@$items, $_[0], 1);
95             }
96              
97             sub html_field
98             {
99 105     105 1 229 my($self) = shift;
100              
101 105         188 my $html;
102              
103 105 100 100     349 if($self->apply_error_class && defined $self->error)
104             {
105 2         8 my $class = $self->html_attr('class');
106 2 50       12 $self->html_attr(class => $class ? "$class error" : 'error');
107             $html = $self->start_html . "\n" .
108 2         8 join("\n", map { $_->html_field } $self->visible_items) . "\n" .
  6         19  
109             $self->end_html;
110 2         10 $self->html_attr(class => $class);
111 2         8 return $html;
112             }
113             else
114             {
115             return $self->start_html . "\n" .
116 103         543 join("\n", map { $_->html_field } $self->visible_items) . "\n" .
  443         1263  
117             $self->end_html;
118             }
119             }
120              
121             sub xhtml_field
122             {
123 43     43 1 110 my($self) = shift;
124              
125 43         78 my $xhtml;
126              
127 43 100 100     129 if($self->apply_error_class && defined $self->error)
128             {
129 2         10 my $class = $self->html_attr('class');
130 2 50       14 $self->html_attr(class => $class ? "$class error" : 'error');
131             $xhtml = $self->start_xhtml . "\n" .
132 2         15 join("\n", map { $_->xhtml_field } $self->visible_items) . "\n" .
  6         26  
133             $self->end_xhtml;
134 2         10 $self->html_attr(class => $class);
135 2         8 return $xhtml;
136             }
137             else
138             {
139             return $self->start_xhtml . "\n" .
140 41         231 join("\n", map { $_->xhtml_field } $self->visible_items) . "\n" .
  138         421  
141             $self->end_xhtml;
142             }
143             }
144              
145             # sub html_field
146             # {
147             # my($self) = shift;
148             # $self->contents("\n" . join("\n", map { $_->html_field } $self->visible_options) . "\n");
149             # return $self->_html_tag(@_);
150             # }
151             #
152             # sub xhtml_field
153             # {
154             # my($self) = shift;
155             # $self->contents("\n" . join("\n", map { $_->xhtml_field } $self->visible_options) . "\n");
156             # return $self->_xhtml_tag(@_);
157             # }
158              
159             sub input_value
160             {
161 85     85 1 240 my($self) = shift;
162              
163 85 100 100     568 if(@_ && (@_ > 1 || (ref $_[0] eq 'ARRAY' && @{$_[0]} > 1)) && !$self->multiple)
      100        
      100        
164             {
165 1         245 Carp::croak "Attempt to select multiple values in a non-multiple " . ref($self);
166             }
167              
168 84         393 my $values = $self->SUPER::input_value(@_);
169              
170 84 50 66     365 Carp::croak "Non-multiple ", ref($self), " has multiple values: ", join(', ', @$values)
171             if(@$values > 1 && !$self->multiple);
172              
173 84 100       442 return wantarray ? @$values : $values;
174             }
175              
176             sub hidden_fields
177             {
178 28     28 1 62 my($self) = shift;
179              
180 28         52 my @hidden;
181              
182 28         98 foreach my $item ($self->items)
183             {
184 156 100       421 if(defined $item->internal_value)
185             {
186             # Derek Watson suggests this conditional modifier, but
187             # I've yet to see the error is works around...
188 42 50       228 $hidden[-1]->name($self->name)
189             if(push(@hidden, $item->hidden_field));
190             }
191             }
192              
193 28 50       159 return (wantarray) ? @hidden : \@hidden;
194             }
195              
196             sub hidden
197             {
198 40     40 0 74 my($self) = shift;
199              
200 40 100       89 if(@_)
201             {
202 6 100       26 if($self->{'_hidden'} = shift(@_) ? 1 : 0)
    100          
203             {
204 3         12 foreach my $option ($self->options)
205             {
206 6         21 $option->selected(undef);
207             }
208             }
209             }
210              
211 40         126 return $self->{'_hidden'};
212             }
213              
214 1     1 0 6 sub hide { shift->hidden(1) }
215 1     1 0 6 sub show { shift->hidden(0) }
216              
217             1;
218