package II3562149e53818b6637b38680e87ba918::switcher; use strict; use Zen::Config; use Zen::PerlHandler; sub get { my ($form, $f) = @_; my ($type, $handler, $width, $default, $styles, $js_addon) = split ',', Zen::PerlHandler->getStatList, 6; $width ||= 350; unless ($styles) { $styles = { bgcolor => '', fontcolor => 'black', active_bgcolor => '#c35e5e', active_fontcolor => 'white', }; } my (@values, %labels); if ($type eq 'conflist') { my $cfg = Zen::Config->get($1)->{$2} if $handler =~ /^(.+?)::(\w+)$/; die "config not found [$cfg]" unless defined $cfg; @values = keys %$cfg; %labels = %$cfg; } elsif ($type eq 'perllist') { my $data = Zen::PerlHandler->get($handler)->do($form, $f); for (@$data) { push @values, $_->[0]; $labels{$_->[0]} = $_->[1]; } } else { die "unknown type '$type'"; } my $object = $form->getDecorated; my $pfx = $form->pfx; my $active_item; for my $i (1..@values) { my $v = $values[$i - 1]; if (! defined $active_item && $v eq $default) { $active_item = $i; } elsif ($v eq $object->{$f}) { $active_item = $i; last; } } my $code; if (Zen->hasRequest) { my $request = Zen->getRequest; my $ctx = $request->getCtx; unless ($ctx->{AtkSwitchersJsIncidence}) { $code .= <<CODE;
<script language="JavaScript" src="$ENV{SCRIPT_NAME}/suppl/atk/switcher.js"></script>
<script language="JavaScript">
atkSwitchersActiveItems = new Array(); atkSwitchersDefault = new Array(); atkSwitchersStyle = new Array();
</script>
CODE
$ctx->{AtkSwitchersJsIncidence}++; } $code .= <<CODE;
<script language="JavaScript">
atkSwitchersActiveItems['$pfx$f'] = '$active_item'; atkSwitchersDefault['$pfx$f'] = '$default'; atkSwitchersStyle['$pfx$f'] = new Array(); atkSwitchersStyle['$pfx$f']['bgcolor'] = '$styles->{bgcolor}'; atkSwitchersStyle['$pfx$f']['fontcolor'] = '$styles->{fontcolor}'; atkSwitchersStyle['$pfx$f']['active_bgcolor'] = '$styles->{active_bgcolor}'; atkSwitchersStyle['$pfx$f']['active_fontcolor'] = '$styles->{active_fontcolor}';
</script>
CODE
} my $active_value = $active_item ? $values[$active_item-1] : ''; $code .= qq(<input type="hidden" name="$pfx$f" id="$pfx$f" value="$active_value">); my $i = 0; my @links; for my $v (@values) { $i++; my $style = $active_item && $active_item == $i ? qq(style="background-color:$styles->{active_bgcolor}; color:$styles->{active_fontcolor};") : qq(style="background-color:$styles->{bgcolor}; color:$styles->{fontcolor};"); push @links, qq(<a $style id="atkSwitcher_$pfx$f\_$i" href="#" onClick="atkSwitcherOnClick('$pfx$f', '$i', '$v');$js_addon return false;">$labels{$v}</a>); } $code.='<div style="width:' . $width .'px;">'; $code .= join "&nbsp;&nbsp; &nbsp;", @links; $code.= qq(</div>); $code; } sub set { my ($form, $f, $v) = @_; my ($type, $handler) = split ',', Zen::PerlHandler->getStatList, 2; my $object = $form->getDecorated; $object->{$f} = $v; } 1; 