-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
173 lines (143 loc) · 4.42 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<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>