11 #---------- "mode" argument parsing and mode menu at top of page ----------
13 # for debugging, invoke as
14 # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
16 @vars= ({ Name => 'Ocean',
18 CmpCanon => sub { ucfirst lc $_[0] },
19 Values => [ ocean_list() ]
20 }, { Name => 'Dropdowns',
21 Before => 'Interface: ',
22 CmpCanon => sub { !!$_[0] },
23 Values => [ [ 0, 'Type in names' ],
24 [ 4, 'Select from menus' ] ]
27 Values => [ [ 'route', 'Trades for route' ],
28 [ 'age', 'Data age' ] ]
31 foreach my $var (@vars) {
32 my $name= $var->{Name};
34 $var->{Before}= '' unless exists $var->{Before};
35 $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
36 foreach my $val (@{ $var->{Values} }) {
38 $val= [ $val, encode_entities($val) ];
40 if (exists $ARGS{$lname}) {
41 $a{$name}= $ARGS{$lname};
42 my @html= grep { $_->[0] eq $a{$name} } @{ $var->{Values} };
43 $ahtml{$name}= @html==1 ? $html[0][1] : '???';
45 $a{$name}= $var->{Values}[0][0];
46 $ahtml{$name}= $var->{Values}[0][1];
51 <html><head><title><% ucfirst $ahtml{Query} %> - YARRG</title></head><body>
53 <a href="<% $m->current_comp()->name() %>">YARRG</a> -
54 Yet Another Revenue Research Gatherer
59 foreach my $var (@vars) {
60 my $lname= lc $var->{Name};
61 next unless exists $ARGS{$lname};
62 $baseqf{$lname}= $ARGS{$lname};
66 foreach my $var (keys %ARGS) {
68 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
70 next if $val eq 'none';
75 my $uri= URI->new($m->current_comp()->name());
80 foreach my $var (@vars) {
81 my $name= $var->{Name};
82 my $lname= lc $var->{Name};
83 my $delim= $var->{Before};
84 my $canon= &{$var->{CmpCanon}}($a{$name});
86 foreach my $valr (@{ $var->{Values} }) {
87 print $delim; $delim= "\n|\n";
88 my ($value,$html) = @$valr;
89 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
95 my %qf= (%baseqf,%queryqf);
97 $qf{$lname}= $value if $cvalix;
98 print '<a href="',$quri->(%qf),'">';
107 #---------- initial checks, startup, main entry form ----------
109 dbw_connect($a{Ocean});
117 %########### query `route' ##########
118 % if ($a{Query} eq 'route') {
120 <h1>Specify route</h1>
121 <form action="<% $quri->() %>" method="get">
123 %#---------- textbox, user enters route as string ----------
124 % if (!$a{Dropdowns}) {
126 Enter route (islands, or archipelagoes, separated by |s or commas;
127 abbreviations are OK):<br/>
129 <script type="text/javascript">
130 tr_uri= "routetextstring?format=json&type=text/xml"
131 + "&ocean=<% uri_escape($a{Ocean}) %>";
138 window.clearTimeout(tr_timeout);
139 tr_timeout = window.setTimeout(tr_Needed, 500);
141 function tr_Needed(){
142 window.clearTimeout(tr_timeout);
143 tr_element= document.getElementById('routestring');
144 tr_needed= tr_element.value;
147 function tr_Request(){
148 if (tr_request || tr_needed==tr_done) return;
150 tr_request= new XMLHttpRequest();
151 uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
152 tr_request.open('GET', uri);
153 tr_request.onreadystatechange= tr_Ready;
154 tr_request.send(null);
156 function tr_Ready() {
157 if (tr_request.readyState != 4) return;
158 if (tr_request.status == 200) {
159 response= tr_request.responseText;
160 eval('results='+response);
161 toedit= document.getElementById('routeresults');
162 toedit.innerHTML= results.show;
167 window.onload= tr_Needed;
170 <input type="text" id="routestring" name="routestring" size=80
171 value="<% $routestring |h %>"
172 onchange="tr_Needed();"
173 onkeyup="tr_Later();"><br>
174 <div id="routeresults"> </div><br/>
176 % } else { #---------- dropdowns, user selects from menus ----------
182 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
184 my $optionlistmap= sub {
185 my ($optlist, $selected) = @_;
187 foreach my $entry (@$optlist) {
188 $out.= sprintf('<option value="%s" %s>%s</option>',
189 encode_entities($entry->[0]),
190 defined $selected && $entry->[0] eq $selected
192 encode_entities($entry->[1]));
197 my $dbh= dbw_connect($a{Ocean});
199 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
200 ORDER BY archipelago;");
203 while ($row=$sth->fetchrow_arrayref) {
205 push @archlistdata, [ $arch, $arch ];
206 $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
209 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
211 ORDER BY islandname;");
214 while ($row=$sth->fetchrow_arrayref) {
216 push @{ $islandlistdata{'none'} }, [ @$row ];
217 push @{ $islandlistdata{$arch} }, [ @$row ];
218 $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
221 my %resetislandlistdata;
222 foreach my $arch (keys %islandlistdata) {
223 $resetislandlistdata{$arch}=
224 $optionlistmap->($islandlistdata{$arch}, '');
229 <input type=hidden name=dropdowns value="<% $a{Dropdowns} %>">
231 <script type="text/javascript">
232 ms_lists= <% to_json(\%resetislandlistdata) %>;
233 function ms_Setarch(dd) {
234 debug('ms_SetArch '+dd+' arch='+arch);
235 var arch= document.getElementsByName('archipelago'+dd).item(0).value;
236 var got= ms_lists[arch];
237 if (got == undefined) return; // unknown arch ? hrm
238 debug('ms_SetArch '+dd+' arch='+arch+' got ok');
239 var select= document.getElementsByName('islandid'+dd).item(0);
240 select.innerHTML= got;
241 debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
245 <table style="table-layout:fixed; width:90%;">
248 % for my $dd (0..$a{Dropdowns}-1) {
250 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
251 <option value="none">Whole ocean</option>
252 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
257 % for my $dd (0..$a{Dropdowns}-1) {
258 % my $arch= $ARGS{"archipelago$dd"};
259 % $arch= 'none' if !defined $arch;
261 <select name="islandid<% $dd %>">
262 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
269 % } #---------- end of dropdowns, now common middle of page code ----------
271 <input type=submit name=submit value="Go">
275 #========== result computations ==========
279 print "<h1>Results</h1>\n";
280 $results_head= sub { };
283 #---------- result computation - textstring ----------
284 if (!$a{Dropdowns}) {
285 if (length $routestring) {
287 my $rsr= $m->comp('routetextstring',
289 string => $routestring,
292 if (length $rsr->{Error}) {
293 print encode_entities($rsr->{Error});
295 foreach my $entry (@{ $rsr->{Results} }) {
297 defined $entry->[1] ? undef : $entry->[0];
298 push @islandids, $entry->[1];
303 } else { #---------- results - dropdowns ----------
305 my $argorundef= sub {
307 my $thing= $ARGS{"${base}${dd}"};
308 $thing= undef if defined $thing and $thing eq 'none';
312 for my $dd (0..$a{Dropdowns}-1) {
313 my $arch= $argorundef->($dd,'archipelago');
314 my $island= $argorundef->($dd,'islandid');
315 next unless defined $arch or defined $island;
316 if (defined $island and defined $arch) {
317 my $ii= $islandid2{$island};
318 my $iarch= $ii->{Arch};
319 if ($iarch ne $arch) {
322 Specified archipelago <% $arch %> but
323 island <% $ii->{Name} %>
324 which is in <% $iarch %>; using the island.<br>
329 push @archipelagoes, $arch;
330 push @islandids, $island;
333 }#---------- result processing, common stuff
339 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
343 % } elsif ($a{Query} eq 'age') {
344 % ########### query `age' ##########
346 <h1>Market data age</h1>
348 % } ########## end of `age' query ##########
350 %#---------- debugging and epilogue ----------
359 <script type="text/javascript">
362 var node= document.getElementById('debug_log');
363 node.innerHTML += "\n" + m + "\n";