Revision f383c423
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/Presenter/Tag.pm | ||
---|---|---|
92 | 92 |
my $value_key = delete($attributes{value_key}) || 'id'; |
93 | 93 |
my $title_key = delete($attributes{title_key}) || $value_key; |
94 | 94 |
my $default_key = delete($attributes{default_key}) || 'selected'; |
95 |
|
|
95 |
my $default_val_key = delete($attributes{default_value_key}); |
|
96 |
my $default_coll = delete($attributes{default}); |
|
96 | 97 |
|
97 | 98 |
my $value_title_sub = delete($attributes{value_title_sub}); |
98 | 99 |
|
... | ... | |
105 | 106 |
|
106 | 107 |
my $with_optgroups = delete($attributes{with_optgroups}); |
107 | 108 |
|
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 |
undef $default_key if $default_sub || $default_val_key; |
|
120 | 110 |
|
121 | 111 |
my $normalize_entry = sub { |
122 | 112 |
my ($type, $entry, $sub, $key) = @_; |
... | ... | |
139 | 129 |
return undef; |
140 | 130 |
}; |
141 | 131 |
|
132 |
my %selected; |
|
133 |
if (defined($default_coll) && !ref $default_coll) { |
|
134 |
%selected = ($default_coll => 1); |
|
135 |
|
|
136 |
} elsif (ref($default_coll) eq 'HASH') { |
|
137 |
%selected = %{ $default_coll }; |
|
138 |
|
|
139 |
} elsif ($default_coll) { |
|
140 |
$default_coll = [ $default_coll ] unless 'ARRAY' eq ref $default_coll; |
|
141 |
|
|
142 |
%selected = $default_val_key ? map({ ($normalize_entry->('value', $_, undef, $default_val_key) => 1) } @{ $default_coll }) |
|
143 |
: map({ ($_ => 1) } @{ $default_coll }); |
|
144 |
} |
|
145 |
|
|
142 | 146 |
my $list_to_code = sub { |
143 | 147 |
my ($sub_collection) = @_; |
144 | 148 |
|
... | ... | |
155 | 159 |
$title = $normalize_entry->('title', $entry, $title_sub, $title_key); |
156 | 160 |
} |
157 | 161 |
|
158 |
my $default = $normalize_entry->('default', $entry, $default_sub, $default_key); |
|
159 |
|
|
160 |
push(@options, [$value, $title, $default]); |
|
161 |
} |
|
162 |
my $default = $default_key ? $normalize_entry->('default', $entry, $default_sub, $default_key) : 0; |
|
162 | 163 |
|
163 |
foreach my $entry (@options) { |
|
164 |
$entry->[2] = 1 if $selected{$entry->[0]}; |
|
164 |
push(@options, [$value, $title, $selected{$value} || $default]); |
|
165 | 165 |
} |
166 | 166 |
|
167 | 167 |
return join '', map { $self->html_tag('option', $self->escape($_->[1]), value => $_->[0], selected => $_->[2]) } @options; |
... | ... | |
218 | 218 |
{ direction => 'right', display => 'To the right', selected => 1 } ], |
219 | 219 |
value_key => 'direction', title_key => 'display')) %] |
220 | 220 |
|
221 |
# Use an RDBO object and it's n:m relatioship as the default |
|
222 |
# values. For example, a user can be a member in many groups. "All |
|
223 |
# groups" is therefore the full collection and "$user->groups" is a |
|
224 |
# list of RDBO AuthGroup objects whose IDs must match the ones in |
|
225 |
# "All groups". This could look like the following: |
|
226 |
[% P.select_tag('user.groups[]', SELF.all_groups, multiple=1, |
|
227 |
default=SELF.user.groups, default_value_key='id' ) %] |
|
228 |
|
|
221 | 229 |
=head1 DESCRIPTION |
222 | 230 |
|
223 | 231 |
A module modeled a bit after Rails' ActionView helpers. Several small |
... | ... | |
312 | 320 |
=back |
313 | 321 |
|
314 | 322 |
For cases 3 and 4 C<$attributes{value_key}> defaults to C<id>, |
315 |
C<$attributes{title_key}> defaults to C<$attributes{value_key}> |
|
316 |
and C<$attributes{default_key}> defaults to C<selected>. |
|
323 |
C<$attributes{title_key}> defaults to C<$attributes{value_key}> and |
|
324 |
C<$attributes{default_key}> defaults to C<selected>. Note that |
|
325 |
C<$attributes{default_key}> is set to C<undef> if |
|
326 |
C<$attributes{default_value_key}> is used as well (see below). |
|
317 | 327 |
|
318 | 328 |
In addition to pure keys/method you can also provide coderefs as I<value_sub> |
319 | 329 |
and/or I<title_sub> and/or I<default_sub>. If present, these take precedence over keys or methods, |
... | ... | |
328 | 338 |
this element can be set with the option C<empty_title> and defaults to |
329 | 339 |
an empty string. |
330 | 340 |
|
331 |
The option C<default> can be either a scalar or an array reference |
|
332 |
containing the values of the options which should be set to be |
|
333 |
selected. |
|
334 |
|
|
335 | 341 |
The tag's C<id> defaults to C<name_to_id($name)>. |
336 | 342 |
|
343 |
The option C<default> can be quite a lot of things: |
|
344 |
|
|
345 |
=over 4 |
|
346 |
|
|
347 |
=item 1. A scalar value. This is the value of the entry that's |
|
348 |
selected by default. |
|
349 |
|
|
350 |
=item 2. A hash reference for C<multiple=1>. Whether or not an entry |
|
351 |
is selected by default is looked up in this hash. |
|
352 |
|
|
353 |
=item 3. An array reference containing scalar values. Same as 1., just |
|
354 |
for the case of C<multiple=1>. |
|
355 |
|
|
356 |
=item 4. If C<default_value_key> is given: an array reference of hash |
|
357 |
references. For each hash reference the value belonging to the key |
|
358 |
C<default_value_key> is treated as one value to select by |
|
359 |
default. Constructs a hash that's treated like 3. |
|
360 |
|
|
361 |
=item 5. If C<default_value_key> is given: an array reference of |
|
362 |
blessed objects. For each object the value returne from calling the |
|
363 |
function named C<default_value_key> on the object is treated as one |
|
364 |
value to select by default. Constructs a hash that's treated like 3. |
|
365 |
|
|
366 |
=back |
|
367 |
|
|
368 |
5. also applies for single RDBO instances (due to 'wantarray' |
|
369 |
shenanigangs assigning RDBO's relationships to a hash key will result |
|
370 |
in a single RDBO object being assigned instead of an array reference |
|
371 |
containing that single RDBO object). |
|
372 |
|
|
337 | 373 |
If the option C<with_optgroups> is set then this function expects |
338 | 374 |
C<\@collection> to be one level deeper. The upper-most level is |
339 | 375 |
translated into a HTML C<optgroup> tag. So the structure becomes: |
Auch abrufbar als: Unified diff
L./P.select_tag: 'default' auf Liste von Rose::DB::Object-Instanzen setzen können -- try #2
Dieses Mal wird die Semantik des Parameters 'default_key' nicht
verändert; statt dessen gibt es den neuen Parameter
'default_value_key'.