This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
forked from jmtrivial/crossroads-evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.html
570 lines (468 loc) · 23.9 KB
/
evaluate.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
<!DOCTYPE html>
<html lang="fr-FR" class="no-js">
<head>
<meta charset="UTF-8">
<title>Evaluation tool</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- bootstrap -->
<link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="node_modules/bootstrap//dist/js/bootstrap.bundle.min.js" ></script>
<!-- bootstrap dark -->
<link rel="stylesheet" href="node_modules/bootstrap-dark/src/bootstrap-dark.css" />
<!-- jquery -->
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<!-- Library Leaflet -->
<link rel="stylesheet" href="node_modules/leaflet/dist/leaflet.css" />
<script src="node_modules/leaflet/dist/leaflet.js"></script>
<script src="js/evaluate-segmentation.js"></script>
<script>
// shuffle crossroad indexes to browse them in a random order
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
// create the question interface (used only once, corresponds to the form in the left panel)
function build_question_interface() {
questions = '';
window.q_json = JSON.parse(Settings.question_list);
qid = 0;
for(var key in window.q_json){
if (window.q_json.hasOwnProperty(key)){
var q = window.q_json[key];
var question_text = q["question"];
var question_type = q["type"];
var default_value = q["default"];
questions += '<div class="mb-3">';
questions += '<label for="question-' + qid + '" class="form-label">' + question_text + '</label>';
if (question_type == "multiple_choice") {
var values = q["values"];
questions += '<select id="question-' + qid + '" class="form-select" aria-label="' + question_text + '">';
i = 0;
for(var k in values) {
element = values[k];
questions += "<option";
if (default_value == element) {
questions += " selected";
}
questions += ' id="q' + qid + '-o' + i+ '" value="' + i + '">' + element + '</option>';
i += 1;
}
questions += "</select>";
}
else if (question_type == "text") {
questions += '<textarea class="form-control" id="question-' + qid + '" rows="3"></textarea>';
}
questions += "</div>";
qid += 1;
}
}
$("#questions").html(questions);
}
// update the question interface. Used at each step to update both form and map
function update_question_interface() {
// left side
$("#crossroad_title").text("Crossroad #" + window.crossroads_index[window.current_id]);
if (window.current_id != 0) {
$("#download").prop("disabled", false);
$("#prev").prop("disabled", false);
}
else {
$("#download").prop('disabled', true);
$("#prev").prop("disabled", true);
}
if (window.current_id == window.crossroads_index.length - 1) {
$("#next").html("Finish");
}
else {
$("#next").html("Next");
}
exist_eval = has_evaluation_current_crossroad();
if (exist_eval) {
$("#crossroad_status").text("(edit)");
}
else {
$("#crossroad_status").text("");
}
qid = 0;
for(var key in window.q_json){
if (window.q_json.hasOwnProperty(key)){
var q = window.q_json[key];
var values = q["values"];
var question_type = q["type"];
var value = q["default"];
if (exist_eval) {
v = get_current_crossroad_evalution(key);
if (v != null) {
value = v;
}
}
if (question_type == "multiple_choice") {
i = 0;
for(var k in values) {
option = $('#q' + qid + '-o' + i);
option.prop("selected", option.text() == value);
i += 1;
}
}
else if (question_type == "text") {
text = $('#question-' + qid);
text.val(value);
}
qid += 1;
}
}
// right side
Settings.render_crossroad(window.crossroads[window.crossroads_index[window.current_id]], "crossroad-rendering", "crossroad_link");
}
// get answers from the form and store them in the data structure (that contains the initial json)
function set_current_crossroad_evaluation() {
result = {"type": "evaluation"};
qid = 0;
for(var key in window.q_json){
if (window.q_json.hasOwnProperty(key)){
var q = window.q_json[key];
var values = q["values"];
var question_type = q["type"];
var default_value = q["default"];
if (question_type == "multiple_choice") {
var values = q["values"];
option = $('#question-' + qid + " option:selected");
result[key] = option.text();
}
else if (question_type == "text") {
text = $('#question-' + qid);
result[key] = text.val();;
}
qid += 1;
}
}
window.crossroads[window.crossroads_index[window.current_id]].push(result);
}
function has_evaluation_crossroad(crossroad) {
for (var idc in crossroad) {
if (crossroad[idc]["type"] == "evaluation")
return true;
}
return false;
}
function has_evaluation_current_crossroad() {
return has_evaluation_crossroad(window.crossroads[window.crossroads_index[window.current_id]]);
}
function get_crossroad_evaluation(crossroad, key) {
for (var idc in crossroad) {
if (crossroad[idc]["type"] == "evaluation")
return crossroad[idc][key];
}
return null;
}
function get_current_crossroad_evalution(key) {
return get_crossroad_evaluation(window.crossroads[window.crossroads_index[window.current_id]], key);
}
function set_question(history_state = true) {
if (history_state)
history.pushState({question: window.current_id}, "titre " + window.current_id, "#" + window.current_id);
$("#nb_processed").text(window.current_id);
update_question_interface();
}
function download(evt) {
var a = document.createElement("a");
var file = new Blob([JSON.stringify(window.crossroads)], { type: "text/plain;charset=utf-8" });
a.href = URL.createObjectURL(file);
var d = new Date();
a.download = window.input_filename.replace(/\.[^/.]+$/, "") + "-evaluation-" + d.toISOString() + ".json";
a.click();
}
// init interface
function start_questions() {
$("#main-container>div").css("display", "none");
$("#question").css("display", "block");
window.current_id = 0;
set_question();
}
function ask_settings(history_state = true) {
if (exists_evaluated_crossroad()) {
if (history_state)
history.pushState({question: "all"}, "all", "#all");
$("#main-container>div").css("display", "none");
$("#settings").css("display", "block");
}
else {
ask_shuffle_or_not();
}
}
function build_index() {
window.crossroads_index = Array.from(Array(window.crossroads.length).keys());
}
function restart() {
$("#main-container>div").css("display", "none");
$("#start").css("display", "block");
}
function the_end() {
$("#main-container>div").css("display", "none");
$("#the_end").css("display", "block");
}
function ask_shuffle_or_not(history_state = true) {
if (history_state)
history.pushState({question: "shuffle"}, "shuffle", "#shuffle");
$("#main-container>div").css("display", "none");
$("#settings2").css("display", "block");
}
function exists_evaluated_crossroad() {
return window.crossroads.filter(function(item){
return has_evaluation_crossroad(item);
}).length > 0;
}
function remove_evaluted_crossroads() {
window.crossroads_index = window.crossroads_index.filter(function(item) {
return ! has_evaluation_crossroad(crossroads[item]);
});
$("#nb_crossroads").text(window.crossroads_index.length);
$("#settings_nb_crossroads").text(window.crossroads_index.length);
nb = window.crossroads.length - window.crossroads_index.length;
$("#prev_crossroads").text(" and " + nb);
}
function set_title() {
$("h1").text(Settings.title_evaluation_page);
document.title = Settings.title_evaluation_page;
}
function set_intro() {
$("#intro").html(Settings.intro_evaluation_page);
}
$(document).ready(function () {
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('tool')) {
tool = urlParams.get('tool');
}
else {
console.log("cannot identify a tool, select the default one");
tool = "evaluate-segmentation";
}
const script = document.createElement('script');
script.src = 'js/' + tool + '.js';
script.id = 'evaluation-parameters';
document.body.appendChild(script);
script.onerror = function() {
$("#main-container>div").css("display", "none");
$("#error-script").css("display", "block");
}
script.onload = () => {
set_title();
set_intro();
$("#next").click(function(evt) {
set_current_crossroad_evaluation();
window.current_id += 1;
if (window.crossroads_index.length == window.current_id) {
the_end();
}
else {
set_question();
}
});
$("#prev").click(function(evt) {
set_current_crossroad_evaluation();
window.current_id -= 1;
set_question();
});
window.addEventListener('popstate', function(event) {
if (event.state == null) {
restart();
}
else {
question = event.state.question;
if (typeof question !== "undefined") {
if (question == "all") {
ask_settings(false);
}
else if (question == "shuffle") {
ask_shuffle_or_not(false);
}
else {
window.current_id = question;
set_question(false);
}
}
else {
restart();
}
}
}, false);
window.addEventListener('pushstate', function(event) {
if (event.state == null) {
console.log("status ?");
}
else {
question = event.state.question;
if (typeof question !== "undefined") {
if (question == "all") {
ask_settings(false);
}
else if (question == "shuffle") {
ask_shuffle_or_not(false);
}
else {
window.current_id = question;
set_question(false);
}
}
else {
console.log("status ?");
}
}
}, false);
$("#download").click(download);
$("#download_end").click(download);
$("#regular").click(function(evt) {
start_questions();
});
$("#shuffle").click(function(evt) {
shuffle(window.crossroads_index);
start_questions();
});
$("#evaluate-all").click(function(evt) {
ask_shuffle_or_not();
});
$("#evaluate-blank").click(function(evt) {
remove_evaluted_crossroads();
ask_shuffle_or_not();
});
$("#inputFile").on("change", function handleFileSelect(evt) {
if (evt == null || evt.target == null || evt.target.files == null || evt.target.files.length == 0) {
console.log("no input file");
return;
}
var file = evt.target.files[0];
if (file.type != "application/json") {
console.log("bad format");
return;
}
const fr = new FileReader();
fr.addEventListener("load", e => {
$("#main-container>div").css("display", "none");
$("#chargement").css("display", "block");
// load data
window.crossroads = JSON.parse(fr.result);
// TODO: test if the json is a valid crossroad list
// set numbers
$("#nb_crossroads").text(window.crossroads.length);
$("#settings_nb_crossroads").text(window.crossroads.length);
$("#nb_processed").text(0);
build_index();
ask_settings();
});
fr.readAsText(file);
window.input_filename = evt.target.files[0].name;
});
build_question_interface();
};
});
</script>
<style>
#question, #chargement, #settings, #settings2, #the_end, #error-script { display: none; }
#crossroad-rendering { height: 100%; }
.badge a {
color: #eee;
text-decoration: none;
}
.badge:hover a {
color: #fff;
}
.badge:hover {
background: #888 !important;
}
</style>
</head>
<body>
<div class="container-xxl" id="main-container">
<h1>Evaluation tool</h1>
<p class="lead" id="intro"></p>
<div id="start">
<div class="position-absolute top-50 start-50 translate-middle fs-1">
<p>Select a <code>json</code> file</p>
<div class="btn btn-primary btn-block" style="width: 100%">
<label style="padding: 0" tkey="chooseFile">Load crossroads</label>
<input type="file" id="inputFile" name="files[]" accept="application/json" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; cursor: pointer; opacity: 0">
</div>
</div>
</div>
<div id="chargement">
<div class="position-absolute top-50 start-50 translate-middle fs-1">Loading data...</div>
</div>
<div id="settings">
<div class="position-absolute top-50 start-50 translate-middle fs-1 col-6">
<p>Some crossroads has been already evaluated. Do you want to reevaluate them?</p>
<div class="container mt-3">
<div class="row">
<div class="col-6">
<button id="evaluate-all" class="btn btn-primary btn-block form-control" type="button">Evaluate all crossroads</button>
</div>
<div class="col-6">
<button id="evaluate-blank" class="btn btn-primary btn-block form-control" type="button">Evaluate only blank crossroads</button>
</div>
</div>
</div>
</div>
</div>
<div id="settings2">
<div class="position-absolute top-50 start-50 translate-middle fs-1 col-6">
<p>Do you want to shuffle the <span id="settings_nb_crossroads"></span> crossroads, or evaluate them in regular order?</p>
<div class="container mt-3">
<div class="row">
<div class="col-6">
<button id="regular" class="btn btn-primary btn-block form-control" type="button">Regular</button>
</div>
<div class="col-6">
<button id="shuffle" class="btn btn-primary btn-block form-control" type="button">Shuffle</button>
</div>
</div>
</div>
</div>
</div>
<div id="question">
<div class="card">
<div class="card-body">
<h2 class="card-title"><span id="crossroad_title"></span> <span id="crossroad_status"></span> <span id="crossroad_link"></span></h2>
<div class="row">
<div class="col-6" id="questions">
</div>
<div class="col-6">
<div id="crossroad-rendering"></div>
</div>
</div>
</div>
</div>
<div class="container-xxl mt-3">
<div class="row">
<div class="col-6">
<button id="download" class="btn btn-secondary" style="width: 100%" type="button" disabled="true">Download evaluations (<span id="nb_processed">0</span> / <span id="nb_crossroads">-</span><span id="prev_crossroads"></span>)</button>
</div>
<div class="col-3">
<button id="prev" class="btn btn-primary" style="width: 100%" type="button">Previous</button>
</div>
<div class="col-3">
<button id="next" class="btn btn-primary" style="width: 100%" type="button">Next</button>
</div>
</div>
</div>
</div>
<div id="the_end">
<div class="position-absolute top-50 start-50 translate-middle fs-1">
<p>Download <code>json</code> file</p>
<button id="download_end" class="btn btn-primary" type="button" style="width: 100%">Get evaluations</button>
<p>Come back to the <a href="index.html">main evaluation page</a>.</p>
</div>
</div>
<div id="error-script">
<div class="position-absolute top-50 start-50 translate-middle fs-1">Error while loading tool</div>
</div>
</div>
</body>
</html>