X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;ds=sidebyside;f=www%2Fjsdemo%2Fexample_client.js;h=8342494859d9a2a72b9ef965f1ace4272a5dd87c;hb=4a8888ef6e781b9cafd44539d12bed864b4f1af3;hp=6a4be33ca55a48ccabfe8da43d735a46163848a4;hpb=d63d842aedbc1c8ce12b3efcfc986ea1c3b7ec2d;p=pazpar2-moved-to-github.git
diff --git a/www/jsdemo/example_client.js b/www/jsdemo/example_client.js
index 6a4be33..8342494 100644
--- a/www/jsdemo/example_client.js
+++ b/www/jsdemo/example_client.js
@@ -1,5 +1,4 @@
/* 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
@@ -7,12 +6,14 @@
// 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,
+var my_paz = new pz2( { "onshow": my_onshow,
"showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
"pazpar2path": pazpar2path,
"oninit": my_oninit,
@@ -21,6 +22,7 @@ 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;
@@ -49,37 +51,40 @@ function my_onshow(data) {
var pager = document.getElementById("pager");
pager.innerHTML = "";
pager.innerHTML +='
Displaying: '
- + data.start + ' to ' + (data.start + data.num) +
+ + (data.start + 1) + ' to ' + (data.start + data.num) +
' of ' + data.merged + ' (found: '
+ data.total + ')
';
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 = ''
+ html.push('
'
+'
'+ (i + 1 + recPerPage * (curPage - 1)) +'. '
+'
'
- + hit["md-title"] +' ';
- if (hit["md-title-remainder"] !== undefined) {
- html += '
' + hit["md-title-remainder"] + ' ';
- }
- if (hit["md-title-responsibility"] !== undefined) {
- html += '
'+ hit["md-title-responsibility"] +'';
- }
- html += '
';
- results.innerHTML += html;
+ + hit["md-title"] +' ');
+ if (hit["md-title-remainder"] !== undefined) {
+ html.push('
' + hit["md-title-remainder"] + ' ');
+ }
+ if (hit["md-title-responsibility"] !== undefined) {
+ html.push('
'+hit["md-title-responsibility"]+'');
+ }
if (hit.recid == curDetRecId) {
- drawCurDetails();
+ html.push(renderDetails(curDetRecData));
}
+ html.push('
');
}
+ replaceHtml(results, html.join(''));
}
function my_onstat(data) {
var stat = document.getElementById("stat");
+ if (stat == null)
+ return;
+
stat.innerHTML = ' .:STATUS INFO -- Active clients: '
+ data.activeclients
+ '/' + data.clients + ' -- '
@@ -88,51 +93,42 @@ function my_onstat(data) {
}
function my_onterm(data) {
- var termlist = document.getElementById("termlist");
- termlist.innerHTML = "
TERMLISTS:
";
-
- termlist.innerHTML += '.::Sources
';
+ var termlists = [];
+ termlists.push('
TERMLISTS:
.::Sources
');
for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
- termlist.innerHTML += ''
- + data.xtargets[i].name
- + ' ('
- + data.xtargets[i].freq
- + ')
';
+ termlists.push('' + data.xtargets[i].name
+ + ' (' + data.xtargets[i].freq + ')
');
}
-
- termlist.innerHTML += "
";
-
- termlist.innerHTML += '.::Subjects
';
+
+ termlists.push('
.::Subjects
');
for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
- termlist.innerHTML += ''
- + data.subject[i].name
- + ' ('
- + data.subject[i].freq
- + ')
';
+ termlists.push('' + data.subject[i].name + ' ('
+ + data.subject[i].freq + ')
');
}
-
- termlist.innerHTML += "
";
-
- termlist.innerHTML += '.::Authors
';
+
+ termlists.push('
.::Authors
');
for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
- termlist.innerHTML += ''
+ termlists.push(''
+ data.author[i].name
+ ' ('
+ data.author[i].freq
- + ')
';
+ + ')
');
}
-
+ 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) {
@@ -249,7 +245,7 @@ function drawPager (pagerDiv)
var prev = '<< Prev | ';
if (curPage > 1)
- var prev = ''
+ prev = ''
+'<< Prev | ';
var middle = '';
@@ -264,14 +260,14 @@ function drawPager (pagerDiv)
var next = ' | Next >>';
if (pages - curPage > 0)
- var next = ' | '
+ next = ' | '
+'Next >>';
- predots = '';
+ var predots = '';
if (firstClkbl > 1)
predots = '...';
- postdots = '';
+ var postdots = '';
if (lastClkbl < pages)
postdots = '...';
@@ -321,40 +317,54 @@ function switchView(view) {
}
// 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 = "";
+ var details = '';
+ if (marker) details += ''+ marker + ' |
';
if (data["md-title"] != undefined) {
details += 'Title | : '+data["md-title"];
- if (data["md-title-remainder"] !== undefined) {
- details += ' : ' + data["md-title-remainder"] + ' ';
- }
- if (data["md-title-responsibility"] !== undefined) {
- details += ' '+ data["md-title-responsibility"] +'';
- }
- details += ' |
';
+ if (data["md-title-remainder"] !== undefined) {
+ details += ' : ' + data["md-title-remainder"] + ' ';
+ }
+ if (data["md-title-responsibility"] !== undefined) {
+ details += ' '+ data["md-title-responsibility"] +'';
+ }
+ details += '';
}
if (data["md-date"] != undefined)
details += 'Date | : ' + data["md-date"] + ' |
';
@@ -366,6 +376,7 @@ function drawCurDetails ()
details += 'Subject | : ' + data["location"][0]["md-subject"] + ' |
';
if (data["location"][0]["@name"] != undefined)
details += 'Location | : ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + ' |
';
- recordDiv.innerHTML += '';
+ details += '
';
+ return details;
}
//EOF