Dsiplaying sub-records
[pazpar2-moved-to-github.git] / www / demo / search.js
index 517f597..b08450d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: search.js,v 1.36 2007-01-17 14:21:29 quinn Exp $
+/* $Id: search.js,v 1.39 2007-01-18 16:21:23 quinn Exp $
  * ---------------------------------------------------
  * Javascript container
  */
@@ -24,7 +24,6 @@ var url_surveillence;
 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;
@@ -96,7 +95,8 @@ function session_pinged()
     var error = xml.getElementsByTagName("error");
     if (error[0])
        location = "?";
-    setTimeout(ping_session, 50000);
+    else
+        setTimeout(ping_session, 50000);
 }
 
 function update_action (new_action) {
@@ -224,6 +224,14 @@ function displayname(name)
        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 == 'recid')
+       return '@';
+    else if (name == 'location')
+       return '@';
     else
        return name;
 }
@@ -246,7 +254,7 @@ function  paint_details_tr(name, dn)
     var dname = displayname(name);
     var ln = create_element('b', dname);
     var tln = document.createElement('td');
-    tln.setAttribute('width', 70);
+    tln.setAttribute('width', 90);
     tln.setAttribute('valign', 'top');
     tln.appendChild(ln);
     var tr = document.createElement('tr');
@@ -255,17 +263,12 @@ function  paint_details_tr(name, dn)
     return tr;
 }
 
-function paint_details(body, xml)
+function paint_data_elements(target, node)
 {
-    // This is some ugly display code. Replace with your own ting o'beauty
-    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 nodes = node.childNodes;
     var dn = 0;
     var lastname = '';
+    var i;
     for (i = 0; i < nodes.length; i++)
     {
        if (nodes[i].nodeType != 1)
@@ -273,17 +276,21 @@ function paint_details(body, xml)
        var name = nodes[i].nodeName;
        if (name == 'recid' || name == 'md-title')
            continue;
-       if (name != lastname)
+       if (name != lastname && lastname != 'location') 
        {
            if (dn)
            {
                var tr = paint_details_tr(lastname, dn);
-               table.appendChild(tr);
+               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;
@@ -311,11 +318,29 @@ function paint_details(body, xml)
            nv = document.createTextNode(value);
        dn.appendChild(nv);
     }
-    if (dn)
+    if (dn && lastname != 'location')
     {
        var tr = paint_details_tr(lastname, dn);
-       table.appendChild(tr);
+       target.appendChild(tr);
     }
+}
+
+function paint_subrecord(node)
+{
+    var table = document.createElement('table');
+    var zurl = node.getAttribute('id');
+    var tr = paint_details_tr('Source', document.createTextNode(zurl));
+    paint_data_elements(table, node);
+    table.appendChild(tr);
+    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';
 }
@@ -522,69 +547,72 @@ function refine_query (obj) {
     start_search();
 }
 
+function clear_termlists()
+{
+    var i;
+    for (i = 0; i < facet_list.length; i++)
+       clear_cell(facet_list[i][1]);
+}
 
-
-function show_termlist()
+function show_termlists()
 {
     if (xtermlist.readyState != 4)
        return;
 
     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);
+    var lists = xml.getElementsByTagName("list");
 
-    cur_facet++;
-
-    if (cur_facet >= facet_list.length)
-        cur_facet = 0;
-
-    if (!hits[0])
-    {
-       termtimer = setTimeout(check_termlist, 500);
-    }
-    else
+    for (i = 0; i < lists.length; i++)
     {
+       var listname = lists[i].getAttribute('name');
+       var body = document.getElementById('facet_' + listname + '_terms');
+       if (body.style.display == 'none')
+           continue;
        clear_cell(body);
-       
-        for (i = 0; i < hits.length; i++)
+       var terms = lists[i].getElementsByTagName('term');
+       var t;
+        for (t = 0; t < terms.length; t++)
        {
-           var namen = hits[i].getElementsByTagName("name");
-           var freqn = hits[i].getElementsByTagName("frequency");
+           var namen = terms[t].getElementsByTagName("name");
+           var freqn = terms[t].getElementsByTagName("frequency");
            if (namen[0])
                 var term = namen[0].childNodes[0].nodeValue;
                var freq = freqn[0].childNodes[0].nodeValue;
                 var refine_cell = create_element('a', term + ' (' + freq + ')');
                 refine_cell.setAttribute('href', '#');
                 refine_cell.setAttribute('term', term);
-                refine_cell.setAttribute('facet', facet_name);
+                refine_cell.setAttribute('facet', listname);
                 refine_cell.onclick = function () {
                     refine_query(this);
                     return false;
                 };
                 body.appendChild(refine_cell);
        }
-
-       if (clients > 0)
-           termtimer = setTimeout(check_termlist, 1000);
     }
+    if (clients > 0)
+       termtimer = setTimeout(check_termlist, 1000);
 }
 
 function check_termlist()
 {
-    var facet_name = facet_list[cur_facet][0];
+    var facet_names = '';
+    var i;
+    for (i = 0; i < facet_list.length; i++)
+       if (facet_list[i][1].style.display != 'none')
+       {
+           if (facet_names)
+               facet_names += ',';
+           facet_names += facet_list[i][0];
+       }
     var url = "search.pz2?" +
         "command=termlist" +
        "&session=" + session +
-       "&name=" + facet_name +
+       "&name=" + facet_names +
        "&num=12";
-    xtermlist = GetXmlHttpObject();
-    xtermlist.onreadystatechange=show_termlist;
-    xtermlist.open("GET", url);
-    xtermlist.send(null);
+    SendXmlHttpObject(xtermlist = GetXmlHttpObject(), url, show_termlists);
 }
 
 function show_stat()
@@ -659,6 +687,7 @@ function start_search()
     clearTimeout(showtimer);
     showtimer = 0;
     cur_id = -1;
+    clear_termlists();
     var query = escape(document.getElementById('query').value);
     var url = "search.pz2?" +
         "command=search" +
@@ -784,6 +813,7 @@ function toggle_facet (obj) {
     if (obj.className == 'selected') {
         obj.className = 'unselected';
         container.style.display = 'inline';
+       check_termlist();
     } else {
         obj.className = 'selected';
         container.style.display = 'none';