1 /* $Id: search.js,v 1.10 2007-01-10 09:19:05 sondberg Exp $
2 * ---------------------------------------------------
14 var targetsloaded = false;
20 var session_cells = Array('query', 'startrec', 'action_type');
21 var old_session = session_read();
24 var page_window = 5; // Number of pages prior to and after the current page
28 function initialize ()
30 facet_list = get_available_facets();
36 function GetXmlHttpObject()
39 if (window.XMLHttpRequest)
41 objXMLHttp=new XMLHttpRequest()
43 else if (window.ActiveXObject)
45 objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
50 function SendXmlHttpObject(obj, url, handler)
52 obj.onreadystatechange=handler;
57 function session_started()
59 if (xinitSession.readyState != 4)
61 var xml = xinitSession.responseXML;
62 var sesid = xml.getElementsByTagName("session")[0].childNodes[0].nodeValue;
63 document.getElementById("status").innerHTML = "Live";
65 setTimeout(ping_session, 50000);
68 function start_session()
70 xinitSession = GetXmlHttpObject();
71 var url="search.pz2?";
72 url += "command=init";
73 xinitSession.onreadystatechange=session_started;
74 xinitSession.open("GET", url);
75 xinitSession.send(null);
78 function ping_session()
82 var url = "search.pz2?command=ping&session=" + session;
83 SendXmlHttpObject(xpingSession = GetXmlHttpObject(), url, session_pinged);
86 function session_pinged()
88 if (xpingSession.readyState != 4)
90 var xml = xpingSession.responseXML;
91 var error = xml.getElementsByTagName("error");
94 var msg = error[0].childNodes[0].nodeValue;
99 setTimeout(ping_session, 50000);
102 function targets_loaded()
104 if (xloadTargets.readyState != 4)
106 var xml = xloadTargets.responseXML;
107 var error = xml.getElementsByTagName("error");
110 var msg = error[0].childNodes[0].nodeValue;
114 document.getElementById("targetstatus").innerHTML = "Targets loaded";
117 function load_targets()
119 var fn = document.getElementById("targetfilename").value;
120 clearTimeout(termtimer);
121 clearTimeout(searchtimer);
122 clearTimeout(stattimer);
123 clearTimeout(showtimer);
124 document.getElementById("stat").innerHTML = "";
127 alert("Please enter a target definition file name");
130 var url="search.pz2?" +
132 "&session=" + session +
134 document.getElementById("targetstatus").innerHTML = "Loading targets...";
135 xloadTargets = GetXmlHttpObject();
136 xloadTargets.onreadystatechange=targets_loaded;
137 xloadTargets.open("GET", url);
138 xloadTargets.send(null);
142 function update_action (new_action) {
143 document.search.action_type.value = new_action;
147 function make_pager (hits, offset, max) {
151 for (off = offset - page_window * max;
152 off < hits && off < (offset + page_window * max);
160 var p = off / max + 1;
162 if ((offset >= off) && (offset < (off + max)))
163 class = ' class="select"';
165 html += '<a href="#" ' + class +
166 'onclick="update_offset(' + off + ')">' + p + '</a>\n';
173 function update_offset (offset) {
174 document.search.startrec.value = offset;
175 update_action('page');
182 function show_records()
184 if (xshow.readyState != 4)
187 var xml = xshow.responseXML;
188 var body = document.getElementById("body");
189 var hits = xml.getElementsByTagName("hit");
190 if (!hits[0]) // We should never get here with blocking operations
192 body.innerHTML = "No records yet";
193 searchtimer = setTimeout(check_search, 250);
198 var total = Number(xml.getElementsByTagName('total')[0].childNodes[0].nodeValue);
199 var merged = Number(xml.getElementsByTagName('merged')[0].childNodes[0].nodeValue);
200 var start = Number(xml.getElementsByTagName('start')[0].childNodes[0].nodeValue);
201 var num = Number(xml.getElementsByTagName('num')[0].childNodes[0].nodeValue);
202 var clients = Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
203 body.innerHTML = '<div class="pages">' +
204 make_pager(merged, start, recstoshow) +
207 body.innerHTML += '<div class="results">Records : ' + (start + 1) +
208 ' to ' + (start + num) + ' of ' + merged +
209 ' (total hits: ' + total + ')</div><br/><br/>';
212 if (start + num < merged)
213 body.innerHTML += ' <a href="" ' +
214 'onclick="document.search.startrec.value=' + (start + recstoshow) +
215 ";update_action('page')" +
216 ';check_search(); update_history(); return false;">Next</a>';
219 body.innerHTML += ' <a href="" ' +
220 'onclick="document.search.startrec.value=' + (start - recstoshow) +
221 ";update_action('page')" +
222 ';check_search(); update_history();return false;">Previous</a>';
224 body.innerHTML += '<br/>';
226 body.innerHTML += '<div class="records">';
228 for (i = 0; i < hits.length; i++)
230 var mk = hits[i].getElementsByTagName("md-title");
231 var html = '<a href="#" class="record">';
235 field = mk[0].childNodes[0].nodeValue;
238 html += field + '</a>';
239 body.innerHTML += html;
242 body.innerHTML += '</div>';
247 searchtimer = setTimeout(check_search, 1000);
249 searchtimer = setTimeout(check_search, 2000);
253 termtimer = setTimeout(check_termlist, 500);
256 function check_search()
258 clearTimeout(searchtimer);
259 var url = "search.pz2?" +
261 "&start=" + document.search.startrec.value +
262 "&num=" + recstoshow +
263 "&session=" + session +
265 xshow = GetXmlHttpObject();
266 xshow.onreadystatechange=show_records;
267 xshow.open("GET", url);
272 function refine_query (obj) {
273 var query_cell = document.getElementById('query');
274 var term = obj.innerHTML;
276 term = term.replace(/[\(\)]/g, '');
277 if (cur_termlist == 'subject')
278 query_cell.value += ' and su=(' + term + ')';
279 else if (cur_termlist == 'author')
280 query_cell.value += ' and au=(' + term + ')';
286 function show_termlist()
288 if (xtermlist.readyState != 4)
292 var xml = xtermlist.responseXML;
293 var body = facet_list[cur_facet][1];
294 var hits = xml.getElementsByTagName("term");
296 Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
300 if (cur_facet >= facet_list.length)
305 termtimer = setTimeout(check_termlist, 500);
311 for (i = 0; i < hits.length; i++)
313 var namen = hits[i].getElementsByTagName("name");
315 body.innerHTML += '<a href="#" onclick="refine_query(this)">' +
316 namen[0].childNodes[0].nodeValue +
321 termtimer = setTimeout(check_termlist, 1000);
325 function check_termlist()
327 var facet_name = facet_list[cur_facet][0];
328 var url = "search.pz2?" +
330 "&session=" + session +
331 "&name=" + facet_name;
332 xtermlist = GetXmlHttpObject();
333 xtermlist.onreadystatechange=show_termlist;
334 xtermlist.open("GET", url);
335 xtermlist.send(null);
340 if (xstat.readyState != 4)
343 var xml = xstat.responseXML;
344 var body = document.getElementById("stat");
345 var nodes = xml.childNodes[0].childNodes;
347 Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
350 stattimer = setTimeout(check_stat, 500);
354 body.innerHTML = "(";
355 for (i = 0; i < nodes.length; i++)
357 if (nodes[i].nodeType != 1)
359 var value = nodes[i].childNodes[0].nodeValue;
362 var name = nodes[i].nodeName;
363 body.innerHTML += ' ' + name + '=' + value;
365 body.innerHTML += ')';
367 stattimer = setTimeout(check_stat, 2000);
371 function check_stat()
373 var url = "search.pz2?" +
375 "&session=" + session;
376 xstat = GetXmlHttpObject();
377 xstat.onreadystatechange=show_stat;
378 xstat.open("GET", url);
382 function search_started()
384 if (xsearch.readyState != 4)
386 var xml = xsearch.responseXML;
387 var error = xml.getElementsByTagName("error");
390 var msg = error[0].childNodes[0].nodeValue;
395 stattimer = setTimeout(check_stat, 1000);
398 function start_search()
400 clearTimeout(termtimer);
402 clearTimeout(searchtimer);
404 clearTimeout(stattimer);
406 clearTimeout(showtimer);
410 alert("Please load targets first");
413 var query = escape(document.getElementById('query').value);
414 var url = "search.pz2?" +
416 "&session=" + session +
418 xsearch = GetXmlHttpObject();
419 xsearch.onreadystatechange=search_started;
420 xsearch.open("GET", url);
422 // document.getElementById("termlist").innerHTML = '';
423 document.getElementById("body").innerHTML = '';
426 document.search.startrec.value = 0;
430 function session_encode ()
435 for (i = 0; i < session_cells.length; i++)
437 var name = session_cells[i];
438 var value = escape(document.getElementById(name).value);
439 session += '&' + name + '=' + value;
446 function session_restore (session)
448 var fields = session.split(/&/);
451 for (i = 1; i < fields.length; i++)
453 var pair = fields[i].split(/=/);
454 var key = pair.shift();
455 var value = pair.join('=');
456 var cell = document.getElementById(key);
464 function session_read ()
466 var ses = window.location.hash.replace(/^#/, '');
471 function session_store (new_value)
473 window.location.hash = '#' + new_value;
477 function update_history ()
479 var session = session_encode();
480 session_store(session);
481 old_session = session;
485 function session_check ()
487 var session = session_read();
488 var action = document.search.action_type.value;
490 clearInterval(url_surveillence);
492 if ( session != unescape(old_session) )
494 session_restore(session);
496 if (action == 'search') {
498 } else if (action == 'page') {
501 alert('Unregocnized action_type: ' + action);
506 url_surveillence = setInterval(session_check, 200);
510 function get_available_facets () {
511 var facet_container = document.getElementById('termlists');
512 var facet_cells = facet_container.childNodes;
513 var facets = Array();
516 for (i = 0; i < facet_cells.length; i++) {
517 var cell = facet_cells.item(i);
519 if (cell.className == 'facet') {
520 var facet_name = cell.id.replace(/^facet_([^_]+)_terms$/, "$1");
521 facets.push(Array(facet_name, cell));
529 function get_facet_container (obj) {
530 return document.getElementById(obj.id + '_terms');
534 function toggle_facet (obj) {
535 var container = get_facet_container(obj);
537 if (obj.className == 'selected') {
538 obj.className = 'unselected';
539 container.style.display = 'inline';
541 obj.className = 'selected';
542 container.style.display = 'none';