-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresenter.html
321 lines (271 loc) · 11.1 KB
/
presenter.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>CodeSynthViewer</title>
<script src="js/jquery.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<link rel="stylesheet" href="css/jquery-ui.css" />
<script src="js/seedrandom.min.js"></script>
<link href="css/solarized.css" rel="stylesheet"/>
<link href="css/blackboard.css" rel="stylesheet"/>
<script type="text/javascript" src="stimuli.json"></script>
<style type="text/css">
#rest_div {
margin:auto;
width:100%;
height:100%;
}
#question {
font-size:36px;
}
.CodeMirror {
font-size:36px;
}
</style>
<script type = "text/javascript">
// Print the loaded stimuli to console
console.log("Loaded stimuli:")
console.log(stimuli)
var min_rest_time = 0.3
var max_rest_time = 0.9
// Helper function
function add(a, b) {
return a + b;
}
// Returns a random number between min (inclusive) and max (exclusive)
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
// Returns num_rands values that sum to val. Used for generating variable rest times between stimuli
function getRandsThatSumTo(num_rands, val) {
var rands = [];
var i;
for (i = 0; i < num_rands; i++) {
rands[i] = getRandomArbitrary(min_rest_time, max_rest_time);
}
var sum = rands.reduce(add, 0);
var to_return = [];
for (i = 0; i < num_rands; i++) {
to_return[i] = rands[i]/sum * val;
}
return to_return;
}
var plusDelay = 0;
var has_started = false;
var stopped = true;
var record_keystrokes = false;
var rest_times = []
var curr_index = -1
var avg_rest_time = 5000;
window.onload = function () {
// Instantiate CodeMirror object
var editableCodeMirror = CodeMirror.fromTextArea(document.getElementById('codesnippet_editable'), {
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true,
mode: "text/plain", // determines the syntax highlighting scheme
theme: "blackboard", // determines coloring. Each theme is accompanied by a CSS file. We chose blackboard.
lineWrapping: true,
autofocus: true
});
editableCodeMirror.setSize(null, $(window).height()-200);
editableCodeMirror.setOption("theme", "blackboard");
editableCodeMirror.setOption("mode", "text/plain");
// Seed the random number generator with the participant ID
// Then, populate rest times
$.ajax({
type: "GET",
url: "http://localhost:3000/get_part_id",
dataType: "json"
}).done (function (data) {
console.log("Populating rest times...")
Math.seedrandom((data.id).toString());
rest_times = getRandsThatSumTo(data.num_stim, data.num_stim * avg_rest_time)
console.log("Rest times:")
console.log(rest_times);
});
};
$(document).ready(function() {
// Method called upon keystroke detection
// Once the start key has been pressed, record all keystrokes.
// Stop recording keystrokes after experiment is done
$(document).keydown(function(event) {
// console.log("Detected keystroke")
// If the experiment has not started, check if the pressed key was the start key
if (!has_started) {
console.log("Has not started")
// key code corresponds to "=" -- 61 on Firefox, 187 otherwise
// If start key detected, start the experiment. Triggers first call to displayNextStimulus
if (event.keyCode == 48) { // default is 187
console.log("Start key detected")
var the_url ="http://localhost:3000/start";
$.ajax(
{ url: the_url, complete: function() {} }
);
has_started = true;
stopped = false;
startTimer();
}
} else if (!stopped) {
// If the experiment has been started and has not yet completed, record the keystroke
if (record_keystrokes) {
console.log("recording keystroke")
var my_url ="http://localhost:3000/keystroke?key="+event.which;
$.ajax(
{ url: my_url, complete: function() {} }
);
}
}
});
// Prepare fixation focal relax
$("#plus_img").width($(window).width());
$("#plus_img").height($(window).height());
$("#plus_img").show();
$('#codesnippet_editable').focus();
$("#textarea_div").height($(window).height() - 100);
$("#codesnippet_editable").css("height", "100%");
});
// Enum for clarity
const Categories = Object.freeze({
ProseWritingFITB: 0,
ProseWritingLR: 1,
CodeWritingFITB: 2,
CodeWritingLR: 3
});
var stimulus_time = 30000 // arbitrary initialization
var counter = 0;
var remember_category = 0;
// Core function -- handles the current stimulus and communicates with the server to retrieve the next one
// The server handles the logic of when the experiment is over (when we have met NUM_STIMULI). This function also handles said decision.
function displayNextStimulus() {
console.log("Displaying next stimulus...")
// Get Code Mirror Object
var editor = $('.CodeMirror')[0].CodeMirror;
var cm = editor.getWrapperElement();
// Hide Code Mirror object and make sure not recording keystrokes
$(cm).readOnly = true;
$(cm).hide();
record_keystrokes = false;
// Record previous answer (regardless of if we should stop!)
var text = editor.getValue();
var prompt = document.getElementById('question').innerHTML;
if (prompt != "Loading..." && counter != 0) {
var query = "http://localhost:3000/write_answer?area=" + encodeURIComponent(text) + "&prompt=" + encodeURIComponent(prompt) + "&index=" + curr_index + "&prev_delay=" + plusDelay + "&stimulus_time=" + stimulus_time;
$.ajax({
type: "GET",
url: query,
dataType: "json"
});
}
// Make GET request for new stimuli
var next_stim_query = "http://localhost:3000/nextstim"
$.ajax({
type: "GET",
url: next_stim_query,
dataType: "json"
}).done (function (data) {
// Handle server's determination on whether or not the experiment is over
if (data.stop) {
// If we finished the current experiment (i.e., the current category), keep the fixation cross shown AND don't make another displayNextStimulus call
console.log("You have completed this section")
$("#plus_img").show();
$("#plus_img").hide();
$("#question").hide();
var para = document.createElement("p");
var node = document.createTextNode("You have completed section: " + remember_category);
para.appendChild(node);
var element = document.getElementById("over");
element.appendChild(para);
stopped = true;
counter = 0;
} else { // Continue with the experiment if we should not stop
// Get the next rest time
plusDelay = rest_times[counter]
// If it is the first stimulus, add the designated amount of time
if (data.number_stim == 0) {
first_stim_buffer = 10000 // add 10s to first plus delay of category
plusDelay += first_stim_buffer
}
console.log("Rest time is", plusDelay)
stimulus_time = data.time;
// console.log(stimulus_time)
// Notify the server that we are starting a new stimulus for keystroke-recording purposes
var stim_marker_url ="http://localhost:3000/keystroke_stim_marker";
$.ajax(
{ url: stim_marker_url, complete: function() {} }
);
// Show the fixation cross for plusDelay amount of time
$("#plus_img").show().delay(plusDelay).queue(
function(n) {
$(this).hide();
$(cm).readOnly = false;
$(cm).show();
editor.refresh();
console.log("Setting to true")
record_keystrokes = true;
counter += 1;
n();
}
);
// Queue the next call to displayNextStimulus to be aligned with the fixation cross timing (via plusDelay)
$(document).delay(stimulus_time + plusDelay).queue(
function(n) {
displayNextStimulus();
n();
}
);
// Get contents of next stimulus contents from GET request
stimulus = stimuli[data.category][data.index]
console.log(stimulus);
// Save the stimulus details for when we want to write the participant's answer to file
curr_index = data.index;
remember_category = data.category;
// Set the CodeMirror syntax highlighting for the stimulus
// Permits multiple styles of stimuli in a single experimental block
if (data.category == Categories.ProseWritingFITB || data.category == Categories.ProseWritingLR) {
editor.setOption("mode", "text/plain");
} else {
editor.setOption("mode", "text/x-c++src");
}
// Replace the HTML contents with the new stimulus
document.getElementById("question").innerHTML = stimulus.prompt;
editor.setValue(stimulus.text);
editor.focus()
// Place the cursor in the position associated with the new stimulus
// Each stimulus includes information on where to place the cursor
editor.setCursor({line: stimulus.line, ch: stimulus.character})
editor.refresh();
editor.focus()
}
}
);
}
// Function called to trigger the beginning of the expeirment (via first call to displayNextStimulus)
function startTimer() {
console.log("Starting timer")
var editor = $('.CodeMirror')[0].CodeMirror;
editor.focus()
displayNextStimulus()
}
</script>
</head>
<body>
<p id="question">Loading...</p>
<link rel="stylesheet" href="codemirror/lib/codemirror.css">
<script src="codemirror/lib/codemirror.js"></script>
<script src="codemirror/addon/edit/matchbrackets.js"></script>
<script src="codemirror/addon/edit/closebrackets.js"></script>
<script src="codemirror/addon/edit/continuecomment.js"></script>
<script src="codemirror/mode/javascript/javascript.js"></script>
<script src="codemirror/mode/clike/clike.js"></script>
<div>
<textarea rows="4" cols="50" name="codesnippet_editable" id="codesnippet_editable" autofocus>Please wait</textarea>
<div id="rest_div" style="display: none;" align="center">
<p style="color:red;font-size:256px;">+</p>
</div>
</div>
<div id="plus_img" style="position:absolute;top:50%;height:100%;left:50%;z-index:1000;font-size:500px;color:red;text-align:center;width:100%;margin:auto;
background-color:white;vertical-align:middle;transform: translate(-50%, -50%);">+</div>
<div id="over"></div>
</body>
</html>