0.06, autodetecting field types is long overdue
authorivan <ivan>
Wed, 20 Dec 2006 09:50:23 +0000 (09:50 +0000)
committerivan <ivan>
Wed, 20 Dec 2006 09:50:23 +0000 (09:50 +0000)
Changes
SelectLayers.pm

diff --git a/Changes b/Changes
index 9ae3de3..677c656 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
 Revision history for Perl extension HTML::Widgets::SelectLayers.
 
+0.06 Wed Dec 20 01:50:00 PST 2006
+        - Also long overdue: add auto-sensing of form field types, pass
+          "form_elements" instead of the old form_* options.
+
 0.05 Wed Aug 24 06:31:24 PDT 2005
        - Long overdue: remove NN4 support.
        - Add under_position option, based on a patch from Ricardo SIGNES
index f37c90a..63887c6 100644 (file)
@@ -3,7 +3,7 @@ package HTML::Widgets::SelectLayers;
 use strict;
 use vars qw($VERSION);
 
-$VERSION = '0.05';
+$VERSION = '0.06';
 
 =head1 NAME
 
@@ -23,10 +23,17 @@ HTML::Widgets::SelectLayers - Perl extension for selectable HTML layers
     'options'       => \%options,
     'form_name'     => 'dummy',
     'form_action'   => 'process.cgi',
-    'form_text'     => [ qw( textfield1 textfield2 ) ],
-    'form_checkbox' => [ qw( checkbox1 ) ],
-    'form_radio'    => [ qw( radio1 ) ],
-    'form_select'   => [ qw( select1 ) ],
+
+    #new code auto-detects form types (radio not yet supported)
+    #'form_elements' => [ qw( textfield1 textfield2 checkbox1 radio1 select1 ) ],
+    'form_elements' => [ qw( textfield1 textfield2 checkbox1 radio1 select1 ) ],
+    
+    #deprecated style still works for now
+    #'form_text'     => [ qw( textfield1 textfield2 ) ],
+    #'form_checkbox' => [ qw( checkbox1 ) ],
+    #'form_radio'    => [ qw( radio1 ) ],
+    #'form_select'   => [ qw( select1 ) ],
+
     'layer_callback' => sub {
       my $layer = shift;
       my $html = qq!<INPUT TYPE="hidden" NAME="layer" VALUE="$layer">!;
@@ -50,7 +57,8 @@ example see http://www.420.am/selectlayers/
 This HTML generated by this module uses JavaScript, but nevertheless attempts
 to be as cross-browser as possible.  The 0.05 release drops Navigator 4
 compatibility and has been tested under Mozilla Firefox 1.0.6, MSIE 6.0, 
-Konqueror 3.3.2, and Opera 8.0.2.
+Konqueror 3.3.2, and Opera 8.0.2 (2006 note: still working under newer
+browsers such as IE7, Firefox 2.0, etc.).
 
 =head1 FORMS
 
@@ -83,6 +91,12 @@ form_name - (optional) Form name to copy values from.  If not supplied, no
 
 form_action - Form action
 
+form_elements - (optional) Array reference of form fields to copy from the
+                B<form_name> form.  Field type is autodetected; currently
+                text, hidden, checkbox, and select (single) fiels are
+                supported.  Select (multiple) and radio fields are not yet
+                supported.
+
 form_text - (optional) Array reference of text (or hidden) form fields to copy
             from the B<form_name> form.
 
@@ -130,6 +144,9 @@ sub html {
   my $between = exists($self->{html_between}) ? $self->{html_between} : '';
   my $options = $self->{options};
   my $form_action = exists($self->{form_action}) ? $self->{form_action} : '';
+
+  my $form_elements =
+    exists($self->{form_elements}) ? $self->{form_elements} : [];
   my $form_text =
     exists($self->{form_text}) ? $self->{form_text} : [];
   my $form_checkbox =
@@ -169,7 +186,7 @@ END
     $html .= <<END;
       <FORM NAME="${key}$layer" ACTION="$form_action" METHOD=POST onSubmit="${key}fixup(this)" STYLE="margin-top: 0; margin-bottom: 0">
 END
-    foreach my $f ( @$form_text, @$form_checkbox, @$form_radio, @$form_select )
+    foreach my $f ( @$form_elements, @$form_text, @$form_checkbox, @$form_radio, @$form_select )
     {
       $html .= <<END;
         <INPUT TYPE="hidden" NAME="$f" VALUE="">
@@ -207,6 +224,9 @@ sub _fixup {
   my $self = shift;
   my $key = exists($self->{unique_key}) ? $self->{unique_key} : '';
   my $form_name = $self->{form_name} or return '';
+
+  my $form_elements =
+    exists($self->{form_elements}) ? $self->{form_elements} : [];
   my $form_text =
     exists($self->{form_text}) ? $self->{form_text} : [];
   my $form_checkbox =
@@ -215,13 +235,45 @@ sub _fixup {
     exists($self->{form_radio}) ? $self->{form_radio} : [];
   my $form_select =
     exists($self->{form_select}) ? $self->{form_select} : [];
-  my $html = "
+  my $html = <<END;
     <SCRIPT>
+
+function copyelement(from, to) {
+  if ( from == undefined ) {
+    to.value = '';
+  } else if ( from.type == 'select-one' ) {
+    to.value = from.options[from.selectedIndex].value;
+    //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
+  } else if ( from.type == 'checkbox' ) {
+    if ( from.checked ) {
+      to.value = from.value;
+    } else {
+      to.value = '';
+    }
+//  } else if ( from.type == 'radio' ) {
+  } else {
+    if ( from.value == undefined ) {
+      to.value = '';
+    } else {
+      to.value = from.value;
+    }
+  }
+  //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
+}
+END
+
+  $html .= "
     function ${key}fchanged(what) {
       ${key}fixup(what.form);
     }
     function ${key}fixup(what) {\n";
 
+  foreach my $f ( @$form_elements ) {
+    $html .= "copyelement( document.$form_name.elements['$f'],
+                           what.elements['$f']
+                         )\n";
+  }
+
   foreach my $f ( @$form_text ) {
     $html .= "what.$f.value = document.$form_name.$f.value;\n";
   }