forked from jspsych/jsPsych
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspsych-categorize-html.js
220 lines (191 loc) · 6.63 KB
/
jspsych-categorize-html.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
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
/**
* jspsych plugin for categorization trials with feedback
* Josh de Leeuw
*
* documentation: docs.jspsych.org
**/
jsPsych.plugins['categorize-html'] = (function() {
var plugin = {};
plugin.info = {
name: 'categorize-html',
description: '',
parameters: {
stimulus: {
type: jsPsych.plugins.parameterType.HTML_STRING,
pretty_name: 'Stimulus',
default: undefined,
description: 'The HTML content to be displayed.'
},
key_answer: {
type: jsPsych.plugins.parameterType.KEYCODE,
pretty_name: 'Key answer',
default: undefined,
description: 'The key to indicate the correct response.'
},
choices: {
type: jsPsych.plugins.parameterType.KEYCODE,
pretty_name: 'Choices',
default: jsPsych.ALL_KEYS,
array: true,
description: 'The keys the subject is allowed to press to respond to the stimulus.'
},
text_answer: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Text answer',
default: null,
description: 'Label that is associated with the correct answer.'
},
correct_text: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Correct text',
default: "<p class='feedback'>Correct</p>",
description: 'String to show when correct answer is given.'
},
incorrect_text: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Incorrect text',
default: "<p class='feedback'>Incorrect</p>",
description: 'String to show when incorrect answer is given.'
},
prompt: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Prompt',
default: null,
description: 'Any content here will be displayed below the stimulus.'
},
force_correct_button_press: {
type: jsPsych.plugins.parameterType.BOOL,
pretty_name: 'Force correct button press',
default: false,
description: 'If set to true, then the subject must press the correct response key after feedback in order to advance to next trial.'
},
show_stim_with_feedback: {
type: jsPsych.plugins.parameterType.BOOL,
default: true,
no_function: false,
description: ''
},
show_feedback_on_timeout: {
type: jsPsych.plugins.parameterType.BOOL,
pretty_name: 'Show feedback on timeout',
default: false,
description: 'If true, stimulus will be shown during feedback. If false, only the text feedback will be displayed during feedback.'
},
timeout_message: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Timeout message',
default: "<p>Please respond faster.</p>",
description: 'The message displayed on a timeout non-response.'
},
stimulus_duration: {
type: jsPsych.plugins.parameterType.INT,
pretty_name: 'Stimulus duration',
default: null,
description: 'How long to hide stimulus.'
},
trial_duration: {
type: jsPsych.plugins.parameterType.INT,
pretty_name: 'Trial duration',
default: null,
description: 'How long to show trial'
},
feedback_duration: {
type: jsPsych.plugins.parameterType.INT,
pretty_name: 'Feedback duration',
default: 2000,
description: 'How long to show feedback.'
}
}
}
plugin.trial = function(display_element, trial) {
display_element.innerHTML = '<div id="jspsych-categorize-html-stimulus" class="jspsych-categorize-html-stimulus">'+trial.stimulus+'</div>';
// hide image after time if the timing parameter is set
if (trial.stimulus_duration !== null) {
jsPsych.pluginAPI.setTimeout(function() {
display_element.querySelector('#jspsych-categorize-html-stimulus').style.visibility = 'hidden';
}, trial.stimulus_duration);
}
// if prompt is set, show prompt
if (trial.prompt !== null) {
display_element.innerHTML += trial.prompt;
}
var trial_data = {};
// create response function
var after_response = function(info) {
// kill any remaining setTimeout handlers
jsPsych.pluginAPI.clearAllTimeouts();
// clear keyboard listener
jsPsych.pluginAPI.cancelAllKeyboardResponses();
var correct = false;
if (trial.key_answer == info.key) {
correct = true;
}
// save data
trial_data = {
"rt": info.rt,
"correct": correct,
"stimulus": trial.stimulus,
"key_press": info.key
};
display_element.innerHTML = '';
var timeout = info.rt == null;
doFeedback(correct, timeout);
}
jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_response,
valid_responses: trial.choices,
rt_method: 'date',
persist: false,
allow_held_key: false
});
if (trial.trial_duration !== null) {
jsPsych.pluginAPI.setTimeout(function() {
after_response({
key: null,
rt: null
});
}, trial.trial_duration);
}
function doFeedback(correct, timeout) {
if (timeout && !trial.show_feedback_on_timeout) {
display_element.innerHTML += trial.timeout_message;
} else {
// show image during feedback if flag is set
if (trial.show_stim_with_feedback) {
display_element.innerHTML = '<div id="jspsych-categorize-html-stimulus" class="jspsych-categorize-html-stimulus">'+trial.stimulus+'</div>';
}
// substitute answer in feedback string.
var atext = "";
if (correct) {
atext = trial.correct_text.replace("%ANS%", trial.text_answer);
} else {
atext = trial.incorrect_text.replace("%ANS%", trial.text_answer);
}
// show the feedback
display_element.innerHTML += atext;
}
// check if force correct button press is set
if (trial.force_correct_button_press && correct === false && ((timeout && trial.show_feedback_on_timeout) || !timeout)) {
var after_forced_response = function(info) {
endTrial();
}
jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_forced_response,
valid_responses: [trial.key_answer],
rt_method: 'date',
persist: false,
allow_held_key: false
});
} else {
jsPsych.pluginAPI.setTimeout(function() {
endTrial();
}, trial.feedback_duration);
}
}
function endTrial() {
display_element.innerHTML = '';
jsPsych.finishTrial(trial_data);
}
};
return plugin;
})();