Author hyperlinking from brief display.
[pazpar2-moved-to-github.git] / www / demo / search.js
index edf3c00..a7119eb 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: search.js,v 1.26 2007-01-16 03:38:50 quinn Exp $
+/* $Id: search.js,v 1.29 2007-01-16 18:19:50 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
@@ -230,30 +231,8 @@ function displayname(name)
        return name;
 }
 
-function show_details()
+function paint_details(body, xml)
 {
-    if (xfetchDetails.readyState != 4)
-       return;
-    var xml = xfetchDetails.responseXML;
-    var error = xml.getElementsByTagName("error");
-    if (error[0])
-    {
-       var msg = error[0].childNodes[0].nodeValue;
-       alert(msg);
-       location = "?";
-       return;
-    }
-
-    // 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;
@@ -268,6 +247,8 @@ function show_details()
        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');
@@ -283,12 +264,57 @@ function show_details()
     body.style.display = 'inline';
 }
 
+function show_details()
+{
+    if (xfetchDetails.readyState != 4)
+       return;
+    var xml = xfetchDetails.responseXML;
+    var error = xml.getElementsByTagName("error");
+    if (error[0])
+    {
+       var msg = error[0].childNodes[0].nodeValue;
+       alert(msg);
+       location = "?";
+       return;
+    }
+
+    // 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;
+    cur_id = id;
+    cur_rec = xml;
+
+    var body = document.getElementById('rec_' + id);
+    if (!body)
+       return;
+    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++)
     {
-       node.style.display = 'none';
+       var dets = nodes[i].getElementsByTagName('div');
+       if (dets[0])
+           dets[0].style.display = 'none';
+    }
+    if (id == cur_id)
+    {
+       cur_id = -1;
        return;
     }
     if (!session)
@@ -363,6 +389,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', '#');
@@ -371,16 +398,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);
        }
@@ -565,6 +598,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" +