-
Notifications
You must be signed in to change notification settings - Fork 25
/
annotate.js
executable file
·550 lines (432 loc) · 15.6 KB
/
annotate.js
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/**
* Created by stephen on 8/30/17.
*/
$(document).ready(function() {
console.log("loading stuff...");
if(typeof baseurl === 'undefined'){
baseurl = "";
}
console.log("baseurl: " + baseurl);
console.log("controller: " + controller);
var highlighting = false;
var range = {start:-1, end:-1, id:""};
function resetrange(){
range = {start:-1, end:-1, id:""};
highlightrange();
}
// This retrieves the text within a range. If the range
// is empty, then it returns the empty string.
function gettextinrange(usetext){
usetext = usetext || false;
if(range.start != -1 && range.end != -1) {
var txt = ""
for (var i = range.start; i <= range.end; i++) {
var el = document.getElementById("tok-" + range.id + "-" + i);
if(usetext){
txt += $(el).text() + " ";
}else{
txt += $(el).attr("orig") + " ";
}
}
return txt.trim();
}else{
return "";
}
}
// This returns a list of all words in the document.
function getalltext() {
var allwords = $.map($("[id^=tok]"), function (n, i) {
return $(n).attr("orig");
});
return allwords;
}
function highlightrange(){
$(".highlighted").removeClass("highlighted");
$(".highlightstart").removeClass("highlightstart");
$(".highlightend").removeClass("highlightend");
$(".highlightsingle").removeClass("highlightsingle");
// NOTE: jquery does not allow colons or periods in id strings. Because of
// this, we use document.getElementById(..).
// NOTE: for highlighting to work correctly, the token id pattern needs to
// match the pattern found in HtmlGenerator.java
if(range.start == -1 && range.end == -1) {
// do nothing.
}else if(range.start == range.end){
var startel = document.getElementById("tok-" + range.id + "-" + range.start);
$(startel).addClass("highlightsingle");
}else{
for(var i = range.start; i <= range.end; i++){
var el = document.getElementById("tok-" + range.id + "-" + i);
if(i == range.start){
$(el).addClass("highlightstart");
}
else if(i == range.end){
$(el).addClass("highlightend");
}else{
$(el).addClass("highlighted");
}
}
}
}
/** This returns true if the range has been changed. **/
function updaterange(id){
var docid = id.split("-")[1];
var intid = parseInt(id.split("-")[2]);
range.id = docid;
var ret = false;
if(range.start == -1 && range.end == -1){
range.start = intid;
range.end = intid;
ret = true;
}
if(range.start - intid == 1){
range.start = intid;
ret = true;
}
if(range.end - intid == -1){
range.end = intid;
ret = true;
}
highlightrange();
return ret;
}
function iscurrentrange(id){
var docid = id.split("-")[1];
var intid = parseInt(id.split("-")[2]);
return range.id == docid && (range.start == intid || range.end == intid);
}
function loadtok(){
console.log("loadtok called...");
$(document).mouseup(function(event){
highlighting = false;
});
// just clean everything out first...
$("[id^=tok]").popover({
placement: "bottom",
content: function () {
var html = $("#buttons").html();
var out = " <div id='popover" + $(this)[0].id + "'>" + html + "</div>";
return out;
},
title: function () {
var text = gettextinrange(true);
var link = "<a class='search' href=\"https://www.google.com/search?q=" + gettextinrange(true) + "\" target=\"_blank\">Google</a>"
return text + " (" + link + ")";
},
html: true,
trigger: "focus",
animation: false,
});
$("[id^=tok]").mouseover(function(event){
if(highlighting && updaterange(this.id)) {
//$(this).addClass("highlighted");
}
});
// click on anything but a word, and they hide.
$(document).mousedown(function(e){
console.log(e);
var hasclass = $(e.target).hasClass("labelbutton") || $(e.target).hasClass("search");
if(!e.target.id.startsWith("tok") && !hasclass && !(e.target.tagName == "MARK")) {
console.log("extraneous click");
$("[id^=tok]").popover("hide");
resetrange();
showtopstats();
}
});
// this solves the problem where the window scrolls to
// the top whenever definput has focus.
var cursorFocus = function(elem) {
var x = window.scrollX, y = window.scrollY;
if(elem != null) {
elem.focus();
}
window.scrollTo(x, y);
}
$("[id^=tok]").mouseup(function(event){
// only toggle on the left click.
if(event.which == 1) {
$("[id^=tok]").not($(this)).popover('hide');
$(this).popover("show");
highlighting = false;
// put focus on the dictionary entry element.
cursorFocus(document.getElementById("definput"));
$(".enter").keydown(function (event) {
var keypressed = event.keyCode || event.which;
if (keypressed == 13) {
submitdict();
}
});
// if(range.start > -1) {
// $.ajax({
// method: "POST",
// url: "/stats/getstats",
// data: {text: gettextinrange(), alltext: getalltext()}
// }).done(function (msg) {
// $("#infobox").html(msg);
// });
// }else{
// showtopstats();
// }
}
});
$("[id^=tok]").mousedown(function(event){
if(event.which == 1) {
highlighting = true;
resetrange();
updaterange(this.id);
console.log(range);
}
});
$("div.text")
.mouseenter(function(){})
.mouseleave(function(){
highlighting = false;
})
// on right click, change label to nothing.
$("[id^=tok]").contextmenu(function(event){
resetrange();
$("[id^=tok]").popover('hide');
showtopstats();
event.preventDefault();
var span = event.currentTarget;
removelabel(span);
});
}
loadtok();
$("#dictbutton").click(function(){
$.ajax({
method: "GET",
url: baseurl + "/" + controller + "/toggledefs",
data: {idlist: getsentids()}
}).done(function (msg) {
console.log("successful toggle");
$("#htmlcontainer").html(msg);
loadtok();
});
});
$("#romanbutton").click(function(){
$.ajax({
method: "GET",
url: baseurl + "/" + controller + "/togglerom",
data: {idlist: getsentids()}
}).done(function (msg) {
console.log("successful toggle");
$("#htmlcontainer").html(msg);
showtopstats();
loadtok();
});
});
$("#googlebutton").click(function(){
$.ajax({
method: "GET",
url: baseurl + "/" + controller + "/togglegoogle",
data: {idlist: getsentids()}
}).done(function (msg) {
console.log("successful toggle");
$("#htmlcontainer").html(msg);
showtopstats();
loadtok();
});
});
$("#copybutton").click(function(){
$.ajax({
method: "GET",
url: baseurl + "/" + controller + "/togglecopy",
data: {idlist: getsentids()}
}).done(function (msg) {
console.log("successful toggle");
$("#htmlcontainer").html(msg);
showtopstats();
loadtok();
});
});
$("#loadtokbutton").click(function(){
loadtok();
});
// when the doc loads, get the top stats.
function showtopstats() {
var taid = getParameterByName("taid");
if(taid != null) {
$.ajax({
method: "POST",
url: "/stats/gettopstats",
data: {docid: taid}
}).done(function (msg) {
$("#infobox").html(msg);
});
}
}
showtopstats();
// this runs when you click on a single button.
$("body").on("click", '.popover button', function(event){
var buttonvalue = $(this)[0].value;
//var spanid = $(this).parents("[id^=popovertok]")[0].id;
console.log(event);
console.log($(this).parents());
//var sentid = $(this).parents(".card-body")[0].id;
var sentid = range.id;
console.log(sentid);
console.log("Clicked on a button..." + buttonvalue);
console.log(range);
console.log(event.target.id);
console.log(event.target.id == "submitdict");
var startid = "tok-" + range.start;
var endid = "tok-" + (range.end+1);
if(!(event.target.id == "submitdict")){
$("[id^=tok]").popover("hide");
resetrange();
showtopstats();
addlabel(sentid, startid, endid, buttonvalue);
}
});
$("body").on("click", '#submitdict', submitdict);
function submitdict(){
// TODO: allow only single word entries at this point.
if (range.start != range.end){
return;
}
var docid = range.id;
var tokid = "tok-" + docid + "-" + range.start ;
var el = document.getElementById(tokid)
var key = $(el).attr("orig");
var val = $("#definput").val();
$("[id^=tok]").popover("hide");
console.log(range);
console.log("you want to submit: " + key + ", " + val);
$.ajax({
method: "GET",
url: "/dict/add",
data: {key:key, val:val}
}).done(function () {
refreshsents();
});
};
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
// given a span id (e.g. tok-4), return the integer
function getnum(spanid){
return parseInt(spanid.split("-").slice(-1)[0]);
}
// Given a span, remove the label.
function removelabel(span) {
$("#savebutton").html("<span class=\"glyphicon glyphicon-floppy-disk\" aria-hidden=\"true\"></span> Save");
$("#savebutton").css({"border-color" : "#c00"});
var sentid = $(span).parents(".card-body")[0].id;
// returns just the number of the token. tok-4, returns 4.
var tokid = getnum(span.id);
console.log("Removing label from: " + sentid + ":" + tokid);
$.ajax({
method: "POST",
url: baseurl + "/"+controller+"/removetoken",
data: {sentid: sentid, tokid: tokid}
}).done(function (msg) {
console.log("successful removal.");
refreshsents();
});
}
// run this function when you click on a token.
function addlabel(sentid, starttokid, endtokid, newclass) {
console.log("Adding " + newclass + " to " + starttokid + "---" + endtokid + ", sentid=" + sentid);
$("#savebutton").html("<i class=\"fa fa-floppy-o\" aria-hidden=\"true\"></i> Save");
$("#savebutton").css({"border-color" : "#c00"});
var srch = window.location.pathname.endsWith("/search");
var srchanno = getParameterByName("searchinanno") == "on";
$.ajax({
method: "POST",
url: baseurl + "/"+controller+"/addspan",
data: {label: newclass,
starttokid: getnum(starttokid),
endtokid:getnum(endtokid),
sentid: sentid,
sentids: getsentids(),
id: sentid,
propagate: srch == srchanno }
}).done(function () {
refreshsents();
});
};
function refreshsents(){
var query= getParameterByName("query");
$.ajax({
method: "POST",
url: baseurl+"/"+controller+"/gethtml",
data: {sentids: getsentids(), query: query}
}).done(function (msg) {
$("#htmlcontainer").html(msg);
loadtok();
performMark();
});
}
$("[id^=addlabel-]").click(function(d){
var label = $(this).text();
var text= getParameterByName("groupid");
$("#savebutton").html("<span class=\"fa fa-floppy-o\" aria-hidden=\"true\"></span> Save");
$("#savebutton").css({"border-color" : "#c00"});
$.ajax({
method: "GET",
url: baseurl+"/"+controller+"/addtext",
data: {text: text, label: label, sentids: getsentids()}
}).done(function (msg) {
console.log(msg);
refreshsents();
});
});
function getsentids(){
var ids = $.map($(".text"), function(n, i){
return n.id;
});
return ids;
}
function save(){
var groupid = getParameterByName("groupid");
console.log("saving group: " + groupid);
$.ajax({
url: baseurl+"/"+controller+"/save",
data: {sentids: getsentids()},
method: "POST",
beforeSend: function() {
// setting a timeout
$("#savebutton").html("<i class=\"fa fa-spinner fa-spin\"></i> Saving...");
}
}).done(function (msg) {
console.log("finished saving!");
$("#savebutton").html("<i class=\"fa fa-check\" aria-hidden=\"true\"></i> Saved!");
$("#savebutton").css({"border-color" : ""});
});
};
$( "#savebutton" ).click(save);
$( ".saveclass" ).click(save);
});
// Create an instance of mark.js and pass an argument containing
// the DOM object of the context (where to search for matches)
var markInstance = new Mark(document.querySelector("#htmlcontainer"));
// Cache DOM elements
var keywordInput = document.querySelector("input[name='keyword']");
function performMark() {
console.log("mark");
// Read the keyword
var keyword = keywordInput.value;
// Determine selected options
var options = {
"accuracy" : {
"value" : "complementary",
"limiters" : ["?"] // this is here to apostrophes in names don't block full match.
}
};
// Remove previous marked elements and mark
// the new keyword inside the context
markInstance.unmark({
done: function(){
markInstance.mark(keyword, options);
}
});
};
// Listen to input and option changes
keywordInput.addEventListener("input", performMark);
console.log($(".table-hover"));
$(".table-hover").mouseover(function() {
console.log("over a td");
// window.location = $(this).data("href");
});