-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.js
More file actions
386 lines (285 loc) · 9.52 KB
/
function.js
File metadata and controls
386 lines (285 loc) · 9.52 KB
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
var now = new Date(); // current date time
const MISSING_VALUE = "NaN"
const VALID_ID = "yes"
const INVALID_ID = "no"
const OUTNAME = "responses_" + now.toLocaleDateString() + ".csv"
const LANGUAGE = "it-IT"
const links = []
let slideIndex = 0;
var responses = {};
let directory = "";
let start = 0;
let stop = Math.floor((0.875 * window.screen.availWidth) * window.devicePixelRatio * .01);
let N = stop; // initial value as fixed
let invalid_cnt = 0;
let valid_cnt = 0;
let none_cnt = -1;
/********* Set text Button ******************/
document.getElementById('title').innerHTML = translations[LANGUAGE]["title"];
document.getElementById('loader').innerHTML = translations[LANGUAGE]["loader_btn"];
document.getElementById('button_no').innerHTML = translations[LANGUAGE]["invalid_btn"];
document.getElementById('button_yes').innerHTML = translations[LANGUAGE]["valid_btn"];
document.getElementById('button_upload').innerHTML = translations[LANGUAGE]["download_btn"];
document.getElementById('tooltip_text').innerHTML = translations[LANGUAGE]["help_msg"];
/********* Callback functions ***************/
// Upload directory button
document.getElementById("filepicker").addEventListener("change", function(event) {
if (directory == "") {
directory = event.target;
}
// hide the uploader button
document.getElementById("initial").style.display = "none";
// get the list of files
let files = directory.files;
// append it to the global LINKS array
for (let i=0; i<files.length; i++) {
links.push(files[i].webkitRelativePath);
};
LoadFiles(directory);
}, false);
// KeyBoard shortcuts
document.addEventListener('keydown', (event) => {
switch(event.code) {
// If left arrow is pressed
case "ArrowLeft":
plusSlides(-1);
break;
// If right arrow is pressed
case "ArrowRight":
plusSlides(+1);
break;
case "KeyS":
document.getElementById('button_yes').click();
break;
case "KeyN":
document.getElementById('button_no').click();
break;
case "Space": {
var current = document.getElementById('expandedImg');
var filename = current.name;
// turn-off border
current.style.borderColor = "#333";
if (responses[filename] == VALID_ID) {
valid_cnt -= 1;
none_cnt += 1;
} else if (responses[filename] == INVALID_ID) {
invalid_cnt -= 1;
none_cnt += 1;
}
document.getElementById("none_counter").innerHTML = "Blanck: " + none_cnt;
document.getElementById("invalid_counter").innerHTML = "Invalid: " + invalid_cnt;
document.getElementById("valid_counter").innerHTML = "Valid: " + valid_cnt;
responses[filename] = MISSING_VALUE;
} break;
case "Enter":
document.getElementById('loader').click();
break;
case "Escape": {
document.getElementById('button_upload').click();
close();
} break;
}
}, false);
// Callback for INVALID button
document.getElementById('button_no').addEventListener('click', (event) => {
// Get the image name
var current = document.getElementById('expandedImg');
var filename = current.name;
// highlight border
current.style.borderColor = "red";
// switch case for the update of the counters
if (responses[filename] == VALID_ID) {
valid_cnt -= 1;
invalid_cnt += 1;
} else if (responses[filename] == INVALID_ID) { // it is already an invalid image
} else { // it is an unknown image
none_cnt -= 1;
invalid_cnt += 1;
}
document.getElementById("none_counter").innerHTML = "Blanck: " + none_cnt;
document.getElementById("invalid_counter").innerHTML = "Invalid: " + invalid_cnt;
document.getElementById("valid_counter").innerHTML = "Valid: " + valid_cnt;
// Update the global dictionary
responses[filename] = INVALID_ID;
// Print message box
SnackBar(translations[LANGUAGE]['snack_no']);
}, false)
// Callback for VALID button
document.getElementById('button_yes').addEventListener('click', (event) => {
// Get the image name
var current = document.getElementById('expandedImg');
var filename = current.name;
// highlight border
current.style.borderColor = "lime";
// switch case for the update of the counters
if (responses[filename] == INVALID_ID) {
invalid_cnt -= 1;
valid_cnt += 1;
} else if (responses[filename] == VALID_ID) { // it is already an valid image
} else { // it is an unknown image
none_cnt -= 1;
valid_cnt += 1;
}
document.getElementById("none_counter").innerHTML = "Blanck: " + none_cnt;
document.getElementById("invalid_counter").innerHTML = "Invalid: " + invalid_cnt;
document.getElementById("valid_counter").innerHTML = "Valid: " + valid_cnt;
// Update the global dictionary
responses[filename] = VALID_ID;
// Print message box
SnackBar(translations[LANGUAGE]['snack_yes']);
}, false)
// Callback for UPLOAD button
document.getElementById('button_upload').addEventListener('click', (event) => {
// Create a temporary object for the download trigger
var hiddenElement = document.createElement('a');
hiddenElement.download = OUTNAME;
// start with the header file
var textToSave = 'Filename,response\n';
// Loop along the available files
// NOTE: in this way we can take care of the full list of
// available files and not just the seen by user
for (key of links) {
// get the associated response
var value = responses[key];
// If it was inserted
if (value != undefined) {
// create the file row with info to dump
textToSave += basename(key) + ',' + value + '\n';
}
// If it was NOT inserted
else {
// insert missing values
textToSave += basename(key) + ',' + MISSING_VALUE + '\n';
}
}
// append the file content
hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
// trigger a click
hiddenElement.click();
// Print message box
SnackBar(translations[LANGUAGE]['snack_download']);
}, false)
/********* Functions ***************/
// Slider for list of files <-
function MoveLeft () {
start -= 1;
stop -= 1;
if (start < 0) {
start = links.length - N;
stop = links.length;
slideIndex = stop;
}
LoadFiles(directory);
};
// Slider for list of files ->
function MoveRight () {
start += 1;
stop += 1;
if (stop > links.length - 1) {
start = 0;
stop = N;
slideIndex = start;
}
LoadFiles(directory);
};
// Update the list gallery of images
function LoadFiles (directory) {
document.getElementById('list').innerHTML = "";
// Dynamic create the image list of files
for (let i=start; i < stop; ++i) {
link = links[i];
document.getElementById('list').innerHTML += `
<img src="${link}" id="${link}" title="${link}" style="width:100px; height:100px" onclick=Enlarge(this); />
`;
}
// show the first slide
showSlides(slideIndex);
// update the counters
if (none_cnt == -1)
none_cnt = links.length
document.getElementById("none_counter").innerHTML = "Blanck: " + none_cnt;
document.getElementById("invalid_counter").innerHTML = "Invalid: " + invalid_cnt;
document.getElementById("valid_counter").innerHTML = "Valid: " + valid_cnt;
}
// Trigger the loader event
function FileLoader() {
document.getElementById('filepicker').click();
}
// Highlight the selected image
function HighlightSel (n) {
for (let i=start; i < stop; ++i) {
let id = document.getElementById(links[i]);
if (id == n) {
document.getElementById(links[i]).style.border = "3px solid yellow";
} else {
document.getElementById(links[i]).style.border = "3px solid #333";
}
}
}
// Show the slide according to list index
function showSlides(n) {
// Force the wrap around
if (n > stop - 1) {slideIndex = stop - 1;}
if (n < start) {slideIndex = start;}
// Get the element
let slides = document.getElementById(links[slideIndex]);
// Pass to the enlarge viewer
Enlarge(slides);
document.getElementById("counter").innerHTML = "Image: " + (slideIndex + 1) + "/" + links.length;
}
// Expand the given image in the viewer box
function Enlarge(imgs) {
for (let i=start; i < stop; ++i) {
let id = document.getElementById(links[i]);
if (id == imgs) {
slideIndex = i;
break;
}
}
// Highlight the selected item
HighlightSel(imgs);
// Get the expanded image
var expandImg = document.getElementById("expandedImg");
// Use the same src in the expanded image as the image being clicked on from the grid
expandImg.src = imgs.src;
// Set the name as id to use in the next save
expandImg.name = imgs.id;
// Set the title name
expandImg.title = imgs.title;
// Show the container element (hidden with CSS)
expandImg.parentElement.style.display = "block";
if (responses[expandImg.name] == INVALID_ID) {
// highlight border
expandImg.style.borderColor = "red";
} else if (responses[expandImg.name] == VALID_ID) {
// highlight border
expandImg.style.borderColor = "lime";
} else {
expandImg.style.borderColor = "#555";
}
}
// Next/previous controls
function plusSlides(n) {
slideIndex += n;
if (slideIndex >= stop) {
MoveRight();
} else if (slideIndex < start) {
MoveLeft();
} else {
showSlides(slideIndex);
}
}
// Get file basename
function basename(path) {
return path.split('/').reverse()[0];
}
// SnackBar helper
function SnackBar(msg) {
// Get the snackbar DIV
var x = document.getElementById("snackbar");
x.innerHTML = msg;
// Add the "show" class to DIV
x.className = "show";
// After 3 seconds, remove the show class from DIV
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 1000);
}