Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
174 lines (143 sloc) 4.42 KB
<html>
<script type="text/javascript" src="gears_init.js"></script>
<script type="text/javascript" src="uuid.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script>
var localServer;
var store;
var db;
var couchdb_name = "patient_database";
function createDocument(key,value){
//db.execute('INSERT INTO hash (key, value) VALUES (?,?)',[key,value]);
db.execute('INSERT INTO hash (key, value) VALUES ("'+key+'","'+value+'")');
}
function concatenateResult(result){
var output = "";
while (result.isValidRow()) {
output += result.fieldName(0) + ' == ' + result.field(0);
result.next();
}
result.close();
return output;
}
function getValueReset(id){
var value = $('#'+id).val();
$('#'+id).val(""); // reset input box
return value
}
function couchRequest(url, getDeletePostPut, data){
//$.ajax({url:'/',type:'put', async:false}).responseText
// a lot of serializing and unserializing going on here
data = eval('('+data+')')
data = JSON.stringify(data)
return eval('('+$.ajax({url:url,type:getDeletePostPut, data:data, async:false }).responseText + ')')
}
function getLocalData(){
var buffer = ""
var result = db.execute('SELECT key,value FROM hash');
while (result.isValidRow()) {
buffer += "<li>" + result.field(0) + ' => ' + result.field(1);
result.next();
}
return buffer
}
function getRemoteData(){
var buffer = ""
url = "/" + couchdb_name + "/_all_docs"
rows = couchRequest(url, "get").rows
for(i=0; i<rows.length; i++){
// buffer += "<li>" + JSON.stringify(rows[i]);
url = "/" + couchdb_name + "/" + rows[i].id;
buffer += "<li>" + JSON.stringify(couchRequest(url, "get"));
}
return buffer;
}
function clearLocalDB(){
db.execute('DELETE FROM hash');
}
function clearRemoteDB(){
}
$(document).ready(function () {
$('online?').innerHTML = navigator.onLine ? "Online" : "Offline";
// setup gears
if (!window.google || !google.gears) {
alert("NOTE: You must install Gears first.");
}
else {
localServer = google.gears.factory.create('beta.localserver', '1.0');
store = localServer.createManagedStore('manifest.json');
db = google.gears.factory.create('beta.database', '1.0');
db.open('my-database');
db.execute('CREATE TABLE IF NOT EXISTS ' + 'hash (key varchar(255), value varchar(255))');
}
// setup couchdb
url = "/" + couchdb_name
$.ajax({url:url,type:'put' })
document.getElementById("localData").innerHTML = getLocalData();
document.getElementById("remoteData").innerHTML = getRemoteData();
$("#save").click(function() {
patientName = getValueReset('patient_name');
diagnosis = getValueReset('diagnosis');
//put it in the local db
createDocument(new UUID(), "{'name':'" + patientName + "', 'diagnosis':'"+ diagnosis +"'}")
});
$("#push").click(function() {
result = db.execute('SELECT key,value FROM hash');
while (result.isValidRow()) {
document_id = result.field(0);
document_contents = result.field(1);
//$.ajax({url:'/',type:'put', async:false}).responseText
var url = '/' + couchdb_name + '/' + document_id
// a lot of serializing and unserializing going on here
var data = eval('('+document_contents+')')
data._id = document_id
data = JSON.stringify(data)
$.ajax({url:url,type:'put', data:data })
result.next();
}
});
$("#pull").click(function() {
clearLocalDB();
url = "/" + couchdb_name + "/_all_docs"
rows = couchRequest(url, "get").rows
for(i=0; i<rows.length; i++){
url = "/" + couchdb_name + "/" + rows[i].id;
result = couchRequest(url, "get")
createDocument(result._id, { name:result.name , diagnosis: result.diagnosis} )
}
})
});
</script>
<style>
.data{
font-size:8pt;
}
.button{
background-color:black;
color: white;
font-weight: bold;
width: 100px;
border: thick solid gray;
}
</style>
<body>
<div id='status'>Currently <span id='online?'>unknown</span></div>
Patient Name: <input id='patient_name' type='text'></input><br/>
Diagnosis: <input id='diagnosis' type='text'></input><br/>
<span class='button' id='save'>Save</span>
<br/>
<br/>
<span class='button' id='push'>Push</span>
<span class='button' id='pull'>Pull</span>
<br/>
<div id="localDataBox">
Local Data:
<div class='data' id='localData'></div>
</div>
<div id="remoteDataBox">
Remote Data:
<div class='data' id='remoteData'></div>
</div>
</body>
</html>