Diff for /loncom/interface/lonhtmlcommon.pm between versions 1.118 and 1.119

version 1.118, 2005/11/15 20:46:40 version 1.119, 2005/11/15 21:08:47
Line 1337  sub role_select_row { Line 1337  sub role_select_row {
     if (defined($title)) {      if (defined($title)) {
         $output = &row_title($col_width,$tablecolor,$title);          $output = &row_title($col_width,$tablecolor,$title);
     }      }
     $output .= qq|               <td>      $output .= qq|               <td valign="top">
                                   <select name="roles" multiple >\n|;                                    <select name="roles" multiple >\n|;
     foreach my $role (@$roles) {      foreach my $role (@$roles) {
         my $plrole;          my $plrole;
Line 1444  sub status_select_row { Line 1444  sub status_select_row {
     if (defined($title)) {      if (defined($title)) {
         $output = &row_title($col_width,$tablecolor,$title);          $output = &row_title($col_width,$tablecolor,$title);
     }      }
     $output .= qq|              <td>      $output .= qq|              <td valign="top">
                                     <select name="types" multiple>\n|;                                      <select name="types" multiple>\n|;
     foreach my $status_type (sort(keys(%{$types}))) {      foreach my $status_type (sort(keys(%{$types}))) {
         $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';          $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
Line 1500  sub submit_row { Line 1500  sub submit_row {
     return $output;      return $output;
 }  }
   
   ##############################################
   ##############################################
                                                                                
   # echo_form_input
   #
   # Generates html markup to add form elements from the referrer page
   # as hidden form elements (values encoded) in the new page.
   #
   # Intended to support two types of use 
   # (a) to allow backing up to earlier pages in a multi-page 
   # form submission process using a breadcrumb trail.
   #
   # (b) to allow the current page to be reloaded with form elements
   # set on previous page to remain unchanged.  An example would
   # be where the a page containing a dynamically-built table of data is 
   # is to be redisplayed, with only the sort order of the data changed. 
   #  
   # Inputs:
   # 1. Reference to array of form elements in the submitted form on 
   # the referrer page which are to be excluded from the echoed elements.
   #
   # 2. Reference to array of regular expressions, which if matched in the  
   # name of the form element n the referrer page will be omitted from echo. 
   #
   # Outputs: A scalar containing the html markup for the echoed form
   # elements (all as hidden elements, with values encoded). 
   
   
   sub echo_form_input {
       my ($excluded,$regexps) = @_;
       my $output = '';
       foreach my $key (keys(%env)) {
           if ($key =~ /^form\.(.+)$/) {
               my $name = $1;
               my $match = 0;
               if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
                   if (defined($regexps)) {
                       if (@{$regexps} > 0) {
                           foreach my $regexp (@{$regexps}) {
                               if ($name =~ /\Q$regexp\E/) {
                                   $match = 1;
                                   last;
                               }
                           }
                       }
                   }
                   if (!$match) {
                       if (ref($env{$key})) {
                           foreach my $value (@{$env{$key}}) {
                               $value = &HTML::Entities::encode($value,'<>&"');
                               $output .= '<input type="hidden" name="'.$name.
                                                '" value="'.$value.'" />'."\n";
                           }
                       } else {
                           my $value = &HTML::Entities::encode($env{$key},'<>&"');
                           $output .= '<input type="hidden" name="'.$name.
                                                '" value="'.$value.'" />'."\n";
                       }
                   }
               }
           }
       }
       return $output;
   }
   
   ##############################################
   ##############################################
                                                                                
   # set_form_elements
   #
   # Generates javascript to set form elements to values based on
   # corresponding values for the same form elements when the page was
   # previously submitted.
   #     
   # Last submission values are read from hidden form elements in referring 
   # page which have the same name, i.e., generated by &echo_form_input(). 
   #
   # Intended to be called by onload event.
   #
   # Input:
   # Reference to hash of echoed form elements to be set.
   #
   # In the hash, keys are the form element names, and the values are the
   # element type (selectbox, radio, checkbox or text -for textbox, textarea or
   # hidden).
   # 
   # Output:
   #  
   # javascript function - set_form_elements() which sets form elements,
   # expects an argument: formname - the name of the form according to 
   # the DOM, e.g., document.compose
   
   sub set_form_elements {
       my ($elements) = @_;
       my $output .= 'function setFormElements(courseForm) {
   ';      
       foreach my $key (keys(%env)) {
           if ($key =~ /^form\.(.+)$/) {
               my $name = $1;
               if (exists($$elements{$name})) {
                   my @values = &Apache::loncommon::get_env_multiple($key);
                   for (my $i=0; $i<@values; $i++) {
                       $values[$i] = &HTML::Entities::decode($values[$i],'<>&"');
                       $values[$i] =~ s/([\r\n\f]+)/\\n/g;
                       $values[$i] =~ s/"/\\"/g;
                   }
                   if ($$elements{$name} eq 'text') {
                       my $numvalues = @values;
                       if ($numvalues > 1) {
                           my $valuestring = join('","',@values);
                           $output .= qq|
     var textvalues = new Array ("$valuestring");
     var total = courseForm.$name.length;
     if (total > $numvalues) {
         total = $numvalues;
     }    
     for (var i=0; i<total; i++) {
         courseForm.$name\[i].value = textvalues[i];
     }
   |;
                       } else {
                           $output .= qq|
     courseForm.$name.value = "$values[0]";
   |;
                       }
                   } else {
                       $output .=  qq|
     var elementLength = courseForm.$name.length;
     if (elementLength==undefined) {
   |;
                       foreach my $value (@values) {
                           if ($$elements{$name} eq 'selectbox') {
                               $output .=  qq|
         if (courseForm.$name.options[0].value == "$value") {
             courseForm.$name.options[0].selected = true;
         }|;
                           } elsif (($$elements{$name} eq 'radio') ||
                                    ($$elements{$name} eq 'checkbox')) {
                               $output .= qq|
         if (courseForm.$name.value == "$value") {
             courseForm.$name.checked = true;
         }|;
                           }
                       }
                       $output .= qq|
     }
     else {
         for (var i=0; i<courseForm.$name.length; i++) {
   |;
                       if ($$elements{$name} eq 'selectbox') {
                           $output .=  qq|
             courseForm.$name.options[i].selected = false;|;
                       } elsif (($$elements{$name} eq 'radio') || 
                                ($$elements{$name} eq 'checkbox')) {
                           $output .= qq|
             courseForm.$name\[i].checked = false;|; 
                       }
                       $output .= qq|
         }
         for (var j=0; j<courseForm.$name.length; j++) {
   |;
                       foreach my $value (@values) {
                           if ($$elements{$name} eq 'selectbox') {
                               $output .=  qq|
             if (courseForm.$name.options[j].value == "$value") {
                 courseForm.$name.options[j].selected = true;
             }|;
                           } elsif (($$elements{$name} eq 'radio') ||
                                    ($$elements{$name} eq 'checkbox')) { 
                               $output .= qq|
             if (courseForm.$name\[j].value == "$value") {
                 courseForm.$name\[j].checked = true;
             }|;
                           }
                       }
                       $output .= qq|
         }
     }
   |;
                   }
               }
           }
       }
       $output .= "
   }\n";
       return $output;
   }
   
 1;  1;
   
 __END__  __END__

Removed from v.1.118  
changed lines
  Added in v.1.119


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>