-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspsych-categorize.js
182 lines (150 loc) · 5.88 KB
/
jspsych-categorize.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
/**
* jspsych plugin for categorization trials with feedback
* Josh de Leeuw
*
* documentation: docs.jspsych.org
**/
jsPsych.plugins.categorize = (function() {
var plugin = {};
jsPsych.pluginAPI.registerPreload('animation', 'stimulus', 'image');
plugin.trial = function(display_element, trial) {
// default parameters
trial.text_answer = (typeof trial.text_answer === 'undefined') ? "" : trial.text_answer;
trial.correct_text = (typeof trial.correct_text === 'undefined') ? "<p class='feedback'>Correct</p>" : trial.correct_text;
trial.incorrect_text = (typeof trial.incorrect_text === 'undefined') ? "<p class='feedback'>Incorrect</p>" : trial.incorrect_text;
trial.show_stim_with_feedback = (typeof trial.show_stim_with_feedback === 'undefined') ? true : trial.show_stim_with_feedback;
trial.is_html = (typeof trial.is_html === 'undefined') ? false : trial.is_html;
trial.force_correct_button_press = (typeof trial.force_correct_button_press === 'undefined') ? false : trial.force_correct_button_press;
trial.prompt = (typeof trial.prompt === 'undefined') ? '' : trial.prompt;
trial.show_feedback_on_timeout = (typeof trial.show_feedback_on_timeout === 'undefined') ? false : trial.show_feedback_on_timeout;
trial.timeout_message = trial.timeout_message || "<p>Please respond faster.</p>";
// timing params
trial.timing_stim = trial.timing_stim || -1; // default is to show image until response
trial.timing_response = trial.timing_response || -1; // default is no max response time
trial.timing_feedback_duration = trial.timing_feedback_duration || 2000;
// if any trial variables are functions
// this evaluates the function and replaces
// it with the output of the function
trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial);
// this array holds handlers from setTimeout calls
// that need to be cleared if the trial ends early
var setTimeoutHandlers = [];
if (!trial.is_html) {
// add image to display
display_element.append($('<img>', {
"src": trial.stimulus,
"class": 'jspsych-categorize-stimulus',
"id": 'jspsych-categorize-stimulus'
}));
} else {
display_element.append($('<div>', {
"id": 'jspsych-categorize-stimulus',
"class": 'jspsych-categorize-stimulus',
"html": trial.stimulus
}));
}
// hide image after time if the timing parameter is set
if (trial.timing_stim > 0) {
setTimeoutHandlers.push(setTimeout(function() {
$('#jspsych-categorize-stimulus').css('visibility', 'hidden');
}, trial.timing_stim));
}
// if prompt is set, show prompt
if (trial.prompt !== "") {
display_element.append(trial.prompt);
}
var trial_data = {};
// create response function
var after_response = function(info) {
// kill any remaining setTimeout handlers
for (var i = 0; i < setTimeoutHandlers.length; i++) {
clearTimeout(setTimeoutHandlers[i]);
}
// 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.html('');
var timeout = info.rt == -1;
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.timing_response > 0) {
setTimeoutHandlers.push(setTimeout(function() {
after_response({
key: -1,
rt: -1
});
}, trial.timing_response));
}
function doFeedback(correct, timeout) {
if (timeout && !trial.show_feedback_on_timeout) {
display_element.append(trial.timeout_message);
} else {
// show image during feedback if flag is set
if (trial.show_stim_with_feedback) {
if (!trial.is_html) {
// add image to display
display_element.append($('<img>', {
"src": trial.stimulus,
"class": 'jspsych-categorize-stimulus',
"id": 'jspsych-categorize-stimulus'
}));
} else {
display_element.append($('<div>', {
"id": 'jspsych-categorize-stimulus',
"class": 'jspsych-categorize-stimulus',
"html": trial.stimulus
}));
}
}
// 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.append(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 {
setTimeout(function() {
endTrial();
}, trial.timing_feedback_duration);
}
}
function endTrial() {
display_element.html("");
jsPsych.finishTrial(trial_data);
}
};
return plugin;
})();