chiark / gitweb /
route menu selection mode seems to work
[ypp-sc-tools.db-test.git] / yarrg / web / pirate-route
1 <html><head><title>Specify route</title></head><body>
2
3 <script type="text/javascript">
4 function debug (m) {
5 % if ($debug) {
6   var node= document.getElementById('debug_log');
7   node.innerHTML += "\n" + m + "\n";
8 % }
9 }
10 </script>
11
12 <%perl>
13 my %a;
14 my @vars;
15
16 @vars= ({       Name => 'Ocean',
17                 Before => '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' ] ]
25         });
26
27 foreach my $var (@vars) {
28         my $name= $var->{Name};
29         $var->{Before}= '' unless exists $var->{Before};
30         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
31         foreach my $val (@{ $var->{Values} }) {
32                 next if ref $val;
33                 $val= [ $val, encode_entities($val) ];
34         }
35         if (exists $ARGS{lc $name}) {
36                 $a{$name}= $ARGS{lc $name};
37         } else {
38                 $a{$name}= $var->{Values}[0][0];
39         }
40 }
41
42 foreach my $var (@vars) {
43         my $name= $var->{Name};
44         my $delim= $var->{Before};
45         my $canon= &{$var->{CmpCanon}}($a{$name});
46         my $cvalix= 0;
47         foreach my $valr (@{ $var->{Values} }) {
48                 print $delim;  $delim= "\n|\n";
49                 my ($value,$html) = @$valr;
50                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
51                 my $after;
52                 if ($iscurrent) {
53                         print '<b>';
54                         $after= '</b>';
55                 } else {
56                         my %qf= ();
57                         foreach my $innerr (@vars) {
58                                 my $n= lc $innerr->{Name};
59                                 if ($n ne lc $name) {
60                                         next unless exists $ARGS{$n};
61                                         $qf{$n}= $ARGS{$n};
62                                 } else {
63                                         next if !$cvalix;
64                                         $qf{$n}= $value;
65                                 }
66                         }
67                         my $uri= URI->new($m->current_comp()->name());
68                         $qf{'debug'}=1 if $debug;
69                         $uri->query_form(%qf);
70                         print '<a href="', $uri->path_query(), '">';
71                         $after= '</a>';
72                 }
73                 print $html, $after;
74                 $cvalix++;
75         }
76         print "<p>\n\n";
77 }
78
79 die "unknown ocean $a{Ocean} ?"
80         unless grep { $_ eq $a{Ocean} } ocean_list();
81
82 db_setocean($a{Ocean});
83 db_connect();
84
85 </%perl>
86 <%args>
87 $debug => 0
88 </%args>
89
90 <h1>Specify route</h1>
91 <form action="/ucgi/~clareb/mason/something" method="get">
92
93 % if (!$a{Dropdowns}) {
94 Enter route (islands, or archipelagoes, separated by |s or commas;
95  abbreviations are OK):<br/>
96
97 <script type="text/javascript">
98 tr_uri= "routetextstring?format=json&type=text/xml"
99                 + "&ocean=<% uri_escape($a{Ocean}) %>";
100
101 tr_timeout=false;
102 tr_request=false;
103 tr_done='';
104 tr_needed='';
105 function tr_Later(){
106   window.clearTimeout(tr_timeout);
107   tr_timeout = window.setTimeout(tr_Needed, 500);
108 }
109 function tr_Needed(){
110   window.clearTimeout(tr_timeout);
111   tr_element= document.getElementById('routestring');
112   tr_needed= tr_element.value;
113   tr_Request();
114 }
115 function tr_Request(){
116   if (tr_request || tr_needed==tr_done) return;
117   tr_done= tr_needed;
118   tr_request= new XMLHttpRequest();
119   uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
120   tr_request.open('GET', uri);
121   tr_request.onreadystatechange= tr_Ready;
122   tr_request.send(null);
123 }
124 function tr_Ready() {
125   if (tr_request.readyState != 4) return;
126   if (tr_request.status == 200) {
127     response= tr_request.responseText;
128     eval('results='+response);
129     toedit= document.getElementById('routeresults');
130     toedit.innerHTML= results.show;
131   }
132   tr_request= false;
133   tr_Request();
134 }
135 </script>
136
137 <input type="text" id="routestring" name="routestring" size=80
138  onchange="tr_Needed();"
139  onkeyup="tr_Later();"><br>
140 <div id="routeresults">&nbsp;</div><br/>
141 % } else {
142
143 <%perl>
144 my ($sth,$row);;
145 my $archlistdata='';
146 my %islandlistdata;
147 $islandlistdata{'none'}= <<END;
148 <option value="none">Select island...</option>
149 END
150
151 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
152                             ORDER BY archipelago;");
153 $sth->execute();
154
155 while ($row=$sth->fetchrow_arrayref) {
156         my ($arch)= @$row;
157         $archlistdata.=
158                 sprintf('<option value="%s">%s</option>',
159                         map { encode_entities($_) } ($arch,$arch));
160         $islandlistdata{$arch}= <<END;
161 <option value="none">Whole arch</option>
162 END
163 }
164
165 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
166                             FROM islands
167                             ORDER BY islandname;");
168 $sth->execute();
169
170 while ($row=$sth->fetchrow_arrayref) {
171         my $arch= $row->[2];
172         my $here= sprintf('<option value="%s">%s</option>',
173                         map { encode_entities($_) } @$row[0..1]);
174         $islandlistdata{'none'} .= $here;
175         $islandlistdata{$arch} .= $here;
176 }
177
178 </%perl>
179
180 <script type="text/javascript">
181 ms_lists= <% to_json(\%islandlistdata) %>;
182 function ms_Setarch(dd) {
183   debug('ms_SetArch '+dd+' arch='+arch);
184   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
185   var got= ms_lists[arch];
186   if (got == undefined) return; // unknown arch ?  hrm
187   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
188   var select= document.getElementsByName('islandid'+dd).item(0);
189   select.innerHTML= got;
190   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
191 }
192 </script>
193
194 % if (0) {
195   var nodes= select.getElementsByTagName('option');
196 alert(ms_lists[dd]);
197   for (var i=0; i<nodes.length; i++) {
198     node= nodes.item(i);
199     value= node.getAttribute('value');
200     debug('ms_Setarch '+dd+' i='+i+' arch='+arch+' value='+value+'.');
201     if (value == 'none') {
202       if (arch == 'none') {
203         node.innerHTML= 'Select island ...';
204       } else {
205       }
206     } else {
207 //      alert('node i='+i+' arch='+arch+' value='+value+'.');
208       node.setAttribute('disabled', !(arch=='none' || value==arch));
209     }
210   }
211 }
212 % }
213
214 % if (0) {
215 <style type="text/css">
216 table
217 {
218 table-layout: fixed;
219 color: red;
220 }
221 pre
222 {
223 color:yellow;
224 }
225 </style>
226 % }
227
228 <table style="table-layout:fixed; width:90%;">
229
230 <tr>
231 %       for my $dd (0..$a{Dropdowns}-1) {
232 <td>
233 <select  name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
234 %# style="min-width:250px;"
235 <option value="none">Whole ocean</option>
236 <% $archlistdata %></select></td>
237 %       }
238 </tr>
239
240 <tr>
241 %       for my $dd (0..$a{Dropdowns}-1) {
242 <td><select name="islandid<% $dd %>">
243 <option value="none">Select island...</option>
244 <% $islandlistdata{'none'} %></select></td>
245 %       }
246 </tr>
247
248 </table>
249
250 <input type=text name=zork onclick="ms_PrepAll()">
251
252 % if (0) {
253 <script type="text/javascript">
254 function ms_Prep(dd) {
255   //debug('ms_Prep'+dd);
256   var allnode= document.getElementsByName('islandid'+dd).item(0);
257   var newentry= {'none': allnode.innerHTML};
258   
259   ms_lists[dd]= newentry;
260 }
261 function ms_PrepAll() {
262   //debug('now');
263 %       for my $dd (0..$a{Dropdowns}-1) {
264   ms_Prep('<% $dd %>');
265 %       }
266   //debug('ms_PrepAll done '+ms_lists);
267 }
268 ms_PrepAll();
269 </script>
270 % }
271
272 % }
273
274 <input type=submit name=submit value="Go">
275 </form>
276
277 % if ($debug) {
278 <p>
279 <pre id="debug_log">
280 Debug log:
281 </pre>
282 % }
283
284
285 <%init>
286 use CommodsWeb;
287 use HTML::Entities;
288 use URI::Escape;
289
290 </%init>