chiark / gitweb /
Data::Dumper output from routetextstring
[ypp-sc-tools.web-live.git] / yarrg / web / route
1 <html><head><title>Specify route</title></head><body>
2
3 <%perl>
4 my %a;
5 my @vars;
6
7 # for output:
8 my @archipelagoes;
9 my @islandids;
10 my %islandid2;
11
12 #---------- "mode" argument parsing and mode menu at top of page ----------
13
14 # for debugging, invoke as
15 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
16
17 @vars= ({       Name => 'Ocean',
18                 Before => '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' ] ]
26         });
27
28 foreach my $var (@vars) {
29         my $name= $var->{Name};
30         my $lname= lc $name;
31         $var->{Before}= '' unless exists $var->{Before};
32         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
33         foreach my $val (@{ $var->{Values} }) {
34                 next if ref $val;
35                 $val= [ $val, encode_entities($val) ];
36         }
37         if (exists $ARGS{$lname}) {
38                 $a{$name}= $ARGS{$lname};
39         } else {
40                 $a{$name}= $var->{Values}[0][0];
41         }
42 }
43
44 my %baseqf;
45 foreach my $var (@vars) {
46         my $lname= lc $var->{Name};
47         next unless exists $ARGS{$lname};
48         $baseqf{$lname}= $ARGS{$lname};
49 }
50
51 my %queryqf;
52 foreach my $var (keys %ARGS) {
53         next unless $var =~
54                 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
55         $queryqf{$var}= $ARGS{$var};
56 }
57
58 my $uri= URI->new($m->current_comp()->name());
59 my $quri= sub { $uri->query_form(@_); $uri->path_query(); };
60
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});
66         my $cvalix= 0;
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;
71                 my $after;
72                 if ($iscurrent) {
73                         print '<b>';
74                         $after= '</b>';
75                 } else {
76                         my %qf= (%baseqf,%queryqf);
77                         delete $qf{$lname};
78                         $qf{$lname}= $value if $cvalix;
79                         print '<a href="',$quri->(%qf),'">';
80                         $after= '</a>';
81                 }
82                 print $html, $after;
83                 $cvalix++;
84         }
85         print "<p>\n\n";
86 }
87
88 #---------- initial checks, startup, main entry form ----------
89
90 die "unknown ocean $a{Ocean} ?"
91         unless grep { $_ eq $a{Ocean} } ocean_list();
92
93 db_setocean($a{Ocean});
94 db_connect();
95
96 </%perl>
97 <%args>
98 $debug => 0
99 $routestring => ''
100 </%args>
101
102 <h1>Specify route</h1>
103 <form action="<% $quri->() %>" method="get">
104
105 %#---------- textbox, user enters route as string ----------
106 % if (!$a{Dropdowns}) {
107
108 Enter route (islands, or archipelagoes, separated by |s or commas;
109  abbreviations are OK):<br/>
110
111 <script type="text/javascript">
112 tr_uri= "routetextstring?format=json&type=text/xml"
113                 + "&ocean=<% uri_escape($a{Ocean}) %>";
114
115 tr_timeout=false;
116 tr_request=false;
117 tr_done='';
118 tr_needed='';
119 function tr_Later(){
120   window.clearTimeout(tr_timeout);
121   tr_timeout = window.setTimeout(tr_Needed, 500);
122 }
123 function tr_Needed(){
124   window.clearTimeout(tr_timeout);
125   tr_element= document.getElementById('routestring');
126   tr_needed= tr_element.value;
127   tr_Request();
128 }
129 function tr_Request(){
130   if (tr_request || tr_needed==tr_done) return;
131   tr_done= tr_needed;
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);
137 }
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;
145   }
146   tr_request= false;
147   tr_Request();
148 }
149 window.onload= tr_Needed;
150 </script>
151
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">&nbsp;</div><br/>
157
158 % } else { #---------- dropdowns, user selects from menus ----------
159
160 <%perl>
161 my ($sth,$row);;
162 my @archlistdata;
163 my %islandlistdata;
164 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
165
166 my $optionlistmap= sub {
167         my ($optlist, $selected) = @_;
168         my $out='';
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
173                                 ? 'selected' : '',
174                         encode_entities($entry->[1]));
175         }
176         return $out;
177 };
178
179 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
180                             ORDER BY archipelago;");
181 $sth->execute();
182
183 while ($row=$sth->fetchrow_arrayref) {
184         my ($arch)= @$row;
185         push @archlistdata, [ $arch, $arch ];
186         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
187 }
188
189 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
190                             FROM islands
191                             ORDER BY islandname;");
192 $sth->execute();
193
194 while ($row=$sth->fetchrow_arrayref) {
195         my $arch= $row->[2];
196         push @{ $islandlistdata{'none'} }, [ @$row ];
197         push @{ $islandlistdata{$arch} }, [ @$row ];
198         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
199 }
200
201 my %resetislandlistdata;
202 foreach my $arch (keys %islandlistdata) {
203         $resetislandlistdata{$arch}=
204                 $optionlistmap->($islandlistdata{$arch}, '');
205 }
206
207 </%perl>
208
209 <input type=hidden name=dropdowns value="<% $a{Dropdowns} %>">
210
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');
222 }
223 </script>
224
225 <table style="table-layout:fixed; width:90%;">
226
227 <tr>
228 %       for my $dd (0..$a{Dropdowns}-1) {
229 <td>
230 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
231 <option value="none">Whole ocean</option>
232 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
233 %       }
234 </tr>
235
236 <tr>
237 %       for my $dd (0..$a{Dropdowns}-1) {
238 %               my $arch= $ARGS{"archipelago$dd"};
239 %               $arch= 'none' if !defined $arch;
240 <td>
241 <select name="islandid<% $dd %>">
242 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
243 </select></td>
244 %       }
245 </tr>
246
247 </table>
248
249 % } #---------- end of dropdowns, now common middle of page code ----------
250
251 <input type=submit name=submit value="Go">
252 </form>
253
254 <%perl>
255 #========== result computations ==========
256
257 my $results_head;
258 $results_head= sub {
259         print "<h1>Results</h1>\n";
260         $results_head= sub { };
261 };
262
263 #---------- result computation - textstring ----------
264 if (!$a{Dropdowns}) {
265   if (length $routestring) {
266         $results_head->();
267         my $rsr= $m->comp('routetextstring',
268                 ocean => $a{Ocean},
269                 string => $routestring,
270                 format => 'return'
271         );
272         if (length $rsr->{Error}) {
273                 print encode_entities($rsr->{Error});
274         } else {
275                 foreach my $entry (@{ $rsr->{Results} }) {
276                         push @archipelagoes,
277                                 defined $entry->[1] ? undef : $entry->[0];
278                         push @islandids, $entry->[1];
279                 } 
280         }
281   }
282
283 } else { #---------- results - dropdowns ----------
284
285 my $argorundef= sub {
286         my ($dd,$base) = @_;
287         my $thing= $ARGS{"${base}${dd}"};
288         $thing= undef if defined $thing and $thing eq 'none';
289         return $thing;
290 };
291
292 for my $dd (0..$a{Dropdowns}-1) {
293         my $arch= $argorundef->($dd,'archipelago');
294         my $island= $argorundef->($dd,'islandid');
295         next unless defined $arch or defined $island;
296         if (defined $island and defined $arch) {
297                 my $ii= $islandid2{$island};
298                 my $iarch= $ii->{Arch};
299                 if ($iarch ne $arch) {
300                         $results_head->();
301 </%perl>
302  Specified archipelago <% $arch %> but
303  island <% $ii->{Name} %>
304  which is in <% $iarch %>; using the island.<br>
305 <%perl>
306                 }
307                 $arch= undef;
308         }
309         push @archipelagoes, $arch;
310         push @islandids, $island;
311 }
312
313 }#---------- result processing, common stuff
314 </%perl>
315
316 % if (@islandids) {
317 %       $results_head->();
318
319 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
320
321 % }
322
323 %#---------- debugging and epilogue ----------
324
325 % if ($debug) {
326 <p>
327 <pre id="debug_log">
328 Debug log:
329 </pre>
330 % }
331
332 <script type="text/javascript">
333 function debug (m) {
334 % if ($debug) {
335   var node= document.getElementById('debug_log');
336   node.innerHTML += "\n" + m + "\n";
337 % }
338 }
339 </script>
340
341 <%init>
342 use CommodsWeb;
343 use HTML::Entities;
344 use URI::Escape;
345
346 </%init>