1 /* $Id: search.js,v 1.14 2007-06-20 19:27:18 adam 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="/pazpar2/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 = "/pazpar2/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="/pazpar2/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, 2000);
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>';
196 searchtimer = setTimeout(check_search, 1000);
198 searchtimer = setTimeout(check_search, 2000);
204 termtimer = setTimeout(check_termlist, 2000);
207 function check_search()
209 clearTimeout(searchtimer);
210 var url = "/pazpar2/search.pz2?" +
212 "&start=" + document.search.startrec.value +
213 "&num=" + recstoshow +
214 "&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.firstChild.nodeValue;
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, 2000);
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 = "/pazpar2/search.pz2?" +
302 "&session=" + session +
304 "&name=" + cur_termlist;
305 xtermlist = GetXmlHttpObject();
306 xtermlist.onreadystatechange=show_termlist;
307 xtermlist.open("GET", url);
308 xtermlist.send(null);
313 if (xstat.readyState != 4)
316 var xml = xstat.responseXML;
317 var body = document.getElementById("stat");
318 var nodes = xml.childNodes[0].childNodes;
320 Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
323 stattimer = setTimeout(check_stat, 500);
327 body.innerHTML = "(";
328 for (i = 0; i < nodes.length; i++)
330 if (nodes[i].nodeType != 1)
332 var value = nodes[i].childNodes[0].nodeValue;
335 var name = nodes[i].nodeName;
336 body.innerHTML += ' ' + name + '=' + value;
338 body.innerHTML += ')';
340 stattimer = setTimeout(check_stat, 2000);
344 function check_stat()
346 var url = "/pazpar2/search.pz2?" +
348 "&session=" + session;
349 xstat = GetXmlHttpObject();
350 xstat.onreadystatechange=show_stat;
351 xstat.open("GET", url);
355 function search_started()
357 if (xsearch.readyState != 4)
359 var xml = xsearch.responseXML;
360 var error = xml.getElementsByTagName("error");
363 var msg = error[0].childNodes[0].nodeValue;
368 stattimer = setTimeout(check_stat, 1000);
371 function start_search()
374 clearTimeout(termtimer);
376 clearTimeout(searchtimer);
378 clearTimeout(stattimer);
380 clearTimeout(showtimer);
384 alert("Please load targets first");
387 var query = escape(document.getElementById('query').value);
388 var url = "/pazpar2/search.pz2?" +
390 "&session=" + session +
392 xsearch = GetXmlHttpObject();
393 xsearch.onreadystatechange=search_started;
394 xsearch.open("GET", url);
396 document.getElementById("termlist").innerHTML = '';
397 document.getElementById("body").innerHTML = '';
399 document.search.startrec.value = 0;
403 function session_encode ()
408 for (i = 0; i < session_cells.length; i++)
410 var name = session_cells[i];
411 var value = escape(document.getElementById(name).value);
412 session += '&' + name + '=' + value;
419 function session_restore (session)
421 var fields = session.split(/&/);
424 for (i = 1; i < fields.length; i++)
426 var pair = fields[i].split(/=/);
427 var key = pair.shift();
428 var value = pair.join('=');
429 var cell = document.getElementById(key);
437 function session_read ()
439 var ses = window.location.hash.replace(/^#/, '');
444 function session_store (new_value)
446 window.location.hash = '#' + new_value;
450 function update_history ()
452 var session = session_encode();
453 session_store(session);
454 old_session = session;
458 function session_check ()
460 var session = session_read();
461 var action = document.search.action_type.value;
463 clearInterval(url_surveillence);
465 if ( session != unescape(old_session) )
467 session_restore(session);
469 if (action == 'search') {
471 } else if (action == 'page') {
474 alert('Unregocnized action_type: ' + action);
479 url_surveillence = setInterval(session_check, 200);