RT# 82949 - changes section name from fees to pricing, better opiton
[freeside.git] / httemplate / elements / select-tiered.html
index 35f9e5a..e76bf76 100644 (file)
@@ -35,6 +35,7 @@ contain the following:
   isn't fully tested.
 - after: an HTML string to be inserted after the select element, before 
   the next one.  By default there's nothing between them.
+- onchange: an additional javascript function to be called on change.
 
 For convenience, "curr_value" and "field" can be passed as part of the 
 main argument list, and will be applied to the last tier.
@@ -43,8 +44,13 @@ main argument list, and will be applied to the last tier.
 % $i = 0;
 % foreach my $tier (@$tiers) {
 %   my $onchange;
-%   $onchange="onchange='${pre}select_change(this, $i)'"
+%   $onchange="${pre}select_change(this, $i)"
 %     if $i < scalar(@$tiers) - 1;
+%
+%   $onchange .= ';'.$tier->{onchange}."(this, $i);"
+%     if $tier->{onchange};
+%
+%   $onchange = "onchange='$onchange'" if $onchange;
 <SELECT 
   NAME="<% $tier->{field} %>"
   ID="<% $pre."select_".$i %>"
@@ -53,16 +59,21 @@ main argument list, and will be applied to the last tier.
   >
 %   if ( $i == 0 ) {
 %     my $options = $tiers_by_key->[0]->{''};
-%     foreach ( sort keys %$options ) {
-  <OPTION VALUE="<%$_ |h%>"><% $options->{$_} |h%></OPTION>
+%     #foreach ( sort keys %$options ) {
+%     foreach ( sort { lc($options->{$a}) cmp lc($options->{$b}) }
+%                 keys %$options
+%             )
+%     {
+  <OPTION VALUE="<%$_ |h%>" <% $curr_values->[$i] eq $_ ? 'SELECTED' : ''%>>
+  <% $options->{$_} |h%></OPTION>
 %     }
 %   }
 %   $i++;
 </SELECT>
 <% $tier->{after} %>
 % } #foreach $tier
+
 <SCRIPT type="text/javascript">
-% my $json = JSON->new->canonical; #sort
 var <% $pre %>tiers = <% $json->encode($tiers_by_key) %>;
 var <% $pre %>curr_values = <% $json->encode($curr_values) %>;
 function <% $pre %>select_change(select_this, i) {
@@ -113,6 +124,9 @@ my %opt = @_;
 my $pre = $opt{prefix} || '';
 my $tiers = $opt{tiers} or die "no tiers defined";
 
+my $json = Cpanel::JSON::XS->new();
+$json->canonical;
+
 my $i;
 for( $i = 0; $i < @$tiers; $i++ ) {
   my $tier = $tiers->[$i];
@@ -160,6 +174,8 @@ for( $i = 0; $i < @$tiers; $i++ ) {
         $children_of{$key}->{''} = $tier->{empty_label};
       }
     }
+    # ensure that there's always at least one empty label
+    $children_of{''}->{''} = $tier->{empty_label};
   }
   $tier->{by_key} = \%children_of;
 }
@@ -178,7 +194,6 @@ while($i >= 1) {
   foreach my $key ( %{ $tier->{by_key} } ) {
     my $options = $tier->{by_key}->{$key};
     if ( exists( $options->{$curr_value} ) ) {
-      warn "tier $i curr_value ($curr_value) found under key $key\n";
       $tiers->[$i-1]->{curr_value} = $key;
       last;
     }