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] },
}
}
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>';
print "<p>\n\n";
}
+#---------- initial checks, startup, main entry form ----------
+
die "unknown ocean $a{Ocean} ?"
unless grep { $_ eq $a{Ocean} } ocean_list();
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/>
response= tr_request.responseText;
eval('results='+response);
toedit= document.getElementById('routeresults');
- toedit.innerHTML= results['show'];
+ toedit.innerHTML= results.show;
}
tr_request= false;
tr_Request();
onchange="tr_Needed();"
onkeyup="tr_Later();"><br>
<div id="routeresults"> </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>
<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;