Skip to content

Commit

Permalink
Add support for dropdown menus to [zoom].
Browse files Browse the repository at this point in the history
  • Loading branch information
racke committed Aug 15, 2010
1 parent 8a5100c commit c365d35
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion lib/WellWell/Zoom.pm
Expand Up @@ -94,7 +94,11 @@ sub zoom {
}

for my $elt (@{$value->{elts}}) {
if ($elt->{zoom_rep_att}) {
if ($elt->{zoom_rep_sub}) {
# call subroutine to handle this element
$elt->{zoom_rep_sub}->($elt, $rep_str);
}
elsif ($elt->{zoom_rep_att}) {
# replace attribute instead of embedded text (e.g. for <input>)
$elt->set_att($elt->{zoom_rep_att}, $rep_str);
}
Expand Down Expand Up @@ -177,6 +181,9 @@ sub parse_handler {
# replace value attribute instead of text
$elt->{zoom_rep_att} = 'value';
}
elsif ($gi eq 'select') {
$elt->{zoom_rep_sub} = \&set_selected;
}
elsif (! $elt->contains_only_text()) {
# contains real elements, so we have to be careful with
# set text and apply it only to the first PCDATA element
Expand Down Expand Up @@ -205,5 +212,28 @@ sub parse_handler {
return $sref;
}

# set_selected - Set selected value in a dropdown menu

sub set_selected {
my ($elt, $value) = @_;
my (@children, $eltval);

@children = $elt->children('option');

for my $node (@children) {
$eltval = $node->att('value');

unless (length($eltval)) {
$eltval = $node->text();
}

if ($eltval eq $value) {
$node->set_att('selected', 'selected');
}
else {
$node->del_att('selected', '');
}
}
}

1;

0 comments on commit c365d35

Please sign in to comment.