pz2.js update
authorJakub Skoczen <jakub@indexdata.dk>
Thu, 8 Mar 2007 15:36:43 +0000 (15:36 +0000)
committerJakub Skoczen <jakub@indexdata.dk>
Thu, 8 Mar 2007 15:36:43 +0000 (15:36 +0000)
www/pz2_js/client_pz2.js
www/pz2_js/index.html
www/pz2_js/pz2.js

index 08cff97..1ae5dcb 100644 (file)
@@ -1,11 +1,22 @@
 function init() {
-    my_paz = new pz2( { "onshow": my_onshow, "onstat": my_onstat, "onterm": my_onterm }, true );
+    my_paz = new pz2( { "onshow": my_onshow,
+                        "onstat": my_onstat,
+                        "onterm": my_onterm,
+                        "termlist": "subject,author",
+                        "onrecord": my_onrecord } );
 }
 
 function my_onshow(data) {
     var body = document.getElementById("body");
-
-    body.innerHTML = '<div>active clients: ' + data.activeclients + '</div>' +
+    body.innerHTML = "";
+    for ( i = 0; i < data.hits.length; i++) {
+        var hit = data.hits[i];
+        body.innerHTML += '<div id="' + hit.recid + '" onclick="my_paz.record(this.id)"><span>' + i + 
+                          '. </span><span><b>' + hit["md-title"] +
+                          ' </b></span> by <span><i>' + hit["md-author"] + '</i></span></div>';
+    }
+    body.innerHTML += "<hr/>";
+    body.innerHTML += '<div>active clients: ' + data.activeclients + '</div>' +
                      '<div>merged: ' + data.merged + '</div>' +
                      '<div>total: ' + data.total + '</div>' +
                      '<div>start: ' + data.start + '</div>' +
@@ -14,7 +25,6 @@ function my_onshow(data) {
 
 function my_onstat(data) {
     var stat = document.getElementById("stat");
-
     stat.innerHTML = '<div>active clients: ' + data.activeclients + '</div>' +
                      '<div>hits: ' + data.hits + '</div>' +
                      '<div>records: ' + data.records + '</div>' +
@@ -24,9 +34,23 @@ function my_onstat(data) {
 
 function my_onterm(data) {
     var termlist = document.getElementById("termlist");
-    
     termlist.innerHTML = "";
+    termlist.innerHTML  += "<div><b> --Author-- </b></div>";
     for ( i = 0; i < data.author.length; i++ ) {
         termlist.innerHTML += '<div><span>' + data.author[i].name + ' </span><span> (' + data.author[i].freq + ')</span></div>';
     }
+    termlist.innerHTML += "<hr/>";
+    termlist.innerHTML += "<div><b> --Subject-- </b></div>";
+    for ( i = 0; i < data.subject.length; i++ ) {
+        termlist.innerHTML += '<div><span>' + data.subject[i].name + ' </span><span> (' + data.subject[i].freq + ')</span></div>';
+    }
+}
+
+function my_onrecord(data) {
+    recordDiv = document.getElementById(data.recid);
+    recordDiv.innerHTML = "<table><tr><td><b>Ttle</b> : </td><td>" + data["md-title"] +
+                            "</td></tr><tr><td><b>Date</b> : </td><td>" + data["md-date"] +
+                            "</td></tr><tr><td><b>Author</b> : </td><td>" + data["md-author"] +
+                            "</td></tr><tr><td><b>Subject</b> : </td><td>" + data["md-subject"] + "</td></tr>";
+
 }
index 4fb3225..38db988 100644 (file)
@@ -6,11 +6,11 @@
   <script type="text/javascript" src="jquery.js"></script>
  </head>
 <body onload="init();">
- <table width="100%" border="1" cellpadding="5">
+ <table width="100%" border="0" cellpadding="5" cellspacing="3">
   <tr>
    <td width="250" height="100" align="center"><b>pz2.js test</b></td>
    <td>
-    <form name="search" onsubmit="my_paz.search(this.query.value, 10); return false;">
+    <form name="search" onsubmit="my_paz.search(this.query.value, 15, 'title'); return false;">
      <b>Search:</b>
      <input id="query" type="text" size="50"/>
      <input type="submit" value="Go"/>
    <td>
   </tr>
   <tr>
-   <td valign="top" id="termlist">&nbsp;</td>
-   <td valign="top" id="body">Show results:<td>
+   <td valign="top"><b>TERMLISTS:</b><div id="termlist"></div></td>
+   <td valign="top"><b>RESULS:</b><hr/><div id="body"><div><td>
   </tr>
   <tr>
-   <td>
-   &nbsp;
-   </td>
-   <td id="stat">
-    Status:
-   </td>
+   <td>&nbsp;</td>
+   <td valign="top"><b>STATUS:<b><div id="stat"></div></td>
   </tr>
  </table>
 </body>
index 95dbf64..84420e5 100644 (file)
@@ -3,17 +3,27 @@ if(typeof window.pz2 == "undefined") {
 window.undefined = window.undefined;
 
 var pz2 = function(callbackArr, autoInit, keepAlive) {
-    // 
-    if ( callbackArr == null )
-        return null;
-
     //for convenience
     myself = this;
+
+    // at least one callback required
+    if ( !callbackArr )
+        throw new Error("Callback parameters array has to be suplied when instantiating a class");   
+    
+    // function callbacks
+    myself.statCallback = callbackArr.onstat;
+    myself.showCallback = callbackArr.onshow;
+    myself.termlistCallback = callbackArr.onterm;
+    myself.recordCallback = callbackArr.onrecord || null;
+
+    // termlist keys
+    myself.termKeys = callbackArr.termlist || "subject";
     
     myself.pz2String = "search.pz2";
-    myself.sessionID;
-    myself.initStatus = 0;
-    myself.pingStatus = 0;
+    myself.sessionID = null;
+    myself.initStatusOK = false;
+    myself.pingStatusOK = false;
+    myself.searchStatusOK = false;
     myself.keepAlive = 50000;
 
     if ( keepAlive < myself.keepAlive )
@@ -21,22 +31,26 @@ var pz2 = function(callbackArr, autoInit, keepAlive) {
 
     // for sorting
     myself.currentSort = "relevance";
-    myself.sortKeywords = [ "relevance", "title", "author" ];
-    
-    // function callbacks
-    myself.statCallback = callbackArr.onstat;
-    myself.showCallback = callbackArr.onshow;
-    myself.termlistCallback = callbackArr.onterm;
+    // where are we?
+    myself.currentStart = 0;
+    myself.currentNum = 20;
 
     //timers
     myself.statTime = 2000;
-    myself.statTimer;
+    myself.statTimer = null;
     myself.termTime = 1000;
-    myself.termTimer;
+    myself.termTimer = null;
     myself.showTime = 1000;
-    myself.showTimer;
+    myself.showTimer = null;
 
-    if (autoInit == true)
+    // error handling
+    $(document).ajaxError( 
+    function (request, settings, exception) {
+        if ( settings.responseXML && settings.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue)
+            throw new Error( settings.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue);
+    });
+
+    if (autoInit !== false)
         myself.init(myself.keepAlive);
 }
 pz2.prototype = {
@@ -45,24 +59,25 @@ pz2.prototype = {
         $.get( myself.pz2String,
             { command: "init" },
             function(data) {
-                if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" )
-                    myself.initStatus = 1;
-                myself.sessionID = data.getElementsByTagName("session")[0].childNodes[0].nodeValue;
-                setTimeout(myself.ping, myself.keepAlive);
+                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);
+                }
             }
         );
     },
     // no need to ping explicitly
     ping: function() {
-        if( !myself.initStatus )
+        if( !myself.initStatusOK )
             return;
             // session is not initialized code here
         $.get( myself.pz2String,
             { command: "ping", session: myself.sessionID },
             function(data) {
                 if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
-                    myself.pingStatus = 1;
-                    setTimeout(myself.ping, myself.keepAlive);
+                    myself.pingStatusOK = true;
+                    setTimeout("myself.ping()", myself.keepAlive);
                 }
                 else
                     location = "?";
@@ -73,19 +88,19 @@ pz2.prototype = {
         clearTimeout(myself.statTimer);
         clearTimeout(myself.showTimer);
         clearTimeout(myself.termTimer);
-        if( !myself.initStatus )
+        if( !myself.initStatusOK )
             return;
         $.get( myself.pz2String,
             { command: "search", session: myself.sessionID, query: query },
             function(data) {
                 if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
-                    myself.searchStatus = 1;
+                    myself.searchStatusOK = true;
                     //piggyback search
                     myself.show(0, num, sort)
                     if ( myself.statCallback )
-                        myself.statTimer = setTimeout(myself.stat, myself.statTime / 2);
+                        myself.statTimer = setTimeout("myself.stat()", myself.statTime / 2);
                     if ( myself.termlistCallback )
-                        myself.termTimer = setTimeout(myself.termlist, myself.termTime / 2);
+                        myself.termTimer = setTimeout("myself.termlist()", myself.termTime / 2);
                 }
                 else
                     location = "?";
@@ -93,8 +108,8 @@ pz2.prototype = {
         );
     },
     // callback, not to be called explicitly
-    stat: function() {
-        if( !myself.searchStatus )
+    stat: function(test) {
+        if( !myself.searchStatusOK )
             return;
         $.get( myself.pz2String,
             { command: "stat", session: myself.sessionID },
@@ -115,10 +130,10 @@ pz2.prototype = {
                     }
                     myself.statCallback(stat);
                     if (activeClients > 0)
-                        myself.statTimer = setTimeout(myself.stat, myself.statTime); 
+                        myself.statTimer = setTimeout("myself.stat()", myself.statTime); 
                 }
                 else
-                    myself.statTimer = setTimeout(myself.stat, myself.statTime / 4)
+                    myself.statTimer = setTimeout("myself.stat()", myself.statTime / 4)
                     //location = "?";
                     // some error handling
             }
@@ -127,12 +142,18 @@ pz2.prototype = {
     //callback not to be called explicitly
     show: function(start, num, sort) {
         clearTimeout(myself.showTimer);
-        if( !myself.searchStatus )
+        if( !myself.searchStatusOK )
             return;
-        if ( myself.sortKeywords.some( function(element, index, arr) { if (element == sort) return true; else return false; } ) )
+        
+        if( sort !== undefined )
             myself.currentSort = sort;
+        if( start !== undefined )
+            myself.currentStart = Number( start );
+        if( num !== undefined )
+            myself.currentNum = Number( num );
+        
         $.get( myself.pz2String,
-            { "command": "show", "session": myself.sessionID, "start": start, "num": num, "sort": myself.currentSort, "block": 1 },
+            { "command": "show", "session": myself.sessionID, "start": myself.currentStart, "num": myself.currentNum, "sort": myself.currentSort, "block": 1 },
             function(data) {
                 if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) {
                     var activeClients = Number( data.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue );
@@ -141,27 +162,66 @@ pz2.prototype = {
                     "merged": Number( data.getElementsByTagName("merged")[0].childNodes[0].nodeValue ),
                     "total": Number( data.getElementsByTagName("total")[0].childNodes[0].nodeValue ),
                     "start": Number( data.getElementsByTagName("start")[0].childNodes[0].nodeValue ),
-                    "num": Number( data.getElementsByTagName("num")[0].childNodes[0].nodeValue )
+                    "num": Number( data.getElementsByTagName("num")[0].childNodes[0].nodeValue ),
+                    "hits": []
+                    }
+
+                    var hits = data.getElementsByTagName("hit");
+                    var hit = new Array();
+                    for (i = 0; i < hits.length; i++) {
+                        show.hits[i] = new Array();
+                        for ( j = 0; j < hits[i].childNodes.length; j++) {
+                            if ( hits[i].childNodes[j].nodeType == Node.ELEMENT_NODE ) {
+                                var nodeName = hits[i].childNodes[j].nodeName;
+                                var nodeText = hits[i].childNodes[j].firstChild.nodeValue;
+                                show.hits[i][nodeName] = nodeText;
+                            }
+                        }
                     }
+
                     //TODO include records
                     myself.showCallback(show);
                     if (activeClients > 0)
-                        myself.showTimer = setTimeout("myself.show(" + start + "," + num + "," + sort + ")", myself.showTime);
+                        myself.showTimer = setTimeout("myself.show()", myself.showTime);
                 }
                 else
-                    myself.showTimer = setTimeout("myself.show(" + start + "," + num + "," + sort + ")", myself.showTime / 4);
+                    myself.showTimer = setTimeout("myself.show()", myself.showTime / 4);
                     // location = "?";
                     // some error handling
             }
         );
     },
     record: function(id) {
+        if( !myself.searchStatusOK )
+            return;
+        $.get( myself.pz2String,
+            { "command": "record", "session": myself.sessionID, "id": id },
+            function(data) {
+                var recordNode;
+                var record = new Array();
+                if ( recordNode = data.getElementsByTagName("record")[0] ) {
+                    for ( i = 0; i < recordNode.childNodes.length; i++) {
+                        if ( recordNode.childNodes[i].nodeType == Node.ELEMENT_NODE ) {
+                            var nodeName = recordNode.childNodes[i].nodeName;
+                            var nodeText = recordNode.childNodes[i].firstChild.nodeValue;
+                            record[nodeName] = nodeText;
+                        }
+                    }
+                    myself.recordCallback(record);
+                }
+                else
+                    alert("");
+                    //location = "?";
+                    // some error handling
+            }
+        );
+        
     },
-    termlist: function(name) {
-        if( !myself.searchStatus )
+    termlist: function() {
+        if( !myself.searchStatusOK )
             return;
         $.get( myself.pz2String,
-            { "command": "termlist", "session": myself.sessionID, "name": "author" },
+            { "command": "termlist", "session": myself.sessionID, "name": myself.termKeys },
             function(data) {
                 if ( data.getElementsByTagName("termlist") ) {
                     var termList = { "activeclients": Number( data.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue ) }
@@ -182,10 +242,10 @@ pz2.prototype = {
                     }
                     myself.termlistCallback(termList);
                     if (termList["activeclients"] > 0)
-                        myself.termTimer = setTimeout("myself.termlist(" + name + ")" , myself.termTime); 
+                        myself.termTimer = setTimeout("myself.termlist()", myself.termTime); 
                 }
                 else
-                    myself.termTimer = setTimeout("myself.termlist(" + name + ")" , myself.termTime / 4); 
+                    myself.termTimer = setTimeout("myself.termlist()", myself.termTime / 4); 
                     //location = "?";
                     // some error handling
             }