chiark / gitweb /
Merge branch 'stable-3.x'
[ypp-sc-tools.db-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 my $checker= $m->fetch_comp("check_${thingstring}");
53 my $significant_nonempty= $checker->attr_exists('significant_nonempty');
54
55 </%perl>
56
57 <&| script &>
58 <%$p%>uri= "qtextstringcheck?format=application/json&ctype=text/xml"
59                 + "&what=<% $thingstring %>"
60                 + "&ocean=<% uri_escape($qa->{Ocean}) %>";
61
62 <%$p%>timeout=false;
63 <%$p%>request=false;
64 <%$p%>done=<% $significant_nonempty ? "null" : "''" %>;
65 <%$p%>needed='';
66 function <%$p%>Later(){
67   window.clearTimeout(<%$p%>timeout);
68   <%$p%>timeout = window.setTimeout(<%$p%>Needed, 500);
69 }
70 function <%$p%>Needed(){
71   window.clearTimeout(<%$p%>timeout);
72   <%$p%>element= document.getElementById('<% $thingstring %>');
73   <%$p%>needed= <%$p%>element.value;
74   <%$p%>Request();
75 }
76 function <%$p%>Request(){
77   if (<%$p%>request || <%$p%>needed==<%$p%>done) return;
78   <%$p%>done= <%$p%>needed;
79   <%$p%>request= new XMLHttpRequest();
80   uri= <%$p%>uri+'&string='+encodeURIComponent(<%$p%>needed);
81   <%$p%>request.open('GET', uri);
82   <%$p%>request.onreadystatechange= <%$p%>Ready;
83   <%$p%>request.send(null);
84 }
85 function <%$p%>Ready() {
86   if (<%$p%>request.readyState != 4) return;
87   if (<%$p%>request.status == 200) {
88     response= <%$p%>request.responseText;
89     debug('<%$p%> got '+response);
90     eval('results='+response);
91     toedit= document.getElementById('<%$p%>results');
92     toedit.innerHTML= results.show;
93   }
94   <%$p%>request= false;
95   <%$p%>Request();
96 }
97 register_onload(<%$p%>Needed);
98 </&script>
99
100 % if (!printable($m)) {
101 <input type="text" <% $m->content %>
102  id="<% $thingstring %>" name="<% $thingstring %>"
103  onchange="<%$p%>Needed();" onkeyup="<%$p%>Later();"
104  value="<% $stringval |h %>"
105  ><% defined($helpref) ? "<a href=\"docs#$helpref\">[?]</a>" : '' %>
106 <br>
107 <div id="<%$p%>results">&nbsp;</div><br>
108 % } else {
109 <kbd><strong><% $stringval |h %></strong></kbd>
110 <br>
111 <br>
112 % }
113
114 <%perl>
115 if ($significant_nonempty || length $thingstring) {
116         my ($emsg,$canonstring,@results)= $m->comp('qtextstringcheck',
117                 what => $thingstring,
118                 ocean => $qa->{Ocean},
119                 string => $stringval,
120                 format => 'return'
121         );
122         if (defined $emsg and length $emsg) {
123                 $$emsgstore='' unless defined $$emsgstore;
124                 $$emsgstore .= $emsg. ' ';
125         }
126         $onresults->(@results);
127 }
128 </%perl>