7 #my $routestring= $queryqf{'routestring'};
16 <h1>Specify route</h1>
17 <form action="<% $quri->() |h %>" method="get">
19 %#---------- textbox, user enters route as string ----------
20 % if (!$a->{Dropdowns}) {
22 Enter route (islands, or archipelagoes, separated by |s or commas;
23 abbreviations are OK):<br>
26 tr_uri= "routetextstring?format=json&type=text/xml"
27 + "&ocean=<% uri_escape($a->{Ocean}) %>";
34 window.clearTimeout(tr_timeout);
35 tr_timeout = window.setTimeout(tr_Needed, 500);
38 window.clearTimeout(tr_timeout);
39 tr_element= document.getElementById('routestring');
40 tr_needed= tr_element.value;
43 function tr_Request(){
44 if (tr_request || tr_needed==tr_done) return;
46 tr_request= new XMLHttpRequest();
47 uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
48 tr_request.open('GET', uri);
49 tr_request.onreadystatechange= tr_Ready;
50 tr_request.send(null);
53 if (tr_request.readyState != 4) return;
54 if (tr_request.status == 200) {
55 response= tr_request.responseText;
56 eval('results='+response);
57 toedit= document.getElementById('routeresults');
58 toedit.innerHTML= results.show;
63 window.onload= tr_Needed;
66 <input type="text" id="routestring" name="routestring" size=80
67 value="<% $routestring |h %>"
68 onchange="tr_Needed();"
69 onkeyup="tr_Later();"><br>
70 <div id="routeresults"> </div><br>
72 % } else { #---------- dropdowns, user selects from menus ----------
78 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
80 my $optionlistmap= sub {
81 my ($optlist, $selected) = @_;
83 foreach my $entry (@$optlist) {
84 $out.= sprintf('<option value="%s" %s>%s</option>',
85 encode_entities($entry->[0]),
86 defined $selected && $entry->[0] eq $selected
88 encode_entities($entry->[1]));
93 my $dbh= dbw_connect($a->{Ocean});
95 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
96 ORDER BY archipelago;");
99 while ($row=$sth->fetchrow_arrayref) {
101 push @archlistdata, [ $arch, $arch ];
102 $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
105 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
107 ORDER BY islandname;");
110 while ($row=$sth->fetchrow_arrayref) {
112 push @{ $islandlistdata{'none'} }, [ @$row ];
113 push @{ $islandlistdata{$arch} }, [ @$row ];
114 $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
117 my %resetislandlistdata;
118 foreach my $arch (keys %islandlistdata) {
119 $resetislandlistdata{$arch}=
120 $optionlistmap->($islandlistdata{$arch}, '');
125 <input type=hidden name=dropdowns value="<% $a->{Dropdowns} |h %>">
128 ms_lists= <% to_json_protecttags(\%resetislandlistdata) %>;
129 function ms_Setarch(dd) {
130 debug('ms_SetArch '+dd+' arch='+arch);
131 var arch= document.getElementsByName('archipelago'+dd).item(0).value;
132 var got= ms_lists[arch];
133 if (got == undefined) return; // unknown arch ? hrm
134 debug('ms_SetArch '+dd+' arch='+arch+' got ok');
135 var select= document.getElementsByName('islandid'+dd).item(0);
136 select.innerHTML= got;
137 debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
141 <table style="table-layout:fixed; width:90%;">
144 % for my $dd (0..$a->{Dropdowns}-1) {
146 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
147 <option value="none">Whole ocean</option>
148 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
153 % for my $dd (0..$a->{Dropdowns}-1) {
154 % my $arch= $ARGS{"archipelago$dd"};
155 % $arch= 'none' if !defined $arch;
157 <select name="islandid<% $dd %>">
158 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
165 % } #---------- end of dropdowns, now common middle of page code ----------
167 <input type=submit name=submit value="Go">
171 #========== result computations ==========
175 print "<h1>Results</h1>\n";
176 $results_head= sub { };
179 #---------- result computation - textstring ----------
180 if (!$a->{Dropdowns}) {
181 if (length $routestring) {
183 my $rsr= $m->comp('routetextstring',
184 ocean => $a->{Ocean},
185 string => $routestring,
188 if (length $rsr->{Error}) {
189 print encode_entities($rsr->{Error});
191 foreach my $entry (@{ $rsr->{Results} }) {
193 defined $entry->[1] ? undef : $entry->[0];
194 push @islandids, $entry->[1];
199 } else { #---------- results - dropdowns ----------
201 my $argorundef= sub {
203 my $thing= $ARGS{"${base}${dd}"};
204 $thing= undef if defined $thing and $thing eq 'none';
208 for my $dd (0..$a->{Dropdowns}-1) {
209 my $arch= $argorundef->($dd,'archipelago');
210 my $island= $argorundef->($dd,'islandid');
211 next unless defined $arch or defined $island;
212 if (defined $island and defined $arch) {
213 my $ii= $islandid2{$island};
214 my $iarch= $ii->{Arch};
215 if ($iarch ne $arch) {
218 Specified archipelago <% $arch %> but
219 island <% $ii->{Name} %>
220 which is in <% $iarch %>; using the island.<br>
225 push @archipelagoes, $arch;
226 push @islandids, $island;
229 }#---------- result processing, common stuff
235 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>