@jspsych/plugin-survey-multi-choice 1.1.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/dist/index.browser.js +252 -260
- package/dist/index.browser.js.map +1 -1
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +250 -258
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +220 -156
- package/dist/index.js +250 -258
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.spec.ts +1 -1
- package/src/index.ts +55 -18
package/README.md
CHANGED
|
@@ -27,9 +27,15 @@ See the [contributing to jsPsych](https://www.jspsych.org/latest/developers/cont
|
|
|
27
27
|
|
|
28
28
|
## Citation
|
|
29
29
|
|
|
30
|
-
If you use this library in academic work,
|
|
30
|
+
If you use this library in academic work, the preferred citation is:
|
|
31
31
|
|
|
32
|
-
de Leeuw, J.R. (
|
|
32
|
+
de Leeuw, J.R., Gilbert, R.A., & Luchterhandt, B. (2023). jsPsych: Enabling an open-source collaborative ecosystem of behavioral experiments. *Journal of Open Source Software*, *8*(85), 5351, [https://joss.theoj.org/papers/10.21105/joss.05351](https://joss.theoj.org/papers/10.21105/joss.05351).
|
|
33
|
+
|
|
34
|
+
This paper is an updated description of jsPsych and includes all current core team members. It replaces the earlier paper that described jsPsych:
|
|
35
|
+
|
|
36
|
+
de Leeuw, J.R. (2015). jsPsych: A JavaScript library for creating behavioral experiments in a Web browser. *Behavior Research Methods*, _47_(1), 1-12. doi:[10.3758/s13428-014-0458-y](http://link.springer.com/article/10.3758%2Fs13428-014-0458-y)
|
|
37
|
+
|
|
38
|
+
Citations help us demonstrate that this library is used and valued, which allows us to continue working on it.
|
|
33
39
|
|
|
34
40
|
## Contributors
|
|
35
41
|
|
package/dist/index.browser.js
CHANGED
|
@@ -1,267 +1,259 @@
|
|
|
1
1
|
var jsPsychSurveyMultiChoice = (function (jspsych) {
|
|
2
2
|
'use strict';
|
|
3
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
|
-
const answers = Object.entries(data.response);
|
|
256
|
-
for (let i = 0; i < answers.length; i++) {
|
|
257
|
-
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector(`#jspsych-survey-multi-choice-response-${i}-${trial.questions[i].options.indexOf(answers[i][1])}`), ((data.rt - 1000) / answers.length) * (i + 1));
|
|
258
|
-
}
|
|
259
|
-
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("#jspsych-survey-multi-choice-next"), data.rt);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
SurveyMultiChoicePlugin.info = info;
|
|
4
|
+
var _package = {
|
|
5
|
+
name: "@jspsych/plugin-survey-multi-choice",
|
|
6
|
+
version: "2.0.0",
|
|
7
|
+
description: "a jspsych plugin for multiple choice survey questions",
|
|
8
|
+
type: "module",
|
|
9
|
+
main: "dist/index.cjs",
|
|
10
|
+
exports: {
|
|
11
|
+
import: "./dist/index.js",
|
|
12
|
+
require: "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
typings: "dist/index.d.ts",
|
|
15
|
+
unpkg: "dist/index.browser.min.js",
|
|
16
|
+
files: [
|
|
17
|
+
"src",
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
source: "src/index.ts",
|
|
21
|
+
scripts: {
|
|
22
|
+
test: "jest",
|
|
23
|
+
"test:watch": "npm test -- --watch",
|
|
24
|
+
tsc: "tsc",
|
|
25
|
+
build: "rollup --config",
|
|
26
|
+
"build:watch": "npm run build -- --watch"
|
|
27
|
+
},
|
|
28
|
+
repository: {
|
|
29
|
+
type: "git",
|
|
30
|
+
url: "git+https://github.com/jspsych/jsPsych.git",
|
|
31
|
+
directory: "packages/plugin-survey-multi-choice"
|
|
32
|
+
},
|
|
33
|
+
author: "Shane Martin",
|
|
34
|
+
license: "MIT",
|
|
35
|
+
bugs: {
|
|
36
|
+
url: "https://github.com/jspsych/jsPsych/issues"
|
|
37
|
+
},
|
|
38
|
+
homepage: "https://www.jspsych.org/latest/plugins/survey-multi-choice",
|
|
39
|
+
peerDependencies: {
|
|
40
|
+
jspsych: ">=7.1.0"
|
|
41
|
+
},
|
|
42
|
+
devDependencies: {
|
|
43
|
+
"@jspsych/config": "^3.0.0",
|
|
44
|
+
"@jspsych/test-utils": "^1.2.0"
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const info = {
|
|
49
|
+
name: "survey-multi-choice",
|
|
50
|
+
version: _package.version,
|
|
51
|
+
parameters: {
|
|
52
|
+
questions: {
|
|
53
|
+
type: jspsych.ParameterType.COMPLEX,
|
|
54
|
+
array: true,
|
|
55
|
+
nested: {
|
|
56
|
+
prompt: {
|
|
57
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
58
|
+
default: void 0
|
|
59
|
+
},
|
|
60
|
+
options: {
|
|
61
|
+
type: jspsych.ParameterType.STRING,
|
|
62
|
+
array: true,
|
|
63
|
+
default: void 0
|
|
64
|
+
},
|
|
65
|
+
required: {
|
|
66
|
+
type: jspsych.ParameterType.BOOL,
|
|
67
|
+
default: false
|
|
68
|
+
},
|
|
69
|
+
horizontal: {
|
|
70
|
+
type: jspsych.ParameterType.BOOL,
|
|
71
|
+
default: false
|
|
72
|
+
},
|
|
73
|
+
name: {
|
|
74
|
+
type: jspsych.ParameterType.STRING,
|
|
75
|
+
default: ""
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
randomize_question_order: {
|
|
80
|
+
type: jspsych.ParameterType.BOOL,
|
|
81
|
+
default: false
|
|
82
|
+
},
|
|
83
|
+
preamble: {
|
|
84
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
85
|
+
default: null
|
|
86
|
+
},
|
|
87
|
+
button_label: {
|
|
88
|
+
type: jspsych.ParameterType.STRING,
|
|
89
|
+
default: "Continue"
|
|
90
|
+
},
|
|
91
|
+
autocomplete: {
|
|
92
|
+
type: jspsych.ParameterType.BOOL,
|
|
93
|
+
default: false
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
data: {
|
|
97
|
+
response: {
|
|
98
|
+
type: jspsych.ParameterType.COMPLEX,
|
|
99
|
+
nested: {
|
|
100
|
+
identifier: {
|
|
101
|
+
type: jspsych.ParameterType.STRING
|
|
102
|
+
},
|
|
103
|
+
response: {
|
|
104
|
+
type: jspsych.ParameterType.STRING | jspsych.ParameterType.INT | jspsych.ParameterType.FLOAT | jspsych.ParameterType.BOOL | jspsych.ParameterType.OBJECT
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
rt: {
|
|
109
|
+
type: jspsych.ParameterType.INT
|
|
110
|
+
},
|
|
111
|
+
question_order: {
|
|
112
|
+
type: jspsych.ParameterType.INT,
|
|
113
|
+
array: true
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
class SurveyMultiChoicePlugin {
|
|
118
|
+
constructor(jsPsych) {
|
|
119
|
+
this.jsPsych = jsPsych;
|
|
120
|
+
}
|
|
121
|
+
static info = info;
|
|
122
|
+
trial(display_element, trial) {
|
|
123
|
+
var plugin_id_name = "jspsych-survey-multi-choice";
|
|
124
|
+
var html = "";
|
|
125
|
+
html += '<style id="jspsych-survey-multi-choice-css">';
|
|
126
|
+
html += ".jspsych-survey-multi-choice-question { margin-top: 2em; margin-bottom: 2em; text-align: left; }.jspsych-survey-multi-choice-text span.required {color: darkred;}.jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-text { text-align: center;}.jspsych-survey-multi-choice-option { line-height: 2; }.jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-option { display: inline-block; margin-left: 1em; margin-right: 1em; vertical-align: top;}label.jspsych-survey-multi-choice-text input[type='radio'] {margin-right: 1em;}";
|
|
127
|
+
html += "</style>";
|
|
128
|
+
if (trial.preamble !== null) {
|
|
129
|
+
html += '<div id="jspsych-survey-multi-choice-preamble" class="jspsych-survey-multi-choice-preamble">' + trial.preamble + "</div>";
|
|
130
|
+
}
|
|
131
|
+
if (trial.autocomplete) {
|
|
132
|
+
html += '<form id="jspsych-survey-multi-choice-form">';
|
|
133
|
+
} else {
|
|
134
|
+
html += '<form id="jspsych-survey-multi-choice-form" autocomplete="off">';
|
|
135
|
+
}
|
|
136
|
+
var question_order = [];
|
|
137
|
+
for (var i = 0; i < trial.questions.length; i++) {
|
|
138
|
+
question_order.push(i);
|
|
139
|
+
}
|
|
140
|
+
if (trial.randomize_question_order) {
|
|
141
|
+
question_order = this.jsPsych.randomization.shuffle(question_order);
|
|
142
|
+
}
|
|
143
|
+
for (var i = 0; i < trial.questions.length; i++) {
|
|
144
|
+
var question = trial.questions[question_order[i]];
|
|
145
|
+
var question_id = question_order[i];
|
|
146
|
+
var question_classes = ["jspsych-survey-multi-choice-question"];
|
|
147
|
+
if (question.horizontal) {
|
|
148
|
+
question_classes.push("jspsych-survey-multi-choice-horizontal");
|
|
149
|
+
}
|
|
150
|
+
html += '<div id="jspsych-survey-multi-choice-' + question_id + '" class="' + question_classes.join(" ") + '" data-name="' + question.name + '">';
|
|
151
|
+
html += '<p class="jspsych-survey-multi-choice-text survey-multi-choice">' + question.prompt;
|
|
152
|
+
if (question.required) {
|
|
153
|
+
html += "<span class='required'>*</span>";
|
|
154
|
+
}
|
|
155
|
+
html += "</p>";
|
|
156
|
+
for (var j = 0; j < question.options.length; j++) {
|
|
157
|
+
var option_id_name = "jspsych-survey-multi-choice-option-" + question_id + "-" + j;
|
|
158
|
+
var input_name = "jspsych-survey-multi-choice-response-" + question_id;
|
|
159
|
+
var input_id = "jspsych-survey-multi-choice-response-" + question_id + "-" + j;
|
|
160
|
+
var required_attr = question.required ? "required" : "";
|
|
161
|
+
html += '<div id="' + option_id_name + '" class="jspsych-survey-multi-choice-option">';
|
|
162
|
+
html += '<label class="jspsych-survey-multi-choice-text" for="' + input_id + '">';
|
|
163
|
+
html += '<input type="radio" name="' + input_name + '" id="' + input_id + '" value="' + question.options[j] + '" ' + required_attr + "></input>";
|
|
164
|
+
html += question.options[j] + "</label>";
|
|
165
|
+
html += "</div>";
|
|
166
|
+
}
|
|
167
|
+
html += "</div>";
|
|
168
|
+
}
|
|
169
|
+
html += '<input type="submit" id="' + plugin_id_name + '-next" class="' + plugin_id_name + ' jspsych-btn"' + (trial.button_label ? ' value="' + trial.button_label + '"' : "") + "></input>";
|
|
170
|
+
html += "</form>";
|
|
171
|
+
display_element.innerHTML = html;
|
|
172
|
+
document.querySelector("form").addEventListener("submit", (event) => {
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
var endTime = performance.now();
|
|
175
|
+
var response_time = Math.round(endTime - startTime);
|
|
176
|
+
var question_data = {};
|
|
177
|
+
for (var i2 = 0; i2 < trial.questions.length; i2++) {
|
|
178
|
+
var match = display_element.querySelector("#jspsych-survey-multi-choice-" + i2);
|
|
179
|
+
var id = "Q" + i2;
|
|
180
|
+
var val;
|
|
181
|
+
if (match.querySelector("input[type=radio]:checked") !== null) {
|
|
182
|
+
val = match.querySelector("input[type=radio]:checked").value;
|
|
183
|
+
} else {
|
|
184
|
+
val = "";
|
|
185
|
+
}
|
|
186
|
+
var obje = {};
|
|
187
|
+
var name = id;
|
|
188
|
+
if (match.attributes["data-name"].value !== "") {
|
|
189
|
+
name = match.attributes["data-name"].value;
|
|
190
|
+
}
|
|
191
|
+
obje[name] = val;
|
|
192
|
+
Object.assign(question_data, obje);
|
|
193
|
+
}
|
|
194
|
+
var trial_data = {
|
|
195
|
+
rt: response_time,
|
|
196
|
+
response: question_data,
|
|
197
|
+
question_order
|
|
198
|
+
};
|
|
199
|
+
this.jsPsych.finishTrial(trial_data);
|
|
200
|
+
});
|
|
201
|
+
var startTime = performance.now();
|
|
202
|
+
}
|
|
203
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
204
|
+
if (simulation_mode == "data-only") {
|
|
205
|
+
load_callback();
|
|
206
|
+
this.simulate_data_only(trial, simulation_options);
|
|
207
|
+
}
|
|
208
|
+
if (simulation_mode == "visual") {
|
|
209
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
create_simulation_data(trial, simulation_options) {
|
|
213
|
+
const question_data = {};
|
|
214
|
+
let rt = 1e3;
|
|
215
|
+
for (const q of trial.questions) {
|
|
216
|
+
const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;
|
|
217
|
+
question_data[name] = this.jsPsych.randomization.sampleWithoutReplacement(q.options, 1)[0];
|
|
218
|
+
rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
|
|
219
|
+
}
|
|
220
|
+
const default_data = {
|
|
221
|
+
response: question_data,
|
|
222
|
+
rt,
|
|
223
|
+
question_order: trial.randomize_question_order ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()]) : [...Array(trial.questions.length).keys()]
|
|
224
|
+
};
|
|
225
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
226
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
227
|
+
return data;
|
|
228
|
+
}
|
|
229
|
+
simulate_data_only(trial, simulation_options) {
|
|
230
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
231
|
+
this.jsPsych.finishTrial(data);
|
|
232
|
+
}
|
|
233
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
234
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
235
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
236
|
+
this.trial(display_element, trial);
|
|
237
|
+
load_callback();
|
|
238
|
+
const answers = Object.entries(data.response);
|
|
239
|
+
for (let i = 0; i < answers.length; i++) {
|
|
240
|
+
this.jsPsych.pluginAPI.clickTarget(
|
|
241
|
+
display_element.querySelector(
|
|
242
|
+
`#jspsych-survey-multi-choice-response-${i}-${trial.questions[i].options.indexOf(
|
|
243
|
+
answers[i][1]
|
|
244
|
+
)}`
|
|
245
|
+
),
|
|
246
|
+
(data.rt - 1e3) / answers.length * (i + 1)
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
this.jsPsych.pluginAPI.clickTarget(
|
|
250
|
+
display_element.querySelector("#jspsych-survey-multi-choice-next"),
|
|
251
|
+
data.rt
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
263
255
|
|
|
264
256
|
return SurveyMultiChoicePlugin;
|
|
265
257
|
|
|
266
258
|
})(jsPsychModule);
|
|
267
|
-
//# sourceMappingURL=index.browser.js.map
|
|
259
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-survey-multi-choice@2.0.0/dist/index.browser.js.map
|