1 /* $Id: search.js,v 1.10 2007-01-15 04:34:29 quinn Exp $
2 * ---------------------------------------------------
14 var targetsloaded = false;
20 var session_cells = Array('query', 'startrec', 'action_type');
21 var old_session = session_read();
24 var cur_termlist = "subject";
26 function initialize ()
33 function GetXmlHttpObject()
36 if (window.XMLHttpRequest)
38 objXMLHttp=new XMLHttpRequest()
40 else if (window.ActiveXObject)
42 objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
47 function SendXmlHttpObject(obj, url, handler)
49 obj.onreadystatechange=handler;
54 function session_started()
56 if (xinitSession.readyState != 4)
58 var xml = xinitSession.responseXML;
59 var sesid = xml.getElementsByTagName("session")[0].childNodes[0].nodeValue;
60 document.getElementById("status").innerHTML = "Live";
62 setTimeout(ping_session, 50000);
65 function start_session()
67 xinitSession = GetXmlHttpObject();
68 var url="search.pz2?";
69 url += "command=init";
70 xinitSession.onreadystatechange=session_started;
71 xinitSession.open("GET", url);
72 xinitSession.send(null);
74 //url_surveillence = setInterval(session_check, 200);
77 function ping_session()
81 var url = "search.pz2?command=ping&session=" + session;
82 SendXmlHttpObject(xpingSession = GetXmlHttpObject(), url, session_pinged);
85 function session_pinged()
87 if (xpingSession.readyState != 4)
89 var xml = xpingSession.responseXML;
90 var error = xml.getElementsByTagName("error");
93 var msg = error[0].childNodes[0].nodeValue;
98 setTimeout(ping_session, 50000);
101 function targets_loaded()
103 if (xloadTargets.readyState != 4)
105 var xml = xloadTargets.responseXML;
106 var error = xml.getElementsByTagName("error");
109 var msg = error[0].childNodes[0].nodeValue;
113 document.getElementById("targetstatus").innerHTML = "Targets loaded";
116 function load_targets()
118 var fn = document.getElementById("targetfilename").value;
119 clearTimeout(termtimer);
120 clearTimeout(searchtimer);
121 clearTimeout(stattimer);
122 clearTimeout(showtimer);
123 document.getElementById("stat").innerHTML = "";
126 alert("Please enter a target definition file name");
129 var url="search.pz2?" +
131 "&session=" + session +
133 document.getElementById("targetstatus").innerHTML = "Loading targets...";
134 xloadTargets = GetXmlHttpObject();
135 xloadTargets.onreadystatechange=targets_loaded;
136 xloadTargets.open("GET", url);
137 xloadTargets.send(null);
141 function update_action (new_action) {
142 document.search.action_type.value = new_action;
146 function show_records()
148 if (xshow.readyState != 4)
151 var xml = xshow.responseXML;
152 var body = document.getElementById("body");
153 var hits = xml.getElementsByTagName("hit");
154 if (!hits[0]) // We should never get here with blocking operations
156 body.innerHTML = "No records yet";
157 searchtimer = setTimeout(check_search, 250);
162 var total = Number(xml.getElementsByTagName('total')[0].childNodes[0].nodeValue);
163 var merged = Number(xml.getElementsByTagName('merged')[0].childNodes[0].nodeValue);
164 var start = Number(xml.getElementsByTagName('start')[0].childNodes[0].nodeValue);
165 var num = Number(xml.getElementsByTagName('num')[0].childNodes[0].nodeValue);
166 var clients = Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
167 body.innerHTML = '<b>Records : ';
168 body.innerHTML += (start + 1) + ' to ' + (start + num) +
169 ' of ' + merged + ' (total hits: ' + total + ')</b>';
171 if (start + num < merged)
172 body.innerHTML += ' <a href="" ' +
173 'onclick="document.search.startrec.value=' + (start + recstoshow) +
174 ";update_action('page')" +
175 ';check_search(); update_history(); return false;">Next</a>';
178 body.innerHTML += ' <a href="" ' +
179 'onclick="document.search.startrec.value=' + (start - recstoshow) +
180 ";update_action('page')" +
181 ';check_search(); update_history();return false;">Previous</a>';
183 body.innerHTML += '<br/>';
184 for (i = 0; i < hits.length; i++)
186 body.innerHTML += '<p>';
187 body.innerHTML += (i + start + 1) + ': ';
188 var mk = hits[i].getElementsByTagName("md-title");
190 body.innerHTML += mk[0].childNodes[0].nodeValue;
191 body.innerHTML += '</p>';
197 searchtimer = setTimeout(check_search, 1000);
199 searchtimer = setTimeout(check_search, 2000);
203 termtimer = setTimeout(check_termlist, 1000);
206 function check_search()
208 clearTimeout(searchtimer);
209 var url = "search.pz2?" +
211 "&start=" + document.search.startrec.value +
212 "&num=" + recstoshow +
213 "&session=" + session +
216 xshow = GetXmlHttpObject();
217 xshow.onreadystatechange=show_records;
218 xshow.open("GET", url);
223 function refine_query (obj) {
224 var query_cell = document.getElementById('query');
225 var term = obj.innerHTML;
227 term = term.replace(/[\(\)]/g, '');
228 if (cur_termlist == 'subject')
229 query_cell.value += ' and su=(' + term + ')';
230 else if (cur_termlist == 'author')
231 query_cell.value += ' and au=(' + term + ')';
235 function set_termlist(termlist)
237 cur_termlist = termlist;
241 clearTimeout(termtimer);
246 function show_termlistoptions(body)
249 Array('subject', 'Subject'),
250 Array('author', 'Author')
255 if (opts[i][0] == cur_termlist)
256 body.innerHTML += opts[i][1];
258 body.innerHTML += '<a href="" onclick="set_termlist(\'' + opts[i][0] +
259 '\'); return false">' + opts[i][1] + '</a>';
260 body.innerHTML += ' ';
262 body.innerHTML += '<p>';
265 function show_termlist()
267 if (xtermlist.readyState != 4)
271 var xml = xtermlist.responseXML;
272 var body = document.getElementById("termlist");
273 var hits = xml.getElementsByTagName("term");
275 Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
278 termtimer = setTimeout(check_termlist, 1000);
282 body.innerHTML = "<b>Limit results:</b><br>";
283 show_termlistoptions(body);
284 for (i = 0; i < hits.length; i++)
286 var namen = hits[i].getElementsByTagName("name");
288 body.innerHTML += '<a href="#" onclick="refine_query(this)">' +
289 namen[0].childNodes[0].nodeValue +
291 body.innerHTML += '<br>';
294 termtimer = setTimeout(check_termlist, 2000);
298 function check_termlist()
300 var url = "search.pz2?" +
302 "&session=" + session +
303 "&name=" + cur_termlist;
304 xtermlist = GetXmlHttpObject();
305 xtermlist.onreadystatechange=show_termlist;
306 xtermlist.open("GET", url);
307 xtermlist.send(null);
312 if (xstat.readyState != 4)
315 var xml = xstat.responseXML;
316 var body = document.getElementById("stat");
317 var nodes = xml.childNodes[0].childNodes;
319 Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
322 stattimer = setTimeout(check_stat, 500);
326 body.innerHTML = "(";
327 for (i = 0; i < nodes.length; i++)
329 if (nodes[i].nodeType != 1)
331 var value = nodes[i].childNodes[0].nodeValue;
334 var name = nodes[i].nodeName;
335 body.innerHTML += ' ' + name + '=' + value;
337 body.innerHTML += ')';
339 stattimer = setTimeout(check_stat, 2000);
343 function check_stat()
345 var url = "search.pz2?" +
347 "&session=" + session;
348 xstat = GetXmlHttpObject();
349 xstat.onreadystatechange=show_stat;
350 xstat.open("GET", url);
354 function search_started()
356 if (xsearch.readyState != 4)
358 var xml = xsearch.responseXML;
359 var error = xml.getElementsByTagName("error");
362 var msg = error[0].childNodes[0].nodeValue;
367 stattimer = setTimeout(check_stat, 1000);
370 function start_search()
372 clearTimeout(termtimer);
374 clearTimeout(searchtimer);
376 clearTimeout(stattimer);
378 clearTimeout(showtimer);
382 alert("Please load targets first");
385 var query = escape(document.getElementById('query').value);
386 var url = "search.pz2?" +
388 "&session=" + session +
390 xsearch = GetXmlHttpObject();
391 xsearch.onreadystatechange=search_started;
392 xsearch.open("GET", url);
394 document.getElementById("termlist").innerHTML = '';
395 document.getElementById("body").innerHTML = '';
398 document.search.startrec.value = 0;
402 function session_encode ()
407 for (i = 0; i < session_cells.length; i++)
409 var name = session_cells[i];
410 var value = escape(document.getElementById(name).value);
411 session += '&' + name + '=' + value;
418 function session_restore (session)
420 var fields = session.split(/&/);
423 for (i = 1; i < fields.length; i++)
425 var pair = fields[i].split(/=/);
426 var key = pair.shift();
427 var value = pair.join('=');
428 var cell = document.getElementById(key);
436 function session_read ()
438 var ses = window.location.hash.replace(/^#/, '');
443 function session_store (new_value)
445 window.location.hash = '#' + new_value;
449 function update_history ()
451 var session = session_encode();
452 session_store(session);
453 old_session = session;
457 function session_check ()
459 var session = session_read();
460 var action = document.search.action_type.value;
462 clearInterval(url_surveillence);
464 if ( session != unescape(old_session) )
466 session_restore(session);
468 if (action == 'search') {
470 } else if (action == 'page') {
473 alert('Unregocnized action_type: ' + action);
478 url_surveillence = setInterval(session_check, 200);