From 006a11e71a041bffbc356f63de3292348f350190 Mon Sep 17 00:00:00 2001 From: Jakub Skoczen Date: Fri, 9 Mar 2007 11:08:03 +0000 Subject: [PATCH] pz2.js lib update --- www/pz2_js/client_pz2.js | 16 ++- www/pz2_js/index.html | 9 +- www/pz2_js/pz2.js | 285 ++++++++++++++++++++++++++++++---------------- 3 files changed, 206 insertions(+), 104 deletions(-) diff --git a/www/pz2_js/client_pz2.js b/www/pz2_js/client_pz2.js index 1ae5dcb..1ba9389 100644 --- a/www/pz2_js/client_pz2.js +++ b/www/pz2_js/client_pz2.js @@ -3,6 +3,7 @@ function init() { "onstat": my_onstat, "onterm": my_onterm, "termlist": "subject,author", + "onbytarget": my_onbytarget, "onrecord": my_onrecord } ); } @@ -51,6 +52,19 @@ function my_onrecord(data) { recordDiv.innerHTML = ""; + "
Ttle : " + data["md-title"] + "
Date : " + data["md-date"] + "
Author : " + data["md-author"] + - "
Subject : " + data["md-subject"] + "
Subject : " + data["md-subject"] + "
"; } + +function my_onbytarget(data) { + targetDiv = document.getElementById("bytarget"); + targetDiv.innerHTML = "IDHitsDiagRecState"; + + for ( i = 0; i < data.length; i++ ) { + targetDiv.innerHTML += "" + data[i].id + + "" + data[i].hits + + "" + data[i].diagnostic + + "" + data[i].records + + "" + data[i].state + ""; + } +} diff --git a/www/pz2_js/index.html b/www/pz2_js/index.html index 38db988..46108c8 100644 --- a/www/pz2_js/index.html +++ b/www/pz2_js/index.html @@ -6,7 +6,7 @@ - +
- - + + + +
pz2.js test @@ -19,12 +19,15 @@
TERMLISTS:
RESULS:
+ RESULTS:
   STATUS:
TARGETS:
diff --git a/www/pz2_js/pz2.js b/www/pz2_js/pz2.js index 84420e5..e8924b0 100644 --- a/www/pz2_js/pz2.js +++ b/www/pz2_js/pz2.js @@ -4,118 +4,161 @@ window.undefined = window.undefined; var pz2 = function(callbackArr, autoInit, keepAlive) { //for convenience - myself = this; + __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; + __myself.statCallback = callbackArr.onstat || null; + __myself.showCallback = callbackArr.onshow || null; + __myself.termlistCallback = callbackArr.onterm || null; + __myself.recordCallback = callbackArr.onrecord || null; + __myself.bytargetCallback = callbackArr.onbytarget || null; // termlist keys - myself.termKeys = callbackArr.termlist || "subject"; + __myself.termKeys = callbackArr.termlist || "subject"; - myself.pz2String = "search.pz2"; - myself.sessionID = null; - myself.initStatusOK = false; - myself.pingStatusOK = false; - myself.searchStatusOK = false; - myself.keepAlive = 50000; + // some configurational stuff + __myself.pz2String = "search.pz2"; + __myself.keepAlive = 50000; - if ( keepAlive < myself.keepAlive ) - myself.keepAlive = keepAlive; + __myself.sessionID = null; + __myself.initStatusOK = false; + __myself.pingStatusOK = false; + __myself.searchStatusOK = false; + + if ( keepAlive < __myself.keepAlive ) + __myself.keepAlive = keepAlive; // for sorting - myself.currentSort = "relevance"; + __myself.currentSort = "relevance"; // where are we? - myself.currentStart = 0; - myself.currentNum = 20; + __myself.currentStart = 0; + __myself.currentNum = 20; + + // last full record retrieved + __myself.currRecID = null; + // current query + __myself.currQuery = null; //timers - myself.statTime = 2000; - myself.statTimer = null; - myself.termTime = 1000; - myself.termTimer = null; - myself.showTime = 1000; - myself.showTimer = null; + __myself.statTime = 2000; + __myself.statTimer = null; + __myself.termTime = 1000; + __myself.termTimer = null; + __myself.showTime = 1000; + __myself.showTimer = null; + __myself.bytargetTime = 1000; + __myself.bytargetTimer = null; + + // active clients, updated by stat and show + // might be an issue since bytarget will poll accordingly + __myself.activeClients = 1; // error handling $(document).ajaxError( function (request, settings, exception) { - if ( settings.responseXML && settings.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue) + if ( settings.responseXML && settings.responseXML.getElementsByTagName("error") ) throw new Error( settings.responseXML.getElementsByTagName("error")[0].childNodes[0].nodeValue); }); - + + // auto init session? if (autoInit !== false) - myself.init(myself.keepAlive); + __myself.init(__myself.keepAlive); } pz2.prototype = { - init: function(keepAlive) { - if ( keepAlive < myself.keepAlive ) myself.keepAlive = keepAlive; - $.get( myself.pz2String, - { command: "init" }, + init: function(keepAlive) + { + 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.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) } ); }, // no need to ping explicitly - ping: function() { - if( !myself.initStatusOK ) + ping: function() + { + if( !__myself.initStatusOK ) return; // session is not initialized code here - $.get( myself.pz2String, - { command: "ping", session: myself.sessionID }, + + $.get( __myself.pz2String, + { "command": "ping", "session": __myself.sessionID }, function(data) { if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) { - myself.pingStatusOK = true; - setTimeout("myself.ping()", myself.keepAlive); + __myself.pingStatusOK = true; + setTimeout("__myself.ping()", __myself.keepAlive); } else - location = "?"; + // 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.ping()", 1000) } ); }, - search: function(query, num, sort) { - clearTimeout(myself.statTimer); - clearTimeout(myself.showTimer); - clearTimeout(myself.termTimer); - if( !myself.initStatusOK ) + search: function(query, num, sort) + { + clearTimeout(__myself.statTimer); + clearTimeout(__myself.showTimer); + clearTimeout(__myself.termTimer); + clearTimeout(__myself.bytargetTimer); + + if( !__myself.initStatusOK ) return; - $.get( myself.pz2String, - { command: "search", session: myself.sessionID, query: query }, + + if( query !== undefined ) + __myself.currQuery = query; + else + throw new Error("You need to supply query to the search command"); + + $.get( __myself.pz2String, + { "command": "search", "session": __myself.sessionID, "query": __myself.currQuery }, function(data) { if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) { - myself.searchStatusOK = true; + __myself.searchStatusOK = true; //piggyback search - myself.show(0, num, sort) - if ( myself.statCallback ) - myself.statTimer = setTimeout("myself.stat()", myself.statTime / 2); - if ( myself.termlistCallback ) - myself.termTimer = setTimeout("myself.termlist()", myself.termTime / 2); + __myself.show(0, num, sort) + if ( __myself.statCallback ) + __myself.statTimer = setTimeout("__myself.stat()", __myself.statTime / 2); + if ( __myself.termlistCallback ) + __myself.termTimer = setTimeout("__myself.termlist()", __myself.termTime / 2); + if ( __myself.bytargetCallback ) + __myself.bytargetTimer = setTimeout("__myself.bytarget()", __myself.bytargetTime / 2); } else - location = "?"; + // 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.search(__myself.currQuery)", 1000); } ); }, - // callback, not to be called explicitly - stat: function(test) { - if( !myself.searchStatusOK ) + stat: function() + { + if( !__myself.searchStatusOK ) return; - $.get( myself.pz2String, - { command: "stat", session: myself.sessionID }, + // if called explicitly takes precedence + clearTimeout(__myself.statTimer); + + $.get( __myself.pz2String, + { "command": "stat", "session": __myself.sessionID }, function(data) { if ( data.getElementsByTagName("stat") ) { var activeClients = Number( data.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue ); + __myself.activeClients = activeClients; var stat = { "activeclients": activeClients, "hits": Number( data.getElementsByTagName("hits")[0].childNodes[0].nodeValue ), @@ -128,34 +171,38 @@ pz2.prototype = { "failed": Number( data.getElementsByTagName("failed")[0].childNodes[0].nodeValue ), "error": Number( data.getElementsByTagName("error")[0].childNodes[0].nodeValue ) } - myself.statCallback(stat); + __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) - //location = "?"; - // some error handling + // if it gets here the http return code was 200 (pz2 errors are 417) + // but the response was invalid, it should never occur + __myself.statTimer = setTimeout("__myself.stat()", __myself.statTime / 4) } ); }, - //callback not to be called explicitly - show: function(start, num, sort) { - clearTimeout(myself.showTimer); - if( !myself.searchStatusOK ) + show: function(start, num, sort) + { + if( !__myself.searchStatusOK ) return; + // if called explicitly takes precedence + clearTimeout(__myself.showTimer); if( sort !== undefined ) - myself.currentSort = sort; + __myself.currentSort = sort; if( start !== undefined ) - myself.currentStart = Number( start ); + __myself.currentStart = Number( start ); if( num !== undefined ) - myself.currentNum = Number( num ); + __myself.currentNum = Number( num ); - $.get( myself.pz2String, - { "command": "show", "session": myself.sessionID, "start": myself.currentStart, "num": myself.currentNum, "sort": myself.currentSort, "block": 1 }, + $.get( __myself.pz2String, + { "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" ) { + // first parse the status data send along with records + // this is strictly bound to the format var activeClients = Number( data.getElementsByTagName("activeclients")[0].childNodes[0].nodeValue ); var show = { "activeclients": activeClients, @@ -165,7 +212,7 @@ pz2.prototype = { "num": Number( data.getElementsByTagName("num")[0].childNodes[0].nodeValue ), "hits": [] } - + // parse all the first-level nodes for all tags var hits = data.getElementsByTagName("hit"); var hit = new Array(); for (i = 0; i < hits.length; i++) { @@ -178,24 +225,27 @@ pz2.prototype = { } } } - - //TODO include records - myself.showCallback(show); + __myself.showCallback(show); if (activeClients > 0) - myself.showTimer = setTimeout("myself.show()", myself.showTime); + __myself.showTimer = setTimeout("__myself.show()", __myself.showTime); } else - myself.showTimer = setTimeout("myself.show()", myself.showTime / 4); - // location = "?"; - // some error handling + // if it gets here the http return code was 200 (pz2 errors are 417) + // but the response was invalid, it should never occur + __myself.showTimer = setTimeout("__myself.show()", __myself.showTime / 4); } ); }, - record: function(id) { - if( !myself.searchStatusOK ) + record: function(id) + { + if( !__myself.searchStatusOK ) return; - $.get( myself.pz2String, - { "command": "record", "session": myself.sessionID, "id": id }, + + if( id !== undefined ) + __myself.currRecID = id; + + $.get( __myself.pz2String, + { "command": "record", "session": __myself.sessionID, "id": __myself.currRecID }, function(data) { var recordNode; var record = new Array(); @@ -207,21 +257,24 @@ pz2.prototype = { record[nodeName] = nodeText; } } - myself.recordCallback(record); + __myself.recordCallback(record); } else - alert(""); - //location = "?"; - // some error handling + // 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.record(__myself.currRecID)", 1000); } ); - }, - termlist: function() { - if( !myself.searchStatusOK ) + termlist: function() + { + if( !__myself.searchStatusOK ) return; - $.get( myself.pz2String, - { "command": "termlist", "session": myself.sessionID, "name": myself.termKeys }, + // if called explicitly takes precedence + clearTimeout(__myself.termTimer); + + $.get( __myself.pz2String, + { "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 ) } @@ -240,19 +293,51 @@ pz2.prototype = { termList[listName][j] = term; } } - myself.termlistCallback(termList); + __myself.termlistCallback(termList); if (termList["activeclients"] > 0) - myself.termTimer = setTimeout("myself.termlist()", myself.termTime); + __myself.termTimer = setTimeout("__myself.termlist()", __myself.termTime); } else - myself.termTimer = setTimeout("myself.termlist()", myself.termTime / 4); - //location = "?"; - // some error handling + // if it gets here the http return code was 200 (pz2 errors are 417) + // but the response was invalid, it should never occur + __myself.termTimer = setTimeout("__myself.termlist()", __myself.termTime / 4); } ); }, - bytarget: function() { + bytarget: function() + { + if( !__myself.searchStatusOK ) + return; + // if called explicitly takes precedence + clearTimeout(__myself.bytargetTimer); + + $.get( __myself.pz2String, + { "command": "bytarget", "session": __myself.sessionID }, + function(data) { + if ( data.getElementsByTagName("status")[0].childNodes[0].nodeValue == "OK" ) { + var targetNodes = data.getElementsByTagName("target"); + var bytarget = new Array(); + for ( i = 0; i < targetNodes.length; i++) { + bytarget[i] = new Array(); + for( j = 0; j < targetNodes[i].childNodes.length; j++ ) { + if ( targetNodes[i].childNodes[j].nodeType == Node.ELEMENT_NODE ) { + var nodeName = targetNodes[i].childNodes[j].nodeName; + var nodeText = targetNodes[i].childNodes[j].firstChild.nodeValue; + bytarget[i][nodeName] = nodeText; + } + } + } + __myself.bytargetCallback(bytarget); + if ( __myself.activeClients > 0 ) + __myself.bytargetTimer = setTimeout("__myself.bytarget()", __myself.bytargetTime); + } + else + // if it gets here the http return code was 200 (pz2 errors are 417) + // but the response was invalid, it should never occur + __myself.bytargetTimer = setTimeout("__myself.bytarget()", __myself.bytargetTime / 4); + } + ); } }; } -- 1.7.10.4