Quiq/Html/Widget/SelectMenu.pm

filter: Statementstmt 61.0% 47 / 77 Branchbran 32.1% 9 / 28 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 52.6% 69 / 131 CC 18 SCAR 32.0
CC
18
Cov
51%
html
42.7
new
19.9
BEGIN
1.0
CRAP 24.5
« Quiq/AsciiTable.pm ↑ Summary Quiq/Option.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Html::Widget::SelectMenu - Liste mit Einzelauswahl
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Html::Widget>
12  
13 =head1 ATTRIBUTES
14  
15 =over 4
16  
17 =item id => $id (Default: undef)
18  
19 CSS Id.
20  
21 =item class => $class (Default: undef)
22  
23 CSS Klasse.
24  
25 =item style => $style (Default: undef)
26  
27 CSS Definition (inline).
28  
29 =item addNull => $bool (Default: 0)
30  
31 Wenn gesetzt, füge Auswahl für Nullwert ('') am Anfang der Liste hinzu.
32 Es erscheint der Text '---'.
33  
34 =item disabled => $bool (Default: 0)
35  
36 Widget kann nicht editiert werden.
37  
38 =item hidden => $bool (Default: 0)
39  
40 Widget ist (aktuell) unsichtbar.
41  
42 =item javaScript => $js (Default: undef)
43  
44 JavaScript-Code, der an den Widget-Code angehängt wird.
45  
46 =item name => $name (Default: undef)
47  
48 Name des Widget.
49  
50 =item undefIf => $bool (Default: 0)
51  
52 Wenn wahr, liefere C<undef> als Widget-Code.
53  
54 =item value => $str (Default: undef)
55  
56 Anfänglich ausgewählter Wert.
57  
58 =item onChange => $js (Default: undef)
59  
60 JavaScript-Code bei Änderung der Auswahl ausgeführt wird.
61  
62 =item options => \@opt (Default: [])
63  
64 Liste der möglichen Werte.
65  
66 =item optionPairs => \@pairs (Default: [])
67  
68 Liste der möglichen Werte und ihrer Anzeigetexte. Beispiel:
69  
70 optionPairs => [
71 0 => 'Nein',
72 1 => 'Ja',
73 2 => 'Vielleicht',
74 ]
75  
76 =item readonly => $bool (Default: 0)
77  
78 Zeige das Feld und seinen Wert unveränderbar an.
79  
80 =item texts => \@text (Default: [])
81  
82 Liste der angezeigten Werte. Wenn nicht angegeben, wird die Liste der
83 möglichen Werte (Attribut "options") angezeigt.
84  
85 =item title => $str (default: undef)
86  
87 Beschreibungstext.
88  
89 =item styles => \@styles (Default: [])
90  
91 Liste der CSS-Definitionen für die einzelnen Optionen. Kann z.B. für
92 verschiedene Hintergrundfarben genutzt werden.
93  
94 =back
95  
96 =cut
97  
98 # -----------------------------------------------------------------------------
99  
100 package Quiq::Html::Widget::SelectMenu;
101 3 ▶use base qw/Quiq::Html::Widget/;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 3 our $VERSION = '1.240';
108  
109 3 ▶use Quiq::Html::Widget::Hidden;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 3 ▶use Quiq::Html::Widget::TextField;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 3 ▶use Quiq::JavaScript;
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 # -----------------------------------------------------------------------------
114  
115 =head1 METHODS
116  
117 =head2 Konstruktor
118  
119 =head3 new() - Konstruktor
120  
121 =head4 Synopsis
122  
123 $e = $class->new(@keyVal);
124  
125 =cut
126  
127 # -----------------------------------------------------------------------------
128  
129 1 ▶sub new {
Subroutine new: called CC=8 SCAR=29.9
Pod documented
130 1 my $class = shift;
131 # @_: @keyVal
132  
133 # Defaultwerte
134  
135 1 my $self = $class->SUPER::new(
136 addNull => 0,
137 class => undef,
138 disabled => 0,
139 hidden => 0,
140 id => undef,
141 javaScript => undef,
142 name => undef,
143 onChange => undef,
144 options => [],
145 optionPairs => [],
146 readonly => 0,
147 style => undef,
148 styles => [],
149 texts => [],
150 title => undef,
151 undefIf => 0,
152 value => undef,
153 );
154  
155 # Werte Konstruktoraufruf
156 1 $self->set(@_);
157  
158 1 my $optionPairs = $self->{'optionPairs'};
159 1 ▶ if (@$optionPairs) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$optionPairs) { } 0 1 1
elsif (@{$self->{'options'};} and not @{$self->{'texts'};}) { } 0 2 2
160 0 my (@options,@texts);
161 0 ▶ for (my $i = 0; $i < @$optionPairs; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$optionPairs) 0 0 0
162 0 push @options,$optionPairs->[$i];
163 0 push @texts,$optionPairs->[$i+1];
164 }
165 0 $self->{'options'} = \@options;
166 0 $self->{'texts'} = \@texts;
167 }
168 0 ▶ elsif (@{$self->{'options'}} && !@{$self->{'texts'}}) {
Condition33%1/3
@{$self->{"options"};} and not @{$self->{"texts"};}
!ll&&!rl&&r
100
MC/DC0%0/2
@{$self->{"options"};} and not @{$self->{"texts"};}
@{$self->{"options"};} not @{$self->{"texts"};}
Truth table33%1/3
A and not(B)
A= @{$self->{"options"};}B= not @{$self->{"texts"};}
ABresult
0X0
100
111
169 # Sind keine Texte gesetzt, setzen wir sie identisch zu den Optionen
170 0 $self->{'texts'} = [@{$self->{'options'}}];
171 }
172  
173 1 ▶ if ($self->{'addNull'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"addNull"}) 0 1 1
174 # Wir fügen eine Nullauswahl hinzu
175 0 $self->{'options'} = ['',@{$self->{'options'}}];
176 0 $self->{'texts'} = ['---',@{$self->{'texts'}}];
177 }
178  
179 # Existenz des Werts prüfen. Wenn nicht existent,
180 # setzen wir den ersten Wert.
181  
182 1 my $value = $self->{'value'};
183 1 my $optionA = $self->{'options'};
184  
185 0 ▶ if (!defined($value) || !grep {$_ eq $value} @$optionA) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and grep {$_ eq $value;} @$optionA) 1 0 1
Condition33%1/3
defined $value and grep {$_ eq $value;} @$optionA
!ll&&!rl&&r
100
MC/DC0%0/2
defined $value and grep {$_ eq $value;} @$optionA
defined $value grep {$_ eq $value;} @$optionA
Truth table33%1/3
A and B
A= defined $valueB= grep {$_ eq $value;} @$optionA
ABresult
0X0
100
111
186 1 $self->{'value'} = $optionA->[0];
187 }
188  
189 1 return $self;
190 }
191  
192 # -----------------------------------------------------------------------------
193  
194 =head2 Objektmethoden
195  
196 =head3 html() - Generiere HTML-Code
197  
198 =head4 Synopsis
199  
200 $html = $e->html($h);
201 $html = $class->html($h,@keyVal);
202  
203 =cut
204  
205 # -----------------------------------------------------------------------------
206  
207 1 ▶sub html {
Subroutine html: called CC=11 SCAR=37.5
Pod documented
208 1 my $this = shift;
209 1 my $h = shift;
210 # @_: @keyVal
211  
212 1 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 1 1
213  
214 # Attribute
215  
216 1 my ($addNull,$class,$disabled,$id,$javaScript,$name,$onChange,
217 $options,$readonly,$style,$styles,$texts,$title,$undefIf,$value) =
218 $self->get(qw/addNull class disabled id javaScript name onChange
219 options readonly style styles texts title undefIf value/);
220  
221 # Generierung
222  
223 1 ▶ if ($undefIf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undefIf) 0 1 1
224 0 return undef;
225 }
226  
227 1 my $html;
228 1 ▶ if ($readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readonly) { } 0 1 1
229 # Anzuzeigenden Wert ermitteln
230  
231 0 my ($option,$text) = ($options->[0],$texts->[0]);
232 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
233 0 ▶ for (my $i = 0; $i < @$options; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$options) 0 0 0
234 0 ▶ if ($options->[$i] eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->[$i] eq $value) 0 0 0
235 0 $option = $options->[$i];
236 0 $text = $texts->[$i];
237 0 last;
238 }
239 }
240 }
241  
242 0 ▶ if (!$disabled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($disabled) 0 0 0
243 0 $html .= Quiq::Html::Widget::Hidden->html($h,
244 name => $name,
245 value => $option,
246 );
247 }
248 0 $html .= Quiq::Html::Widget::TextField->html($h,
249 # name => $name,
250 # id => $id,
251 class => $class,
252 style => $style,
253 disabled => $disabled,
254 readonly => 1,
255 size => length($text),
256 title => $title,
257 value => $text,
258 );
259 }
260 else {
261 1 my $str;
262 1 ▶ for (my $i = 0; $i < @$options; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$options) 0 1 1
263 0 my $option = $options->[$i];
264 0 my $text = $texts->[$i];
265 0 my $style = $styles->[$i];
266  
267 0 ▶ $str .= $h->tag('option',
Condition0%0/3
defined $value && $option eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value && $option eq $value
defined $value $option eq $value
Truth table0%0/3
A && B
A= defined $valueB= $option eq $value
ABresult
0X0
100
111
268 -nl => 0,
269 value => $option,
270 style => $style,
271 selected => defined($value) && $option eq $value,
272 $text
273 );
274 }
275  
276 1 $html = $h->tag('select',
277 name => $name,
278 id => $id,
279 class => $class,
280 style => $style,
281 disabled => $disabled,
282 onchange => Quiq::JavaScript->line($onChange),
283 title => $title,
284 '-',
285 $str
286 );
287 }
288 1 chomp $html;
289  
290 1 ▶ if ($javaScript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($javaScript) 0 1 1
291 0 $html .= "\n".$h->tag('script',$javaScript);
292 }
293  
294 1 return $html;
295 }
296  
297 # -----------------------------------------------------------------------------
298  
299 =head1 VERSION
300  
301 1.240
302  
303 =head1 AUTHOR
304  
305 Frank Seitz, L<http://fseitz.de/>
306  
307 =head1 COPYRIGHT
308  
309 Copyright (C) 2026 Frank Seitz
310  
311 =head1 LICENSE
312  
313 This code is free software; you can redistribute it and/or modify
314 it under the same terms as Perl itself.
315  
316 =cut
317  
318 # -----------------------------------------------------------------------------
319  
320 3 1;
321  
322 # eof
« Quiq/AsciiTable.pm ↑ Summary Quiq/Option.pm »