1 /* $Id: search.js,v 1.53 2007-04-11 02:11:29 quinn Exp $
2 * ---------------------------------------------------
15 var targetsloaded = false;
21 var session_cells = Array('query', 'startrec', 'action_type');
22 var old_session = session_read();
25 var page_window = 5; // Number of pages prior to and after the current page
27 var cur_sort = "relevance";
33 function initialize ()
35 facet_list = get_available_facets();
41 function GetXmlHttpObject()
44 if (window.XMLHttpRequest)
46 objXMLHttp=new XMLHttpRequest()
48 else if (window.ActiveXObject)
50 objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
55 function SendXmlHttpObject(obj, url, handler)
57 obj.onreadystatechange=handler;
62 function session_started()
64 if (xinitSession.readyState != 4)
66 var xml = xinitSession.responseXML;
67 var sesid = xml.getElementsByTagName("session")[0].childNodes[0].nodeValue;
68 assign_text(document.getElementById("status"), 'Live');
70 setTimeout(ping_session, 50000);
73 function start_session()
75 xinitSession = GetXmlHttpObject();
76 var url="search.pz2?";
77 url += "command=init";
78 xinitSession.onreadystatechange=session_started;
79 xinitSession.open("GET", url);
80 xinitSession.send(null);
83 function ping_session()
87 var url = "search.pz2?command=ping&session=" + session;
88 SendXmlHttpObject(xpingSession = GetXmlHttpObject(), url, session_pinged);
91 function session_pinged()
93 if (xpingSession.readyState != 4)
95 var xml = xpingSession.responseXML;
96 var error = xml.getElementsByTagName("error");
100 setTimeout(ping_session, 50000);
103 function update_action (new_action) {
104 document.search.action_type.value = new_action;
108 function make_pager (hits, offset, max) {
111 var start_offset = offset - page_window * max;
112 var div_elem = document.createElement('div');
114 div_elem.className = 'pages';
116 if (start_offset < 0) {
120 for (off = start_offset;
121 off < hits && off < (start_offset + 2 * page_window * max);
124 var p = off / max + 1;
125 var page_elem = create_element('a', p);
126 var newline_node = document.createTextNode(' ');
128 if ((offset >= off) && (offset < (off + max))) {
129 page_elem.className = 'select';
132 page_elem.setAttribute('off', off);
133 page_elem.style.cursor = 'pointer';
134 page_elem.onclick = function () {
135 update_offset(this.getAttribute('off'));
138 div_elem.appendChild(page_elem);
139 div_elem.appendChild(newline_node);
146 function update_offset (offset) {
147 clearTimeout(searchtimer);
148 document.search.startrec.value = offset;
149 update_action('page');
156 function create_element (name, cdata) {
157 var elem_node = document.createElement(name);
158 elem_node.innerHTML = cdata;
164 function clear_cell (cell) {
165 while (cell.hasChildNodes())
166 cell.removeChild(cell.firstChild);
170 function append_text(cell, text) {
171 text_node = document.createTextNode(text);
172 cell.appendChild(text_node);
176 function assign_text (cell, text) {
178 append_text(cell, text);
181 function set_sort_opt(n, opt, str)
183 var txt = document.createTextNode(str);
188 var a = document.createElement('a');
190 a.setAttribute('href', "");
191 a.setAttribute('id', opt);
192 a.onclick = function() { set_sort(this.getAttribute('id')); return false; };
197 function set_sort(sort)
199 if (sort && sort != cur_sort)
206 var t = document.getElementById("sortselect");
208 t.appendChild(document.createTextNode("Sort results by: "));
209 set_sort_opt(t, 'relevance', 'Relevance');
210 t.appendChild(document.createTextNode(" or "));
211 set_sort_opt(t, 'title:1', 'Title');
214 function displayname(name)
216 if (name == 'md-author')
218 else if (name == 'md-subject')
220 else if (name == 'md-date')
222 else if (name == 'md-isbn')
224 else if (name == 'md-publisher')
226 else if (name == 'md-url')
228 else if (name == 'md-title')
230 else if (name == 'md-id')
232 else if (name == 'md-description')
233 return 'Description';
234 else if (name == 'md-lccn')
236 else if (name == 'recid')
238 else if (name == 'location')
244 function hyperlink_field(name)
246 if (name == 'md-author')
248 else if (name == 'md-subject')
250 else if (name == 'md-url')
256 function paint_details_tr(name, dn)
259 var dname = displayname(name);
260 var ln = create_element('b', dname);
261 var tln = document.createElement('td');
265 var tr = document.createElement('tr');
274 var v2 = v1.replace(/\?.*$/, "");
275 var v3 = v2.replace(/http:\/\//, "");
279 function paint_data_elements(target, node)
281 var nodes = node.childNodes;
285 for (i = 0; i < nodes.length; i++)
287 if (nodes[i].nodeType != 1)
289 var name = nodes[i].nodeName;
290 if (name == 'recid' || name == 'md-title')
292 if (name != lastname && lastname != 'location')
296 var tr = paint_details_tr(lastname, dn);
297 target.appendChild(tr);
299 dn = document.createElement('td');
302 if (name == 'location')
304 dn = document.createElement('td');
305 dn.appendChild(paint_subrecord(nodes[i]));
306 target.appendChild(paint_details_tr('Location', dn));
309 if (!nodes[i].childNodes[0])
311 var value = nodes[i].childNodes[0].nodeValue;
312 if (dn.childNodes[0])
313 dn.appendChild(document.createTextNode('; '));
314 var hyl = hyperlink_field(name);
318 nv = create_element('a', cleanurl(value));
322 nv.target = '_blank';
327 nv.setAttribute('term', value);
328 nv.setAttribute('searchfield', hyl);
329 nv.onclick = function() { hyperlink_search(this); return false; };
332 else if (name == 'md-lccn')
334 nv = document.createElement('span');
335 nv.appendChild(document.createTextNode(value + ' '));
336 var link = create_element('a', 'Show title in LoC');
337 link.setAttribute('target', '_blank');
338 link.setAttribute('href', 'http://catalog.loc.gov/cgi-bin/Pwebrecon.cgi?DB=local&CNT=10&CMD=10+records+per+page&CMD=lccn+' + value);
339 nv.appendChild(link);
342 else if (name == 'md-isbn')
344 nv = document.createElement('span');
345 nv.appendChild(document.createTextNode(value + ' '));
346 var link = create_element('a', 'search OpenWorldCat');
347 link.setAttribute('target', '_blank');
348 link.setAttribute('href', 'http://www.worldcat.org/search?q=isbn%3A' + value);
349 nv.appendChild(link);
352 nv = document.createTextNode(value);
355 if (dn && lastname != 'location')
357 var tr = paint_details_tr(lastname, dn);
358 target.appendChild(tr);
362 function paint_subrecord(node)
364 var table = document.createElement('table');
365 var tbody = document.createElement('tbody');
366 var zurl = node.getAttribute('id');
367 var name = node.getAttribute('name');
371 td = create_element('td', name);
373 td = create_element('td', zurl);
374 tr = paint_details_tr('Source', td);
375 tbody.appendChild(tr);
376 paint_data_elements(tbody, node);
377 table.appendChild(tbody);
381 function paint_details(body, xml)
384 var table = document.createElement('table');
385 var tbody = document.createElement('tbody');
386 table.setAttribute('cellpadding', 2);
387 paint_data_elements(tbody, xml.childNodes[0]);
388 table.appendChild(tbody);
389 body.appendChild(table);
390 body.style.display = 'inline';
393 function show_details()
395 if (xfetchDetails.readyState != 4)
397 var xml = xfetchDetails.responseXML;
398 var error = xml.getElementsByTagName("error");
401 var msg = error[0].childNodes[0].nodeValue;
407 var idn = xml.getElementsByTagName('recid');
410 var id = idn[0].childNodes[0].nodeValue;
414 var nodes = document.getElementsByName('listrecord');
416 for (i = 0; i < nodes.length; i++)
418 var dets = nodes[i].getElementsByTagName('div');
420 dets[0].style.display = 'none';
423 var body = document.getElementById('rec_' + id);
426 paint_details(body, xml);
429 function hyperlink_search(obj)
431 var field = obj.getAttribute('searchfield');
432 var term = obj.getAttribute('term');
433 var queryfield = document.getElementById('query');
434 queryfield.value = field + '="' + term + '"';
438 function fetch_details(id)
448 var url = "search.pz2?session=" + session +
451 SendXmlHttpObject(xfetchDetails = GetXmlHttpObject(), url, show_details);
454 function show_records()
456 if (xshow.readyState != 4)
459 var xml = xshow.responseXML;
460 var body = document.getElementById("body");
461 var hits = xml.getElementsByTagName("hit");
465 if (!hits[0]) // We should never get here with blocking operations
467 assign_text(body, 'No records yet');
468 searchtimer = setTimeout(check_search, 250);
472 var total = Number(xml.getElementsByTagName('total')[0].childNodes[0].nodeValue);
473 var merged = Number(xml.getElementsByTagName('merged')[0].childNodes[0].nodeValue);
474 var start = Number(xml.getElementsByTagName('start')[0].childNodes[0].nodeValue);
475 var num = Number(xml.getElementsByTagName('num')[0].childNodes[0].nodeValue);
476 var clients = Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
477 var pager = make_pager(merged, start,recstoshow);
478 var break_node1 = document.createElement('br');
479 var break_node2 = document.createElement('br');
480 var record_container = document.createElement('div');
481 var interval = create_element('div', 'Records : ' + (start + 1) +
482 ' to ' + (start + num) + ' of ' +
483 merged + ' (total hits: ' +
486 interval.className = 'results';
487 record_container.className = 'records';
489 body.appendChild(pager);
490 body.appendChild(interval);
491 body.appendChild(break_node1);
492 body.appendChild(break_node2);
493 body.appendChild(record_container);
495 for (i = 0; i < hits.length; i++)
497 var tn = hits[i].getElementsByTagName("md-title");
499 var an = hits[i].getElementsByTagName("md-author");
501 var cn = hits[i].getElementsByTagName("count");
503 var idn = hits[i].getElementsByTagName("recid");
505 if (tn[0] && tn[0].childNodes[0]) {
506 title = tn[0].childNodes[0].nodeValue;
510 if (an[0] && an[0].childNodes[0])
511 author = an[0].childNodes[0].nodeValue;
513 count = Number(cn[0].childNodes[0].nodeValue);
514 var id = idn[0].childNodes[0].nodeValue;
516 var record_div = document.createElement('div');
517 record_div.className = 'record';
518 record_div.setAttribute('name', 'listrecord');
520 var record_cell = create_element('a', title);
521 record_cell.setAttribute('href', '#' + id);
522 record_cell.setAttribute('id', id);
523 //record_cell.onclick = function() { fetch_details(this.getAttribute('id')); return false; }
524 record_cell.onclick = function() { fetch_details(this.getAttribute('id')); return false; };
525 record_div.appendChild(record_cell);
528 record_div.appendChild(document.createTextNode(' by '));
529 var al = create_element('a', author);
530 al.setAttribute('href', '#');
531 al.setAttribute('term', author);
532 al.setAttribute('searchfield', 'au');
533 al.onclick = function() { hyperlink_search(this); return false; };
534 record_div.appendChild(al);
537 record_div.appendChild(document.createTextNode(
538 ' (' + count + ')'));
539 var det_div = document.createElement('div');
541 paint_details(det_div, cur_rec);
543 det_div.style.display = 'none';
544 det_div.setAttribute('id', 'rec_' + id);
545 det_div.setAttribute('name', 'details');
546 record_div.appendChild(det_div);
547 record_container.appendChild(record_div);
554 searchtimer = setTimeout(check_search, 1000);
556 searchtimer = setTimeout(check_search, 2000);
560 termtimer = setTimeout(check_termlist, 500);
563 function check_search()
565 clearTimeout(searchtimer);
566 var url = "search.pz2?" +
568 "&start=" + document.search.startrec.value +
569 "&num=" + recstoshow +
570 "&session=" + session +
571 "&sort=" + cur_sort +
573 xshow = GetXmlHttpObject();
574 xshow.onreadystatechange=show_records;
575 xshow.open("GET", url);
580 function refine_query (obj) {
581 var term = obj.getAttribute('term');
582 var cur_termlist = obj.getAttribute('facet');
583 var query_cell = document.getElementById('query');
584 var id = obj.getAttribute('target_id');
586 term = term.replace(/[\(\)]/g, '');
588 if (cur_termlist == 'subject')
589 query_cell.value += ' and su="' + term + '"';
590 else if (cur_termlist == 'author')
591 query_cell.value += ' and au="' + term + '"';
592 else if (cur_termlist == 'date')
593 query_cell.value += ' and date="' + term + '"';
594 else if (cur_termlist == 'xtargets')
595 filter ='pz:id=' + id;
600 function clear_termlists()
603 for (i = 0; i < facet_list.length; i++)
604 clear_cell(facet_list[i][1]);
607 function show_termlists()
609 if (xtermlist.readyState != 4)
613 var xml = xtermlist.responseXML;
615 Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
616 var lists = xml.getElementsByTagName("list");
618 for (i = 0; i < lists.length; i++)
620 var listname = lists[i].getAttribute('name');
621 var body = document.getElementById('facet_' + listname + '_terms');
622 if (body.style.display == 'none')
625 var terms = lists[i].getElementsByTagName('term');
627 for (t = 0; t < terms.length; t++)
629 var namen = terms[t].getElementsByTagName("name");
630 var freqn = terms[t].getElementsByTagName("frequency");
631 var idn = terms[t].getElementsByTagName("id");
634 var term = namen[0].childNodes[0].nodeValue;
635 var freq = freqn[0].childNodes[0].nodeValue;
638 id = idn[0].childNodes[0].nodeValue;
639 var refine_cell = create_element('a', term + ' (' + freq + ')');
640 refine_cell.setAttribute('href', '#');
641 refine_cell.setAttribute('term', term);
642 refine_cell.setAttribute('facet', listname);
643 refine_cell.setAttribute('target_id', id);
644 refine_cell.onclick = function () {
648 body.appendChild(refine_cell);
653 termtimer = setTimeout(check_termlist, 1000);
656 function check_termlist()
658 var facet_names = '';
660 for (i = 0; i < facet_list.length; i++)
661 if (facet_list[i][1].style.display != 'none')
665 facet_names += facet_list[i][0];
667 var url = "search.pz2?" +
669 "&session=" + session +
670 "&name=" + facet_names +
672 SendXmlHttpObject(xtermlist = GetXmlHttpObject(), url, show_termlists);
677 if (xstat.readyState != 4)
680 var xml = xstat.responseXML;
681 var body = document.getElementById("stat");
682 var nodes = xml.childNodes[0].childNodes;
684 Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue);
687 stattimer = setTimeout(check_stat, 500);
691 assign_text(body, '(');
692 for (i = 0; i < nodes.length; i++)
694 if (nodes[i].nodeType != 1)
696 var value = nodes[i].childNodes[0].nodeValue;
699 var name = nodes[i].nodeName;
700 append_text(body, ' ' + name + '=' + value);
703 append_text(body, ')');
705 stattimer = setTimeout(check_stat, 2000);
709 function check_stat()
711 var url = "search.pz2?" +
713 "&session=" + session;
714 xstat = GetXmlHttpObject();
715 xstat.onreadystatechange=show_stat;
716 xstat.open("GET", url);
720 function search_started()
722 if (xsearch.readyState != 4)
724 var xml = xsearch.responseXML;
725 var error = xml.getElementsByTagName("error");
728 var msg = error[0].childNodes[0].nodeValue;
733 stattimer = setTimeout(check_stat, 1000);
736 function clear_filter()
741 function start_search()
743 clearTimeout(termtimer);
745 clearTimeout(searchtimer);
747 clearTimeout(stattimer);
749 clearTimeout(showtimer);
753 var query = escape(document.getElementById('query').value);
754 var url = "search.pz2?" +
756 "&session=" + session +
758 "&filter=" + escape(filter);
759 xsearch = GetXmlHttpObject();
760 xsearch.onreadystatechange=search_started;
761 xsearch.open("GET", url);
763 clear_cell(document.getElementById("body"));
766 document.search.startrec.value = 0;
769 function session_encode ()
774 for (i = 0; i < session_cells.length; i++)
776 var name = session_cells[i];
777 var value = escape(document.getElementById(name).value);
778 session += '&' + name + '=' + value;
785 function session_restore (session)
787 var fields = session.split(/&/);
790 for (i = 1; i < fields.length; i++)
792 var pair = fields[i].split(/=/);
793 var key = pair.shift();
794 var value = pair.join('=');
795 var cell = document.getElementById(key);
803 function session_read ()
805 var ses = window.location.hash.replace(/^#/, '');
810 function session_store (new_value)
812 window.location.hash = '#' + new_value;
816 function update_history ()
818 var session = session_encode();
819 session_store(session);
820 old_session = session;
824 function session_check ()
826 var session = session_read();
827 var action = document.search.action_type.value;
829 clearInterval(url_surveillence);
831 if ( session != unescape(old_session) )
833 session_restore(session);
835 if (action == 'search') {
837 } else if (action == 'page') {
840 alert('Unregocnized action_type: ' + action);
845 url_surveillence = setInterval(session_check, 200);
849 function get_available_facets () {
850 var facet_container = document.getElementById('termlists');
851 var facet_cells = facet_container.childNodes;
852 var facets = Array();
855 for (i = 0; i < facet_cells.length; i++) {
856 var cell = facet_cells.item(i);
858 if (cell.className == 'facet') {
859 var facet_name = cell.id.replace(/^facet_([^_]+)_terms$/, "$1");
860 facets.push(Array(facet_name, cell));
868 function get_facet_container (obj) {
869 return document.getElementById(obj.id + '_terms');
873 function toggle_facet (obj) {
874 var container = get_facet_container(obj);
876 if (obj.className == 'selected') {
877 obj.className = 'unselected';
878 container.style.display = 'inline';
881 obj.className = 'selected';
882 container.style.display = 'none';