More responsive record view.
[pazpar2-moved-to-github.git] / www / demo / search.js
index 054558c..c996c1b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: search.js,v 1.10 2007-01-10 09:19:05 sondberg Exp $
+/* $Id: search.js,v 1.15 2007-01-10 12:20:51 sondberg Exp $
  * ---------------------------------------------------
  * Javascript container
  */
@@ -147,26 +147,35 @@ function update_action (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';
 
-    for (off = offset - page_window * max;
-         off < hits && off < (offset + page_window * max); 
-         off += max) {
+    if (start_offset < 0) {
+        start_offset = 0;
+    }
 
-        var class = '';
+    for (off = start_offset;
+         off < hits && off < (start_offset + 2 * page_window * max); 
+         off += max) {
         
-        if (off < 0)
-            off = 0; 
-            
         var p = off / max + 1;
+        var page_elem = create_element('a', p);
+        var newline_node = document.createTextNode('\n');
 
-        if ((offset >= off) && (offset < (off + max)))
-            class = ' class="select"';
+        if ((offset >= off) && (offset < (off + max))) {
+            page_elem.className = 'select';
+        }
+
+        page_elem.setAttribute('href', '#');
+        page_elem.setAttribute('onclick', 'update_offset(' + off + ')');
 
-        html += '<a href="#" ' + class +
-                'onclick="update_offset(' + off + ')">' + p + '</a>\n';
+        div_elem.appendChild(page_elem);
+        div_elem.appendChild(newline_node);
     }
 
-    return html;
+    return div_elem;
 }
 
 
@@ -179,6 +188,21 @@ function update_offset (offset) {
 }
 
 
+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 show_records()
 {
     if (xshow.readyState != 4)
@@ -187,6 +211,9 @@ 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";
@@ -200,46 +227,40 @@ function show_records()
        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 = '<div class="pages">' +
-                         make_pager(merged, start, recstoshow) +
-                         '</div>';
-                         
-       body.innerHTML += '<div class="results">Records : ' + (start + 1) +
-                          ' to ' + (start + num) + ' of ' + merged +
-                          ' (total hits: ' + total + ')</div><br/><br/>';
-
-/*
-       if (start + num < merged)
-           body.innerHTML += ' <a href="" ' +
-               'onclick="document.search.startrec.value=' + (start + recstoshow) +
-                ";update_action('page')" +
-               ';check_search(); update_history(); return false;">Next</a>';
-
-       if (start > 0)
-           body.innerHTML += ' <a href="" ' +
-               'onclick="document.search.startrec.value=' + (start - recstoshow) +
-                ";update_action('page')" +
-               ';check_search(); update_history();return false;">Previous</a>';
-
-       body.innerHTML += '<br/>';
-*/
-        body.innerHTML += '<div class="records">';
+        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 + ')');
+        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++)
        {
            var mk = hits[i].getElementsByTagName("md-title");
-            var html = '<a href="#" class="record">';
             var field = '';
 
            if (mk[0]) {
                 field = mk[0].childNodes[0].nodeValue;
+            } else {
+                field = 'N/A';
             }
-
-           html += field + '</a>';
-            body.innerHTML += html;
+            
+            var record_cell = create_element('a', field);
+            record_cell.setAttribute('href', '#');
+            record_cell.className = 'record';
+            record_container.appendChild(record_cell);
        }
 
-        body.innerHTML += '</div>';
        shown++;
        if (clients > 0)
        {
@@ -312,9 +333,11 @@ function show_termlist()
        {
            var namen = hits[i].getElementsByTagName("name");
            if (namen[0])
-               body.innerHTML += '<a href="#" onclick="refine_query(this)">' +
-                                  namen[0].childNodes[0].nodeValue +
-                                  '</a>';
+                var refine_cell = create_element('a',
+                                    namen[0].childNodes[0].nodeValue);
+                refine_cell.setAttribute('href', '#');
+                refine_cell.setAttribute('onclick', 'refine_query(this)');
+                body.appendChild(refine_cell);
        }
 
        if (clients > 0)
@@ -419,7 +442,6 @@ function start_search()
     xsearch.onreadystatechange=search_started;
     xsearch.open("GET", url);
     xsearch.send(null);
-//    document.getElementById("termlist").innerHTML = '';
     document.getElementById("body").innerHTML = '';
     update_history();
     shown = 0;