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   88 use strict;
  11         26  
  11         379  
4              
5 11     11   60 use Carp();
  11         22  
  11         330  
6              
7 11     11   55 use base 'Rose::HTML::Form::Field::Group::OnOff';
  11         23  
  11         6161  
8              
9 11     11   89 use Rose::HTML::Form::Field::Group;
  11         28  
  11         138  
10              
11             require Rose::HTML::Form::Field::Option;
12             require Rose::HTML::Form::Field::OptionGroup;
13              
14             our $VERSION = '0.606';
15              
16 63     63   405 sub _item_class { shift->object_type_class_loaded('option') }
17 823     823   3670 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   87 no warnings 'uninitialized';
  11         30  
  11         5354  
59 407 100   407 1 1558 return (grep { /\S/ } shift->internal_value) ? 0 : 1;
  407         2612  
60             }
61              
62             sub children
63             {
64 12     12 1 2209 my($self) = shift;
65 12 50       44 Carp::croak "Cannot set children() for an option container ($_[0]). Use options() instead." if(@_);
66 12         76 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   129 no warnings;
  11         27  
  11         10401  
94 0         0 splice(@$items, $_[0], 1);
95             }
96              
97             sub html_field
98             {
99 105     105 1 302 my($self) = shift;
100              
101 105         215 my $html;
102              
103 105 100 100     541 if($self->apply_error_class && defined $self->error)
104             {
105 2         12 my $class = $self->html_attr('class');
106 2 50       2307 $self->html_attr(class => $class ? "$class error" : 'error');
107             $html = $self->start_html . "\n" .
108 2         11 join("\n", map { $_->html_field } $self->visible_items) . "\n" .
  6         23  
109             $self->end_html;
110 2         13 $self->html_attr(class => $class);
111 2         11 return $html;
112             }
113             else
114             {
115             return $self->start_html . "\n" .
116 103         701 join("\n", map { $_->html_field } $self->visible_items) . "\n" .
  443         1537  
117             $self->end_html;
118             }
119             }
120              
121             sub xhtml_field
122             {
123 43     43 1 127 my($self) = shift;
124              
125 43         86 my $xhtml;
126              
127 43 100 100     274 if($self->apply_error_class && defined $self->error)
128             {
129 2         13 my $class = $self->html_attr('class');
130 2 50       13 $self->html_attr(class => $class ? "$class error" : 'error');
131             $xhtml = $self->start_xhtml . "\n" .
132 2         14 join("\n", map { $_->xhtml_field } $self->visible_items) . "\n" .
  6         20  
133             $self->end_xhtml;
134 2         11 $self->html_attr(class => $class);
135 2         33 return $xhtml;
136             }
137             else
138             {
139             return $self->start_xhtml . "\n" .
140 41         340 join("\n", map { $_->xhtml_field } $self->visible_items) . "\n" .
  138         527  
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     689 if(@_ && (@_ > 1 || (ref $_[0] eq 'ARRAY' && @{$_[0]} > 1)) && !$self->multiple)
      100        
      100        
164             {
165 1         267 Carp::croak "Attempt to select multiple values in a non-multiple " . ref($self);
166             }
167              
168 84         450 my $values = $self->SUPER::input_value(@_);
169              
170 84 50 66     410 Carp::croak "Non-multiple ", ref($self), " has multiple values: ", join(', ', @$values)
171             if(@$values > 1 && !$self->multiple);
172              
173 84 100       515 return wantarray ? @$values : $values;
174             }
175              
176             sub hidden_fields
177             {
178 28     28 1 68 my($self) = shift;
179              
180 28         63 my @hidden;
181              
182 28         134 foreach my $item ($self->items)
183             {
184 156 100       607 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       234 $hidden[-1]->name($self->name)
189             if(push(@hidden, $item->hidden_field));
190             }
191             }
192              
193 28 50       248 return (wantarray) ? @hidden : \@hidden;
194             }
195              
196             sub hidden
197             {
198 40     40 0 88 my($self) = shift;
199              
200 40 100       116 if(@_)
201             {
202 6 100       30 if($self->{'_hidden'} = shift(@_) ? 1 : 0)
    100          
203             {
204 3         15 foreach my $option ($self->options)
205             {
206 6         24 $option->selected(undef);
207             }
208             }
209             }
210              
211 40         168 return $self->{'_hidden'};
212             }
213              
214 1     1 0 6 sub hide { shift->hidden(1) }
215 1     1 0 5 sub show { shift->hidden(0) }
216              
217             1;
218