-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectSort.html
More file actions
516 lines (344 loc) · 13.1 KB
/
ObjectSort.html
File metadata and controls
516 lines (344 loc) · 13.1 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sort Objects</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>
<script>
//SET GLOBAL VARIABLES
var TURK = 0;
// list of image filenames
var IMAGE_FILES = ['objC1_dark-01.jpg','objB1_dark-01.jpg','objA1_dark-01.jpg','objD1_dark-01.jpg','objE1_dark-01.jpg','objJ1_dark-01.jpg'];
//where stimuli live on the internet
var URL_PATH = 'https://www.sas.upenn.edu/~alesh/images/SLSTIM2/'
// list of possible positions for the images (numbers should match)
var LEFT_POS = [100,225,350,475,600,725];
var TOP_POS = [50,50,50,50,50,50]
var SCREEN_WIDTH = 0;
var CENTER_POS = 0;
var CONTINUE_POS = 600; //position of continue button; this can be changed dynamically
// ---- INITIALIZE ------ //
// first function call when page is loaded
window.onload = function() {
// get width of user's screen
SCREEN_WIDTH = $(window).width();
// get horizontal center coordinates
CENTER_POS = (SCREEN_WIDTH/2);
// hide the submit button if on turk
if (TURK==1) {
document.getElementById('submitButton').style.display="none"
}
// show first instruction screen and wait for start button press
$(document.getElementById("instructions0")).show();
switchContinueButtonLink('')
};
// ---- UTILITY FUNCTIONS------ //
// takes an array of elements and sets their left-right positions
function centerAll(arr,centerInd){
l = arr.length;
for (i = 0; i < l; i++) {
w = $(arr[i]).width()
// console.log(w)
arr[i].style.left = (centerInd-(w/2))+'px';
}
}
function randomNubers(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
new_run = permArr.join()
allRuns = new_run.split(',')
return allRuns;
};
function chunkArray(myArray,arraySize) {
// var RUN_ORDER_ARRAY = [];
while (myArray.length) {
RUN_ORDER_ARRAY.push(myArray.splice(0, arraySize));
}
return RUN_ORDER_ARRAY;
}
function permuteThis(input,inputLength) {
RUN_ORDER_ARRAY = [];
permute(input);
var outputArray = chunkArray(allRuns,inputLength);
return outputArray
}
function waitForTrigger(functionName) {
window.onkeydown = function (e) {
var code = e.keyCode ? e.keyCode : e.which;
if ( code == 84 ) { //84 = 't'
setDefaultKeyResponse()
eval(functionName);
}
}
};
function switchContinueButtonLink(functionName){
// console.log(functionName)
if(functionName==''){
// c.setAttribute("style","display: none; position: fixed; top: 250; left: 250; font-family: myriad; font-size: 18px")
$(document.getElementById("Continue")).hide();
}else{
sc = (SCREEN_WIDTH/2)-125
$(document.getElementById("Continue")).show();
c = document.getElementById("Continue");
c.setAttribute("style","display: block; position: absolute; top:"+CONTINUE_POS+"px; left: "+sc+"px; font-family: myriad; font-size: 18px")
c.setAttribute('onclick',functionName);
// c = document.getElementById("Continue_target");
// c.setAttribute("href",functionName);
// $(document.getElementById("Continue")).show();
;
}
};
// function to shuffle order of an array
shuffle = function(o) {
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
function isMember(set,item){
var ismem = false;
for(var i = 0, l = set.length; i < l; i++) {
if (item==set[i]){
ismem = true;
}
}
return ismem;
};
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function arraySort(inds,oldArray) {
var newArray = [];
for( var i = 0, l = oldArray.length ; i < l ; i++ ) {
newArray[i] = oldArray[inds[i]];
}
return newArray
}
function sortBy(inds,oldArray) {
var newArray = [];
var j = 0;
for( var i = 0, l = inds.length ; i < l ; i++ ) {
newArray[j] = oldArray[inds[i]];
newArray[j+1] = oldArray[inds[i]+4];
j = j+2;
}
return newArray
}
function mergeTrainAndFam(train,fam) {
console.log("*");
var j = 0;
for (var i = 2; i <= train.length; i=i+3) {
train.splice(i,0,fam[j]);
j++;
}
console.log(train);
}
function calcSum(set) {
sum = 0;
for(var i = 0, l = set.length; i < l; i++) {
sum=sum+set[i];
}
return sum;
}
function writeToForm(value,name){
newElem = document.createElement("div");
newElem.id= name;
if (value instanceof Array) {
newElem.innerHTML = '<textarea name="'+name+'" style="display:none">' + value.join(',') +'</textarea>'
}
else {
newElem.innerHTML = '<textarea name="'+name+'" style="display:none">' + value +'</textarea>'
}
if(TURK==1){
document.forms[0].appendChild(newElem);
} else {
document.body.appendChild(newElem);
}
};
function setDefaultKeyResponse(){
window.onkeydown = function (e) {
var code = e.keyCode ? e.keyCode : e.which;
if ( code == 13 ) { return false; } //enter key
if (code == 187 ) { $(document.getElementById('Continue')).click(); }
if (code == 192 ) { endTask()} //`~ key
};
}
function hideAllInstructions(){
//get all elements with the 'instructions' tag
var elems = $(document.getElementsByClassName("instr2"));
//hide them
elems.hide();
var elems2 = $(document.getElementsByClassName("instr1"));
//hide them
elems2.hide();
};
// function to start and stop keypress recording
function toggleKeyResponse(stepNum){
//if the step number that was passed is > 0
if(stepNum>=0) {
// give window a keydown function
window.onkeydown = function (e) {
var code = e.keyCode ? e.keyCode : e.which;
// console.log(code)
if ( code == 84 ) { return false; } //trigger: explicitly ignore
// if ( code == 13 ) { return false; } // enter key
if (code == 187 ) { return false; } //equal sign
else { //any other key
KEYPRESSES[stepNum] = code;
// console.log(ONSETS[stepNum])
RT[stepNum] = new Date().getTime()-ONSETS[stepNum]; //from trial onset
console.log(RT)
toggleKeyResponse(-1); //stop keypress recording
};
};
} else {
//remove recording functionality
setDefaultKeyResponse()
};
};
function removeNaNs(arr){
var newArr= [];
count = 0;
for(var i = 0, l = arr.length; i < l; i++) {
if(!isNaN(arr[i])){
newArr[count] = arr[i]
count++ }
}
return newArr;
}
function remove99(arr){
var newArr= [];
count = 0;
for(var i = 0, l = arr.length; i < l; i++) {
if(!(arr[i]==99)){
newArr[count] = arr[i]
count++ }
}
return newArr;
}
function shuffle2(obj1, obj2) {
var index = obj1.length;
var rnd, tmp1, tmp2;
while (index) {
rnd = Math.floor(Math.random() * index);
index -= 1;
tmp1 = obj1[index];
tmp2 = obj2[index];
obj1[index] = obj1[rnd];
obj2[index] = obj2[rnd];
obj1[rnd] = tmp1;
obj2[rnd] = tmp2;
}
}
// -------------------------------
// MAIN TASK
//present a screen with each object image, making them draggable, and ask subjects to spatially sort them
function objectSort(){
$(document.getElementById("instructions0")).hide();
$(document.getElementById("acceptButton")).hide();
setDefaultKeyResponse()
//randomize positions (along horizontal axis)
shuffle2(LEFT_POS, TOP_POS);
document.getElementById('instructions_objSort').style = 'display:block; width: 250px; position: absolute; top: 10px'
// $(document.getElementById('instructions_objSort')).show()
nImages = IMAGE_FILES.length
for (var i = 0; i < nImages; i++) {
curName = IMAGE_FILES[i]
console.log(curName)
newElem=document.createElement("div");
newElem.innerHTML = "<img width=100 height=100 src="+URL_PATH+curName+">";
newElem.id = curName;
newElem.setAttribute("class","ui-widget-content");
newElem.setAttribute("style", "width: 100px; height: 100px; padding: 0.5em; position: fixed; left: " + (200+LEFT_POS[i]) + "px; top: " + TOP_POS[i] + "px; margin: 0 10px 10px 0; font-size: 12pt")
document.body.appendChild(newElem);
$(document.getElementById(curName)).draggable();
}
switchContinueButtonLink('endObjectSort()');
};
function endObjectSort(){
//check whether every item has been placed
if (checkPlaced()){
//hide all the elements
$(document.getElementById('instructions_objSort')).hide()
hideAndSave();
askDemographics();
}else {
alert('Please place each of the items!')
};
}
function checkPlaced(){
// for each item in IMAGE_FILES, check that it is not in its exact original position
nImages = IMAGE_FILES.length
for (var i = 0; i < nImages; i++) {
curElem = document.getElementById(IMAGE_FILES[i]);
if(curElem.style.left==(200+LEFT_POS[i])+"px" && curElem.style.top==TOP_POS[i]+"px"){
return false
}
}
return true
};
function hideAndSave(){
nImages = IMAGE_FILES.length
// for each item in IMAGE_FILES, read its position and save to form
for (var i = 0; i < nImages; i++) {
curItemName = IMAGE_FILES[i];
curElem = document.getElementById(curItemName);
$(document.getElementById(curItemName)).hide();
curPos = [curElem.style.left,curElem.style.top];
writeToForm(curPos,curItemName+"_position")
};
};
function askDemographics(){
$(document.getElementById('demographicQ')).show();
switchContinueButtonLink('javascript: endTask()');
}
function endTask() {
if(TURK==0){
alert("Thanks for your responses!")
console.log("end!")
}
else{
alert("Thanks for your responses! Please hit the 'submit' button to finish the HIT.")
document.getElementById('submitButton').style.display="block" //reveal the submit button
}
}
</script>
</head>
<body bgcolor="#35354c" >
<!-- consent form -->
<div id="instructions0" style="width:800px; height: 800px; position: fixed;overflow: scroll;">
<p><span style= " color:white;font-weight:bold;font-size: 16pt; font-family: myriad;align-text:center ">Consent Form: please scroll to view all. Clicking 'Accept' indicates that you agree to participate (in lieu of a signature).</span></p>
<img src="https://www.sas.upenn.edu/~alesh/images/SLSTIM2/Conset_mturk_2019-01.jpg" width=800px>
</div>
<div id="acceptButton" style = "display:block; position:relative; left: 850px; top: 100px; width:1200px; height: 1200px;">
<button id="startButton" type="button" onclick = "javascript:objectSort()" style = "position: relative; top: 100px; font-family: myriad; font-size:18px"> ACCEPT </button><p><br><br><br>    </p>
<br>
</div>
<span><button id="Continue" type="button" onclick = "javascript:StartTask()" style= 'display: none' > Continue </button></span>
<div id="instructions_objSort" style= "width:600px; display:none; position: absolute; left: 50px; top: 10px;align-text:center;color: white;font-size: 14pt;font-family: myriad"><p><center> <span style= "color:white;font-weight:bold;font-size: 16pt; font-family: myriad;align-text:center"> You learned about 12 objects by watching animations involving them. We are interested in which objects you think are most similar to each other in terms of what you learned. Arrange the object images on the screen so that the ones you think are more similar are closer together, and the ones that are more different are farther apart. They should be as far apart as they are different. Please move each element away from its initial position in order to continue.</b> <br><br></center></p></div>
<!-- demographics DIVs -->
<div id='demographicQ' style= 'display:none; position: absolute; top: 100px; left: 80px; font-family: myriad; font-size: 16pt'> <span style= "color:white;font-weight:bold;font-size: 16pt; font-family: myriad;align-text:center; ">Demographic questions<br><br>Please enter your age. <input name="demQ_Age" size="12" type="text" /> <br> <br> Please enter your gender. <br> <select name="demQ_Gender"><option>Female</option><option>Male</option></select><br><br>
If you have any comments about the task, or experienced any technical glitches, please feel free to enter them below.<br><input id="Q5comments" name="Q5comments" style= "width: 400px; height: 75px" type="text" /></div>
</body>