/*
-** $Id: pz2.js,v 1.30 2007-06-04 12:24:59 jakub Exp $
+** $Id: pz2.js,v 1.45 2007-07-10 10:17:17 adam Exp $
** pz2.js - pazpar2's javascript client library.
*/
//supported pazpar2's protocol version
__myself.suppProtoVer = '1';
- __myself.pz2String = "search.pz2";
+ __myself.pz2String = paramArray.pazpar2path || "search.pz2";
__myself.stylesheet = paramArray.detailstylesheet || null;
-
+ __myself.useSessions = true;
+ if (paramArray.usesessions != undefined) {
+ __myself.useSessions = paramArray.usesessions;
+ }
+
//load stylesheet if required in async mode
if( __myself.stylesheet ) {
var request = new pzHttpRequest( __myself.stylesheet );
- request.get(
- {},
- function ( doc ) {
- __myself.xslDoc = doc;
- }
- );
+ request.get( {}, function ( doc ) { __myself.xslDoc = doc; } );
}
-
+
// at least one callback required
if ( !paramArray )
throw new Error("An array with parameters has to be suplied when instantiating a class");
if (paramArray.autoInit !== false)
__myself.init();
};
-pz2.prototype = {
+pz2.prototype =
+{
+ stop: function ()
+ {
+ clearTimeout(__myself.statTimer);
+ clearTimeout(__myself.showTimer);
+ clearTimeout(__myself.termTimer);
+ clearTimeout(__myself.bytargetTimer);
+ },
+
reset: function ()
{
__myself.sessionID = null;
__myself.pingStatusOK = false;
__myself.searchStatusOK = false;
- clearTimeout(__myself.statTimer);
- clearTimeout(__myself.showTimer);
- clearTimeout(__myself.termTimer);
- clearTimeout(__myself.bytargetTimer);
+ __myself.stop();
if ( __myself.resetCallback )
__myself.resetCallback();
init: function ( sessionId )
{
__myself.reset();
+
if ( sessionId != undefined ) {
__myself.initStatusOK = true;
__myself.sessionID = sessionId;
__myself.ping();
-
- } else {
+ } else if (__myself.useSessions) {
var request = new pzHttpRequest(__myself.pz2String, __myself.errorHandler);
request.get(
{ "command": "init" },
setTimeout("__myself.init()", 1000);
}
);
- }
+ } else {
+ __myself.initStatusOK = true;
+ }
},
// no need to ping explicitly
ping: function ()
else
throw new Error("You need to supply query to the search command");
- if( filter !== undefined )
- var searchParams = { "command": "search", "session": __myself.sessionID, "query": __myself.currQuery, "filter": filter };
- else
- var searchParams = { "command": "search", "session": __myself.sessionID, "query": __myself.currQuery };
+ var searchParams = { "command": "search", "query": __myself.currQuery, "session": __myself.sessionID };
+
+ if (filter !== undefined)
+ searchParams["filter"] = filter;
+
var request = new pzHttpRequest(__myself.pz2String, __myself.errorHandler);
request.get(
searchParams,
}
else {
var nodeName = hits[i].childNodes[j].nodeName;
- var nodeText = hits[i].childNodes[j].firstChild.nodeValue;
+ var nodeText = 'ERROR'
+ if ( hits[i].childNodes[j].firstChild )
+ nodeText = hits[i].childNodes[j].firstChild.nodeValue;
show.hits[i][nodeName] = nodeText;
}
}
}
);
},
- record: function(id)
+ record: function(id,offset)
{
- if( !__myself.searchStatusOK )
+ if( !__myself.searchStatusOK && __myself.useSessions)
return;
if( id !== undefined )
__myself.currRecID = id;
var request = new pzHttpRequest(__myself.pz2String, __myself.errorHandler);
+
+ var recordParams = { "command": "record", "session": __myself.sessionID, "id": __myself.currRecID };
+ if (offset !== undefined) {
+ recordParams["offset"] = offset;
+ }
+ __myself.currRecOffset = offset;
request.get(
- { "command": "record", "session": __myself.sessionID, "id": __myself.currRecID },
+ recordParams,
function(data) {
var recordNode;
var record = new Array();
- if ( recordNode = data.getElementsByTagName("record")[0] ) {
+ record['xmlDoc'] = data;
+ if (__myself.currRecOffset !== undefined) {
+ record['offset'] = __myself.currRecOffset;
+ __myself.recordCallback(record);
+ } else if ( recordNode = data.getElementsByTagName("record")[0] ) {
// if stylesheet was fetched do not parse the response
if ( __myself.xslDoc ) {
record['recid'] = recordNode.getElementsByTagName("recid")[0].firstChild.nodeValue;
- record['xmlDoc'] = data;
record['xslDoc'] = __myself.xslDoc;
} else {
for ( i = 0; i < recordNode.childNodes.length; i++) {
//for each term in the list
for (j = 0; j < terms.length; j++) {
var term = {
- "name": terms[j].getElementsByTagName("name")[0].childNodes[0].nodeValue || 'ERROR',
+ "name": (terms[j].getElementsByTagName("name")[0].childNodes.length
+ ? terms[j].getElementsByTagName("name")[0].childNodes[0].nodeValue
+ : 'ERROR'),
"freq": terms[j].getElementsByTagName("frequency")[0].childNodes[0].nodeValue || 'ERROR'
};
this.request = null;
this.url = url;
this.errorHandler = errorHandler || null;
+ this.async = true;
if ( window.XMLHttpRequest ) {
this.request = new XMLHttpRequest();
{
get: function ( params, callback )
{
- this.callback = callback;
-
- var getUrl = this.url;
- var paramArr = new Array();
+ this._send( 'GET', params, '', callback );
+ },
- for ( var key in params ) {
- paramArr.push(key + '=' + encodeURI(params[key]) );
- }
+ post: function ( params, data, callback )
+ {
+ this._send( 'POST', params, data, callback );
+ },
- if ( paramArr.length )
- getUrl += '?' + paramArr.join('&');
+ load: function ()
+ {
+ this.async = false;
+ this.request.open( 'GET', this.url, this.async );
+ this.request.send('');
+ if ( this.request.status == 200 )
+ return this.request.responseXML;
+ },
+ _send: function ( type, params, data, callback )
+ {
+ this.callback = callback;
var context = this;
- this.request.open( 'GET', getUrl, true );
- this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
- //this.request.setRequestHeader('Accept-Charset', 'UTF-8');
+ this.async = true;
+ this.request.open( type, this._urlAppendParams(params), this.async );
this.request.onreadystatechange = function () {
context._handleResponse();
}
- this.request.send(null);
+ this.request.send(data);
+ },
+
+ _urlAppendParams: function (params)
+ {
+ var getUrl = this.url;
+
+ var sep = '?';
+ var el = params;
+ for (var key in el) {
+ if (el[key] != null) {
+ getUrl += sep + key + '=' + encodeURI(el[key]);
+ sep = '&';
+ }
+ }
+ return getUrl;
},
_handleResponse: function ()
this.simpleFilter = null;
this.numTerms = 0;
},
+ clearSimpleQuery: function()
+ {
+ this.simpleQuery = '';
+ },
addTerm: function(field, value)
{
var term = {"field": field, "value": value};