chiark / gitweb /
Fix nonunique island/arch name reporting
[ypp-sc-tools.main.git] / yarrg / web / pirate-route
1 <html><head><title>Specify route</title></head><body>
2
3 <%perl>
4 my %a;
5 my @vars;
6
7 @vars= ({       Name => 'Ocean',
8                 Before => 'Ocean: ',
9                 CmpCanon => sub { ucfirst lc $_[0] },
10                 Values => [ ocean_list() ]
11         }, {    Name => 'Dropdowns',
12                 Before => 'Interface: ',
13                 CmpCanon => sub { !!$_[0] },
14                 Values => [     [ 0, 'Type in names' ],
15                                 [ 4, 'Select from menus' ] ]
16         });
17
18 foreach my $var (@vars) {
19         my $name= $var->{Name};
20         $var->{Before}= '' unless exists $var->{Before};
21         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
22         foreach my $val (@{ $var->{Values} }) {
23                 next if ref $val;
24                 $val= [ $val, encode_entities($val) ];
25         }
26         if (exists $ARGS{lc $name}) {
27                 $a{$name}= $ARGS{lc $name};
28         } else {
29                 $a{$name}= $var->{Values}[0][0];
30         }
31 }
32
33 foreach my $var (@vars) {
34         my $name= $var->{Name};
35         my $delim= $var->{Before};
36         my $canon= &{$var->{CmpCanon}}($a{$name});
37         my $cvalix= 0;
38         foreach my $valr (@{ $var->{Values} }) {
39                 print $delim;  $delim= "\n|\n";
40                 my ($value,$html) = @$valr;
41                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
42                 my $after;
43                 if ($iscurrent) {
44                         print '<b>';
45                         $after= '</b>';
46                 } else {
47                         my %qf= ();
48                         foreach my $innerr (@vars) {
49                                 my $n= lc $innerr->{Name};
50                                 if ($n ne lc $name) {
51                                         next unless exists $ARGS{$n};
52                                         $qf{$n}= $ARGS{$n};
53                                 } else {
54                                         next if !$cvalix;
55                                         $qf{$n}= $value;
56                                 }
57                         }
58                         my $uri= URI->new($m->current_comp()->name());
59                         $uri->query_form(%qf);
60                         print '<a href="', $uri->path_query(), '">';
61                         $after= '</a>';
62                 }
63                 print $html, $after;
64                 $cvalix++;
65         }
66         print '<p>';
67 }
68
69 die "unknown ocean $a{Ocean} ?"
70         unless grep { $_ eq $a{Ocean} } ocean_list();
71
72 db_setocean($a{Ocean});
73 db_connect();
74
75 </%perl>
76
77 <h1>Specify route</h1>
78 <form action="/ucgi/~clareb/mason/something" method="get">
79
80 % if (!$a{Dropdowns}) {
81 Enter route (islands, or archipelagoes, separated by |s or commas;
82  abbreviations are OK):<br/>
83
84 <script type="text/javascript">
85 tr_uri= "routetextstring?format=json&type=text/xml"
86                 + "&ocean=<% uri_escape($a{Ocean}) %>";
87
88 tr_timeout=false;
89 tr_request=false;
90 tr_done='';
91 tr_needed='';
92 function tr_Later(){
93   window.clearTimeout(tr_timeout);
94   tr_timeout = window.setTimeout(tr_Needed, 500);
95 }
96 function tr_Needed(){
97   window.clearTimeout(tr_timeout);
98   tr_element= document.getElementById('routestring');
99   tr_needed= tr_element.value;
100   tr_Request();
101 }
102 function tr_Request(){
103   if (tr_request || tr_needed==tr_done) return;
104   tr_done= tr_needed;
105   tr_request= new XMLHttpRequest();
106   uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
107   tr_request.open('GET', uri);
108   tr_request.onreadystatechange= tr_Ready;
109   tr_request.send(null);
110 }
111 function tr_Ready() {
112   if (tr_request.readyState != 4) return;
113   if (tr_request.status == 200) {
114     response= tr_request.responseText;
115     eval('results='+response);
116     toedit= document.getElementById('routeresults');
117     toedit.innerHTML= results['show'];
118   }
119   tr_request= false;
120   tr_Request();
121 }
122 </script>
123
124 <input type="text" id="routestring" name="routestring" size=80
125  onchange="tr_Needed();"
126  onkeyup="tr_Later();"><br>
127 <div id="routeresults">&nbsp;</div><br/>
128 % } else {
129
130 <%perl>
131 my $sth=$dbh->prepare("SELECT islandid,islandname,archipelago
132                               FROM islands
133                               ORDER BY islandname;");
134 $sth->execute();
135 my $row;
136 my $islandlistdata='';
137 my %archmap=();
138 while ($row=$sth->fetchrow_arrayref) {
139         $islandlistdata.=
140                 sprintf('<option value="%s">%s</option>',
141                         map { encode_entities($_) } @$row[0..1]);
142         $archmap{$row->[0]}= $row->[2];
143 }
144
145 $sth=$dbh->prepare("SELECT DISTINCT archipelago FROM islands
146                            ORDER BY archipelago;");
147 $sth->execute();
148 my $archlistdata='';
149
150 while ($row=$sth->fetchrow_arrayref) {
151         $archlistdata.=
152                 sprintf('<option value="%s">%s</option>',
153                         map { encode_entities($_) } (@$row, @$row));
154 }
155 </%perl>
156
157 <script type="text/javascript">
158 sel_archmap= <% to_json(\%archmap) %>;
159 function setarch(dd) {
160   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
161   var select= document.getElementsByName('islandid'+dd).item(0);
162   var nodes= select.getElementsByTagName('option');
163   for (var i=0; i<nodes.length; i++) {
164     node= nodes.item(i);
165     value= node.getAttribute('value');
166     if (value == 'none') {
167       if (arch == 'none') {
168         node.innerHTML= 'Select island ...';
169       } else {
170         node.innerHTML= 'Whole arch';
171       }
172     } else {
173 //      alert('node i='+i+' arch='+arch+' value='+value+'.');
174       node.setAttribute('disabled', !(arch=='none' || value==arch));
175     }
176   }
177 }
178 </script>
179
180 <table>
181
182 <tr>
183 %       for my $dd (0..$a{Dropdowns}-1) {
184 <td><select name="archipelago<% $dd %>" onchange="setarch(<% $dd %>)">
185 <option name="none">Whole ocean</option>
186 <% $archlistdata %></select></td>
187 %       }
188 </tr>
189
190 <tr>
191 %       for my $dd (0..$a{Dropdowns}-1) {
192 <td><select name="islandid<% $dd %>">
193 <option name="none">Select island...</option>
194 <% $islandlistdata %></select></td>
195 %       }
196 </tr>
197
198 </table>
199 % }
200
201 <input type=submit name=submit value="Go">
202 </form>
203
204 <%init>
205 use CommodsWeb;
206 use HTML::Entities;
207 use URI::Escape;
208
209 </%init>