X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=js%2Fpz2.js;h=718c74d14494c07e5ced52ee63b2271417d1e337;hb=96ddfff120e5c0ce386ee1b953f55bfcf00f63ac;hp=e879197b6e81de2cbf11ebbc0ca3a9188b5b8389;hpb=aa622908a43254464ce8aa3099f863459b024a72;p=pazpar2-moved-to-github.git diff --git a/js/pz2.js b/js/pz2.js index e879197..718c74d 100644 --- a/js/pz2.js +++ b/js/pz2.js @@ -1,5 +1,5 @@ /* -** $Id: pz2.js,v 1.61 2007-10-02 11:47:50 jakub Exp $ +** $Id: pz2.js,v 1.64 2007-10-31 10:29:01 jakub Exp $ ** pz2.js - pazpar2's javascript client library. */ @@ -29,7 +29,7 @@ var pz2 = function ( paramArray ) // at least one callback required if ( !paramArray ) - throw new Error("Pz2.js: An array with parameters has to be suplied when instantiating a class"); + throw new Error("Pz2.js: Array with parameters has to be suplied."); //supported pazpar2's protocol version this.suppProtoVer = '1'; @@ -50,6 +50,7 @@ var pz2 = function ( paramArray ) this.errorHandler = paramArray.errorhandler || null; // function callbacks + this.initCallback = paramArray.oninit || null; this.statCallback = paramArray.onstat || null; this.showCallback = paramArray.onshow || null; this.termlistCallback = paramArray.onterm || null; @@ -178,7 +179,9 @@ pz2.prototype = if ( data.getElementsByTagName("protocol")[0] .childNodes[0].nodeValue != context.suppProtoVer ) - throw new Error("Server's protocol not supported by the client"); + throw new Error( + "Server's protocol not supported by the client" + ); context.initStatusOK = true; context.sessionID = data.getElementsByTagName("session")[0] @@ -189,6 +192,8 @@ pz2.prototype = }, context.keepAlive ); + if ( context.initCallback ) + context.initCallback(); } else context.throwError('Init failed. Malformed WS resonse.', @@ -205,9 +210,9 @@ pz2.prototype = { // pinging only makes sense when using pazpar2 directly if( !this.initStatusOK || !this.useSessions ) - throw new Error('Pz2.js: Ping not allowed (proxy mode) or session not initialized.'); - // session is not initialized code here - + throw new Error( + 'Pz2.js: Ping not allowed (proxy mode) or session not initialized.' + ); var context = this; var request = new pzHttpRequest(this.pz2String, this.errorHandler); request.get( @@ -359,7 +364,9 @@ pz2.prototype = show: function(start, num, sort) { if( !this.searchStatusOK && this.useSessions ) - throw new Error('Pz2.js: show command has to be preceded with a search command.'); + throw new Error( + 'Pz2.js: show command has to be preceded with a search command.' + ); // if called explicitly takes precedence clearTimeout(this.showTimer); @@ -413,18 +420,19 @@ pz2.prototype = for (i = 0; i < hits.length; i++) { show.hits[i] = new Array(); show.hits[i]['location'] = new Array(); + var locCount = 0; for ( j = 0; j < hits[i].childNodes.length; j++) { - var locCount = 0; if ( hits[i].childNodes[j].nodeType == Node.ELEMENT_NODE ) { if (hits[i].childNodes[j].nodeName == 'location') { var locNode = hits[i].childNodes[j]; var id = locNode.getAttribute('id'); - show.hits[i]['location'][id] = { + show.hits[i]['location'][locCount] = { "id": locNode.getAttribute("id"), "name": locNode.getAttribute("name") }; + locCount++; } else { var nodeName = @@ -542,7 +550,9 @@ pz2.prototype = "name": locationNodes[i].getAttribute("name") }; - for ( j = 0; j < locationNodes[i].childNodes.length; j++) { + for (j = 0; + j < locationNodes[i].childNodes.length; + j++) { if ( locationNodes[i].childNodes[j].nodeType == Node.ELEMENT_NODE ) { var nodeName = @@ -571,7 +581,9 @@ pz2.prototype = termlist: function() { if( !this.searchStatusOK && this.useSessions ) - throw new Error('Pz2.js: termlist command has to be preceded with a search command.'); + throw new Error( + 'Pz2.js: termlist command has to be preceded with a search command.' + ); // if called explicitly takes precedence clearTimeout(this.termTimer); @@ -645,7 +657,9 @@ pz2.prototype = bytarget: function() { if( !this.initStatusOK && this.useSessions ) - throw new Error('Pz2.js: bytarget command has to be preceded with a search command.'); + throw new Error( + 'Pz2.js: bytarget command has to be preceded with a search command.' + ); // no need to continue if( !this.searchStatusOK ) @@ -798,12 +812,21 @@ pzHttpRequest.prototype = // pick up pazpr2 errors first if ( this.request.responseXML && this.request.responseXML.documentElement.nodeName == 'error' - && this.request.responseXML.getElementsByTagName("error").length ) { + && this.request.responseXML.getElementsByTagName("error") + .length ) { var errAddInfo = ''; - if ( this.request.responseXML.getElementsByTagName("error")[0].childNodes.length ) - errAddInfo = ': ' + this.request.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue; - var errMsg = this.request.responseXML.getElementsByTagName("error")[0].getAttribute("msg"); - var errCode = this.request.responseXML.getElementsByTagName("error")[0].getAttribute("code"); + if ( this.request.responseXML.getElementsByTagName("error")[0] + .childNodes.length ) + errAddInfo = ': ' + + this.request.responseXML + .getElementsByTagName("error")[0] + .childNodes[0].nodeValue; + var errMsg = + this.request.responseXML.getElementsByTagName("error")[0] + .getAttribute("msg"); + var errCode = + this.request.responseXML.getElementsByTagName("error")[0] + .getAttribute("code"); var err = new Error(errMsg + errAddInfo); err.code = errCode; @@ -834,9 +857,9 @@ pzHttpRequest.prototype = }; /* -********************************************************************************* -** XML HELPER CLASS ************************************************************ -********************************************************************************* +******************************************************************************** +** XML HELPER FUNCTIONS ******************************************************** +******************************************************************************** */ // DOMDocument @@ -956,4 +979,5 @@ Element_getTextContent = function (DOM_Element) } } +/* do not remove trailing bracket */ }