chiark / gitweb /
only one "none" in each island dropdown
[ypp-sc-tools.db-live.git] / yarrg / web / pirate-route
index 0f2fff729405a4c31bfd6a4f621a28f6d61dab8e..84b61e40acc00b29865ed85466d4bd180cf60c2f 100644 (file)
@@ -4,6 +4,11 @@
 my %a;
 my @vars;
 
+#---------- "mode" argument parsing and mode menu at top of page ----------
+
+# for debugging, invoke as
+#  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
+
 @vars= ({      Name => 'Ocean',
                Before => 'Ocean: ',
                CmpCanon => sub { ucfirst lc $_[0] },
@@ -56,6 +61,7 @@ foreach my $var (@vars) {
                                }
                        }
                        my $uri= URI->new($m->current_comp()->name());
+                       $qf{'debug'}=1 if $debug;
                        $uri->query_form(%qf);
                        print '<a href="', $uri->path_query(), '">';
                        $after= '</a>';
@@ -66,6 +72,8 @@ foreach my $var (@vars) {
        print "<p>\n\n";
 }
 
+#---------- initial checks, startup, main entry form ----------
+
 die "unknown ocean $a{Ocean} ?"
        unless grep { $_ eq $a{Ocean} } ocean_list();
 
@@ -73,11 +81,16 @@ db_setocean($a{Ocean});
 db_connect();
 
 </%perl>
+<%args>
+$debug => 0
+</%args>
 
 <h1>Specify route</h1>
 <form action="/ucgi/~clareb/mason/something" method="get">
 
+%#---------- textbox, user enters route as string ----------
 % if (!$a{Dropdowns}) {
+
 Enter route (islands, or archipelagoes, separated by |s or commas;
  abbreviations are OK):<br/>
 
@@ -114,7 +127,7 @@ function tr_Ready() {
     response= tr_request.responseText;
     eval('results='+response);
     toedit= document.getElementById('routeresults');
-    toedit.innerHTML= results['show'];
+    toedit.innerHTML= results.show;
   }
   tr_request= false;
   tr_Request();
@@ -125,64 +138,67 @@ function tr_Ready() {
  onchange="tr_Needed();"
  onkeyup="tr_Later();"><br>
 <div id="routeresults">&nbsp;</div><br/>
-% } else {
+
+% } else { #---------- dropdowns, user selects from menus ----------
 
 <%perl>
-my $sth=$dbh->prepare("SELECT islandid,islandname,archipelago
-                              FROM islands
-                             ORDER BY islandname;");
+my ($sth,$row);;
+my $archlistdata='';
+my %islandlistdata;
+$islandlistdata{'none'}= <<END;
+<option value="none">Select island...</option>
+END
+
+$sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
+                           ORDER BY archipelago;");
 $sth->execute();
-my $row;
-my $islandlistdata='';
-my %archmap=();
+
 while ($row=$sth->fetchrow_arrayref) {
-       $islandlistdata.=
+       my ($arch)= @$row;
+       $archlistdata.=
                sprintf('<option value="%s">%s</option>',
-                       map { encode_entities($_) } @$row[0..1]);
-       $archmap{$row->[0]}= $row->[2];
+                       map { encode_entities($_) } ($arch,$arch));
+       $islandlistdata{$arch}= <<END;
+<option value="none">Whole arch</option>
+END
 }
 
-$sth=$dbh->prepare("SELECT DISTINCT archipelago FROM islands
-                          ORDER BY archipelago;");
+$sth= $dbh->prepare("SELECT islandid,islandname,archipelago
+                            FROM islands
+                           ORDER BY islandname;");
 $sth->execute();
-my $archlistdata='';
 
 while ($row=$sth->fetchrow_arrayref) {
-       $archlistdata.=
-               sprintf('<option value="%s">%s</option>',
-                       map { encode_entities($_) } (@$row, @$row));
+       my $arch= $row->[2];
+       my $here= sprintf('<option value="%s">%s</option>',
+                       map { encode_entities($_) } @$row[0..1]);
+       $islandlistdata{'none'} .= $here;
+       $islandlistdata{$arch} .= $here;
 }
+
 </%perl>
 
 <script type="text/javascript">
-sel_archmap= <% to_json(\%archmap) %>;
-function setarch(dd) {
+ms_lists= <% to_json(\%islandlistdata) %>;
+function ms_Setarch(dd) {
+  debug('ms_SetArch '+dd+' arch='+arch);
   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
+  var got= ms_lists[arch];
+  if (got == undefined) return; // unknown arch ?  hrm
+  debug('ms_SetArch '+dd+' arch='+arch+' got ok');
   var select= document.getElementsByName('islandid'+dd).item(0);
-  var nodes= select.getElementsByTagName('option');
-  for (var i=0; i<nodes.length; i++) {
-    node= nodes.item(i);
-    value= node.getAttribute('value');
-    if (value == 'none') {
-      if (arch == 'none') {
-       node.innerHTML= 'Select island ...';
-      } else {
-       node.innerHTML= 'Whole arch';
-      }
-    } else {
-//      alert('node i='+i+' arch='+arch+' value='+value+'.');
-      node.setAttribute('disabled', !(arch=='none' || value==arch));
-    }
-  }
+  select.innerHTML= got;
+  debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
 }
 </script>
 
-<table>
+<table style="table-layout:fixed; width:90%;">
 
 <tr>
 %      for my $dd (0..$a{Dropdowns}-1) {
-<td><select name="archipelago<% $dd %>" onchange="setarch(<% $dd %>)">
-<option name="none">Whole ocean</option>
+<td>
+<select  name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
+<option value="none">Whole ocean</option>
 <% $archlistdata %></select></td>
 %      }
 </tr>
@@ -190,17 +206,33 @@ function setarch(dd) {
 <tr>
 %      for my $dd (0..$a{Dropdowns}-1) {
 <td><select name="islandid<% $dd %>">
-<option name="none">Select island...</option>
-<% $islandlistdata %></select></td>
+<% $islandlistdata{'none'} %></select></td>
 %      }
 </tr>
 
 </table>
-% }
+
+% } #---------- end of dropdowns, now common code ----------
 
 <input type=submit name=submit value="Go">
 </form>
 
+% if ($debug) {
+<p>
+<pre id="debug_log">
+Debug log:
+</pre>
+% }
+
+<script type="text/javascript">
+function debug (m) {
+% if ($debug) {
+  var node= document.getElementById('debug_log');
+  node.innerHTML += "\n" + m + "\n";
+% }
+}
+</script>
+
 <%init>
 use CommodsWeb;
 use HTML::Entities;