Top-aligned data label
[pazpar2-moved-to-github.git] / www / demo / search.js
index 69cca05..40c418f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: search.js,v 1.27 2007-01-16 05:29:48 quinn Exp $
+/* $Id: search.js,v 1.31 2007-01-16 19:24:44 quinn Exp $
  * ---------------------------------------------------
  * Javascript container
  */
@@ -27,6 +27,8 @@ var facet_list;
 var cur_facet = 0;
 var cur_sort = "relevance";
 var searched = 0;
+var cur_id = -1;
+var cur_rec = 0;
 
 function initialize ()
 {
@@ -36,7 +38,6 @@ function initialize ()
     set_sort();
 }
 
-
 function GetXmlHttpObject()
 { 
     var objXMLHttp=null
@@ -94,12 +95,7 @@ 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;
-    }
     setTimeout(ping_session, 50000);
 }
 
@@ -230,6 +226,66 @@ function displayname(name)
        return name;
 }
 
+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', 70);
+    tln.setAttribute('valign', 'top');
+    tln.appendChild(ln);
+    var tr = document.createElement('tr');
+    tr.appendChild(tln);
+    tr.appendChild(dn);
+    return tr;
+}
+
+function paint_details(body, xml)
+{
+    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);
+    var dn = 0;
+    var lastname = '';
+    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)
+       {
+           if (dn)
+           {
+               var tr = paint_details_tr(lastname, dn);
+               table.appendChild(tr);
+           }
+           dn = document.createElement('td');
+           lastname = name;
+       }
+
+       if (!nodes[i].childNodes[0])
+               continue;
+       var value = nodes[i].childNodes[0].nodeValue;
+       if (dn.childNodes[0])
+           value = '; ' + value;
+       var nv = document.createTextNode(value);
+       dn.appendChild(nv);
+    }
+    if (dn)
+    {
+       var tr = paint_details_tr(lastname, dn);
+       table.appendChild(tr);
+    }
+    body.appendChild(table);
+    body.style.display = 'inline';
+}
+
 function show_details()
 {
     if (xfetchDetails.readyState != 4)
@@ -250,47 +306,37 @@ function show_details()
     if (!idn[0])
        return;
     var id = idn[0].childNodes[0].nodeValue;
+    cur_id = id;
+    cur_rec = xml;
 
     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);
-       if (!nodes[i].childNodes[0])
-               continue;
-       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';
+    paint_details(body, xml);
+}
+
+function hyperlink_search(field, obj)
+{
+    var term = obj.getAttribute('term');
+    var queryfield  = document.getElementById('query');
+    queryfield.value = field + '=' + term;
+    start_search();
 }
 
 function fetch_details(id)
 {
-    var node = document.getElementById('rec_' + id);
-    if (node && node.style.display == 'inline')
+    cur_id = -1;
+    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';
+    }
+    if (id == cur_id)
     {
-       node.style.display = 'none';
+       cur_id = -1;
        return;
     }
     if (!session)
@@ -365,6 +411,7 @@ function show_records()
             
            var record_div = document.createElement('div');
            record_div.className = 'record';
+           record_div.setAttribute('name', 'listrecord');
 
             var record_cell = create_element('a', title);
             record_cell.setAttribute('href', '#');
@@ -373,16 +420,22 @@ function show_records()
            if (author)
            {
                record_div.appendChild(document.createTextNode(', by '));
-               record_div.appendChild(document.createTextNode(author));
+               var al = create_element('a', author);
+               al.setAttribute('href', '#');
+               al.setAttribute('term', author);
+               al.onclick = function() { hyperlink_search('au', this); return false; };
+               record_div.appendChild(al);
            }
            if (count > 1)
                record_div.appendChild(document.createTextNode(
                        ' (' + count + ')'));
            var det_div = document.createElement('div');
-           det_div.style.display = 'none';
+           if (id == cur_id)
+               paint_details(det_div, cur_rec);
+           else
+               det_div.style.display = 'none';
            det_div.setAttribute('id', 'rec_' + id);
-           det_div.appendChild(document.createElement('br'));
-           det_div.appendChild(document.createTextNode('Hugo'));
+           det_div.setAttribute('name', 'details');
            record_div.appendChild(det_div);
            record_container.appendChild(record_div);
        }
@@ -567,6 +620,7 @@ function start_search()
     stattimer = 0;
     clearTimeout(showtimer);
     showtimer = 0;
+    cur_id = -1;
     var query = escape(document.getElementById('query').value);
     var url = "search.pz2?" +
         "command=search" +