Added IE/FF compatibility functions.
[pazpar2-moved-to-github.git] / js / pz2.js
index 9626b83..028a555 100644 (file)
--- a/js/pz2.js
+++ b/js/pz2.js
@@ -1,5 +1,5 @@
 /*
-** $Id: pz2.js,v 1.44 2007-07-04 12:33:51 jakub Exp $
+** $Id: pz2.js,v 1.49 2007-07-26 13:47:52 jakub Exp $
 ** pz2.js - pazpar2's javascript client library.
 */
 
@@ -30,7 +30,7 @@ var pz2 = function(paramArray) {
 
     //supported pazpar2's protocol version
     __myself.suppProtoVer = '1';
-    __myself.pz2String = paramArray.pazpar2path || "search.pz2";
+    __myself.pz2String = paramArray.pazpar2path || "/pazpar2/search.pz2";
     __myself.stylesheet = paramArray.detailstylesheet || null;
     __myself.useSessions = true;
     if (paramArray.usesessions != undefined) {
@@ -346,8 +346,22 @@ pz2.prototype =
             }
         );
     },
-    record: function(id,offset)
+    record: function(id,offset, params)
     {
+        if ( params == undefined )
+            params = {};
+
+        if ( params.callback != undefined ) {
+            callback = params.callback;
+        } else {
+            callback = __myself.recordCallback;
+        }
+
+        if ( params['handle'] == undefined )
+            handle = {};
+        else
+            handle = params['handle'];
+
         if( !__myself.searchStatusOK && __myself.useSessions)
             return;
 
@@ -358,9 +372,12 @@ pz2.prototype =
        var recordParams = { "command": "record", "session": __myself.sessionID, "id": __myself.currRecID };
        if (offset !== undefined) {
                recordParams["offset"] = offset;
-               recordParams["syntax"] = "usmarc";
-               recordParams["esn"] = "F";
        }
+
+        if (params.syntax != undefined) {
+            recordParams['syntax'] = params.syntax;
+        }
+
        __myself.currRecOffset = offset;
         request.get(
            recordParams,
@@ -370,7 +387,7 @@ pz2.prototype =
                 record['xmlDoc'] = data;
                if (__myself.currRecOffset !== undefined) {
                     record['offset'] = __myself.currRecOffset;
-                    __myself.recordCallback(record);
+                    callback(record, handle);
                 } else if ( recordNode = data.getElementsByTagName("record")[0] ) {
                     // if stylesheet was fetched do not parse the response
                     if ( __myself.xslDoc ) {
@@ -406,7 +423,7 @@ pz2.prototype =
                         }
                     }
                     
-                    __myself.recordCallback(record);
+                    callback(record, handle);
                 }
                 else
                     // if it gets here the http return code was 200 (pz2 errors are 417)
@@ -636,6 +653,116 @@ pzHttpRequest.prototype =
 
 /*
 *********************************************************************************
+** XML HELPER CLASS ************************************************************
+*********************************************************************************
+*/
+
+// DOMDocument
+
+if ( window.ActiveXObject) {
+    var DOMDoc = document;
+} else {
+    var DOMDoc = Document.prototype;
+}
+
+DOMDoc.newXmlDoc = function ( root )
+{
+    var doc;
+
+    if (document.implementation && document.implementation.createDocument) {
+        doc = document.implementation.createDocument('', root, null);
+    } else if ( window.ActiveXObject ) {
+        doc = new ActiveXObject("MSXML2.DOMDocument");
+        doc.loadXML('<' + root + '/>');
+    } else {
+        throw new Error ('No XML support in this browser');
+    }
+
+    return doc;
+}
+
+   
+DOMDoc.parseXmlFromString = function ( xmlString ) 
+{
+    var doc;
+
+    if ( window.DOMParser ) {
+        var parser = new DOMParser();
+        doc = parser.parseFromString( xmlString, "text/xml");
+    } else if ( window.ActiveXObject ) {
+        doc = new ActiveXObject("MSXML2.DOMDocument");
+        doc.loadXML( xmlString );
+    } else {
+        throw new Error ("No XML parsing support in this browser.");
+    }
+
+    return doc;
+}
+
+// DOMElement
+
+Element_removeFromDoc = function (DOM_Element)
+{
+    DOM_Element.parentNode.removeChild(DOM_Element);
+}
+
+Element_emptyChildren = function (DOM_Element)
+{
+    while( DOM_Element.firstChild ) {
+        DOM_Element.removeChild( DOM_Element.firstChild )
+    }
+}
+
+Element_appendTransformResult = function ( DOM_Element, xmlDoc, xslDoc )
+{
+    if ( window.XSLTProcessor ) {
+        var proc = new XSLTProcessor();
+        proc.importStylesheet( xslDoc );
+        var docFrag = false;
+        docFrag = proc.transformToFragment( xmlDoc, DOM_Element.ownerDocument );
+        DOM_Element.appendChild(docFrag);
+    } else if ( window.ActiveXObject ) {
+        DOM_Element.innerHTML = xmlDoc.transformNode( xslDoc );
+    } else {
+        alert( 'Unable to perform XSLT transformation in this browser' );
+    }
+}
+Element_appendTextNode = function (DOM_Element, tagName, textContent )
+{
+    var node = DOM_Element.ownerDocument.createElement(tagName);
+    var text = DOM_Element.ownerDocument.createTextNode(textContent);
+
+    DOM_Element.appendChild(node);
+    node.appendChild(text);
+
+    return node;
+}
+
+Element_setTextContent = function ( DOM_Element, textContent )
+{
+    if (typeof DOM_Element.textContent !== "undefined") {
+        DOM_Element.textContent = textContent;
+    } else if (typeof DOM_Element.innerText !== "undefined" ) {
+        DOM_Element.innerText = textContent;
+    } else {
+        throw new Error("Cannot set text content of the node, no such method.");
+    }
+}
+
+Element_getTextContent = function (DOM_Element)
+{
+    if (DOM_Element.textContent) {
+        return DOM_Element.textContent;
+    } else if (DOM_Element.text ) {
+        return DOM_Element.text;
+    } else {
+        throw new Error("Cannot get text content of the node, no such method.");
+    }
+}
+
+/*
+*********************************************************************************
 ** QUERY CLASS ******************************************************************
 *********************************************************************************
 */