/ anagram.js
anagram.js
 1  /*
 2   * Copyright © 2013 Jeff Epler <jepler@unpythonic.net>
 3   *
 4   * This program is free software; you can redistribute it and/or modify
 5   * it under the terms of the GNU General Public License as published by
 6   * the Free Software Foundation; either version 2 of the License, or
 7   * (at your option) any later version.
 8   *
 9   * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  var request = null, lastquery = '';
19  
20  function updatejax() {
21      if(!request) return;
22      if(request.readyState > 1) {
23          var response = '';
24          try {
25              if(request.response)
26                  response = request.response;
27              else if(request.responseText)
28                  response = request.responseText;
29          } catch(unused) {}
30          if(response != '') {
31              response = response.replace(/&/g, "&amp;");
32              response = response.replace(/</g, "&lt;");
33              response = response.replace(/>/g, "&gt;");
34              var el = document.getElementById('results');
35              if(el.outerHTML) {
36                  el.outerHTML = '<pre id="results">' + response + '</pre>';
37              } else {
38                  el.innerHTML = response;
39              }
40          }
41          if(request.readyState == 4) {
42              clearjax();
43          }
44      }
45  }
46  
47  function clearjax() {
48      request = null;
49      mayjax();
50  }
51  
52  function makejax() {
53      try { return new XMLHttpRequest(); } catch(e) {}
54      try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
55      return null;
56  }
57  
58  function queuejax (suffix) {
59      var query = $('#query').val();
60      lastquery = query;
61      query = query.replace(/[=<>-]\s*$/, '')
62      var loc = (
63          document.location.toString().replace(/[?#].*$/, "")
64          + '?' + $.param({'p': 1, 'q': query + suffix}));
65      request = makejax();
66      request.open("GET", loc, true);
67      try {
68          request.overrideMimeType('text/plain')
69      } catch (unused) {}
70      request.onreadystatechange = updatejax;
71      request.onprogress = updatejax;
72      request.onloadend = updatejax;
73      request.onload = updatejax;
74      request.onerror = updatejax;
75      request.send(null);
76      if(window && window.history && window.history.replaceState)
77          window.history.replaceState({}, '',
78              document.location.toString().replace(/[?#].*$/, "")
79              + '?' + $.param({'q': query}));
80  }
81  
82  function fulljax (e) {
83      if(request) request.abort();
84      queuejax('');
85      if(e) e.preventDefault();
86      return false;
87  }
88  
89  function mayjax() {
90      if(request) return;
91      var query = $('#query').val();
92      if(query == '' || query == lastquery) return;
93      queuejax(' -50');
94  }
95  
96  lastquery = $('#query').val();
97  $('#query').keyup(mayjax).change(mayjax);
98  $('#f').submit(fulljax);