-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspsych-instructions.js
231 lines (189 loc) · 7.08 KB
/
jspsych-instructions.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
221
222
223
224
225
226
227
228
229
230
231
/* jspsych-instructions.js
* Josh de Leeuw
*
* This plugin displays text (including HTML formatted strings) during the experiment.
* Use it to show instructions, provide performance feedback, etc...
*
* Page numbers can be displayed to help with navigation by setting show_page_number
* to true.
*
* documentation: docs.jspsych.org
*
*
*/
jsPsych.plugins.instructions = (function() {
var plugin = {};
plugin.info = {
name: 'instructions',
description: '',
parameters: {
pages: {
type: jsPsych.plugins.parameterType.HTML_STRING,
pretty_name: 'Pages',
default: undefined,
array: true,
description: 'Each element of the array is the content for a single page.'
},
key_forward: {
type: jsPsych.plugins.parameterType.KEYCODE,
pretty_name: 'Key forward',
default: 'rightarrow',
description: 'The key the subject can press in order to advance to the next page.'
},
key_backward: {
type: jsPsych.plugins.parameterType.KEYCODE,
pretty_name: 'Key backward',
default: 'leftarrow',
description: 'The key that the subject can press to return to the previous page.'
},
allow_backward: {
type: jsPsych.plugins.parameterType.BOOL,
pretty_name: 'Allow backward',
default: true,
description: 'If true, the subject can return to the previous page of the instructions.'
},
allow_keys: {
type: jsPsych.plugins.parameterType.BOOL,
pretty_name: 'Allow keys',
default: true,
description: 'If true, the subject can use keyboard keys to navigate the pages.'
},
show_clickable_nav: {
type: jsPsych.plugins.parameterType.BOOL,
pretty_name: 'Show clickable nav',
default: false,
description: 'If true, then a "Previous" and "Next" button will be displayed beneath the instructions.'
},
show_page_number: {
type: jsPsych.plugins.parameterType.BOOL,
pretty_name: 'Show page number',
default: false,
description: 'If true, and clickable navigation is enabled, then Page x/y will be shown between the nav buttons.'
},
button_label_previous: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Button label previous',
default: 'Previous',
description: 'The text that appears on the button to go backwards.'
},
button_label_next: {
type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Button label next',
default: 'Next',
description: 'The text that appears on the button to go forwards.'
}
}
}
plugin.trial = function(display_element, trial) {
var current_page = 0;
var view_history = [];
var start_time = performance.now();
var last_page_update_time = start_time;
function btnListener(evt){
evt.target.removeEventListener('click', btnListener);
if(this.id === "jspsych-instructions-back"){
back();
}
else if(this.id === 'jspsych-instructions-next'){
next();
}
}
function show_current_page() {
var html = trial.pages[current_page];
var pagenum_display = "";
if(trial.show_page_number) {
pagenum_display = "<span style='margin: 0 1em;' class='"+
"jspsych-instructions-pagenum'>Page "+(current_page+1)+"/"+trial.pages.length+"</span>";
}
if (trial.show_clickable_nav) {
var nav_html = "<div class='jspsych-instructions-nav' style='padding: 10px 0px;'>";
if (trial.allow_backward) {
var allowed = (current_page > 0 )? '' : "disabled='disabled'";
nav_html += "<button id='jspsych-instructions-back' class='jspsych-btn' style='margin-right: 5px;' "+allowed+">< "+trial.button_label_previous+"</button>";
}
if (trial.pages.length > 1 && trial.show_page_number) {
nav_html += pagenum_display;
}
nav_html += "<button id='jspsych-instructions-next' class='jspsych-btn'"+
"style='margin-left: 5px;'>"+trial.button_label_next+
" ></button></div>";
html += nav_html;
display_element.innerHTML = html;
if (current_page != 0 && trial.allow_backward) {
display_element.querySelector('#jspsych-instructions-back').addEventListener('click', btnListener);
}
display_element.querySelector('#jspsych-instructions-next').addEventListener('click', btnListener);
} else {
if (trial.show_page_number && trial.pages.length > 1) {
// page numbers for non-mouse navigation
html += "<div class='jspsych-instructions-pagenum'>"+pagenum_display+"</div>"
}
display_element.innerHTML = html;
}
}
function next() {
add_current_page_to_view_history()
current_page++;
// if done, finish up...
if (current_page >= trial.pages.length) {
endTrial();
} else {
show_current_page();
}
}
function back() {
add_current_page_to_view_history()
current_page--;
show_current_page();
}
function add_current_page_to_view_history() {
var current_time = performance.now();
var page_view_time = current_time - last_page_update_time;
view_history.push({
page_index: current_page,
viewing_time: page_view_time
});
last_page_update_time = current_time;
}
function endTrial() {
if (trial.allow_keys) {
jsPsych.pluginAPI.cancelKeyboardResponse(keyboard_listener);
}
display_element.innerHTML = '';
var trial_data = {
"view_history": JSON.stringify(view_history),
"rt": performance.now() - start_time
};
jsPsych.finishTrial(trial_data);
}
var after_response = function(info) {
// have to reinitialize this instead of letting it persist to prevent accidental skips of pages by holding down keys too long
keyboard_listener = jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_response,
valid_responses: [trial.key_forward, trial.key_backward],
rt_method: 'performance',
persist: false,
allow_held_key: false
});
// check if key is forwards or backwards and update page
if (jsPsych.pluginAPI.compareKeys(info.key, trial.key_backward)) {
if (current_page !== 0 && trial.allow_backward) {
back();
}
}
if (jsPsych.pluginAPI.compareKeys(info.key, trial.key_forward)) {
next();
}
};
show_current_page();
if (trial.allow_keys) {
var keyboard_listener = jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_response,
valid_responses: [trial.key_forward, trial.key_backward],
rt_method: 'performance',
persist: false
});
}
};
return plugin;
})();