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)$/;
60 next if $val eq 'none';
64 my $uri= URI->new($m->current_comp()->name());
65 my $quri= sub { $uri->query_form(@_); $uri->path_query(); };
67 foreach my $var (@vars) {
68 my $name= $var->{Name};
69 my $lname= lc $var->{Name};
70 my $delim= $var->{Before};
71 my $canon= &{$var->{CmpCanon}}($a{$name});
73 foreach my $valr (@{ $var->{Values} }) {
74 print $delim; $delim= "\n|\n";
75 my ($value,$html) = @$valr;
76 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
82 my %qf= (%baseqf,%queryqf);
84 $qf{$lname}= $value if $cvalix;
85 print '<a href="',$quri->(%qf),'">';
94 #---------- initial checks, startup, main entry form ----------
96 dbw_connect($a{Ocean});
104 <h1>Specify route</h1>
105 <form action="<% $quri->() %>" method="get">
107 %#---------- textbox, user enters route as string ----------
108 % if (!$a{Dropdowns}) {
110 Enter route (islands, or archipelagoes, separated by |s or commas;
111 abbreviations are OK):<br/>
113 <script type="text/javascript">
114 tr_uri= "routetextstring?format=json&type=text/xml"
115 + "&ocean=<% uri_escape($a{Ocean}) %>";
122 window.clearTimeout(tr_timeout);
123 tr_timeout = window.setTimeout(tr_Needed, 500);
125 function tr_Needed(){
126 window.clearTimeout(tr_timeout);
127 tr_element= document.getElementById('routestring');
128 tr_needed= tr_element.value;
131 function tr_Request(){
132 if (tr_request || tr_needed==tr_done) return;
134 tr_request= new XMLHttpRequest();
135 uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
136 tr_request.open('GET', uri);
137 tr_request.onreadystatechange= tr_Ready;
138 tr_request.send(null);
140 function tr_Ready() {
141 if (tr_request.readyState != 4) return;
142 if (tr_request.status == 200) {
143 response= tr_request.responseText;
144 eval('results='+response);
145 toedit= document.getElementById('routeresults');
146 toedit.innerHTML= results.show;
151 window.onload= tr_Needed;
154 <input type="text" id="routestring" name="routestring" size=80
155 value="<% $routestring |h %>"
156 onchange="tr_Needed();"
157 onkeyup="tr_Later();"><br>
158 <div id="routeresults"> </div><br/>
160 % } else { #---------- dropdowns, user selects from menus ----------
166 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
168 my $optionlistmap= sub {
169 my ($optlist, $selected) = @_;
171 foreach my $entry (@$optlist) {
172 $out.= sprintf('<option value="%s" %s>%s</option>',
173 encode_entities($entry->[0]),
174 defined $selected && $entry->[0] eq $selected
176 encode_entities($entry->[1]));
181 my $dbh= dbw_connect($a{Ocean});
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 ];
202 $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
205 my %resetislandlistdata;
206 foreach my $arch (keys %islandlistdata) {
207 $resetislandlistdata{$arch}=
208 $optionlistmap->($islandlistdata{$arch}, '');
213 <input type=hidden name=dropdowns value="<% $a{Dropdowns} %>">
215 <script type="text/javascript">
216 ms_lists= <% to_json(\%resetislandlistdata) %>;
217 function ms_Setarch(dd) {
218 debug('ms_SetArch '+dd+' arch='+arch);
219 var arch= document.getElementsByName('archipelago'+dd).item(0).value;
220 var got= ms_lists[arch];
221 if (got == undefined) return; // unknown arch ? hrm
222 debug('ms_SetArch '+dd+' arch='+arch+' got ok');
223 var select= document.getElementsByName('islandid'+dd).item(0);
224 select.innerHTML= got;
225 debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
229 <table style="table-layout:fixed; width:90%;">
232 % for my $dd (0..$a{Dropdowns}-1) {
234 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
235 <option value="none">Whole ocean</option>
236 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
241 % for my $dd (0..$a{Dropdowns}-1) {
242 % my $arch= $ARGS{"archipelago$dd"};
243 % $arch= 'none' if !defined $arch;
245 <select name="islandid<% $dd %>">
246 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
253 % } #---------- end of dropdowns, now common middle of page code ----------
255 <input type=submit name=submit value="Go">
259 #========== result computations ==========
263 print "<h1>Results</h1>\n";
264 $results_head= sub { };
267 #---------- result computation - textstring ----------
268 if (!$a{Dropdowns}) {
269 if (length $routestring) {
271 my $rsr= $m->comp('routetextstring',
273 string => $routestring,
276 if (length $rsr->{Error}) {
277 print encode_entities($rsr->{Error});
279 foreach my $entry (@{ $rsr->{Results} }) {
281 defined $entry->[1] ? undef : $entry->[0];
282 push @islandids, $entry->[1];
287 } else { #---------- results - dropdowns ----------
289 my $argorundef= sub {
291 my $thing= $ARGS{"${base}${dd}"};
292 $thing= undef if defined $thing and $thing eq 'none';
296 for my $dd (0..$a{Dropdowns}-1) {
297 my $arch= $argorundef->($dd,'archipelago');
298 my $island= $argorundef->($dd,'islandid');
299 next unless defined $arch or defined $island;
300 if (defined $island and defined $arch) {
301 my $ii= $islandid2{$island};
302 my $iarch= $ii->{Arch};
303 if ($iarch ne $arch) {
306 Specified archipelago <% $arch %> but
307 island <% $ii->{Name} %>
308 which is in <% $iarch %>; using the island.<br>
313 push @archipelagoes, $arch;
314 push @islandids, $island;
317 }#---------- result processing, common stuff
323 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
327 %#---------- debugging and epilogue ----------
336 <script type="text/javascript">
339 var node= document.getElementById('debug_log');
340 node.innerHTML += "\n" + m + "\n";