function init() {
- my_paz = new pz2( { "onshow": my_onshow, "onstat": my_onstat, "onterm": my_onterm }, true );
+ my_paz = new pz2( { "onshow": my_onshow,
+ "onstat": my_onstat,
+ "onterm": my_onterm,
+ "termlist": "subject,author",
+ "onrecord": my_onrecord } );
}
function my_onshow(data) {
var body = document.getElementById("body");
-
- body.innerHTML = '<div>active clients: ' + data.activeclients + '</div>' +
+ body.innerHTML = "";
+ for ( i = 0; i < data.hits.length; i++) {
+ var hit = data.hits[i];
+ body.innerHTML += '<div id="' + hit.recid + '" onclick="my_paz.record(this.id)"><span>' + i +
+ '. </span><span><b>' + hit["md-title"] +
+ ' </b></span> by <span><i>' + hit["md-author"] + '</i></span></div>';
+ }
+ body.innerHTML += "<hr/>";
+ body.innerHTML += '<div>active clients: ' + data.activeclients + '</div>' +
'<div>merged: ' + data.merged + '</div>' +
'<div>total: ' + data.total + '</div>' +
'<div>start: ' + data.start + '</div>' +
function my_onstat(data) {
var stat = document.getElementById("stat");
-
stat.innerHTML = '<div>active clients: ' + data.activeclients + '</div>' +
'<div>hits: ' + data.hits + '</div>' +
'<div>records: ' + data.records + '</div>' +
function my_onterm(data) {
var termlist = document.getElementById("termlist");
-
termlist.innerHTML = "";
+ termlist.innerHTML += "<div><b> --Author-- </b></div>";
for ( i = 0; i < data.author.length; i++ ) {
termlist.innerHTML += '<div><span>' + data.author[i].name + ' </span><span> (' + data.author[i].freq + ')</span></div>';
}
+ termlist.innerHTML += "<hr/>";
+ termlist.innerHTML += "<div><b> --Subject-- </b></div>";
+ for ( i = 0; i < data.subject.length; i++ ) {
+ termlist.innerHTML += '<div><span>' + data.subject[i].name + ' </span><span> (' + data.subject[i].freq + ')</span></div>';
+ }
+}
+
+function my_onrecord(data) {
+ recordDiv = document.getElementById(data.recid);
+ recordDiv.innerHTML = "<table><tr><td><b>Ttle</b> : </td><td>" + data["md-title"] +
+ "</td></tr><tr><td><b>Date</b> : </td><td>" + data["md-date"] +
+ "</td></tr><tr><td><b>Author</b> : </td><td>" + data["md-author"] +
+ "</td></tr><tr><td><b>Subject</b> : </td><td>" + data["md-subject"] + "</td></tr>";
+
}
window.undefined = window.undefined;
var pz2 = function(callbackArr, autoInit, keepAlive) {
- //
- if ( callbackArr == null )
- return null;
-
//for convenience
myself = this;
+
+ // at least one callback required
+ if ( !callbackArr )
+ throw new Error("Callback parameters array has to be suplied when instantiating a class");
+
+ // function callbacks
+ myself.statCallback = callbackArr.onstat;
+ myself.showCallback = callbackArr.onshow;
+ myself.termlistCallback = callbackArr.onterm;
+ myself.recordCallback = callbackArr.onrecord || null;
+
+ // termlist keys
+ myself.termKeys = callbackArr.termlist || "subject";
myself.pz2String = "search.pz2";
- myself.sessionID;
- myself.initStatus = 0;
- myself.pingStatus = 0;
+ myself.sessionID = null;
+ myself.initStatusOK = false;
+ myself.pingStatusOK = false;
+ myself.searchStatusOK = false;
myself.keepAlive = 50000;
if ( keepAlive < myself.keepAlive )
// for sorting
myself.currentSort = "relevance";
- myself.sortKeywords = [ "relevance", "title", "author" ];
-
- // function callbacks
- myself.statCallback = callbackArr.onstat;
- myself.showCallback = callbackArr.onshow;
- myself.termlistCallback = callbackArr.onterm;
+ // where are we?
+ myself.currentStart = 0;
+ myself.currentNum = 20;
//timers
myself.statTime = 2000;
- myself.statTimer;
+ myself.statTimer = null;
myself.termTime = 1000;
- myself.termTimer;
+ myself.termTimer = null;
myself.showTime = 1000;
- myself.showTimer;
+ myself.showTimer = null;
- if (autoInit == true)
+ // error handling
+ $(document).ajaxError(
+ function (request, settings, exception) {
+ if ( settings.responseXML && settings.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue)
+ throw new Error( settings.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue);
+ });
+
+ if (autoInit !== false)
myself.init(myself.keepAlive);
}
pz2.prototype = {
$.get( myself.pz2String,
{ command: "init" },
function(data) {
- if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" )
- myself.initStatus = 1;
- myself.sessionID = data.getElementsByTagName("session")[0].childNodes[0].nodeValue;
- setTimeout(myself.ping, myself.keepAlive);
+ if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
+ myself.initStatusOK = true;
+ myself.sessionID = data.getElementsByTagName("session")[0].childNodes[0].nodeValue;
+ setTimeout(myself.ping, myself.keepAlive);
+ }
}
);
},
// no need to ping explicitly
ping: function() {
- if( !myself.initStatus )
+ if( !myself.initStatusOK )
return;
// session is not initialized code here
$.get( myself.pz2String,
{ command: "ping", session: myself.sessionID },
function(data) {
if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
- myself.pingStatus = 1;
- setTimeout(myself.ping, myself.keepAlive);
+ myself.pingStatusOK = true;
+ setTimeout("myself.ping()", myself.keepAlive);
}
else
location = "?";
clearTimeout(myself.statTimer);
clearTimeout(myself.showTimer);
clearTimeout(myself.termTimer);
- if( !myself.initStatus )
+ if( !myself.initStatusOK )
return;
$.get( myself.pz2String,
{ command: "search", session: myself.sessionID, query: query },
function(data) {
if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
- myself.searchStatus = 1;
+ myself.searchStatusOK = true;
//piggyback search
myself.show(0, num, sort)
if ( myself.statCallback )
- myself.statTimer = setTimeout(myself.stat, myself.statTime / 2);
+ myself.statTimer = setTimeout("myself.stat()", myself.statTime / 2);
if ( myself.termlistCallback )
- myself.termTimer = setTimeout(myself.termlist, myself.termTime / 2);
+ myself.termTimer = setTimeout("myself.termlist()", myself.termTime / 2);
}
else
location = "?";
);
},
// callback, not to be called explicitly
- stat: function() {
- if( !myself.searchStatus )
+ stat: function(test) {
+ if( !myself.searchStatusOK )
return;
$.get( myself.pz2String,
{ command: "stat", session: myself.sessionID },
}
myself.statCallback(stat);
if (activeClients > 0)
- myself.statTimer = setTimeout(myself.stat, myself.statTime);
+ myself.statTimer = setTimeout("myself.stat()", myself.statTime);
}
else
- myself.statTimer = setTimeout(myself.stat, myself.statTime / 4)
+ myself.statTimer = setTimeout("myself.stat()", myself.statTime / 4)
//location = "?";
// some error handling
}
//callback not to be called explicitly
show: function(start, num, sort) {
clearTimeout(myself.showTimer);
- if( !myself.searchStatus )
+ if( !myself.searchStatusOK )
return;
- if ( myself.sortKeywords.some( function(element, index, arr) { if (element == sort) return true; else return false; } ) )
+
+ if( sort !== undefined )
myself.currentSort = sort;
+ if( start !== undefined )
+ myself.currentStart = Number( start );
+ if( num !== undefined )
+ myself.currentNum = Number( num );
+
$.get( myself.pz2String,
- { "command": "show", "session": myself.sessionID, "start": start, "num": num, "sort": myself.currentSort, "block": 1 },
+ { "command": "show", "session": myself.sessionID, "start": myself.currentStart, "num": myself.currentNum, "sort": myself.currentSort, "block": 1 },
function(data) {
if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
var activeClients = Number( data.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue );
"merged": Number( data.getElementsByTagName("merged")[0].childNodes[0].nodeValue ),
"total": Number( data.getElementsByTagName("total")[0].childNodes[0].nodeValue ),
"start": Number( data.getElementsByTagName("start")[0].childNodes[0].nodeValue ),
- "num": Number( data.getElementsByTagName("num")[0].childNodes[0].nodeValue )
+ "num": Number( data.getElementsByTagName("num")[0].childNodes[0].nodeValue ),
+ "hits": []
+ }
+
+ var hits = data.getElementsByTagName("hit");
+ var hit = new Array();
+ for (i = 0; i < hits.length; i++) {
+ show.hits[i] = new Array();
+ for ( j = 0; j < hits[i].childNodes.length; j++) {
+ if ( hits[i].childNodes[j].nodeType == Node.ELEMENT_NODE ) {
+ var nodeName = hits[i].childNodes[j].nodeName;
+ var nodeText = hits[i].childNodes[j].firstChild.nodeValue;
+ show.hits[i][nodeName] = nodeText;
+ }
+ }
}
+
//TODO include records
myself.showCallback(show);
if (activeClients > 0)
- myself.showTimer = setTimeout("myself.show(" + start + "," + num + "," + sort + ")", myself.showTime);
+ myself.showTimer = setTimeout("myself.show()", myself.showTime);
}
else
- myself.showTimer = setTimeout("myself.show(" + start + "," + num + "," + sort + ")", myself.showTime / 4);
+ myself.showTimer = setTimeout("myself.show()", myself.showTime / 4);
// location = "?";
// some error handling
}
);
},
record: function(id) {
+ if( !myself.searchStatusOK )
+ return;
+ $.get( myself.pz2String,
+ { "command": "record", "session": myself.sessionID, "id": id },
+ function(data) {
+ var recordNode;
+ var record = new Array();
+ if ( recordNode = data.getElementsByTagName("record")[0] ) {
+ for ( i = 0; i < recordNode.childNodes.length; i++) {
+ if ( recordNode.childNodes[i].nodeType == Node.ELEMENT_NODE ) {
+ var nodeName = recordNode.childNodes[i].nodeName;
+ var nodeText = recordNode.childNodes[i].firstChild.nodeValue;
+ record[nodeName] = nodeText;
+ }
+ }
+ myself.recordCallback(record);
+ }
+ else
+ alert("");
+ //location = "?";
+ // some error handling
+ }
+ );
+
},
- termlist: function(name) {
- if( !myself.searchStatus )
+ termlist: function() {
+ if( !myself.searchStatusOK )
return;
$.get( myself.pz2String,
- { "command": "termlist", "session": myself.sessionID, "name": "author" },
+ { "command": "termlist", "session": myself.sessionID, "name": myself.termKeys },
function(data) {
if ( data.getElementsByTagName("termlist") ) {
var termList = { "activeclients": Number( data.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue ) }
}
myself.termlistCallback(termList);
if (termList["activeclients"] > 0)
- myself.termTimer = setTimeout("myself.termlist(" + name + ")" , myself.termTime);
+ myself.termTimer = setTimeout("myself.termlist()", myself.termTime);
}
else
- myself.termTimer = setTimeout("myself.termlist(" + name + ")" , myself.termTime / 4);
+ myself.termTimer = setTimeout("myself.termlist()", myself.termTime / 4);
//location = "?";
// some error handling
}