1 <html><head><title>Route - YARRG</title></head><body>
3 <a href="<% $m->current_comp()->name() %>">YARRG</a> -
4 Yet Another Revenue Research Gatherer
16 #---------- "mode" argument parsing and mode menu at top of page ----------
18 # for debugging, invoke as
19 # http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
21 @vars= ({ Name => 'Ocean',
23 CmpCanon => sub { ucfirst lc $_[0] },
24 Values => [ ocean_list() ]
25 }, { Name => 'Dropdowns',
26 Before => 'Interface: ',
27 CmpCanon => sub { !!$_[0] },
28 Values => [ [ 0, 'Type in names' ],
29 [ 4, 'Select from menus' ] ]
32 foreach my $var (@vars) {
33 my $name= $var->{Name};
35 $var->{Before}= '' unless exists $var->{Before};
36 $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
37 foreach my $val (@{ $var->{Values} }) {
39 $val= [ $val, encode_entities($val) ];
41 if (exists $ARGS{$lname}) {
42 $a{$name}= $ARGS{$lname};
44 $a{$name}= $var->{Values}[0][0];
49 foreach my $var (@vars) {
50 my $lname= lc $var->{Name};
51 next unless exists $ARGS{$lname};
52 $baseqf{$lname}= $ARGS{$lname};
56 foreach my $var (keys %ARGS) {
58 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
59 $queryqf{$var}= $ARGS{$var};
62 my $uri= URI->new($m->current_comp()->name());
63 my $quri= sub { $uri->query_form(@_); $uri->path_query(); };
65 foreach my $var (@vars) {
66 my $name= $var->{Name};
67 my $lname= lc $var->{Name};
68 my $delim= $var->{Before};
69 my $canon= &{$var->{CmpCanon}}($a{$name});
71 foreach my $valr (@{ $var->{Values} }) {
72 print $delim; $delim= "\n|\n";
73 my ($value,$html) = @$valr;
74 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
80 my %qf= (%baseqf,%queryqf);
82 $qf{$lname}= $value if $cvalix;
83 print '<a href="',$quri->(%qf),'">';
92 #---------- initial checks, startup, main entry form ----------
94 dbw_connect($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 my $dbh= dbw_connect($a{Ocean});
181 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
182 ORDER BY archipelago;");
185 while ($row=$sth->fetchrow_arrayref) {
187 push @archlistdata, [ $arch, $arch ];
188 $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
191 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
193 ORDER BY islandname;");
196 while ($row=$sth->fetchrow_arrayref) {
198 push @{ $islandlistdata{'none'} }, [ @$row ];
199 push @{ $islandlistdata{$arch} }, [ @$row ];
200 $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
203 my %resetislandlistdata;
204 foreach my $arch (keys %islandlistdata) {
205 $resetislandlistdata{$arch}=
206 $optionlistmap->($islandlistdata{$arch}, '');
211 <input type=hidden name=dropdowns value="<% $a{Dropdowns} %>">
213 <script type="text/javascript">
214 ms_lists= <% to_json(\%resetislandlistdata) %>;
215 function ms_Setarch(dd) {
216 debug('ms_SetArch '+dd+' arch='+arch);
217 var arch= document.getElementsByName('archipelago'+dd).item(0).value;
218 var got= ms_lists[arch];
219 if (got == undefined) return; // unknown arch ? hrm
220 debug('ms_SetArch '+dd+' arch='+arch+' got ok');
221 var select= document.getElementsByName('islandid'+dd).item(0);
222 select.innerHTML= got;
223 debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
227 <table style="table-layout:fixed; width:90%;">
230 % for my $dd (0..$a{Dropdowns}-1) {
232 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
233 <option value="none">Whole ocean</option>
234 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
239 % for my $dd (0..$a{Dropdowns}-1) {
240 % my $arch= $ARGS{"archipelago$dd"};
241 % $arch= 'none' if !defined $arch;
243 <select name="islandid<% $dd %>">
244 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
251 % } #---------- end of dropdowns, now common middle of page code ----------
253 <input type=submit name=submit value="Go">
257 #========== result computations ==========
261 print "<h1>Results</h1>\n";
262 $results_head= sub { };
265 #---------- result computation - textstring ----------
266 if (!$a{Dropdowns}) {
267 if (length $routestring) {
269 my $rsr= $m->comp('routetextstring',
271 string => $routestring,
274 if (length $rsr->{Error}) {
275 print encode_entities($rsr->{Error});
277 foreach my $entry (@{ $rsr->{Results} }) {
279 defined $entry->[1] ? undef : $entry->[0];
280 push @islandids, $entry->[1];
285 } else { #---------- results - dropdowns ----------
287 my $argorundef= sub {
289 my $thing= $ARGS{"${base}${dd}"};
290 $thing= undef if defined $thing and $thing eq 'none';
294 for my $dd (0..$a{Dropdowns}-1) {
295 my $arch= $argorundef->($dd,'archipelago');
296 my $island= $argorundef->($dd,'islandid');
297 next unless defined $arch or defined $island;
298 if (defined $island and defined $arch) {
299 my $ii= $islandid2{$island};
300 my $iarch= $ii->{Arch};
301 if ($iarch ne $arch) {
304 Specified archipelago <% $arch %> but
305 island <% $ii->{Name} %>
306 which is in <% $iarch %>; using the island.<br>
311 push @archipelagoes, $arch;
312 push @islandids, $island;
315 }#---------- result processing, common stuff
321 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
325 %#---------- debugging and epilogue ----------
334 <script type="text/javascript">
337 var node= document.getElementById('debug_log');
338 node.innerHTML += "\n" + m + "\n";