X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=www%2Fdemo%2Fsearch.js;h=edf3c005a66af6f461fc72f6a9900ee53fa11337;hb=df1fd6502c261c3939c5799b23cbb1bf66c13c0c;hp=1907d9eaeeef9bb6badabccc03fd3f41a89af81d;hpb=e5ce7770c017d1027db53d00001caa537fa65320;p=pazpar2-moved-to-github.git
diff --git a/www/demo/search.js b/www/demo/search.js
index 1907d9e..edf3c00 100644
--- a/www/demo/search.js
+++ b/www/demo/search.js
@@ -1,4 +1,4 @@
-/* $Id: search.js,v 1.2 2007-01-05 13:31:10 sondberg Exp $
+/* $Id: search.js,v 1.26 2007-01-16 03:38:50 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,15 +21,19 @@ 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;
function initialize ()
{
facet_list = get_available_facets();
start_session();
session_check();
+ set_sort();
}
@@ -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()
@@ -100,51 +103,202 @@ function session_pinged()
setTimeout(ping_session, 50000);
}
-function targets_loaded()
+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;
+ }
+
+ 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 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)
+{
+ 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
+ return name;
+}
+
+function show_details()
{
- if (xloadTargets.readyState != 4)
+ 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";
+
+ // This is some ugly display code. Replace with your own ting o'beauty
+
+ var idn = xml.getElementsByTagName('recid');
+ if (!idn[0])
+ return;
+ var id = idn[0].childNodes[0].nodeValue;
+
+ var body = document.getElementById('rec_' + id);
+ if (!body)
+ return;
+ clear_cell(body);
+ //body.appendChild(document.createElement('br'));
+ var nodes = xml.childNodes[0].childNodes;
+ var i;
+ var table = document.createElement('table');
+ table.setAttribute('cellpadding', 2);
+ 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;
+ name = displayname(name);
+ var value = nodes[i].childNodes[0].nodeValue;
+ var lbl = create_element('b', name );
+ var lbln = document.createElement('td');
+ lbln.setAttribute('width', 70);
+ lbln.appendChild(lbl);
+ var val = create_element('td', value);
+ var tr = document.createElement('tr');
+ tr.appendChild(lbln);
+ tr.appendChild(val);
+ table.appendChild(tr);
+ }
+ body.appendChild(table);
+ body.style.display = 'inline';
}
-function load_targets()
+function fetch_details(id)
{
- var fn = document.getElementById("targetfilename").value;
- clearTimeout(termtimer);
- clearTimeout(searchtimer);
- clearTimeout(stattimer);
- clearTimeout(showtimer);
- document.getElementById("stat").innerHTML = "";
- if (!fn)
+ var node = document.getElementById('rec_' + id);
+ if (node && node.style.display == 'inline')
{
- alert("Please enter a target definition file name");
+ node.style.display = 'none';
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);
-}
-
-
-function update_action (new_action) {
- document.search.action_type.value = new_action;
+ if (!session)
+ return;
+ var url = "search.pz2?session=" + session +
+ "&command=record" +
+ "&id=" + id;
+ SendXmlHttpObject(xfetchDetails = GetXmlHttpObject(), url, show_details);
}
-
function show_records()
{
if (xshow.readyState != 4)
@@ -153,45 +307,84 @@ 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 = 'Records : ';
- body.innerHTML += (start + 1) + ' to ' + (start + num) +
- ' of ' + merged + ' (total hits: ' + total + ')';
-
- if (start + num < merged)
- body.innerHTML += ' Next';
-
- if (start > 0)
- body.innerHTML += ' Previous';
-
- body.innerHTML += '
';
+ var pager = make_pager(merged, start,recstoshow);
+ var break_node1 = document.createElement('br');
+ var break_node2 = document.createElement('br');
+ var record_container = document.createElement('div');
+ var interval = create_element('div', 'Records : ' + (start + 1) +
+ ' to ' + (start + num) + ' of ' +
+ merged + ' (total hits: ' +
+ total + ')');
+ searched = 1;
+ interval.className = 'results';
+ record_container.className = 'records';
+
+ body.appendChild(pager);
+ body.appendChild(interval);
+ body.appendChild(break_node1);
+ body.appendChild(break_node2);
+ body.appendChild(record_container);
+
for (i = 0; i < hits.length; i++)
{
- body.innerHTML += '
'; - body.innerHTML += (i + start + 1) + ': '; - var mk = hits[i].getElementsByTagName("title"); - if (mk[0]) - body.innerHTML += mk[0].childNodes[0].nodeValue; - body.innerHTML += '
'; + var tn = hits[i].getElementsByTagName("md-title"); + var title = ''; + var an = hits[i].getElementsByTagName("md-author"); + var author = ''; + var cn = hits[i].getElementsByTagName("count"); + var count = 1; + var idn = hits[i].getElementsByTagName("recid"); + + if (tn[0]) { + title = tn[0].childNodes[0].nodeValue; + } else { + title = 'N/A'; + } + if (an[0] && an[0].childNodes[0]) + author = an[0].childNodes[0].nodeValue; + if (cn[0]) + count = Number(cn[0].childNodes[0].nodeValue); + var id = idn[0].childNodes[0].nodeValue; + + var record_div = document.createElement('div'); + record_div.className = 'record'; + + var record_cell = create_element('a', title); + record_cell.setAttribute('href', '#'); + record_cell.setAttribute('onclick', 'fetch_details(' + id + '); return false'); + record_div.appendChild(record_cell); + if (author) + { + record_div.appendChild(document.createTextNode(', by ')); + record_div.appendChild(document.createTextNode(author)); + } + if (count > 1) + record_div.appendChild(document.createTextNode( + ' (' + count + ')')); + var det_div = document.createElement('div'); + det_div.style.display = 'none'; + det_div.setAttribute('id', 'rec_' + id); + det_div.appendChild(document.createElement('br')); + det_div.appendChild(document.createTextNode('Hugo')); + record_div.appendChild(det_div); + record_container.appendChild(record_div); } + shown++; if (clients > 0) { @@ -202,7 +395,7 @@ function show_records() } } if (!termtimer) - termtimer = setTimeout(check_termlist, 1000); + termtimer = setTimeout(check_termlist, 500); } function check_search() @@ -213,6 +406,7 @@ function check_search() "&start=" + document.search.startrec.value + "&num=" + recstoshow + "&session=" + session + + "&sort=" + cur_sort + "&block=1"; xshow = GetXmlHttpObject(); xshow.onreadystatechange=show_records; @@ -222,14 +416,17 @@ function check_search() function refine_query (obj) { + var term = obj.getAttribute('term'); + var cur_termlist = obj.getAttribute('facet'); var query_cell = document.getElementById('query'); - var term = obj.innerHTML; term = term.replace(/[\(\)]/g, ''); + if (cur_termlist == 'subject') query_cell.value += ' and su=(' + term + ')'; else if (cur_termlist == 'author') query_cell.value += ' and au=(' + term + ')'; + start_search(); } @@ -243,6 +440,7 @@ function show_termlist() var i; var xml = xtermlist.responseXML; var body = facet_list[cur_facet][1]; + var facet_name = facet_list[cur_facet][0]; var hits = xml.getElementsByTagName("term"); var clients = Number(xml.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue); @@ -254,23 +452,32 @@ function show_termlist() if (!hits[0]) { - termtimer = setTimeout(check_termlist, 1000); + termtimer = setTimeout(check_termlist, 500); } else { - body.innerHTML = "Limit results: