1 <html><head><title>Specify route</title></head><body>
7 #---------- "mode" argument parsing and mode menu at top of page ----------
9 # for debugging, invoke as
10 # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
12 @vars= ({ Name => 'Ocean',
14 CmpCanon => sub { ucfirst lc $_[0] },
15 Values => [ ocean_list() ]
16 }, { Name => 'Dropdowns',
17 Before => 'Interface: ',
18 CmpCanon => sub { !!$_[0] },
19 Values => [ [ 0, 'Type in names' ],
20 [ 4, 'Select from menus' ] ]
23 foreach my $var (@vars) {
24 my $name= $var->{Name};
26 $var->{Before}= '' unless exists $var->{Before};
27 $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
28 foreach my $val (@{ $var->{Values} }) {
30 $val= [ $val, encode_entities($val) ];
32 if (exists $ARGS{$lname}) {
33 $a{$name}= $ARGS{$lname};
35 $a{$name}= $var->{Values}[0][0];
40 foreach my $var (@vars) {
41 my $lname= lc $var->{Name};
42 next unless exists $ARGS{$lname};
43 $baseqf{$lname}= $ARGS{$lname};
47 foreach my $var (keys %ARGS) {
49 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
50 $queryqf{$var}= $ARGS{$var};
53 my $uri= URI->new($m->current_comp()->name());
54 my $quri= sub { $uri->query_form(@_); $uri->path_query(); };
56 foreach my $var (@vars) {
57 my $name= $var->{Name};
58 my $lname= lc $var->{Name};
59 my $delim= $var->{Before};
60 my $canon= &{$var->{CmpCanon}}($a{$name});
62 foreach my $valr (@{ $var->{Values} }) {
63 print $delim; $delim= "\n|\n";
64 my ($value,$html) = @$valr;
65 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
71 my %qf= (%baseqf,%queryqf);
73 $qf{$lname}= $value if $cvalix;
74 print '<a href="',&$quri(%qf),'">';
83 #---------- initial checks, startup, main entry form ----------
85 die "unknown ocean $a{Ocean} ?"
86 unless grep { $_ eq $a{Ocean} } ocean_list();
88 db_setocean($a{Ocean});
97 <h1>Specify route</h1>
98 <form action="<% &$quri() %>" method="get">
100 %#---------- textbox, user enters route as string ----------
101 % if (!$a{Dropdowns}) {
103 Enter route (islands, or archipelagoes, separated by |s or commas;
104 abbreviations are OK):<br/>
106 <script type="text/javascript">
107 tr_uri= "routetextstring?format=json&type=text/xml"
108 + "&ocean=<% uri_escape($a{Ocean}) %>";
115 window.clearTimeout(tr_timeout);
116 tr_timeout = window.setTimeout(tr_Needed, 500);
118 function tr_Needed(){
119 window.clearTimeout(tr_timeout);
120 tr_element= document.getElementById('routestring');
121 tr_needed= tr_element.value;
124 function tr_Request(){
125 if (tr_request || tr_needed==tr_done) return;
127 tr_request= new XMLHttpRequest();
128 uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
129 tr_request.open('GET', uri);
130 tr_request.onreadystatechange= tr_Ready;
131 tr_request.send(null);
133 function tr_Ready() {
134 if (tr_request.readyState != 4) return;
135 if (tr_request.status == 200) {
136 response= tr_request.responseText;
137 eval('results='+response);
138 toedit= document.getElementById('routeresults');
139 toedit.innerHTML= results.show;
146 <input type="text" id="routestring" name="routestring" size=80
147 value="<% $routestring |h %>"
148 onchange="tr_Needed();"
149 onkeyup="tr_Later();"><br>
150 <div id="routeresults"> </div><br/>
152 % if (length $routestring) {
156 <% $routestring |h %>
162 % } else { #---------- dropdowns, user selects from menus ----------
168 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
170 my $optionlistmap= sub {
171 my ($optlist, $selected) = @_;
173 foreach my $entry (@$optlist) {
174 $out.= sprintf('<option value="%s" %s>%s</option>',
175 encode_entities($entry->[0]),
176 defined $selected && $entry->[0] eq $selected
178 encode_entities($entry->[1]));
183 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
184 ORDER BY archipelago;");
187 while ($row=$sth->fetchrow_arrayref) {
189 push @archlistdata, [ $arch, $arch ];
190 $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
193 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
195 ORDER BY islandname;");
198 while ($row=$sth->fetchrow_arrayref) {
200 push @{ $islandlistdata{'none'} }, [ @$row ];
201 push @{ $islandlistdata{$arch} }, [ @$row ];
204 my %resetislandlistdata;
205 foreach my $arch (keys %islandlistdata) {
206 $resetislandlistdata{$arch}=
207 &$optionlistmap($islandlistdata{$arch}, '');
212 <input type=hidden name=dropdowns value="<% $a{Dropdowns} %>">
214 <script type="text/javascript">
215 ms_lists= <% to_json(\%resetislandlistdata) %>;
216 function ms_Setarch(dd) {
217 debug('ms_SetArch '+dd+' arch='+arch);
218 var arch= document.getElementsByName('archipelago'+dd).item(0).value;
219 var got= ms_lists[arch];
220 if (got == undefined) return; // unknown arch ? hrm
221 debug('ms_SetArch '+dd+' arch='+arch+' got ok');
222 var select= document.getElementsByName('islandid'+dd).item(0);
223 select.innerHTML= got;
224 debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
228 <table style="table-layout:fixed; width:90%;">
231 % for my $dd (0..$a{Dropdowns}-1) {
233 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
234 <option value="none">Whole ocean</option>
235 <% &$optionlistmap(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
240 % for my $dd (0..$a{Dropdowns}-1) {
241 % my $arch= $ARGS{"archipelago$dd"};
242 % $arch= 'none' if !defined $arch;
244 <select name="islandid<% $dd %>">
245 <% &$optionlistmap($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
252 % } #---------- end of dropdowns, now common code ----------
254 <input type=submit name=submit value="Go">
257 %#---------- debugging and epilogue ----------
266 <script type="text/javascript">
269 var node= document.getElementById('debug_log');
270 node.innerHTML += "\n" + m + "\n";