doc
[freeside.git] / httemplate / elements / selectlayers.html
index 4496892..89fe41b 100644 (file)
@@ -14,7 +14,7 @@ Example:
 
     #XXX put this handling it its own selectlayers-fields.html element?
     'layer_prefix' => 'prefix_', #optional prefix for fieldnames
-    'layer_fields' => [ 'layer'  => [ 'fieldname',
+    'layer_fields' => { 'layer'  => [ 'fieldname',
                                       { label => 'fieldname2',
                                         type  => 'text', #implemented:
                                                          # text, money, fixed,
@@ -23,6 +23,7 @@ Example:
                                                          # select, select-agent,
                                                          # select-pkg_class,
                                                          # select-part_referral,
+                                                         # select-taxclass,
                                                          # select-table,
                                                          #XXX tbd:
                                                          # more?
@@ -32,6 +33,7 @@ Example:
                         'layer2' => [ 'l2fieldname',
                                       ...
                                     ],
+                      },
 
     #current values for layer fields above
     'layer_values' => { 'layer'  => { 'fieldname'  => 'current_value',
@@ -44,31 +46,48 @@ Example:
                         ...
                       },
 
+    #or manual control, instead of layer_fields and layer_values above
+    #called with args: my( $layer, $layer_fields, $layer_values, $layer_prefix )
+    'layer_callback' => 
+
     'html_between  => '', #optional HTML displayed between the SELECT and the
                           #layers, scalar or coderef ('field' passed as a param)
     'onchange'     => '', #javascript code run when the SELECT changes
                           # ("what" is the element)
     'js_only'      => 0, #set true to return only the JS portions
     'html_only'    => 0, #set true to return only the HTML portions
+    'select_only'  => 0, #set true to return only the <SELECT> HTML
+    'layers_only'  => 0, #set true to return only the layers <DIV> HTML
   )
 
 </%doc>
-% unless ( $opt{html_only} || $opt{js_only} ) {
+% unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
     <SCRIPT TYPE="text/javascript">
 % }
-% unless ( $opt{html_only} ) {
-      //alert('start function define');
+% unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
+
+%     if ( $opt{layermap} ) {
+%       my %map = %{ $opt{layermap} };
+        var layermap = { "":"",
+                         <% join(',', map { qq("$_":"$map{$_}") } keys %map ) %>
+                       };
+%     }
+
       function <% $key %>changed(what) {
 
         <% $opt{'onchange'} %>
 
         var <% $key %>layer = what.options[what.selectedIndex].value;
 
-%       foreach my $layer ( keys %$options ) {
-
+%       foreach my $layer ( @layers ) {
+%
+%         if ( $opt{layermap} ) {
+          if ( layermap[ <% $key %>layer ] == "<% $layer %>" ) {
+%         } else {
           if (<% $key %>layer == "<% $layer %>" ) {
+%         }
 
-%           foreach my $not ( grep { $_ ne $layer } keys %$options ) {
+%           foreach my $not ( grep { $_ ne $layer } @layers ) {
 %             my $element = "document.getElementById('${key}d$not').style";
               <% $element %>.display = "none";
               <% $element %>.zIndex = 0;
@@ -84,13 +103,12 @@ Example:
         //<% $opt{'onchange'} %>
 
       }
-      //alert('end function define');
 % }
-% unless ( $opt{html_only} || $opt{js_only} ) {
+% unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
     </SCRIPT>
 % }
 %
-% unless ( $opt{js_only} ) {
+% unless ( grep $opt{$_}, qw(js_only layers_only) ) {
 
     <SELECT NAME          = "<% $key %>"
             ID            = "<% $key %>"
@@ -109,18 +127,31 @@ Example:
 
     </SELECT>
 
+% }
+% unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
+
 <% ref($between) ? &{$between}($key) : $between %>
 
-%   foreach my $layer ( keys %$options ) {
+% }
+%
+% unless ( grep $opt{$_}, qw(js_only select_only) ) {
+
+%   foreach my $layer ( @layers ) {
+%     my $selected_layer;
+%     if ( $opt{layermap} ) {
+%       $selected_layer = $opt{layermap}->{$selected};
+%     } else {
+%       $selected_layer = $selected;
+%     }
 
       <DIV ID="<% $key %>d<% $layer %>"
-           STYLE="<% $layer eq $selected
+           STYLE="<% $selected_layer eq $layer
                        ? 'display: ""  ; z-index: 1'
                        : 'display: none; z-index: 0'
                   %>"
       >
 
-        <% layer_callback($layer, $layer_fields, $layer_values, $layer_prefix) %>
+        <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>
 
       </DIV>
 
@@ -149,6 +180,14 @@ tie my %options, 'Tie::IxHash',
 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
 my $options = \%options;
 
+my @layers = ();
+if ( $opt{layermap} ) {
+  my %layers = map { $opt{layermap}->{$_} => 1 } keys %options;
+  @layers = keys %layers;
+} else {
+  @layers = keys %options;
+}
+
 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
 
 #XXX eek.  also eek $layer_fields in the layer_callback() call...
@@ -156,6 +195,8 @@ my $layer_fields = $opt{layer_fields};
 my $layer_values = $opt{layer_values};
 my $layer_prefix = $opt{layer_prefix};
 
+my $layer_callback = $opt{layer_callback} || \&layer_callback;
+
 sub layer_callback {
   my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;