1 /* completion.js - Suggestions for input line in forms
2 * Copyright (c) 2007 Martin Schulze <joey@infodrom.org>
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.
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.
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, USA.
19 /* This file provides some completion/suggestion for an input field in a form
22 - The input field in question is named "keyword"
23 - Suggestions will be emitted into a <div> named "suggest"
24 - The lookup URIs need to accept a keyword= parameter
25 - The lookup URIs need to accept GET requests
26 - Set the variable completion_func_complete to a function that will
27 return the base url for keyword lookup that is only missing the
28 keyword=%s parameter (i.e. add other form fields if they exist)
29 - Set the variable completion_func_lookup to a function that will
30 return the base url for the form action which is only missing the
31 keyword=%s parameter (i.e. add other form fields if they exist)
32 - Install the suggestion function in the form on the keypress event:
33 onkeypress="document.forms[0].keyword.onkeypress = complete;">
37 <form method="get" action="lookup.php" onsubmit="return completion_check();">
38 <input type="text" size="48" maxlength="64" id="keyword" name="keyword"
39 onkeyup="complete();">
40 <div class="suggest" id="suggest">Empty...</div>
44 var suggestion = new Array();
46 var completion_func_complete;
47 var completion_func_lookup;
49 function completion_dummy()
51 alert ("If you see this, Joey's completion.js is not configured properly");
55 function completion_info(bug)
57 window.alert (bug + " You'll need to provide at least a keyword to search for.");
61 function completion_check()
63 var form = document.forms[0];
65 if (!form.keyword.value.length) {
67 key = prompt ("Please provide a keyword to search for.");
69 return completion_info ("No keyword supplied.");
71 form.keyword.value = key;
77 function completion_display (req, id)
79 if (req.readyState == 4 && req.status == 200) {
80 suggestion = eval (req.responseText);
81 var info = document.getElementById (id);
83 if (suggestion.length) {
85 var urlbase = completion_func_lookup();
88 info.style.visibility = "hidden";
92 for (var i = 0; i < suggestion.length; i++) {
95 out += '<a href="' + urlbase + 'keyword='
96 + encodeURIComponent(suggestion[i])
97 + '">' + suggestion[i] + '</a>';
100 info.innerHTML = out;
101 info.style.visibility = "visible";
103 info.style.visibility = "hidden";
114 keyword = document.forms[0].keyword.value;
116 if (!keyword.length) {
117 document.getElementById("suggest").style.visibility = "hidden";
121 if (keyword.length > 1 && keyword.length > oldkeyword.length && !suggestion.length)
124 oldkeyword = keyword;
125 req = new XMLHttpRequest();
126 urlbase = completion_func_complete();
128 if (!req || !urlbase) {
129 document.getElementById("suggest").style.visibility = "hidden";
133 req.open ("GET", urlbase + "keyword=" + encodeURIComponent(keyword));
135 req.onreadystatechange = function() { completion_display (req, "suggest"); }
140 completion_func_complete = dummy;
141 completion_func_lookup = dummy;