Revision 2a171f0e
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/Presenter/Tag.pm | ||
---|---|---|
105 | 105 |
|
106 | 106 |
my $with_optgroups = delete($attributes{with_optgroups}); |
107 | 107 |
|
108 |
my $default = delete($attributes{default}); |
|
108 |
my %selected; |
|
109 |
|
|
110 |
if ( ref($attributes{default}) eq 'ARRAY' ) { |
|
111 |
|
|
112 |
foreach my $entry (@{$attributes{default}}) { |
|
113 |
$selected{$entry} = 1; |
|
114 |
} |
|
115 |
} elsif ( defined($attributes{default}) ) { |
|
116 |
$selected{$attributes{default}} = 1; |
|
117 |
} |
|
118 |
|
|
119 |
delete($attributes{default}); |
|
109 | 120 |
|
110 | 121 |
my $normalize_entry = sub { |
111 | 122 |
my ($type, $entry, $sub, $key) = @_; |
... | ... | |
128 | 139 |
return undef; |
129 | 140 |
}; |
130 | 141 |
|
131 |
my %selected = map { ( $normalize_entry->('value', $_, $default_sub, $default_key) => 1 ) } |
|
132 |
(@{ ref($default) eq 'ARRAY' ? $default : [ $default ]}); |
|
133 |
|
|
134 | 142 |
my $list_to_code = sub { |
135 | 143 |
my ($sub_collection) = @_; |
136 | 144 |
|
... | ... | |
149 | 157 |
|
150 | 158 |
my $default = $normalize_entry->('default', $entry, $default_sub, $default_key); |
151 | 159 |
|
152 |
push(@options, [$value, $title, !!$selected{$value}]); |
|
160 |
push(@options, [$value, $title, $default]); |
|
161 |
} |
|
162 |
|
|
163 |
foreach my $entry (@options) { |
|
164 |
$entry->[2] = 1 if $selected{$entry->[0]}; |
|
153 | 165 |
} |
154 | 166 |
|
155 | 167 |
return join '', map { $self->html_tag('option', $self->escape($_->[1]), value => $_->[0], selected => $_->[2]) } @options; |
... | ... | |
206 | 218 |
{ direction => 'right', display => 'To the right', selected => 1 } ], |
207 | 219 |
value_key => 'direction', title_key => 'display')) %] |
208 | 220 |
|
209 |
# With Rose::DB::Object instances. For example a group membership |
|
210 |
# (SL::DB::AuthGroup) for a user (SL::DB::AuthUser) via the user's |
|
211 |
# "groups" relationship: |
|
212 |
[% P.select_tag('direction', SELF.all_groups, default=SELF.user.groups, |
|
213 |
title_key='name', default_key='id', multiple=1) %] |
|
214 |
|
|
215 | 221 |
=head1 DESCRIPTION |
216 | 222 |
|
217 | 223 |
A module modeled a bit after Rails' ActionView helpers. Several small |
... | ... | |
324 | 330 |
|
325 | 331 |
The option C<default> can be either a scalar or an array reference |
326 | 332 |
containing the values of the options which should be set to be |
327 |
selected. How the value from the elements is derived depends on three |
|
328 |
things: the parameters C<default_sub> (unset by default) and |
|
329 |
C<default_key> (default: "selected") as well as the element's type. |
|
330 |
|
|
331 |
=over 4 |
|
332 |
|
|
333 |
=item * If C<default_sub> is a code reference then that reference is |
|
334 |
called with the element as its only parameter. The return value is the |
|
335 |
value compared to the value from the elements in C<\@collection>. |
|
336 |
|
|
337 |
=item * If the element in question is a scalar then its value is used. |
|
338 |
|
|
339 |
=item * If the element is a hash then C<default_key> names the index |
|
340 |
into the hash used as the value to select by default. |
|
341 |
|
|
342 |
=item * If the element is a blessed object then C<default_key> names |
|
343 |
the method to call on the object. That method's return value is used |
|
344 |
as the value to select by default. |
|
345 |
|
|
346 |
=back |
|
347 |
|
|
348 |
See the synopsis for an example using C<default> with Rose::DB::Object |
|
349 |
instances. |
|
333 |
selected. |
|
350 | 334 |
|
351 | 335 |
The tag's C<id> defaults to C<name_to_id($name)>. |
352 | 336 |
|
Auch abrufbar als: Unified diff
Reverting "L./P.select_tag: 'default' auf Liste von Rose::DB::Object-Instanzen setzen können"
This reverts commit cb3431bcc4eae2bc5a2851cae10f9dfd8f517c4a.
Macht bisheriges Verhalten kaputt und ist auch leider von der
Bedienung her nicht eindeutig. Zu sehr corner case, also zurück.