1 <html><head><title>Specify route</title></head><body>
12 #---------- "mode" argument parsing and mode menu at top of page ----------
14 # for debugging, invoke as
15 # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
17 @vars= ({ Name => 'Ocean',
19 CmpCanon => sub { ucfirst lc $_[0] },
20 Values => [ ocean_list() ]
21 }, { Name => 'Dropdowns',
22 Before => 'Interface: ',
23 CmpCanon => sub { !!$_[0] },
24 Values => [ [ 0, 'Type in names' ],
25 [ 4, 'Select from menus' ] ]
28 foreach my $var (@vars) {
29 my $name= $var->{Name};
31 $var->{Before}= '' unless exists $var->{Before};
32 $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
33 foreach my $val (@{ $var->{Values} }) {
35 $val= [ $val, encode_entities($val) ];
37 if (exists $ARGS{$lname}) {
38 $a{$name}= $ARGS{$lname};
40 $a{$name}= $var->{Values}[0][0];
45 foreach my $var (@vars) {
46 my $lname= lc $var->{Name};
47 next unless exists $ARGS{$lname};
48 $baseqf{$lname}= $ARGS{$lname};
52 foreach my $var (keys %ARGS) {
54 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
55 $queryqf{$var}= $ARGS{$var};
58 my $uri= URI->new($m->current_comp()->name());
59 my $quri= sub { $uri->query_form(@_); $uri->path_query(); };
61 foreach my $var (@vars) {
62 my $name= $var->{Name};
63 my $lname= lc $var->{Name};
64 my $delim= $var->{Before};
65 my $canon= &{$var->{CmpCanon}}($a{$name});
67 foreach my $valr (@{ $var->{Values} }) {
68 print $delim; $delim= "\n|\n";
69 my ($value,$html) = @$valr;
70 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
76 my %qf= (%baseqf,%queryqf);
78 $qf{$lname}= $value if $cvalix;
79 print '<a href="',&$quri(%qf),'">';
88 #---------- initial checks, startup, main entry form ----------
90 die "unknown ocean $a{Ocean} ?"
91 unless grep { $_ eq $a{Ocean} } ocean_list();
93 db_setocean($a{Ocean});
102 <h1>Specify route</h1>
103 <form action="<% &$quri() %>" method="get">
105 %#---------- textbox, user enters route as string ----------
106 % if (!$a{Dropdowns}) {
108 Enter route (islands, or archipelagoes, separated by |s or commas;
109 abbreviations are OK):<br/>
111 <script type="text/javascript">
112 tr_uri= "routetextstring?format=json&type=text/xml"
113 + "&ocean=<% uri_escape($a{Ocean}) %>";
120 window.clearTimeout(tr_timeout);
121 tr_timeout = window.setTimeout(tr_Needed, 500);
123 function tr_Needed(){
124 window.clearTimeout(tr_timeout);
125 tr_element= document.getElementById('routestring');
126 tr_needed= tr_element.value;
129 function tr_Request(){
130 if (tr_request || tr_needed==tr_done) return;
132 tr_request= new XMLHttpRequest();
133 uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
134 tr_request.open('GET', uri);
135 tr_request.onreadystatechange= tr_Ready;
136 tr_request.send(null);
138 function tr_Ready() {
139 if (tr_request.readyState != 4) return;
140 if (tr_request.status == 200) {
141 response= tr_request.responseText;
142 eval('results='+response);
143 toedit= document.getElementById('routeresults');
144 toedit.innerHTML= results.show;
149 window.onload= tr_Needed;
152 <input type="text" id="routestring" name="routestring" size=80
153 value="<% $routestring |h %>"
154 onchange="tr_Needed();"
155 onkeyup="tr_Later();"><br>
156 <div id="routeresults"> </div><br/>
158 % } else { #---------- dropdowns, user selects from menus ----------
164 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
166 my $optionlistmap= sub {
167 my ($optlist, $selected) = @_;
169 foreach my $entry (@$optlist) {
170 $out.= sprintf('<option value="%s" %s>%s</option>',
171 encode_entities($entry->[0]),
172 defined $selected && $entry->[0] eq $selected
174 encode_entities($entry->[1]));
179 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
180 ORDER BY archipelago;");
183 while ($row=$sth->fetchrow_arrayref) {
185 push @archlistdata, [ $arch, $arch ];
186 $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
189 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
191 ORDER BY islandname;");
194 while ($row=$sth->fetchrow_arrayref) {
196 push @{ $islandlistdata{'none'} }, [ @$row ];
197 push @{ $islandlistdata{$arch} }, [ @$row ];
198 $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
201 my %resetislandlistdata;
202 foreach my $arch (keys %islandlistdata) {
203 $resetislandlistdata{$arch}=
204 &$optionlistmap($islandlistdata{$arch}, '');
209 <input type=hidden name=dropdowns value="<% $a{Dropdowns} %>">
211 <script type="text/javascript">
212 ms_lists= <% to_json(\%resetislandlistdata) %>;
213 function ms_Setarch(dd) {
214 debug('ms_SetArch '+dd+' arch='+arch);
215 var arch= document.getElementsByName('archipelago'+dd).item(0).value;
216 var got= ms_lists[arch];
217 if (got == undefined) return; // unknown arch ? hrm
218 debug('ms_SetArch '+dd+' arch='+arch+' got ok');
219 var select= document.getElementsByName('islandid'+dd).item(0);
220 select.innerHTML= got;
221 debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
225 <table style="table-layout:fixed; width:90%;">
228 % for my $dd (0..$a{Dropdowns}-1) {
230 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
231 <option value="none">Whole ocean</option>
232 <% &$optionlistmap(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
237 % for my $dd (0..$a{Dropdowns}-1) {
238 % my $arch= $ARGS{"archipelago$dd"};
239 % $arch= 'none' if !defined $arch;
241 <select name="islandid<% $dd %>">
242 <% &$optionlistmap($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
249 % } #---------- end of dropdowns, now common middle of page code ----------
251 <input type=submit name=submit value="Go">
257 #========== result computations ==========
259 #---------- result computation - textstring ----------
260 if (!$a{Dropdowns}) {
261 if (length $routestring) {
262 my $rsr= $m->comp('routetextstring',
264 string => $routestring,
267 if (length $rsr->{Error}) {
268 print encode_entities($rsr->{Error});
270 foreach my $entry (@{ $rsr->{Results} }) {
272 defined $entry->[1] ? undef : $entry->[0];
273 push @islandids, $entry->[1];
278 } else { #---------- results - dropdowns ----------
280 my $argorundef= sub {
282 my $thing= $ARGS{"${base}${dd}"};
283 $thing= undef if defined $thing and $thing eq 'none';
287 for my $dd (0..$a{Dropdowns}-1) {
288 my $arch= &$argorundef($dd,'archipelago');
289 my $island= &$argorundef($dd,'islandid');
290 next unless defined $arch or defined $island;
291 if (defined $island and defined $arch) {
292 my $ii= $islandid2{$island};
293 my $iarch= $ii->{Arch};
294 if ($iarch ne $arch) {
296 Specified archipelago <% $arch %> but
297 island <% $ii->{Name} %>
298 which is in <% $iarch %>; using the island.<br>
303 push @archipelagoes, $arch;
304 push @islandids, $island;
307 }#---------- result processing, common stuff
312 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
316 %#---------- debugging and epilogue ----------
325 <script type="text/javascript">
328 var node= document.getElementById('debug_log');
329 node.innerHTML += "\n" + m + "\n";