@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 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, please cite the [paper that describes jsPsych](http://link.springer.com/article/10.3758%2Fs13428-014-0458-y):
30
+ If you use this library in academic work, the preferred citation is:
31
31
 
32
- 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
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
 
@@ -1,267 +1,259 @@
1
1
  var jsPsychSurveyMultiChoice = (function (jspsych) {
2
2
  'use strict';
3
3
 
4
- const info = {
5
- name: "survey-multi-choice",
6
- parameters: {
7
- /** Array containing one or more objects with parameters for the question(s) that should be shown on the page. */
8
- questions: {
9
- type: jspsych.ParameterType.COMPLEX,
10
- array: true,
11
- pretty_name: "Questions",
12
- nested: {
13
- /** Question prompt. */
14
- prompt: {
15
- type: jspsych.ParameterType.HTML_STRING,
16
- pretty_name: "Prompt",
17
- default: undefined,
18
- },
19
- /** Array of multiple choice options for this question. */
20
- options: {
21
- type: jspsych.ParameterType.STRING,
22
- pretty_name: "Options",
23
- array: true,
24
- default: undefined,
25
- },
26
- /** Whether or not a response to this question must be given in order to continue. */
27
- required: {
28
- type: jspsych.ParameterType.BOOL,
29
- pretty_name: "Required",
30
- default: false,
31
- },
32
- /** If true, then the question will be centered and options will be displayed horizontally. */
33
- horizontal: {
34
- type: jspsych.ParameterType.BOOL,
35
- pretty_name: "Horizontal",
36
- default: false,
37
- },
38
- /** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */
39
- name: {
40
- type: jspsych.ParameterType.STRING,
41
- pretty_name: "Question Name",
42
- default: "",
43
- },
44
- },
45
- },
46
- /** If true, the order of the questions in the 'questions' array will be randomized. */
47
- randomize_question_order: {
48
- type: jspsych.ParameterType.BOOL,
49
- pretty_name: "Randomize Question Order",
50
- default: false,
51
- },
52
- /** HTML-formatted string to display at top of the page above all of the questions. */
53
- preamble: {
54
- type: jspsych.ParameterType.HTML_STRING,
55
- pretty_name: "Preamble",
56
- default: null,
57
- },
58
- /** Label of the button to submit responses. */
59
- button_label: {
60
- type: jspsych.ParameterType.STRING,
61
- pretty_name: "Button label",
62
- default: "Continue",
63
- },
64
- /** Setting this to true will enable browser auto-complete or auto-fill for the form. */
65
- autocomplete: {
66
- type: jspsych.ParameterType.BOOL,
67
- pretty_name: "Allow autocomplete",
68
- default: false,
69
- },
70
- },
71
- };
72
- /**
73
- * **survey-multi-choice**
74
- *
75
- * jsPsych plugin for presenting multiple choice survey questions
76
- *
77
- * @author Shane Martin
78
- * @see {@link https://www.jspsych.org/plugins/jspsych-survey-multi-choice/ survey-multi-choice plugin documentation on jspsych.org}
79
- */
80
- class SurveyMultiChoicePlugin {
81
- constructor(jsPsych) {
82
- this.jsPsych = jsPsych;
83
- }
84
- trial(display_element, trial) {
85
- var plugin_id_name = "jspsych-survey-multi-choice";
86
- var html = "";
87
- // inject CSS for trial
88
- html += '<style id="jspsych-survey-multi-choice-css">';
89
- html +=
90
- ".jspsych-survey-multi-choice-question { margin-top: 2em; margin-bottom: 2em; text-align: left; }" +
91
- ".jspsych-survey-multi-choice-text span.required {color: darkred;}" +
92
- ".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-text { text-align: center;}" +
93
- ".jspsych-survey-multi-choice-option { line-height: 2; }" +
94
- ".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-option { display: inline-block; margin-left: 1em; margin-right: 1em; vertical-align: top;}" +
95
- "label.jspsych-survey-multi-choice-text input[type='radio'] {margin-right: 1em;}";
96
- html += "</style>";
97
- // show preamble text
98
- if (trial.preamble !== null) {
99
- html +=
100
- '<div id="jspsych-survey-multi-choice-preamble" class="jspsych-survey-multi-choice-preamble">' +
101
- trial.preamble +
102
- "</div>";
103
- }
104
- // form element
105
- if (trial.autocomplete) {
106
- html += '<form id="jspsych-survey-multi-choice-form">';
107
- }
108
- else {
109
- html += '<form id="jspsych-survey-multi-choice-form" autocomplete="off">';
110
- }
111
- // generate question order. this is randomized here as opposed to randomizing the order of trial.questions
112
- // so that the data are always associated with the same question regardless of order
113
- var question_order = [];
114
- for (var i = 0; i < trial.questions.length; i++) {
115
- question_order.push(i);
116
- }
117
- if (trial.randomize_question_order) {
118
- question_order = this.jsPsych.randomization.shuffle(question_order);
119
- }
120
- // add multiple-choice questions
121
- for (var i = 0; i < trial.questions.length; i++) {
122
- // get question based on question_order
123
- var question = trial.questions[question_order[i]];
124
- var question_id = question_order[i];
125
- // create question container
126
- var question_classes = ["jspsych-survey-multi-choice-question"];
127
- if (question.horizontal) {
128
- question_classes.push("jspsych-survey-multi-choice-horizontal");
129
- }
130
- html +=
131
- '<div id="jspsych-survey-multi-choice-' +
132
- question_id +
133
- '" class="' +
134
- question_classes.join(" ") +
135
- '" data-name="' +
136
- question.name +
137
- '">';
138
- // add question text
139
- html += '<p class="jspsych-survey-multi-choice-text survey-multi-choice">' + question.prompt;
140
- if (question.required) {
141
- html += "<span class='required'>*</span>";
142
- }
143
- html += "</p>";
144
- // create option radio buttons
145
- for (var j = 0; j < question.options.length; j++) {
146
- // add label and question text
147
- var option_id_name = "jspsych-survey-multi-choice-option-" + question_id + "-" + j;
148
- var input_name = "jspsych-survey-multi-choice-response-" + question_id;
149
- var input_id = "jspsych-survey-multi-choice-response-" + question_id + "-" + j;
150
- var required_attr = question.required ? "required" : "";
151
- // add radio button container
152
- html += '<div id="' + option_id_name + '" class="jspsych-survey-multi-choice-option">';
153
- html += '<label class="jspsych-survey-multi-choice-text" for="' + input_id + '">';
154
- html +=
155
- '<input type="radio" name="' +
156
- input_name +
157
- '" id="' +
158
- input_id +
159
- '" value="' +
160
- question.options[j] +
161
- '" ' +
162
- required_attr +
163
- "></input>";
164
- html += question.options[j] + "</label>";
165
- html += "</div>";
166
- }
167
- html += "</div>";
168
- }
169
- // add submit button
170
- html +=
171
- '<input type="submit" id="' +
172
- plugin_id_name +
173
- '-next" class="' +
174
- plugin_id_name +
175
- ' jspsych-btn"' +
176
- (trial.button_label ? ' value="' + trial.button_label + '"' : "") +
177
- "></input>";
178
- html += "</form>";
179
- // render
180
- display_element.innerHTML = html;
181
- document.querySelector("form").addEventListener("submit", (event) => {
182
- event.preventDefault();
183
- // measure response time
184
- var endTime = performance.now();
185
- var response_time = Math.round(endTime - startTime);
186
- // create object to hold responses
187
- var question_data = {};
188
- for (var i = 0; i < trial.questions.length; i++) {
189
- var match = display_element.querySelector("#jspsych-survey-multi-choice-" + i);
190
- var id = "Q" + i;
191
- var val;
192
- if (match.querySelector("input[type=radio]:checked") !== null) {
193
- val = match.querySelector("input[type=radio]:checked").value;
194
- }
195
- else {
196
- val = "";
197
- }
198
- var obje = {};
199
- var name = id;
200
- if (match.attributes["data-name"].value !== "") {
201
- name = match.attributes["data-name"].value;
202
- }
203
- obje[name] = val;
204
- Object.assign(question_data, obje);
205
- }
206
- // save data
207
- var trial_data = {
208
- rt: response_time,
209
- response: question_data,
210
- question_order: question_order,
211
- };
212
- display_element.innerHTML = "";
213
- // next trial
214
- this.jsPsych.finishTrial(trial_data);
215
- });
216
- var startTime = performance.now();
217
- }
218
- simulate(trial, simulation_mode, simulation_options, load_callback) {
219
- if (simulation_mode == "data-only") {
220
- load_callback();
221
- this.simulate_data_only(trial, simulation_options);
222
- }
223
- if (simulation_mode == "visual") {
224
- this.simulate_visual(trial, simulation_options, load_callback);
225
- }
226
- }
227
- create_simulation_data(trial, simulation_options) {
228
- const question_data = {};
229
- let rt = 1000;
230
- for (const q of trial.questions) {
231
- const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;
232
- question_data[name] = this.jsPsych.randomization.sampleWithoutReplacement(q.options, 1)[0];
233
- rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
234
- }
235
- const default_data = {
236
- response: question_data,
237
- rt: rt,
238
- question_order: trial.randomize_question_order
239
- ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])
240
- : [...Array(trial.questions.length).keys()],
241
- };
242
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
243
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
244
- return data;
245
- }
246
- simulate_data_only(trial, simulation_options) {
247
- const data = this.create_simulation_data(trial, simulation_options);
248
- this.jsPsych.finishTrial(data);
249
- }
250
- simulate_visual(trial, simulation_options, load_callback) {
251
- const data = this.create_simulation_data(trial, simulation_options);
252
- const display_element = this.jsPsych.getDisplayElement();
253
- this.trial(display_element, trial);
254
- load_callback();
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