/* A very simple client that shows a basic usage of the pz2.js
-** $Id: example_client.js,v 1.6 2008-01-15 13:59:18 jakub Exp $
*/
// create a parameters array and pass it to the pz2's constructor
// autoInit is set to true on default
var usesessions = true;
var pazpar2path = '/pazpar2/search.pz2';
-if (document.location.hash == '#nosessions') {
+var showResponseType = '';
+if (document.location.hash == '#useproxy') {
usesessions = false;
- pazpar2path = '/pazpar2-proxy/';
+ pazpar2path = '/service-proxy/';
+ showResponseType = 'json';
}
my_paz = new pz2( { "onshow": my_onshow,
"termlist": "xtargets,subject,author",
"onbytarget": my_onbytarget,
"usesessions" : usesessions,
+ "showResponseType": showResponseType,
"onrecord": my_onrecord } );
// some state vars
var curPage = 1;
var curSort = 'relevance';
var curFilter = null;
var submitted = false;
+var SourceMax = 16;
+var SubjectMax = 10;
+var AuthorMax = 10;
//
// pz2.js event handlers:
var pager = document.getElementById("pager");
pager.innerHTML = "";
pager.innerHTML +='<hr/><div style="float: right">Displaying: '
- + data.start + ' to ' + (data.start + data.num) +
+ + (data.start + 1) + ' to ' + (data.start + data.num) +
' of ' + data.merged + ' (found: '
+ data.total + ')</div>';
drawPager(pager);
// navi
var results = document.getElementById("results");
- results.innerHTML = "";
-
+
+ var html = [];
for (var i = 0; i < data.hits.length; i++) {
var hit = data.hits[i];
- var html = '<div class="record" id="recdiv_'+hit.recid+'" >'
+ html.push('<div class="record" id="recdiv_'+hit.recid+'" >'
+'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>'
+'<a href="#" id="rec_'+hit.recid
+'" onclick="showDetails(this.id);return false;"><b>'
- + hit["md-title"] +' </b></a>';
- if (hit["md-title-remainder"] !== undefined) {
- html += '<span>' + hit["md-title-remainder"] + ' </span>';
- }
- if (hit["md-title-responsibility"] !== undefined) {
- html += '<span><i>'+ hit["md-title-responsibility"] +'</i></span>';
- }
- html += '</div>';
- results.innerHTML += html;
+ + hit["md-title"] +' </b></a>');
+ if (hit["md-title-remainder"] !== undefined) {
+ html.push('<span>' + hit["md-title-remainder"] + ' </span>');
+ }
+ if (hit["md-title-responsibility"] !== undefined) {
+ html.push('<span><i>'+hit["md-title-responsibility"]+'</i></span>');
+ }
if (hit.recid == curDetRecId) {
- drawCurDetails();
+ html.push(renderDetails(curDetRecData));
}
+ html.push('</div>');
}
+ replaceHtml(results, html.join(''));
}
function my_onstat(data) {
var stat = document.getElementById("stat");
+ if (stat == null)
+ return;
+
stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
+ data.activeclients
+ '/' + data.clients + ' -- </span>'
}
function my_onterm(data) {
- var termlist = document.getElementById("termlist");
- termlist.innerHTML = "<hr/><b>TERMLISTS:</b><hr/>";
-
- termlist.innerHTML += '<div class="termtitle">.::Sources</div>';
- for (var i = 0; i < data.xtargets.length; i++ ) {
- termlist.innerHTML += '<a href="#" target_id='
- + data.xtargets[i].id
- + ' onclick="limitTarget(this.getAttribute(\'target_id\'), this.firstChild.nodeValue);return false;">'
- + data.xtargets[i].name
- + ' </a><span> ('
- + data.xtargets[i].freq
- + ')</span><br/>';
+ var termlists = [];
+ termlists.push('<hr/><b>TERMLISTS:</b><hr/><div class="termtitle">.::Sources</div>');
+ for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
+ termlists.push('<a href="#" target_id='+data.xtargets[i].id
+ + ' onclick="limitTarget(this.getAttribute(\'target_id\'), this.firstChild.nodeValue);return false;">' + data.xtargets[i].name
+ + ' </a><span> (' + data.xtargets[i].freq + ')</span><br/>');
}
-
- termlist.innerHTML += "<hr/>";
-
- termlist.innerHTML += '<div class="termtitle">.::Subjects</div>';
- for (var i = 0; i < data.subject.length; i++ ) {
- termlist.innerHTML += '<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">'
- + data.subject[i].name
- + '</a><span> ('
- + data.subject[i].freq
- + ')</span><br/>';
+
+ termlists.push('<hr/><div class="termtitle">.::Subjects</div>');
+ for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
+ termlists.push('<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">' + data.subject[i].name + '</a><span> ('
+ + data.subject[i].freq + ')</span><br/>');
}
-
- termlist.innerHTML += "<hr/>";
-
- termlist.innerHTML += '<div class="termtitle">.::Authors</div>';
- for (var i = 0; i < data.author.length; i++ ) {
- termlist.innerHTML += '<a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
+
+ termlists.push('<hr/><div class="termtitle">.::Authors</div>');
+ for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
+ termlists.push('<a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
+ data.author[i].name
+ ' </a><span> ('
+ data.author[i].freq
- + ')</span><br/>';
+ + ')</span><br/>');
}
-
+ var termlist = document.getElementById("termlist");
+ replaceHtml(termlist, termlists.join(''));
}
function my_onrecord(data) {
+ // FIXME: record is async!!
+ clearTimeout(my_paz.recordTimer);
// in case on_show was faster to redraw element
var detRecordDiv = document.getElementById('det_'+data.recid);
- if ( detRecordDiv )
- return;
+ if (detRecordDiv) return;
curDetRecData = data;
- drawCurDetails();
+ var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
+ var html = renderDetails(curDetRecData);
+ recordDiv.innerHTML += html;
}
function my_onbytarget(data) {
}
// detailed record drawing
-function showDetails ( prefixRecId ) {
+function showDetails (prefixRecId) {
var recId = prefixRecId.replace('rec_', '');
+ var oldRecId = curDetRecId;
+ curDetRecId = recId;
// remove current detailed view if any
- var detRecordDiv = document.getElementById('det_'+curDetRecId);
+ var detRecordDiv = document.getElementById('det_'+oldRecId);
// lovin DOM!
- if ( detRecordDiv )
- detRecordDiv.parentNode.removeChild(detRecordDiv);
+ if (detRecordDiv)
+ detRecordDiv.parentNode.removeChild(detRecordDiv);
- // if the same clicked do not redraw
- if ( recId == curDetRecId ) {
+ // if the same clicked, just hide
+ if (recId == oldRecId) {
curDetRecId = '';
+ curDetRecData = null;
return;
}
-
- curDetRecId = recId;
// request the record
my_paz.record(recId);
}
-function drawCurDetails ()
+function replaceHtml(el, html) {
+ var oldEl = typeof el === "string" ? document.getElementById(el) : el;
+ /*@cc_on // Pure innerHTML is slightly faster in IE
+ oldEl.innerHTML = html;
+ return oldEl;
+ @*/
+ var newEl = oldEl.cloneNode(false);
+ newEl.innerHTML = html;
+ oldEl.parentNode.replaceChild(newEl, oldEl);
+ /* Since we just removed the old element from the DOM, return a reference
+ to the new element, which can be used to restore variable references. */
+ return newEl;
+};
+
+function renderDetails(data, marker)
{
- var data = curDetRecData;
- var recordDiv = document.getElementById('recdiv_'+data.recid);
- var details = "";
- if (data["md-title"] != undefined)
- details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"] + '</td></tr>';
+ var details = '<div class="details" id="det_'+data.recid+'"><table>';
+ if (marker) details += '<tr><td>'+ marker + '</td></tr>';
+ if (data["md-title"] != undefined) {
+ details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
+ if (data["md-title-remainder"] !== undefined) {
+ details += ' : <span>' + data["md-title-remainder"] + ' </span>';
+ }
+ if (data["md-title-responsibility"] !== undefined) {
+ details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
+ }
+ details += '</td></tr>';
+ }
if (data["md-date"] != undefined)
details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
if (data["md-author"] != undefined)
details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
if (data["location"][0]["@name"] != undefined)
details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
- recordDiv.innerHTML += '<div class="details" id="det_'+data.recid+'"><table>' + details + '</table></div>';
+ details += '</table></div>';
+ return details;
}
//EOF