Experimental version.. Trying to move state information into URL parameters
authorSebastian Hammer <quinn@indexdata.com>
Wed, 20 Dec 2006 20:36:58 +0000 (20:36 +0000)
committerSebastian Hammer <quinn@indexdata.com>
Wed, 20 Dec 2006 20:36:58 +0000 (20:36 +0000)
to facilitate browser navigation and bookmarking. Not really functional yet, but
Can start a session and a search.. issues remain, however.

www/test2/index.html [new file with mode: 0644]

diff --git a/www/test2/index.html b/www/test2/index.html
new file mode 100644 (file)
index 0000000..711cbcb
--- /dev/null
@@ -0,0 +1,222 @@
+<html>
+
+<head>
+<script>
+
+var xinitSession
+var args = Array();
+var searchstring;
+var session;
+var asearch;
+var shown = 0;
+
+// Query and result management
+// ======================================================
+
+function check_query_start()
+{
+    if (!args['query'])
+       return;
+    if (!args['status'] || args['status'] != 'searching')
+       start_search();
+    else
+       check_search();
+}
+
+function start_search()
+{
+    var url = "search.pz2?session=" + session +
+       "&command=search" +
+       "&query=" + args['query'];
+    SendXmlHttpObject(xsearch = GetXmlHttpObject(), url, search_started);
+}
+
+function search_started()
+{
+    if (xsearch.readyState != 4)
+       return;
+    var xml = xsearch.responseXML;
+    var error = xml.getElementsByTagName("error");
+    if (error[0])
+    {
+       var msg = error[0].childNodes[0].nodeValue;
+       alert(msg);
+       return;
+    }
+    location = "?" + searchstring + "&status=searching";
+}
+
+function check_search()
+{
+    clearTimeout(asearch);
+    var url = "search.pz2?" +
+        "command=show" +
+       "&start=" + args['start'] +
+       "&session=" + session +
+       "&block=1";
+    SendXmlHttpObject(xshow = GetXmlHttpObject(), url, show_records);
+}
+
+function show_records()
+{
+    if (xshow.readyState != 4)
+       return;
+    var i;
+    var xml = xshow.responseXML;
+    var body = document.getElementById("body");
+    var hits = xml.getElementsByTagName("hit");
+    if (!hits[0]) // We should never get here with blocking operations
+    {
+       body.innerHTML = "No records yet";
+       asearch = setTimeout(check_search, 250);
+    }
+    else
+    {
+
+       var total = Number(xml.getElementsByTagName('total')[0].childNodes[0].nodeValue);
+       var merged = Number(xml.getElementsByTagName('merged')[0].childNodes[0].nodeValue);
+       var start = Number(xml.getElementsByTagName('start')[0].childNodes[0].nodeValue);
+       var num = Number(xml.getElementsByTagName('num')[0].childNodes[0].nodeValue);
+       body.innerHTML = '<b>Records : ';
+       body.innerHTML += (start + 1) + ' to ' + (start + num) +
+               ' of ' + merged + ' (total hits: ' + total + ')</b>';
+
+/*
+       if (start + num < merged)
+           body.innerHTML += ' <a href="" ' +
+               'onclick="startrec=' + (start + 20) +
+               ';check_search(); return false;">Next</a>';
+
+       if (start > 0)
+           body.innerHTML += ' <a href="" ' +
+               'onclick="startrec=' + (start - 20) +
+               ';check_search(); return false;">Previous</a>';
+*/
+
+       body.innerHTML += '<br/>';
+       for (i = 0; i < hits.length; i++)
+       {
+           body.innerHTML += '<p>';
+           body.innerHTML += (i + start + 1) + ': ';
+           var mk = hits[i].getElementsByTagName("title");
+           if (mk[0])
+               body.innerHTML += mk[0].childNodes[0].nodeValue;
+           body.innerHTML += '</p>';
+       }
+       shown++;
+       if (shown < 5)
+           asearch = setTimeout(check_search, 1000);
+       else
+           asearch = setTimeout(check_search, 2000);
+    }
+}
+
+
+// Session management
+// ======================================================
+
+function session_started()
+{
+    if (xinitSession.readyState != 4)
+       return;
+    var xml = xinitSession.responseXML;
+    var sesid = xml.getElementsByTagName("session")[0].childNodes[0].nodeValue;
+    location = '?session=' + sesid;
+}
+
+function start_session()
+{
+    var url="search.pz2?command=init";
+    SendXmlHttpObject(xinitSession = GetXmlHttpObject(), url, session_started);
+}
+
+function start()
+{
+    splitargs();
+    if (!args['session'])
+       start_session();
+    else
+    {
+       session = args['session'];
+       document.getElementById("status").innerHTML = "Live";
+       document.searchform.elements['session'].value = session;
+       check_query_start();
+    }
+}
+
+// Utility
+// ======================================================
+
+function splitargs()
+{
+    searchstring = location.search.substring(1);
+    var list = searchstring.split('&');
+    var l;
+    for (l in list)
+    {
+       var listparms = list[l].split('=');
+       args[listparms[0]] = listparms[1];
+    }
+}
+
+function GetXmlHttpObject()
+{ 
+    var objXMLHttp=null
+    if (window.XMLHttpRequest)
+       objXMLHttp=new XMLHttpRequest()
+    else if (window.ActiveXObject)
+       objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
+    return objXMLHttp;
+} 
+
+function SendXmlHttpObject(obj, url, handler)
+{
+    obj.onreadystatechange=handler;
+    obj.open("GET", url);
+    obj.send(null);
+}
+
+</script>
+</head>
+
+<body onload="start();">
+
+<table width="100%" border="1" cellpadding="5">
+    <tr>
+       <td width="250" height="100" align="center">
+           <font size="+2"><b>MasterKey mk II</b></font>
+       </td>
+
+       <td>
+           <form name="searchform">
+               <b>Search:</b> <input name="query" type="text" size="50"/>
+               <input type="submit" value="Go"/>
+               <input type="hidden" name="session" value=""/>
+           </form>
+
+       <td>
+    </tr>
+
+    <tr>
+       <td valign="top" id="termlist">&nbsp;</td>
+
+       <td valign="top" id="body">
+       Funky search prototype.<br><br><br><br>
+       <td>
+    </tr>
+
+    <tr>
+       <td>
+       &nbsp;
+       </td>
+
+       <td>
+           Status: <span id="status">Initializing</span> <span id="targetstatus"></span><br/><span id="stat"></span>
+       </td>
+
+    </tr>
+
+</table>
+
+</body>
+</html>