-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.html
632 lines (563 loc) · 22.4 KB
/
app.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>SignalAnnotation</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/open-iconic/1.1.1/font/css/open-iconic-bootstrap.min.css" integrity="sha256-BJ/G+e+y7bQdrYkS2RBTyNfBHpA9IuGaPmf9htub5MQ=" crossorigin="anonymous" />
<style type="text/css">
/* context menu */
.context-menu {
display: none;
position: absolute;
z-index: 10;
padding: 12px 0;
width: 240px;
background-color: #fff;
border: solid 1px #dfdfdf;
box-shadow: 1px 1px 2px #cfcfcf;
}
.context-menu--active {
display: block;
}
.context-menu__items {
list-style: none;
margin: 0;
padding: 0;
}
.context-menu__item {
display: block;
margin-bottom: 4px;
}
.context-menu__item:last-child {
margin-bottom: 0;
}
.context-menu__link {
display: block;
padding: 4px 12px;
color: #0066aa;
text-decoration: none;
}
.context-menu__link:hover {
color: #fff;
background-color: #0066aa;
}
.inputfile {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
label {
display: block;
margin-left: auto;
margin-right: auto
}
.inputfile + label {
padding: 10px;
font-size: 1.25em;
font-weight: 700;
color: white;
background-color: #ff3349 ;
display: inline-block;
}
.inputfile:focus + label,
.inputfile + label:hover {
background-color: red;
}
.inputfile + label {
cursor: pointer; /* "hand" cursor */
}
</style>
</head>
<body>
<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">How to use this Signal Annotator?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h3>Move in the record</h3>
<ul>
<li><b>Zoom</b> = ctrl + Mouse Scrool</li>
<li><b>Move forward/backward</b> = using your <span class="oi oi-arrow-thick-left"></span> and <span class="oi oi-arrow-thick-right"></span> keys on your keyboard, or the arrows buttons available under the signals.</li>
</ul>
<h3>Annotate</h3>
<p>An annotation is defined by two timestamps: the beginning and the end of the annotation. When an annotation is a unique sample (not a timerange) the beginning equals the end.</p>
<ul>
<li><b>Annotate a sample</b> = Double click on the place where you would like to set your annotation. This will draw a vertical line.</li>
<li><b>Annotate a timerange</b> = First click on the beginning and then click on the end. This will draw a rectangle over the annotation.</li>
<li><b>Edit an annotation</b> = Right click on the annotation and click on the Edit option.</li>
<li><b>Delete an annotation</b> = Right click on the annotation and click on the Delete option.</li>
</ul>
</div>
<div class="modal-footer">
<a target="_blank" type="button" class="btn btn-primary" href="https://github.com/Dubrzr/SignalAnnotation/issues/new">I have one more question</a>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="container">
<br>
<div class="row">
<div class="col-sm">
</div>
<div class="col-sm">
<div id="canvass"></div>
</div>
<div class="col-sm">
</div>
</div>
<div class="row">
<input type="file" id="file" name="file" class="inputfile" enctype="multipart/form-data" multiple />
<label for="file">Upload new signal(s)</label>
</div>
<div class="row">
<div class="col-sm">
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="annotations" checked>
<label class="form-check-label" for="annotations">Display annotations</label>
<br>
<input type="checkbox" class="form-check-input" id="grid">
<label class="form-check-label" for="grid">Display grid</label>
<br>
<input type="checkbox" class="form-check-input" id="magnetism">
<label class="form-check-label" for="magnetism">Magnetism (todo)</label>
</div>
</div>
<div class="col-sm">
<button id="moveLeft" type="button" class="btn btn-outline-secondary" title="Move backward"><span class="oi oi-arrow-thick-left"></span></button>
<button id="moveRight" type="button" class="btn btn-outline-secondary" title="Move forward"><span class="oi oi-arrow-thick-right"></span></button>
</div>
<div class="col-sm">
<button type="button" class="btn btn-success" id="save">Export annotations</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#helpModal">Help</button>
<a type="button" class="btn btn-primary" target="_blank" href="https://github.com/Dubrzr/SignalAnnotation">Contribute</a>
</div>
</div>
</div>
<nav id="context-menu" class="context-menu">
<ul class="context-menu__items">
<li class="context-menu__item">
<a href="#" class="context-menu__link" data-action="Edit"><i class="fa fa-edit"></i> Edit</a>
</li>
<li class="context-menu__item">
<a href="#" class="context-menu__link" data-action="Delete"><i class="fa fa-times"></i> Delete</a>
</li>
</ul>
</nav>
<!-- Bootstrap -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<!-- Signal annotation -->
<script src="concrete.min.js"></script>
<script src="annotator.js"></script>
<script type="text/javascript">
const sig_colors = {
"II": "#00FF00",
"AVR": "#00FF00",
"V": "#00FF00",
"RESP": "#FFFF00",
"PLETH": "#82CEFC",
"ABP": "#FF0000",
"CVP": "#FAA804",
};
const color_space = [
"#d65db1",
"#ff6f91",
"#ff9671",
"#ffc75f",
"#f9f871",
"#00ce9a"
]
const canvass = document.getElementById("canvass");
function getCanvasAnnotatorCount() {
return canvass.children.length;
}
/* let request = new XMLHttpRequest();
request.open('GET', 'test_data.json', false); // `false` makes the request synchronous
request.send(null);
let sig_name = null;
let values = null;
if (request.status === 200) {
const data = JSON.parse(request.responseText);
sig_name = data.sig_name;
values = data.data;
}
console.assert(sig_name.length == values.length);*/
const contextMenuClassName = "context-menu";
const contextMenuItemClassName = "context-menu__item";
const contextMenuLinkClassName = "context-menu__link";
const contextMenuActive = "context-menu--active";
const taskItemClassName = "task";
let taskItemInContext;
let clickCoords;
let clickCoordsX;
let clickCoordsY;
const menu = document.querySelector("#context-menu");
const menuItems = menu.querySelectorAll(".context-menu__item");
let menuState = 0;
let menuWidth;
let menuHeight;
let menuPosition;
let menuPositionX;
let menuPositionY;
let windowWidth;
let windowHeight;
let currentAnnotation;
let currentAnnotator;
let annotators = [];
// Function to check if we clicked inside an element with a particular class name.
function clickInsideElement(event, className) {
let el = event.srcElement || event.target;
if (el.classList.contains(className)) {
return el;
} else {
while (el = el.parentNode) {
if (el.classList && el.classList.contains(className)) {
return el;
}
}
}
return false;
}
function getPosition(event) {
let posx = 0;
let posy = 0;
if (event.pageX || event.pageY) {
posx = event.pageX;
posy = event.pageY;
} else if (event.clientX || event.clientY) {
posx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
return {
x: posx,
y: posy
}
}
function positionMenu(event) {
clickCoords = getPosition(event);
clickCoordsX = clickCoords.x;
clickCoordsY = clickCoords.y;
menuWidth = menu.offsetWidth + 4;
menuHeight = menu.offsetHeight + 4;
windowWidth = window.innerWidth;
windowHeight = window.innerHeight;
if ( (windowWidth - clickCoordsX) < menuWidth ) {
menu.style.left = windowWidth - menuWidth + "px";
} else {
menu.style.left = clickCoordsX + "px";
}
if ( (windowHeight - clickCoordsY) < menuHeight ) {
menu.style.top = windowHeight - menuHeight + "px";
} else {
menu.style.top = clickCoordsY + "px";
}
}
function toggleMenuOn() {
if (menuState !== 1) {
menuState = 1;
menu.classList.add(contextMenuActive);
for (let idx in annotators) {
const ann = annotators[idx];
ann.disable_annotations();
}
}
}
function toggleMenuOff() {
if (menuState !== 0) {
menuState = 0;
menu.classList.remove(contextMenuActive);
for (let idx in annotators) {
const ann = annotators[idx];
ann.enable_annotations();
}
}
}
function annotationLeftClickCallback(idx) {
function fun(event, annotation) {
// TODO: needed?
}
return fun;
}
function annotationRightClickCallback(idx) {
function fun(event, annotation) {
console.log("annotationRightClickCallback");
taskItemInContext = clickInsideElement(event, taskItemClassName);
event.preventDefault();
toggleMenuOn();
positionMenu(event);
currentAnnotation = annotation;
currentAnnotator = idx;
}
return fun;
}
// Handle clicks on edit/delete buttons inside right-click ContextMenu
document.addEventListener("click", function(event) {
console.log("Clicked")
var clickeElIsLink = clickInsideElement(event, contextMenuLinkClassName);
if (clickeElIsLink) {
event.preventDefault();
const action = clickeElIsLink.getAttribute("data-action");
if (action === "Edit") {
}
else if (action === "Delete") {
annotators[currentAnnotator].remove_annotation(currentAnnotation);
toggleMenuOff();
}
console.log(action);
} else {
var button = event.which || event.button;
if (button === 1) {
toggleMenuOff();
}
}
});
function read_json(contents) {
var json_contents;
var error;
try {
json_contents = JSON.parse(contents);
} catch(e) {
return [null, e];
}
if ('values' in json_contents) {
var values = json_contents['values'];
if (Array.isArray(values)) {
if (values.length > 0) {
if (typeof values[0] === 'number')
return [values, null];
else
return [null, 'values array must contain numbers!']
}
else
return [null, 'values array is empty!']
}
else
return [null, 'JSON `values` key` is not a valid array!']
}
else
return [null, 'JSON dictionary has no `values` key!']
}
function read_csv(contents) {
}
function add_signal(name, values) {
let subDiv = canvass.appendChild(document.createElement('div'));
console.log(subDiv)
const ann = new Annotator(
subDiv,
1100,
110,
name,
values,
125,
0,
"#000",
color_space[annotators.length%(color_space.length)],
document.querySelector('#magnetism:checked') !== null,
1000,
125/2, 125/8, 100, 10);
ann.setAnnotationLeftClickCallback(annotationLeftClickCallback(annotators.length));
ann.setAnnotationRightClickCallback(annotationRightClickCallback(annotators.length));
annotators.push(ann);
}
function get_unique_filename(name) {
var tmp = name;
for (var ann in annotators) {
if (tmp === annotators[ann].sig_name) {
tmp = tmp + ".1";
}
}
return tmp;
}
// Read file from client
document.getElementById('file').addEventListener('change', function() {
for (var i = 0, numFiles = this.files.length; i < numFiles; i++) {
var file = this.files[i];
if (file.type === 'application/json' || file.type === 'application/vnd.ms-excel') {
} else {
alert(file.name + ": Unsupported file format! Supported file formats are: .json and .csv.");
continue;
}
var reader = new FileReader();
reader.onload = function(event) {
var contents = event.target.result;
if (file.type === 'application/json') {
console.log("Detected json file...")
var res = read_json(contents);
if (res[1] === null) {
console.log("Successfuly red JSON values...")
add_signal(get_unique_filename(file.name), res[0])
}
else {
alert(file.name + ": " + res[1]);
}
} else if (file.type === 'application/vnd.ms-excel') {
console.log("Detected csv file...")
var res = read_csv(contents);
if (res[1] === null) {
console.log("Successfuly red JSON values...")
add_signal(get_unique_filename(file.name), res[0])
}
else {
alert(res[1]);
}
}
}
reader.readAsText(file)
}
}, false);
/*
for (let i = 0; i < sig_name.length; i++) {
let subDiv = canvass.appendChild(document.createElement('div'));
const ann = new Annotator(
subDiv,
1100,
110,
sig_name[i] + " [mV]",
values[i],
125,
0,
"#000",
sig_colors[sig_name[i]],
document.querySelector('#magnetism:checked') !== null,
1000,
125/2, 125/8, 100, 10);
ann.setAnnotationLeftClickCallback(annotationLeftClickCallback(i));
ann.setAnnotationRightClickCallback(annotationRightClickCallback(i));
annotators.push(ann);
}
*/
function send_json_to_client(filename, json_contents) {
var element = document.createElement('a');
element.setAttribute('href', "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json_contents)));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Send annotations to client
document.getElementById("save").addEventListener("click", () => {
const result = {};
for (let idx in annotators) {
const ann = annotators[idx];
result[ann.sig_name] = ann.get_annotations();
//console.log(annotations);
}
send_json_to_client("annotations.json", result);
});
function smooth(x) {
// Returns smoothed values between zero and one
if (x <= -1)
return 0;
if (x >= 1)
return 1;
return 2/(1+Math.exp(-5*x))/2;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let moving = false;
async function moveToSmooth(diff_offset, animation_length_ms) {
moving = true;
if (annotators.length == 0)
return;
var start_offset = annotators[0].current_offset;
let start_offsets = [];
for (let idx in annotators) {
start_offsets.push(annotators[idx].current_offset);
}
// let diff = offset - start_offset;
// Smoothed
const start = (new Date()).getTime(); // -1
const end = start + animation_length_ms; // +1
let now = (new Date()).getTime();
while (now < end) {
const elapsed = now - start;
const expected_offset = Math.floor(smooth(2*(elapsed/animation_length_ms)-1) * diff_offset);
//console.log(elapsed, expected_offset, start_offset + expected_offset);
for (let idx in annotators) {
annotators[idx].moveTo(start_offsets[idx] + expected_offset);
}
now = (new Date()).getTime();
await sleep(1);
}
for (let idx in annotators) {
annotators[idx].moveTo(start_offsets[idx] + diff_offset);
}
moving = false;
}
document.getElementById("moveLeft").addEventListener("click", () => {
if (moving)
return;
moveToSmooth(-125, 500);
});
document.getElementById("moveRight").addEventListener("click", () => {
if (moving)
return;
moveToSmooth(125, 500);
});
document.getElementById("magnetism").addEventListener("change", (event) => {
for (let idx in annotators) {
const ann = annotators[idx];
if (event.target.checked) {
ann.enable_magnetism();
console.log("Enabled magnetism :D");
} else {
ann.disable_magnetism();
console.log("Disabled magnetism :o");
}
}
});
document.getElementById("grid").addEventListener("change", (event) => {
for (let idx in annotators) {
const ann = annotators[idx];
if (event.target.checked) {
ann.enable_grids();
} else {
ann.disable_grids();
}
}
});
document.getElementById("annotations").addEventListener("change", (event) => {
for (let idx in annotators) {
const ann = annotators[idx];
if (event.target.checked) {
ann.enable_annotations();
} else {
ann.disable_annotations();
}
}
});
document.addEventListener('keydown', function(event) {
if (moving)
return;
if(event.keyCode == 37) {
moveToSmooth(-125, 220);
}
else if(event.keyCode == 39) {
moveToSmooth(125, 220);
}
});
document.addEventListener("wheel", function(event){
if(event.ctrlKey){
event.preventDefault();
for (let idx in annotators) {
const ann = annotators[idx];
ann.zoomToEvent(event);
}
}
});
</script>
</body>
</html>