Minor changes to allow logging out in the client.
[pazpar2-moved-to-github.git] / js / pz2.js
index 5870bae..8d1e944 100644 (file)
--- a/js/pz2.js
+++ b/js/pz2.js
@@ -1,5 +1,5 @@
 /*
-** $Id: pz2.js,v 1.7 2007-04-15 03:24:18 quinn Exp $
+** $Id: pz2.js,v 1.13 2007-05-14 12:57:43 jakub Exp $
 ** pz2.js - pazpar2's javascript client library.
 */
 
@@ -33,7 +33,11 @@ var pz2 = function(paramArray) {
 
     // at least one callback required
     if ( !paramArray )
-        throw new Error("An array with parameters has to be suplied when instantiating a class");   
+        throw new Error("An array with parameters has to be suplied when instantiating a class");
+
+    //supported pazpar2's protocol version
+    __myself.suppProtoVer = '1';
+    __myself.errorHandler = paramArray.errorhandler || null;
     
     // function callbacks
     __myself.statCallback = paramArray.onstat || null;
@@ -41,6 +45,7 @@ var pz2 = function(paramArray) {
     __myself.termlistCallback = paramArray.onterm || null;
     __myself.recordCallback = paramArray.onrecord || null;
     __myself.bytargetCallback = paramArray.onbytarget || null;
+    __myself.resetCallback = paramArray.onreset || null;
 
     // termlist keys
     __myself.termKeys = paramArray.termlist || "subject";
@@ -75,6 +80,7 @@ var pz2 = function(paramArray) {
     __myself.termTimer = null;
     __myself.showTime = paramArray.showtime || 1000;
     __myself.showTimer = null;
+    __myself.showFastCount = 4;
     __myself.bytargetTime = paramArray.bytargettime || 1000;
     __myself.bytargetTimer = null;
 
@@ -89,38 +95,80 @@ var pz2 = function(paramArray) {
 
     // error handling
     $(document).ajaxError( 
-    function (request, settings, exception) {
-        if ( settings.responseXML && settings.responseXML.getElementsByTagName("error") )
-            throw new Error( settings.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue);
+    function (ajaxError, xhr, reqSettings, prevException)
+    {
+        if ( xhr.responseXML && xhr.responseXML.getElementsByTagName("error").length )
+       {
+           var errMsg = xhr.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue;
+            var errCode = xhr.responseXML.getElementsByTagName("error")[0].getAttribute("code");
+            
+            var err = new Error(errMsg);
+            err.code = errCode;
+           
+            if (__myself.errorHandler) {
+                __myself.errorHandler(err);
+            }
+            else {
+                throw err;
+            }
+       }
+        // ensure the errors are propagated
+       else if (prevException != undefined ) {
+           throw prevException;
+        }
+        else {
+                throw new Error("XMLHttpRequest error. STATUS: " + xhr.status + " STATUS TEXT: " + xhr.statusText);
+        }
     });
     
     // auto init session?
     if (paramArray.autoInit !== false)
-        __myself.init(__myself.keepAlive);
+        __myself.init();
 };
 pz2.prototype = {
-    init: function(keepAlive) 
+    reset: function ()
     {
-        if ( keepAlive < __myself.keepAlive )
-            __myself.keepAlive = keepAlive;  
-        
-        $.get( __myself.pz2String,
-            { "command": "init" },
-            function(data) {
-                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);
+        __myself.sessionID = null;
+        __myself.initStatusOK = false;
+        __myself.pingStatusOK = false;
+        __myself.searchStatusOK = false;
+
+        clearTimeout(__myself.statTimer);
+        clearTimeout(__myself.showTimer);
+        clearTimeout(__myself.termTimer);
+        clearTimeout(__myself.bytargetTimer);
+
+        __myself.resetCallback();
+    },
+    init: function ( sessionId ) 
+    {
+        __myself.reset();
+        if ( sessionId != undefined ) {
+            __myself.initStatusOK = true;
+            __myself.sessionID = sessionId;
+            __myself.ping();
+
+        } else {
+            $.get( __myself.pz2String,
+                { "command": "init" },
+                function(data) {
+                    if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
+                        if ( data.getElementsByTagName("protocol")[0].childNodes[0].nodeValue != __myself.suppProtoVer )
+                            throw new Error("Server's protocol not supported by the client");
+                        __myself.initStatusOK = true;
+                        __myself.sessionID = data.getElementsByTagName("session")[0].childNodes[0].nodeValue;
+                        setTimeout("__myself.ping()", __myself.keepAlive);
+                    }
+                    else
+                        // if it gets here the http return code was 200 (pz2 errors are 417)
+                        // but the response was invalid, it should never occur
+                        setTimeout("__myself.init()", 1000);
                 }
-                else
-                    // if it gets here the http return code was 200 (pz2 errors are 417)
-                    // but the response was invalid, it should never occur
-                    setTimeout("__myself.init()", 1000);
-            }
-        );
+            );
+        }
     },
     // no need to ping explicitly
-    ping: function() 
+    ping: function () 
     {
         if( !__myself.initStatusOK )
             return;
@@ -140,7 +188,7 @@ pz2.prototype = {
             }
         );
     },
-    search: function(query, num, sort, filter)
+    search: function (query, num, sort, filter)
     {
         clearTimeout(__myself.statTimer);
         clearTimeout(__myself.showTimer);
@@ -270,7 +318,7 @@ pz2.prototype = {
                                }
                                else {
                                    var nodeName = hits[i].childNodes[j].nodeName;
-                                   var nodeText = hits[i].childNodes[j].firstChild.nodeValue;
+                                    var nodeText = hits[i].childNodes[j].firstChild.nodeValue;
                                    show.hits[i][nodeName] = nodeText;
                                }
                             }
@@ -278,8 +326,11 @@ pz2.prototype = {
                     }
                     __myself.showCallback(show);
                     __myself.showCounter++;
+                   var delay = __myself.showTime;
+                   if (__myself.showCounter > __myself.showFastCount)
+                           delay *= 2;
                     if (activeClients > 0)
-                        __myself.showTimer = setTimeout("__myself.show()", (__myself.showTime + __myself.showCounter*__myself.dumpFactor));
+                        __myself.showTimer = setTimeout("__myself.show()", delay);
                 }
                 else
                     // if it gets here the http return code was 200 (pz2 errors are 417)