Added simple example of the pz2.js usage.
authorJakub Skoczen <jakub@indexdata.dk>
Fri, 18 May 2007 11:36:39 +0000 (11:36 +0000)
committerJakub Skoczen <jakub@indexdata.dk>
Fri, 18 May 2007 11:36:39 +0000 (11:36 +0000)
www/README [new file with mode: 0644]
www/example/example_client.js [new file with mode: 0644]
www/example/index.html [new file with mode: 0644]

diff --git a/www/README b/www/README
new file mode 100644 (file)
index 0000000..90baef1
--- /dev/null
@@ -0,0 +1,4 @@
+For the time being example/ (and masterkey/js/ as well, but this will be removed from the repo anyway) requires the pz2.js
+to be symlinked to work. Solutions on its way, (need to talk to Adam).
+
+www$ ln -s ../js/pz2.js example/pz2.js
diff --git a/www/example/example_client.js b/www/example/example_client.js
new file mode 100644 (file)
index 0000000..5629656
--- /dev/null
@@ -0,0 +1,157 @@
+/* A very simple client that shows a basic usage of the pz2.js
+** $Id: example_client.js,v 1.1 2007-05-18 11:36:39 jakub Exp $
+*/
+
+// create a parameters array and pass it to the pz2's constructor
+// then register the form submit event with the pz2.search function
+// autoInit is set to true on default
+
+my_paz = new pz2( { "onshow": my_onshow,
+                    "showtime": 500,            //each timer (show, stat, term, bytarget) can be specified this way
+                    "onstat": my_onstat,
+                    "onterm": my_onterm,
+                    "termlist": "subject,author",
+                    "onbytarget": my_onbytarget,
+                    "onrecord": my_onrecord } );
+// some state vars
+var curPage = 1;
+var recPerPage = 15;
+var totalRec = 0;
+var curDetRecId = -1;
+var curDetRecData = null;
+
+// wait until the DOM is ready
+function domReady () 
+{ 
+    document.search.onsubmit = onFormSubmitEventHandler;
+}
+
+// when search button pressed
+function onFormSubmitEventHandler() 
+{
+    my_paz.search(document.search.query.value, recPerPage, 'relevance');
+    return false;
+}
+
+//
+// pz2.js event handlers:
+//
+
+function my_onshow(data) {
+    totalRec = data.merged;
+    
+    var body = document.getElementById("body");
+    body.innerHTML = "";
+
+    body.innerHTML +='<div>Displaying: ' + data.start + ' to ' + (data.start + data.num) +
+                     ' of ' + data.merged + ' (total not merged hits: ' + data.total + ')</div><hr/>';
+
+    body.innerHTML += '<span id="prev" onclick="pagerPrev();" style="cursor: pointer;">Prev</span> <b>|</b> ' 
+                + '<span id="next" onclick="pagerNext()" style="cursor: pointer;">Next</span><hr/>';
+    
+    for (var i = 0; i < data.hits.length; i++) {
+        var hit = data.hits[i];
+        body.innerHTML += '<div id="rec_' + hit.recid + '" onclick="showDetails(this.id)">'
+                        +'<span>' + (i + 1 + recPerPage * ( curPage - 1)) + '. </span>'
+                        +'<span style="cursor: pointer;"><b>' + hit["md-title"] +
+                        ' </b></span> by <span><i>' + hit["md-author"] + '</i></span></div>';
+
+        if ( hit.recid == curDetRecId ) {
+            drawCurDetails();
+        }
+    }
+    
+}
+
+function my_onstat(data) {
+    var stat = document.getElementById("stat");
+    stat.innerHTML = '<span>active clients: ' + data.activeclients + ' </span>' +
+                     '<span>hits: ' + data.hits + ' </span>' +
+                     '<span>records: ' + data.records + ' </span>' +
+                     '<span>clients: ' + data.clients + ' </span>' +
+                     '<span>searching: ' + data.searching + '</span>';
+}
+
+function my_onterm(data) {
+    var termlist = document.getElementById("termlist");
+    termlist.innerHTML = "";
+    termlist.innerHTML  += "<div><b> --Author </b></div>";
+    for (var 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 (var 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) {
+    // in case on_show was faster to redraw element
+    var detRecordDiv = document.getElementById('det_'+data.recid);
+    if ( detRecordDiv )
+        return;
+
+    curDetRecData = data;
+    drawCurDetails();
+}
+
+function my_onbytarget(data) {
+    var targetDiv = document.getElementById("bytarget");
+    targetDiv.innerHTML = '<thead><tr><td>ID</td><td>Hits</td><td>Diag</td><td>Rec</td><td>State</td></tr></thead>';
+    
+    for (var i = 0; i < data.length; i++ ) {
+        targetDiv.innerHTML += "<tr><td><b>" + data[i].id +
+                               "</b></td><td>" + data[i].hits +
+                               "</td><td>" + data[i].diagnostic +
+                               "</td><td>" + data[i].records +
+                               "</td><td>" + data[i].state + "</td></tr>";
+    }
+}
+
+// detailed record drawing
+function showDetails ( prefixRecId ) {
+    var recId = Number(prefixRecId.replace('rec_', ''));
+    
+    // if the same clicked ignore
+    if ( recId == curDetRecId )
+        return;
+    
+    // if different remove the old one 
+    var detRecordDiv = document.getElementById('det_'+curDetRecId);
+    // lovin DOM!
+    if ( detRecordDiv )
+            detRecordDiv.parentNode.removeChild(detRecordDiv);
+
+    curDetRecId = recId;
+
+    // request the record
+    my_paz.record(recId);
+}
+
+function drawCurDetails ()
+{
+    var data = curDetRecData;
+    var recordDiv = document.getElementById('rec_'+data.recid);
+    recordDiv.innerHTML += '<div id="det_'+data.recid+'"><table><tr><td><b>Ttle</b></td><td><b>:</b> ' + data["md-title"] +
+                            "</td></tr><tr><td><b>Date</b></td><td><b>:</b> " + data["md-date"] +
+                            "</td></tr><tr><td><b>Author</b></td><td><b>:</b> " + data["md-author"] +
+                            "</td></tr><tr><td><b>Subject</b></td><td><b>:</b> " + data["md-subject"] + 
+                            "</td></tr><tr><td><b>Location</b></td><td><b>:</b> " + data["location"][0].name + 
+                            "</td></tr></table></div>";
+}
+
+
+// simple paging functions
+
+function pagerNext() {
+    if ( totalRec - recPerPage*curPage > 0) {
+        my_paz.showNext();
+        curPage++;
+    }
+}
+
+function pagerPrev() {
+    if ( my_paz.showPrev() != false )
+        curPage--;
+}
diff --git a/www/example/index.html b/www/example/index.html
new file mode 100644 (file)
index 0000000..58eb769
--- /dev/null
@@ -0,0 +1,57 @@
+<!-- $Id: index.html,v 1.1 2007-05-18 11:36:39 jakub Exp $ -->
+<html>
+ <head>
+  <title>Exemplary pazpar2 client [using pz2.js]</title>
+  <style type="text/css"></style>
+  <script type="text/javascript" src="pz2.js"></script>
+  <script type="text/javascript" src="example_client.js"></script>
+ </head>
+ <body onload="domReady();">
+  
+  <table width="100%" border="0" cellpadding="6" cellspacing="6">
+  
+  <tr>
+   <td width="250" height="100" align="center"><b>Exemplary pazpar2 client [using pz2.js]</b></td>
+   <td>
+    <form id="searchForm" name="search">
+     <b>Search:</b>
+     <input id="query" type="text" size="50"/>
+     <input type="submit" value="Go"/>
+    </form>
+   <td>
+  </tr>
+  
+  <tr>
+   <td width="250" valign="top">
+    <b>TERMLISTS:</b>
+    <br/><br/>
+     <div id="termlist"></div>
+   </td>
+   <td valign="top">
+    <b>RESULTS:</b>
+    <br/><br/>
+    <div id="body"><div>
+   <td>
+  </tr>
+  
+  </table>
+
+  <hr/>
+  
+  <b>STATUS INFO:<b>
+  <br/>
+  <br/>
+  <div id="stat"></div>
+  <br/>
+  <hr/>
+  <b>TARGET IFO:</b>
+  <br/>
+  <br/>
+  <table id="bytarget"></table>
+
+ </body>
+</html>