-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
195 lines (154 loc) · 6.26 KB
/
script.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
let stats = {};
let allStats = [];
let allData;
let index = -1;
window.onload = async () => {
let params = new URLSearchParams(window.location.search);
let dataPath = params.get('data') ? params.get('data') : 'data.json';
let response = await fetch(dataPath);
allData = await response.json();
document.addEventListener('keydown', (e) => {
if (e.code == 'Enter') {
this.submitDescription();
}
});
document.getElementById('ctx_url').addEventListener('click', (e) => {if (!stats.clickSourceTime) stats.clickSourceTime = new Date().getTime();});
document.addEventListener('blur', () => {
if (!stats.leftWindowTime) stats.leftWindowTime = new Date().getTime();
});
showNextEntry();
}
function clearFields() {
stats = {};
// hide revealed content
document.getElementById('ctx').hidden = true;
// clear the input field
document.getElementById('user_description').value = "";
// clear tags
let ctxTags = document.getElementById('ctx_tags');
while (ctxTags.firstChild) {
ctxTags.removeChild(ctxTags.firstChild);
}
let imgTags = document.getElementById('img_tags');
while (imgTags.firstChild) {
imgTags.removeChild(imgTags.firstChild);
}
}
function showNextEntry() {
index += 1;
clearFields();
if (index >= allData.length) {
console.log('completed final task! Showing finished view.');
document.getElementById('task-view').hidden = true;
document.getElementById('finished-view').hidden = false;
document.getElementById('result-object-final').innerText = JSON.stringify(allStats, undefined, 2);
return;
}
let data = allData[index];
// place all values into the correct HTML fields
console.log(data);
console.log(Object.keys(data));
for (let key of Object.keys(data)) {
if (data[key] instanceof Array) {
// append with font size depending on relative score
let minScore = data[key][data[key].length - 1].score;
let maxScore = data[key][0].score;
let fontSizes = data[key].map((entry, index) => {
return mapNumberToRange(entry.score, minScore, maxScore, 16, 28) + 'px';
});
// create and append list items
let items = data[key].map((entry, index) => {
let listItem = document.createElement('li');
console.log('setting font size of', entry.tag, 'to', fontSizes[index]);
listItem.style.fontSize = fontSizes[index];
listItem.appendChild(document.createTextNode(entry.tag));
return listItem;
});
items.forEach((item) => {
document.getElementById(key).appendChild(item);
});
} else if (key.includes('url')) {
document.getElementById(key).href = data[key];
} else if (key === 'img') {
// image is source
document.getElementById('img').src = data[key];
} else {
// just set the inner text
document.getElementById(key).innerText = data[key];
}
}
document.getElementById('submit_button').className = "btn btn-info";
// start the timer
stats.title = data['ctx_title'];
stats.startTime = new Date().getTime();
}
function revealOrHideAutomaticDescription() {
const descriptionButton = document.getElementById('button-show-description');
const caption = document.getElementById('img_caption');
console.log('Toggling description!');
caption.hidden = !caption.hidden;
// set button text appropriately
descriptionButton.innerText = caption.hidden ? 'Klik om te tonen' : 'Klik om te verbergen';
// timestamp is only set the first time
if (!stats.revealDescriptionTime) stats.revealDescriptionTime = new Date().getTime();
}
function showOrHideFullContext() {
const contextButton = document.getElementById('button-show-context');
const context = document.getElementById('ctx');
console.log('Toggling context!');
context.hidden = !context.hidden;
// set button text appropriately
contextButton.innerText = context.hidden ? 'Klik om te tonen' : 'Klik om te verbergen';
// timestamp is only set the first time
if (!stats.revealContextTime) stats.revealContextTime = new Date().getTime();
}
function showCopyWarningToast() {
console.warn('showing copy warning toast');
if (!stats.attemptCopyTime) stats.attemptCopyTime = new Date().getTime();
$('#toast-warn-copy').toast('show');
setTimeout(() => {
$('#toast-warn-copy').toast('hide');
}, 2000);
navigator.clipboard.writeText('');
return false;
}
async function submitDescription() {
let description = document.getElementById('user_description').value;
if (!description || description.length < 2) {
console.warn('Submitted description was not present or too short!');
$('#toast-warn-short').toast('show');
setTimeout(() => {
$('#toast-warn-short').toast('hide');
}, 2000);
return;
}
document.getElementById('submit_button').className = "btn btn-success";
console.log('Submitting description');
stats.endTime = new Date().getTime();
stats.description = description;
allStats.push(stats);
showNextEntry();
}
// utility function to map numbers (for font size)
// retrieved from https://gist.github.com/xposedbones/75ebaef3c10060a3ee3b246166caab56
function mapNumberToRange(number, in_min, in_max, out_min, out_max) {
return Math.round((number - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
}
function downloadData() {
console.log('downloading json...');
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(allStats)));
element.setAttribute('download', 'result.json');
// add the new element to the page
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function copyData() {
navigator.clipboard.writeText(JSON.stringify(allStats));
$('#toast-copy').toast('show');
setTimeout(() => {
$('#toast-copy').toast('hide');
}, 2000);
}