-
Notifications
You must be signed in to change notification settings - Fork 0
/
eurisko.html
357 lines (336 loc) · 9.08 KB
/
eurisko.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<html>
<head>
<script src="http://cdn.jsdelivr.net/pouchdb/4.0.0/pouchdb.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css">
<script>
var db = new PouchDB('eurisko'),
remote = 'https://istersurovedgeoppossells:[email protected]/eurisko',
opts = {
continuous: true
};
db.replicate.to(remote, opts);
db.replicate.from(remote, opts);
var timerSet = false;
function openNote(note_id)
{
db.get(note_id.toString(), function (err, response)
{
console.log(err || response);
$("#note").html(response['content']);
$("#note").attr("note_id", note_id);
$("#note").attr('title',response['title']);
$("#note").attr("rev", response['_rev']);
parseTags(response['tags']);
});
setupChangeHandler();
}
function parseTags(tags)
{
//need to be clickable links
for(var i=tags.length; i--;)
{
$("#tags").append(tags[i] + " ");
}
}
function createNote(title)
{
$("#note").html("");
$("#note").attr('title',title);
setupChangeHandler();
}
function setupChangeHandler()
{
/*document.getElementById("editor").addEventListener("input", function() {
alert("input event fired");
}, false);*/
$("#note").bind("input change", function()
{
console.log("change happend");
dealWithChange();
});
$("#tags").bind("input change", function()
{
console.log("change happend");
dealWithChange();
});
}
function dealWithChange()
{
if(timerSet == true)
{
//do nothing
}
else
{
setTimeout(update, 10000);//10 seconds
timerSet = true;
}
}
function read()
{
db.get('1', function (err, response)
{
console.log(err || response);
$("#note").html(response['content']);
});
//response is:
//{title: "first note", content: "hello world again", _id: "1", _rev: "2-0266dc1ee2bc449514ea7d25106542cd"}
}
// create a document; log the response
function write()
{
db.post({title: 'note2 title', content: 'second note'}, function (err, response)
{
console.log(err || response);
});
//response is:
//{ok: true, id: "1EB83EE4-494C-0E55-A3DF-5B13955810DE", rev: "1-35fa8e78cc189bc7a9253a2f31296c40"}
}
function update()
{
var note_id = $("#note").attr('note_id');
var value = $("#note").html();
var rev = $("#note").attr('rev');
var title = $("#note").attr('title');
var tags = $("#tags").html();
var tag_array = null;
if(tags!="")
{
tag_array = tags.split(",");
for(var i=tag_array.length; i--;)
{
tag_array[i] = $.trim(tag_array[i]);//strip whitespace
}
console.log(tag_array);
}
if(note_id==null && rev==null && title!=null)
{
//new note
var tags = []
if(tag_array!=null)
{
tags = tag_array;
}
db.post({title: title, content: value, customer_id: 0, tags: tags}, function (err, response)
{
console.log(err || response);
});
}
else
{
// update a document; log the response
var tags = []
if(tag_array!=null)
{
tags = tag_array;
}
db.put({_id: note_id, _rev: rev, title: title, content: value, customer_id: 0, tags: tags}, function (err, response)
{
console.log(err || response);
});
//response is:
//{ok: true, id: "1EB83EE4-494C-0E55-A3DF-5B13955810DE", rev: "2-07b996d6afbba32f73b7d772f061397a"}
}
}
function remove()
{
// delete a document; log the response
db.remove({
_id: "1EB83EE4-494C-0E55-A3DF-5B13955810DE",
_rev: "2-07b996d6afbba32f73b7d772f061397a"
}, function (err, response)
{
console.log(err || response);
});
// response is:
// {ok: true, id: "1EB83EE4-494C-0E55-A3DF-5B13955810DE", rev: "3-c35dceb6732ec828befdf67e817565e7"}
}
function search(type, query, response)
{
// separate the type and query by a colon
url = "https://eurisko.cloudant.com/eurisko/_design/searchDoc/_search/searchIndex?q="
if(type=='both')
{
//title:firststse OR content:worldssdf
suffix = 'title:' + query + ' OR content:' + query + ' OR title:' + query + '*';
}
else
{
suffix = type + ":" + query
}
console.log(suffix);
$.ajax
({
type: "GET",
url: url + suffix,
dataType: 'json',
async: true,
headers: {
"Authorization": "Basic " + btoa('istersurovedgeoppossells' + ":" + '613aa703284b318205b178a486b6f7344d7b0c2a')
},
success: function (data)
{
console.log(data);
results = [];
if(data['total_rows']>0)
{
rows = data['rows'];
for(var i=rows.length; i--;)
{
var row=rows[i];
data = { label: row['fields']['title'], value: row['fields']['title'], type: 'note', note_id: row['id']}
results.push(data);
}
}
console.log(results);
response(results);
}
});
// response is:
// {"total_rows":1,"bookmark":"g2wAAAABaANkACFkYmNvcmVAZGIxMy5tZXJpdGFnZS5jbG91ZGFudC5uZXRsAAAAAm4EAAAAAIBuBAD___-_amgCRj_IjFiAAAAAYQBq","rows":[{"id":"1","order":[0.19178301095962524,0],"fields":{"content":"hello world again","title":"first note"}}]}
// or when no results:
// {"total_rows":0,"bookmark":"g2o","rows":[]}
}
function generate_autocomplete(request, response)
{
console.log(request.term);
query = parse_input(request.term);
results = [];
if(query!=null)
{
if(query['type']=='new')
{
data = { label: 'Add new note: ' + query['text'], value: query['text'], type: 'new_note'}
results.push(data);
console.log(results);
response(results);
}
else if(query['type']=='tag')
{
search('tag', query['text'], response);
}
else if(query['type']=='append')
{
data = { label: 'Append data to note ' + query['note_to_append_to'], value: query['text_to_append'], type: 'append_to_note', note_to_append_to: query['note_to_append_to']}
results.push(data);
console.log(results);
response(results);
}
else if(query['type']=='search')
{
search('both', query['text'], response);
}
}
}
function appendToNote(title, text_to_append)
{
//first, get the note
search('title',title,function(data)
{
console.log('got search result back');
if(data.length>1)
{
console.log('picking first title');
}
var note_id = data[0]['note_id'];
console.log(note_id);
db.get(note_id.toString(), function (err, response)
{
console.log('got note info back');
var content = response['content'];
var rev = response['_rev'];
//then append the text to the bottom of note
content = content + '<br/>' + text_to_append;
//then update the note
db.put({_id: note_id, _rev: rev, title: title, customer_id: 0, tags: [], content: content}, function (err, response)
{
console.log('updated note info');
console.log(err || response);
});
});
});
}
$( document ).ready(function() {
$('#search').autocomplete({
minLength: 2,
source: function(request, response)
{
generate_autocomplete(request, response);
},
select: function(event, ui)
{
// this is called when you've pressed enter on an item in dropdown list
if(ui)
{
console.log(ui.item.type);
if (ui.item.type=="note")
{
openNote(ui.item.note_id);
}
else if (ui.item.type=="append_to_note")
{
appendToNote(ui.item.note_to_append_to, ui.item.value);
}
else if (ui.item.type=="new_note")
{
createNote(ui.item.value);
}
}
}
});
});
function parse_input(input)
{
query = {};
if(input.search("\\+")==0)
{
//+ to append
console.log('new note');
query['type'] = 'new';
query['text'] = input.substring(1);
}
else if(input.search("\\#")==0)
{
//#search tags
console.log('searching tags');
query['type'] = 'tag';
query['text'] = input.substring(1);
}
else if(input.search("\\>")==0)
{
console.log('appending to note');
query['type'] = 'append';
parts = input.substring(1).split("\"");
if(parts.length>2)
{
query['text_to_append'] = parts[1];
query['note_to_append_to'] = parts[0];
console.log(query['text_to_append']);
console.log(query['note_to_append_to']);
}
}
else
{
console.log('searching');
query['type'] = 'search';
query['text'] = input;
}
return query;
}
//other features (delete note)
//UI - display all titles in expandable list... display all tags, can hover to see other notes with tag. parse @'title links'. each line snippet of its own... swipe left/right to delete or move. hierarchy of bullets that can re-arrange.
//edge cases: spaces, apostrophes in title name
//note format:
//customer_id, title, content, child_of, children, last_updated, tags: []
</script>
</head>
<body style="background-image:url(images/bg.jpg);background-repeat:repeat;">
<div id='title' style='margin: auto; width: 10%;'>
<input type='text' id='search'>
</div>
<div id='note' contentEditable='true' style='background-color:white; max-height: 300px; width: 100%; overflow-x:hidden; overflow-y:auto;' class='doc rounded {bottom transparent}'></div>
<div id='tags' contentEditable='true' style='background-color:white; max-height: 300px; width: 100%; overflow-x:hidden; overflow-y:auto;' class='doc rounded {bottom transparent}'></div>
</body>
</html>