X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=www%2Fdemo%2Fsearch.js;h=4eb2d467d3da530e03e6725141d54cd19c582a48;hb=9bc5218957a69468292c9588981a8479ae5ab12c;hp=b5b462a6912117cbe5a81f5c574f9f16df151a39;hpb=5017b103ece3eeb4e3ab181d5e49d50af8e56018;p=pazpar2-moved-to-github.git diff --git a/www/demo/search.js b/www/demo/search.js index b5b462a..4eb2d46 100644 --- a/www/demo/search.js +++ b/www/demo/search.js @@ -1,4 +1,4 @@ -/* $Id: search.js,v 1.6 2007-01-05 14:56:05 sondberg Exp $ +/* $Id: search.js,v 1.43 2007-01-18 19:32:06 quinn Exp $ * --------------------------------------------------- * Javascript container */ @@ -10,6 +10,7 @@ var xsearch; var xshow; var xstat; var xtermlist; +var xfetchDetails; var session = false; var targetsloaded = false; var shown; @@ -20,18 +21,22 @@ var stattimer; var session_cells = Array('query', 'startrec', 'action_type'); var old_session = session_read(); var url_surveillence; -var recstoshow = 15; +var recstoshow = 20; +var page_window = 5; // Number of pages prior to and after the current page var facet_list; -var cur_facet = 0; +var cur_sort = "relevance"; +var searched = 0; +var cur_id = -1; +var cur_rec = 0; function initialize () { facet_list = get_available_facets(); start_session(); session_check(); + set_sort(); } - function GetXmlHttpObject() { var objXMLHttp=null @@ -59,7 +64,7 @@ function session_started() return; var xml = xinitSession.responseXML; var sesid = xml.getElementsByTagName("session")[0].childNodes[0].nodeValue; - document.getElementById("status").innerHTML = "Live"; + assign_text(document.getElementById("status"), 'Live'); session = sesid; setTimeout(ping_session, 50000); } @@ -72,8 +77,6 @@ function start_session() xinitSession.onreadystatechange=session_started; xinitSession.open("GET", url); xinitSession.send(null); - - //url_surveillence = setInterval(session_check, 200); } function ping_session() @@ -91,64 +94,334 @@ function session_pinged() var xml = xpingSession.responseXML; var error = xml.getElementsByTagName("error"); if (error[0]) - { - var msg = error[0].childNodes[0].nodeValue; - alert(msg); location = "?"; - return; + else + setTimeout(ping_session, 50000); +} + +function update_action (new_action) { + document.search.action_type.value = new_action; +} + + +function make_pager (hits, offset, max) { + var html = ''; + var off; + var start_offset = offset - page_window * max; + var div_elem = document.createElement('div'); + + div_elem.className = 'pages'; + + if (start_offset < 0) { + start_offset = 0; } - setTimeout(ping_session, 50000); + + for (off = start_offset; + off < hits && off < (start_offset + 2 * page_window * max); + off += max) { + + var p = off / max + 1; + var page_elem = create_element('a', p); + var newline_node = document.createTextNode(' '); + + if ((offset >= off) && (offset < (off + max))) { + page_elem.className = 'select'; + } + + page_elem.setAttribute('off', off); + page_elem.style.cursor = 'pointer'; + page_elem.onclick = function () { + update_offset(this.getAttribute('off')); + }; + + div_elem.appendChild(page_elem); + div_elem.appendChild(newline_node); + } + + return div_elem; +} + + +function update_offset (offset) { + clearTimeout(searchtimer); + document.search.startrec.value = offset; + update_action('page'); + check_search(); + update_history(); + return false; +} + + +function create_element (name, cdata) { + var elem_node = document.createElement(name); + var text_node = document.createTextNode(cdata); + elem_node.appendChild(text_node); + + return elem_node; +} + + +function clear_cell (cell) { + while (cell.hasChildNodes()) + cell.removeChild(cell.firstChild); } -function targets_loaded() + +function append_text(cell, text) { + text_node = document.createTextNode(text); + cell.appendChild(text_node); +} + + +function assign_text (cell, text) { + clear_cell(cell); + append_text(cell, text); +} + +function set_sort_opt(n, opt, str) { - if (xloadTargets.readyState != 4) + var txt = document.createTextNode(str); + if (opt == cur_sort) + n.appendChild(txt); + else + { + var a = document.createElement('a'); + a.appendChild(txt); + a.setAttribute('href', ""); + a.setAttribute('onclick', "set_sort('" + opt + "'); return false"); + n.appendChild(a); + } +} + +function set_sort(sort) +{ + if (sort && sort != cur_sort) + { + cur_sort = sort; + if (searched) + check_search(); + } + + var t = document.getElementById("sortselect"); + clear_cell(t); + t.appendChild(document.createTextNode("Sort results by: ")); + set_sort_opt(t, 'relevance', 'Relevance'); + t.appendChild(document.createTextNode(" or ")); + set_sort_opt(t, 'title:1', 'Title'); +} + +function displayname(name) +{ + if (name == 'md-author') + return 'Author'; + else if (name == 'md-subject') + return 'Subject'; + else if (name == 'md-date') + return 'Date'; + else if (name == 'md-isbn') + return 'ISBN'; + else if (name == 'md-publisher') + return 'Publisher'; + else if (name == 'md-url') + return 'URL'; + else if (name == 'md-title') + return '@'; + else if (name == 'md-id') + return 'Local ID'; + else if (name == 'md-lccn') + return 'LCCN'; + else if (name == 'recid') + return '@'; + else if (name == 'location') + return '@'; + else + return name; +} + +function hyperlink_field(name) +{ + if (name == 'md-author') + return 'au'; + else if (name == 'md-subject') + return 'su'; + else if (name == 'md-url') + return 'URL'; + else + return 0; +} + +function paint_details_tr(name, dn) +{ + //emit a table row + var dname = displayname(name); + var ln = create_element('b', dname); + var tln = document.createElement('td'); + tln.setAttribute('width', 90); + tln.setAttribute('valign', 'top'); + tln.appendChild(ln); + var tr = document.createElement('tr'); + tr.appendChild(tln); + tr.appendChild(dn); + return tr; +} + +function paint_data_elements(target, node) +{ + var nodes = node.childNodes; + var dn = 0; + var lastname = ''; + var i; + for (i = 0; i < nodes.length; i++) + { + if (nodes[i].nodeType != 1) + continue; + var name = nodes[i].nodeName; + if (name == 'recid' || name == 'md-title') + continue; + if (name != lastname && lastname != 'location') + { + if (dn) + { + var tr = paint_details_tr(lastname, dn); + target.appendChild(tr); + } + dn = document.createElement('td'); + lastname = name; + } + if (name == 'location') + { + target.appendChild(paint_details_tr('Location', paint_subrecord(nodes[i]))); + continue; + } + if (!nodes[i].childNodes[0]) + continue; + var value = nodes[i].childNodes[0].nodeValue; + if (dn.childNodes[0]) + dn.appendChild(document.createTextNode('; ')); + var hyl = hyperlink_field(name); + var nv; + if (hyl) + { + nv = create_element('a', value); + if (hyl == 'URL') + { + nv.setAttribute('href', value); + nv.setAttribute('target', '_blank'); + } + else + { + nv.setAttribute('href', '#'); + nv.setAttribute('term', value); + nv.setAttribute('searchfield', hyl); + nv.onclick = function() { hyperlink_search(this); return false; }; + } + } + else if (name == 'md-lccn') + { + nv = document.createElement('span'); + nv.appendChild(document.createTextNode(value + ' ')); + var link = create_element('a', 'Show title in LoC'); + link.setAttribute('target', '_blank'); + link.setAttribute('href', 'http://catalog.loc.gov/cgi-bin/Pwebrecon.cgi?DB=local&CNT=10&CMD=10+records+per+page&CMD=lccn+' + value); + nv.appendChild(link); + + } + else + nv = document.createTextNode(value); + dn.appendChild(nv); + } + if (dn && lastname != 'location') + { + var tr = paint_details_tr(lastname, dn); + target.appendChild(tr); + } +} + +function paint_subrecord(node) +{ + var table = document.createElement('table'); + var zurl = node.getAttribute('id'); + var name = node.getAttribute('name'); + var tr; + if (name) + tr = paint_details_tr('Source', document.createTextNode(name)); + else + tr = paint_details_tr('Source', document.createTextNode(zurl)); + table.appendChild(tr); + paint_data_elements(table, node); + return table; +} + +function paint_details(body, xml) +{ + clear_cell(body); + var table = document.createElement('table'); + table.setAttribute('cellpadding', 2); + paint_data_elements(table, xml.childNodes[0]); + body.appendChild(table); + body.style.display = 'inline'; +} + +function show_details() +{ + if (xfetchDetails.readyState != 4) return; - var xml = xloadTargets.responseXML; + var xml = xfetchDetails.responseXML; var error = xml.getElementsByTagName("error"); if (error[0]) { var msg = error[0].childNodes[0].nodeValue; alert(msg); + location = "?"; return; } - document.getElementById("targetstatus").innerHTML = "Targets loaded"; -} -function load_targets() -{ - var fn = document.getElementById("targetfilename").value; - clearTimeout(termtimer); - clearTimeout(searchtimer); - clearTimeout(stattimer); - clearTimeout(showtimer); - document.getElementById("stat").innerHTML = ""; - if (!fn) - { - alert("Please enter a target definition file name"); + var idn = xml.getElementsByTagName('recid'); + if (!idn[0]) return; - } - var url="search.pz2?" + - "command=load" + - "&session=" + session + - "&name=" + fn; - document.getElementById("targetstatus").innerHTML = "Loading targets..."; - xloadTargets = GetXmlHttpObject(); - xloadTargets.onreadystatechange=targets_loaded; - xloadTargets.open("GET", url); - xloadTargets.send(null); -} + var id = idn[0].childNodes[0].nodeValue; + cur_id = id; + cur_rec = xml; + var nodes = document.getElementsByName('listrecord'); + var i; + for (i = 0; i < nodes.length; i++) + { + var dets = nodes[i].getElementsByTagName('div'); + if (dets[0]) + dets[0].style.display = 'none'; + } -function update_action (new_action) { - document.search.action_type.value = new_action; + var body = document.getElementById('rec_' + id); + if (!body) + return; + paint_details(body, xml); } - -function make_pager (hits, offset, max) { - return '1 Next'; +function hyperlink_search(obj) +{ + var field = obj.getAttribute('searchfield'); + var term = obj.getAttribute('term'); + var queryfield = document.getElementById('query'); + queryfield.value = field + '=' + term; + start_search(); } +function fetch_details(id) +{ + cur_id = -1; + if (id == cur_id) + { + cur_id = -1; + return; + } + if (!session) + return; + var url = "search.pz2?session=" + session + + "&command=record" + + "&id=" + id; + SendXmlHttpObject(xfetchDetails = GetXmlHttpObject(), url, show_details); +} function show_records() { @@ -158,57 +431,92 @@ function show_records() var xml = xshow.responseXML; var body = document.getElementById("body"); var hits = xml.getElementsByTagName("hit"); + + clear_cell(body); + if (!hits[0]) // We should never get here with blocking operations { - body.innerHTML = "No records yet"; + assign_text(body, 'No records yet'); searchtimer = setTimeout(check_search, 250); } else { - var total = Number(xml.getElementsByTagName('total')[0].childNodes[0].nodeValue); var merged = Number(xml.getElementsByTagName('merged')[0].childNodes[0].nodeValue); var start = Number(xml.getElementsByTagName('start')[0].childNodes[0].nodeValue); var num = Number(xml.getElementsByTagName('num')[0].childNodes[0].nodeValue); var clients = Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue); - body.innerHTML = '