pzHttpRequest: added synchronous load() methd.
[pazpar2-moved-to-github.git] / js / pz2.js
index 4cd86ef..9626b83 100644 (file)
--- a/js/pz2.js
+++ b/js/pz2.js
@@ -1,5 +1,5 @@
 /*
-** $Id: pz2.js,v 1.41 2007-07-02 10:16:46 jakub Exp $
+** $Id: pz2.js,v 1.44 2007-07-04 12:33:51 jakub Exp $
 ** pz2.js - pazpar2's javascript client library.
 */
 
@@ -40,13 +40,7 @@ var pz2 = function(paramArray) {
     //load stylesheet if required in async mode
     if( __myself.stylesheet ) {
         var request = new pzHttpRequest( __myself.stylesheet );
-        request.async = false;
-        request.get(
-                [],
-                function ( doc ) {
-                    __myself.xslDoc = doc;
-                }
-        );
+        request.get( {}, function ( doc ) { __myself.xslDoc = doc; } );
     }
        
     // at least one callback required
@@ -207,10 +201,11 @@ pz2.prototype =
         else
             throw new Error("You need to supply query to the search command");
 
-       var searchParams = [{ "command": "search", "query": __myself.currQuery }];
-       searchParams.push({"session":__myself.sessionID});
-       if (filter !== undefined)
-           searchParams.push({"filter": filter});
+       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,
@@ -351,24 +346,35 @@ pz2.prototype =
             }
         );
     },
-    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;
+               recordParams["syntax"] = "usmarc";
+               recordParams["esn"] = "F";
+       }
+       __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++) {
@@ -546,7 +552,7 @@ pzHttpRequest.prototype =
 {
     get: function ( params, callback ) 
     {
-        this._send( 'GET', params, null, callback );
+        this._send( 'GET', params, '', callback );
     },
 
     post: function ( params, data, callback )
@@ -554,12 +560,21 @@ pzHttpRequest.prototype =
         this._send( 'POST', params, data, callback );
     },
 
+    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.async = true;
         this.request.open( type, this._urlAppendParams(params), this.async );
-        //this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
         this.request.onreadystatechange = function () {
             context._handleResponse();
         }