Added these.
[pazpar2-moved-to-github.git] / js / pz2.js
index 84fd696..a925994 100644 (file)
--- a/js/pz2.js
+++ b/js/pz2.js
@@ -1,5 +1,5 @@
 /*
-** $Id: pz2.js,v 1.31 2007-06-04 12:31:07 jakub Exp $
+** $Id: pz2.js,v 1.36 2007-06-13 17:25:02 jakub Exp $
 ** pz2.js - pazpar2's javascript client library.
 */
 
@@ -109,7 +109,16 @@ var pz2 = function(paramArray) {
     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;
@@ -117,10 +126,7 @@ pz2.prototype = {
         __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();
@@ -314,7 +320,9 @@ pz2.prototype = {
                                }
                                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;
                                }
                             }
@@ -514,6 +522,7 @@ var pzHttpRequest = function ( url, errorHandler ) {
         this.request = null;
         this.url = url;
         this.errorHandler = errorHandler || null;
+        this.async = true;
         
         if ( window.XMLHttpRequest ) {
             this.request = new XMLHttpRequest();
@@ -530,8 +539,28 @@ pzHttpRequest.prototype =
 {
     get: function ( params, callback ) 
     {
+        this._send( 'GET', params, null, callback );
+    },
+
+    post: function ( params, data, callback )
+    {
+        this._send( 'POST', params, data, callback );
+    },
+
+    _send: function ( type, params, data, callback )
+    {
         this.callback = callback;
-        
+        var context = this;
+        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();
+        }
+        this.request.send(data);
+    },
+
+    _urlAppendParams: function (params)
+    {
         var getUrl = this.url;
         var paramArr = new Array();
 
@@ -542,14 +571,7 @@ pzHttpRequest.prototype =
         if ( paramArr.length )
             getUrl += '?' + paramArr.join('&');
 
-        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.request.onreadystatechange = function () {
-            context._handleResponse();
-        }
-        this.request.send(null);
+        return getUrl;
     },
 
     _handleResponse: function ()
@@ -612,6 +634,10 @@ pzQuery.prototype = {
         this.simpleFilter = null;
         this.numTerms = 0;
     },
+    clearSimpleQuery: function()
+    {
+        this.simpleQuery = '';
+    },
     addTerm: function(field, value)
     {
         var term = {"field": field, "value": value};