chiark / gitweb /
Break out code for dbw_lookup_string; support capacity adjustments in terms of commod...
[ypp-sc-tools.web-live.git] / yarrg / web / qtextstring
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
9   YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10   The YARRG website is covered by the GNU Affero GPL v3 or later, which
11    basically means that every installation of the website will let you
12    download the source.
13
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Affero General Public License as
16  published by the Free Software Foundation, either version 3 of the
17  License, or (at your option) any later version.
18
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU Affero General Public License for more details.
23
24  You should have received a copy of the GNU Affero General Public License
25  along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28  are used without permission.  This program is not endorsed or
29  sponsored by Three Rings.
30
31
32  This Mason component handles analysis of text string entries, including
33  both the AJAX calls from web page javascript and the entry validation
34  and processing calls from other components.
35
36
37 </%doc>
38 <%args>
39 $qa => $m->caller_args(1)->{'qa'}
40 $dbh
41 $thingstring
42 $emsgstore
43 $onresults
44 $prefix => 'ts';
45 $helpref => undef;
46 </%args>
47 <%perl>
48 my $stringval= $qa->{$thingstring};
49 $stringval='' if !defined $stringval;
50
51 my $p= $prefix.'_';
52 </%perl>
53
54 <&| script &>
55 <%$p%>uri= "qtextstringcheck?format=application/json&ctype=text/xml"
56                 + "&what=<% $thingstring %>"
57                 + "&ocean=<% uri_escape($qa->{Ocean}) %>";
58
59 <%$p%>timeout=false;
60 <%$p%>request=false;
61 <%$p%>done='';
62 <%$p%>needed='';
63 function <%$p%>Later(){
64   window.clearTimeout(<%$p%>timeout);
65   <%$p%>timeout = window.setTimeout(<%$p%>Needed, 500);
66 }
67 function <%$p%>Needed(){
68   window.clearTimeout(<%$p%>timeout);
69   <%$p%>element= document.getElementById('<% $thingstring %>');
70   <%$p%>needed= <%$p%>element.value;
71   <%$p%>Request();
72 }
73 function <%$p%>Request(){
74   if (<%$p%>request || <%$p%>needed==<%$p%>done) return;
75   <%$p%>done= <%$p%>needed;
76   <%$p%>request= new XMLHttpRequest();
77   uri= <%$p%>uri+'&string='+encodeURIComponent(<%$p%>needed);
78   <%$p%>request.open('GET', uri);
79   <%$p%>request.onreadystatechange= <%$p%>Ready;
80   <%$p%>request.send(null);
81 }
82 function <%$p%>Ready() {
83   if (<%$p%>request.readyState != 4) return;
84   if (<%$p%>request.status == 200) {
85     response= <%$p%>request.responseText;
86     debug('<%$p%> got '+response);
87     eval('results='+response);
88     toedit= document.getElementById('<%$p%>results');
89     toedit.innerHTML= results.show;
90   }
91   <%$p%>request= false;
92   <%$p%>Request();
93 }
94 register_onload(<%$p%>Needed);
95 </&script>
96
97 <input type="text" <% $m->content %>
98  id="<% $thingstring %>" name="<% $thingstring %>"
99  onchange="<%$p%>Needed();" onkeyup="<%$p%>Later();"
100  value="<% $stringval |h %>"
101  ><% defined($helpref) ? "<a href=\"docs#$helpref\">[?]</a>" : '' %>
102 <br>
103 <div id="<%$p%>results">&nbsp;</div><br>
104
105 <%perl>
106 if (length $thingstring) {
107         my ($emsg,$canonstring,@results)= $m->comp('qtextstringcheck',
108                 what => $thingstring,
109                 ocean => $qa->{Ocean},
110                 string => $stringval,
111                 format => 'return'
112         );
113         if (defined $emsg and length $emsg) {
114                 $$emsgstore='' unless defined $$emsgstore;
115                 $$emsgstore .= $emsg. ' ';
116         }
117         $onresults->(@results);
118 }
119 </%perl>