[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 26 Apr 2002 18:57:27 -0000


matthew		Fri Apr 26 14:57:27 2002 EDT

  Modified files:              
    /loncom/interface	loncommon.pm 
  Log:
  Added &linked_select_forms to build 2 <select> forms where the choice in
  the first <select> menu will determine the choices available in the second 
  menu.
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.35 loncom/interface/loncommon.pm:1.36
--- loncom/interface/loncommon.pm:1.35	Tue Apr 23 17:42:01 2002
+++ loncom/interface/loncommon.pm	Fri Apr 26 14:57:27 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.35 2002/04/23 21:42:01 matthew Exp $
+# $Id: loncommon.pm,v 1.36 2002/04/26 18:57:27 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -188,6 +188,153 @@
     
 }
 # ============================================================= END BEGIN BLOCK
+
+=item linked_select_forms(...)
+
+linked_select_forms returns a string containing a <script></script> block
+and html for two <select> menus.  The select menus will be linked in that
+changing the value of the first menu will result in new values being placed
+in the second menu.  The values in the select menu will appear in alphabetical
+order.
+
+linked_select_forms takes the following ordered inputs:
+
+=over 4
+
+=item $formname, the name of the <form> tag
+
+=item $middletext, the text which appears between the <select> tags
+
+=item $firstdefault, the default value for the first menu
+
+=item $firstselectname, the name of the first <select> tag
+
+=item $secondselectname, the name of the second <select> tag
+
+=item $hashref, a reference to a hash containing the data for the menus.
+
+Below is an example of such a hash.  Only the 'text', 'default', and 
+'select2' keys must appear as stated.  keys(%menu) are the possible 
+values for the first select menu.  The text that coincides with the 
+first menu values is given in $menu{$choice1}->{'text'}.  The values 
+and text for the second menu are given in the hash pointed to by 
+$menu{$choice1}->{'select2'}.  
+
+ my %menu = ( A1 => { text =>"Choice A1" ,
+                      default => "B3",
+                      select2 => { 
+                          B1 => "Choice B1",
+                          B2 => "Choice B2",
+                          B3 => "Choice B3",
+                          B4 => "Choice B4"
+                          }
+                  },
+              A2 => { text =>"Choice A2" ,
+                      default => "C2",
+                      select2 => { 
+                          C1 => "Choice C1",
+                          C2 => "Choice C2",
+                          C3 => "Choice C3"
+                          }
+                  },
+              A3 => { text =>"Choice A3" ,
+                      default => "D6",
+                      select2 => { 
+                          D1 => "Choice D1",
+                          D2 => "Choice D2",
+                          D3 => "Choice D3",
+                          D4 => "Choice D4",
+                          D5 => "Choice D5",
+                          D6 => "Choice D6",
+                          D7 => "Choice D7"
+                          }
+                  }
+              );
+
+=back
+
+=cut
+
+# ------------------------------------------------
+
+sub linked_select_forms {
+    my ($formname,
+        $middletext,
+        $firstdefault,
+        $firstselectname,
+        $secondselectname, 
+        $hashref
+        ) = @_;
+    my $second = "document.$formname.$secondselectname";
+    my $first = "document.$formname.$firstselectname";
+    # output the javascript to do the changing
+    my $result = '';
+    $result.="<script>\n";
+    $result.="var select2data = new Object();\n";
+    $" = '","';
+    my $debug = '';
+    foreach my $s1 (sort(keys(%$hashref))) {
+        $result.="select2data.d_$s1 = new Object();\n";        
+        $result.="select2data.d_$s1.def = new String('".
+            $hashref->{$s1}->{'default'}."');\n";
+        $result.="select2data.d_$s1.values = new Array(";        
+        my @s2values = sort(keys( %{ $hashref->{$s1}->{'select2'} } ));
+        $result.="\"@s2values\");\n";
+        $result.="select2data.d_$s1.texts = new Array(";        
+        my @s2texts;
+        foreach my $value (@s2values) {
+            push @s2texts, $hashref->{$s1}->{'select2'}->{$value};
+        }
+        $result.="\"@s2texts\");\n";
+    }
+    $"=' ';
+    $result.= <<"END";
+
+function select1_changed() {
+    // Determine new choice
+    var newvalue = "d_" + $first.value;
+    // update select2
+    var values     = select2data[newvalue].values;
+    var texts      = select2data[newvalue].texts;
+    var select2def = select2data[newvalue].def;
+    var i;
+    // out with the old
+    for (i = 0; i < $second.options.length; i++) {
+        $second.options[i] = null;
+    }
+    // in with the nuclear
+    for (i=0;i<values.length; i++) {
+        $second.options[i] = new Option(values[i]);
+        $second.options[i].text = texts[i];
+        if (values[i] == select2def) {
+            $second.options[i].selected = true;
+        }
+    }
+}
+</script>
+END
+    # output the initial values for the selection lists
+    $result .= "<select size=\"1\" name=\"$firstselectname\" onchange=\"select1_changed()\">\n";
+    foreach my $value (sort(keys(%$hashref))) {
+        $result.="    <option value=\"$value\" ";
+        $result.=" selected=\"true\" " if ($value eq $firstdefault);
+        $result.=">$hashref->{$value}->{'text'}</option>\n";
+    }
+    $result .= "</select>\n";
+    my %select2 = %{$hashref->{$firstdefault}->{'select2'}};
+    $result .= $middletext;
+    $result .= "<select size=\"1\" name=\"$secondselectname\">\n";
+    my $seconddefault = $hashref->{$firstdefault}->{'default'};
+    foreach my $value (sort(keys(%select2))) {
+        $result.="    <option value=\"$value\" ";        
+        $result.=" selected=\"true\" " if ($value eq $seconddefault);
+        $result.=">$select2{$value}</option>\n";
+    }
+    $result .= "</select>\n";
+    #    return $debug;
+    return $result;
+}   #  end of sub linked_select_forms {
+
 ###############################################################
 ##        Home server <option> list generating code          ##
 ###############################################################