select.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« password.pm ↑ Summary table/basic.pm »
1 package HTML::GUI::select;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 HTML::GUI::select - Create and control a select input for webapp
9  
10 =head1 VERSION
11  
12 Version 0.01
13  
14 =cut
15  
16 our $VERSION = '0.01';
17  
18  
19 0 ▶use HTML::GUI::input;
Subroutine BEGIN: called CC=1 SCAR=2.6
20 our @ISA = qw(HTML::GUI::input);
21  
22 =head1 SELECT
23  
24 The select widget is made for choose an item in limited list.
25  
26 =cut
27  
28  
29 # Define the specific default values for a select widget
30 my %GHW_defaultValue = (options => []);
31  
32 #array of string : list of all specifric public properties of the widget
33 my @GHW_publicPropList = qw/options/;
34  
35 =head1 PUBLIC ATTRIBUTES
36  
37 =pod
38  
39  
40  
41 =cut
42  
43  
44 =head1 PUBLIC METHODS
45  
46 =pod
47  
48 =head3 new
49  
50 =cut
51  
52 sub new
53 {
54 my($class,
55 $params, # widget :
56 ) = @_;
57 $params->{type} = "select";
58 my $this = $class->SUPER::new($params);
59 if (!$this){
60 return undef;
61 }
62 $this->{options} = [];
63  
64 bless($this, $class);
65 if (exists $params->{options}){
66 $this->setOptions($params->{options});
67 }
68 return $this;
69 }
70  
71 =pod
72  
73 =head3 isNewValue
74 Description :
75 return 1 if the value already exist in the widget's options
76 else return 0
77  
78 parameter :
79 the newValue (scalar) to test
80 =cut
81  
82 sub isNewValue($$)
83 {
84 my ($this,$newValue) = @_;
85  
86 foreach my $option (@{$this->{options}}){
87 if ($newValue eq $option->{value}){
88 return 0;
89 }
90 }
91  
92 return 1;
93 }
94  
95 =pod
96  
97 =head3 setOptions
98  
99 Description :
100 Define the options of the select box.
101  
102 parameters :
103 $options is a ref to an array of hash ref
104 [{ value => "myvalue",
105 label => "my label},
106 { value => "myvalue2",
107 label => "my second label}]
108 =cut
109  
110 sub setOptions
111 {
112 my ($this,$options) = @_;
113 if (!$options || ref $options ne 'ARRAY'){
114 $this->error("priv",{
115 type=>'incorrect use of API',
116 'explanation' => '$option should be an ARRAY ref',
117 option => $options});
118
119 }
120 foreach my $oneOption (@$options){
121 if (ref $oneOption ne "HASH"
122 || !exists $oneOption->{value}
123 || !exists $oneOption->{label}){
124 #wrong data structure !!
125 $this->error("priv",{
126 type=>'incorrect use of API',
127 'explanation' => '$oneOption should be a hash ref '
128 .'with ˝value" and "label" keys',
129 option => $oneOption});
130 next;
131  
132 }
133 $oneOption->{value} ||= '';
134 $oneOption->{label} ||= '';
135 if (!$this->isNewValue($oneOption->{value}) ){
136 #value already used !!!
137 $this->error("priv",{
138 type=>'incorrect use of API',
139 'explanation' => 'the value of $oneOption is already '
140 .'present in the widget',
141 option => $oneOption});
142 next;
143 }
144 push @{$this->{options}} , $oneOption;
145 }
146  
147 }
148  
149 =pod
150  
151 =head3 getDefinitionData
152
153 This method is the specialisation of the widget.pm method, refer to the widget.pm manual for more information.
154  
155 =cut
156 sub getDefinitionData($)
157 {
158 my ($self) = @_;
159  
160 my $publicProperties = $self->SUPER::getDefinitionData();
161
162 return $self->SUPER::getDefinitionData($publicProperties,
163 \%GHW_defaultValue,\@GHW_publicPropList);
164 }
165  
166  
167 =pod
168  
169 =head3 getNudeHtml
170  
171 Description :
172 Return the html of the widget to be inserted in a <p> tag or a a table.
173  
174 =cut
175  
176 sub getNudeHtml
177 {
178 my($self) = @_;
179 my %tagProp=();
180 my %styleProp=();
181
182  
183 if (exists $self->{display} && 0==$self->{display}){
184 $styleProp{display} = 'none';
185 }
186  
187 $tagProp{style} = $self->getStyleContent(\%styleProp);
188 $tagProp{name} = $tagProp{id} = $self->{id};
189 $tagProp{size} = '1';
190  
191 my $optionHtml = '';
192 my $currentValue = $self->getValue();
193 foreach my $option (@{$self->{options}}){
194 my $optionProp = {};
195  
196 if (defined $currentValue
197 && ($currentValue eq $option->{value})){
198 $optionProp->{selected} = 'selected';
199 }
200 $optionProp->{value} = $option->{value};
201  
202 $optionHtml .= $self->getHtmlTag("option",
203 $optionProp,
204 $self->escapeHtml($option->{label}));
205
206 }
207
208 return $self->getHtmlTag("select", \%tagProp,$optionHtml);
209 }
210  
211  
212 =head1 AUTHOR
213  
214 Jean-Christian Hassler, C<< <jhassler at free.fr> >>
215  
216 =head1 BUGS
217  
218 Please report any bugs or feature requests to
219 C<bug-gui-libhtml-select at rt.cpan.org>, or through the web interface at
220 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-GUI-widget>.
221 I will be notified, and then you'll automatically be notified of progress on
222 your bug as I make changes.
223  
224 =head1 SUPPORT
225  
226 You can find documentation for this module with the perldoc command.
227  
228 perldoc HTML::GUI::widget
229  
230 You can also look for information at:
231  
232 =over 4
233  
234 =item * AnnoCPAN: Annotated CPAN documentation
235  
236 L<http://annocpan.org/dist/HTML-GUI-widget>
237  
238 =item * CPAN Ratings
239  
240 L<http://cpanratings.perl.org/d/HTML-GUI-widget>
241  
242 =item * RT: CPAN's request tracker
243  
244 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-GUI-widget>
245  
246 =item * Search CPAN
247  
248 L<http://search.cpan.org/dist/HTML-GUI-widget>
249  
250 =back
251  
252 =head1 ACKNOWLEDGEMENTS
253  
254 =head1 COPYRIGHT & LICENSE
255  
256 Copyright 2007 Jean-Christian Hassler, all rights reserved.
257  
258 This program is free software; you can redistribute it and/or modify it
259 under the same terms as Perl itself.
260  
261 =cut
262  
263 1; # End of HTML::GUI::select
« password.pm ↑ Summary table/basic.pm »