From 12af7f286ec9d68fa3f9da31f85a652f3c3889cb Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 21 Jun 2007 09:44:17 +0000 Subject: [PATCH] example moved to jsdemo --- www/jsdemo/example_client.js | 204 ++++++++++++++++++++++++++++++++++++++++++ www/jsdemo/index.html | 141 +++++++++++++++++++++++++++++ 2 files changed, 345 insertions(+) create mode 100644 www/jsdemo/example_client.js create mode 100644 www/jsdemo/index.html diff --git a/www/jsdemo/example_client.js b/www/jsdemo/example_client.js new file mode 100644 index 0000000..a9fe032 --- /dev/null +++ b/www/jsdemo/example_client.js @@ -0,0 +1,204 @@ +/* A very simple client that shows a basic usage of the pz2.js +** $Id: example_client.js,v 1.1 2007-06-21 09:44:17 adam Exp $ +*/ + +// create a parameters array and pass it to the pz2's constructor +// then register the form submit event with the pz2.search function +// autoInit is set to true on default + +my_paz = new pz2( { "onshow": my_onshow, + "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way + "pazpar2path": "/pazpar2/search.pz2", + "onstat": my_onstat, + "onterm": my_onterm, + "termlist": "subject,author", + "onbytarget": my_onbytarget, + "onrecord": my_onrecord } ); +// some state vars +var curPage = 1; +var recPerPage = 20; +var totalRec = 0; +var curDetRecId = -1; +var curDetRecData = null; + +// wait until the DOM is ready +function domReady () +{ + document.search.onsubmit = onFormSubmitEventHandler; + my_paz.stat(); + my_paz.bytarget(); +} + +// when search button pressed +function onFormSubmitEventHandler() +{ + curPage = 1; + curDetRecId = -1; + totalRec = 0; + my_paz.search(document.search.query.value, recPerPage, 'relevance'); + return false; +} + +// +// pz2.js event handlers: +// + +function my_onshow(data) { + totalRec = data.merged; + + var body = document.getElementById("body"); + body.innerHTML = ""; + + body.innerHTML +='
Displaying: ' + + data.start + ' to ' + (data.start + data.num) + + ' of ' + data.merged + ' (total not merged hits: ' + + data.total + ')
'; + + body.innerHTML += '
' + +'<< Prev | ' + +'' + +'Next >>

'; + + for (var i = 0; i < data.hits.length; i++) { + var hit = data.hits[i]; + body.innerHTML += '
' + +'' + (i + 1 + recPerPage * ( curPage - 1)) + '. ' + +'' + hit["md-title"] + + ' by ' + hit["md-author"] + '
'; + + if ( hit.recid == curDetRecId ) { + drawCurDetails(); + } + } + +} + +function my_onstat(data) { + var stat = document.getElementById("stat"); + stat.innerHTML = 'Active clients: '+ data.activeclients + + '/' + data.clients + ' | ' + + 'Retrieved records: ' + data.records + + '/' + data.hits + ''; +} + +function my_onterm(data) { + var termlist = document.getElementById("termlist"); + termlist.innerHTML = "
TERMLISTS:
"; + termlist.innerHTML += '
.::Subjects
'; + for (var i = 0; i < data.subject.length; i++ ) { + termlist.innerHTML += '' + + data.subject[i].name + + ' (' + + data.subject[i].freq + + ')
'; + } + termlist.innerHTML += "
"; + termlist.innerHTML += '
.::Authors
'; + for (var i = 0; i < data.author.length; i++ ) { + termlist.innerHTML += '' + + data.author[i].name + + ' (' + + data.author[i].freq + + ')
'; + } + +} + +function my_onrecord(data) { + // in case on_show was faster to redraw element + var detRecordDiv = document.getElementById('det_'+data.recid); + if ( detRecordDiv ) + return; + + curDetRecData = data; + drawCurDetails(); +} + +function my_onbytarget(data) { + var targetDiv = document.getElementById("bytarget"); + var table = 'Target IDHitsDiags' + +'RecordsState'; + + for (var i = 0; i < data.length; i++ ) { + table += "" + data[i].id + + "" + data[i].hits + + "" + data[i].diagnostic + + "" + data[i].records + + "" + data[i].state + ""; + } + + table += ''; + targetDiv.innerHTML = table; + +} + +// detailed record drawing +function showDetails ( prefixRecId ) { + var recId = Number(prefixRecId.replace('rec_', '')); + + // remove current detailed view if any + var detRecordDiv = document.getElementById('det_'+curDetRecId); + // lovin DOM! + if ( detRecordDiv ) + detRecordDiv.parentNode.removeChild(detRecordDiv); + + // if the same clicked do not redraw + if ( recId == curDetRecId ) { + curDetRecId = -1; + return; + } + + curDetRecId = recId; + + // request the record + my_paz.record(recId); +} + +function drawCurDetails () +{ + var data = curDetRecData; + var recordDiv = document.getElementById('rec_'+data.recid); + recordDiv.innerHTML += '
Ttle: '+data["md-title"] + + "
Date: " + data["md-date"] + + "
Author: " + data["md-author"] + + "
Subject: " + data["md-subject"] + + "
Location: " + data["location"][0].name + + "
"; +} + + +// simple paging functions + +function pagerNext() { + if ( totalRec - recPerPage*curPage > 0) { + my_paz.showNext(); + curPage++; + } +} + +function pagerPrev() { + if ( my_paz.showPrev() != false ) + curPage--; +} + +// swithing view between targets and records + +function switchView(view) { + + var targets = document.getElementById('targetview'); + var records = document.getElementById('recordview'); + + switch(view) { + case 'targetview': + targets.style.display = "block"; + records.style.display = "none"; + break; + case 'recordview': + targets.style.display = "none"; + records.style.display = "block"; + break; + default: + alert('Unknown view.'); + } +} diff --git a/www/jsdemo/index.html b/www/jsdemo/index.html new file mode 100644 index 0000000..324008c --- /dev/null +++ b/www/jsdemo/index.html @@ -0,0 +1,141 @@ + + + + + + + + Exemplary pazpar2 client [using pz2.js] + + + + + + + + Records Browser + | + Targets Information + +

+ + + + + + +
PazPar2 +
+ + +
+
+ +
+ + + + + + +
+
+
+
+
+ +
+
+ STATUS INFO: +
+
+
+ +
+ + + + + -- 1.7.10.4