@jspsych/plugin-survey-likert 1.1.2 → 1.1.3

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/dist/index.js CHANGED
@@ -1,247 +1,247 @@
1
1
  import { ParameterType } from 'jspsych';
2
2
 
3
- const info = {
4
- name: "survey-likert",
5
- parameters: {
6
- /** Array containing one or more objects with parameters for the question(s) that should be shown on the page. */
7
- questions: {
8
- type: ParameterType.COMPLEX,
9
- array: true,
10
- pretty_name: "Questions",
11
- nested: {
12
- /** Question prompt. */
13
- prompt: {
14
- type: ParameterType.HTML_STRING,
15
- pretty_name: "Prompt",
16
- default: undefined,
17
- },
18
- /** Array of likert labels to display for this question. */
19
- labels: {
20
- type: ParameterType.STRING,
21
- array: true,
22
- pretty_name: "Labels",
23
- default: undefined,
24
- },
25
- /** Whether or not a response to this question must be given in order to continue. */
26
- required: {
27
- type: ParameterType.BOOL,
28
- pretty_name: "Required",
29
- default: false,
30
- },
31
- /** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */
32
- name: {
33
- type: ParameterType.STRING,
34
- pretty_name: "Question Name",
35
- default: "",
36
- },
37
- },
38
- },
39
- /** If true, the order of the questions in the 'questions' array will be randomized. */
40
- randomize_question_order: {
41
- type: ParameterType.BOOL,
42
- pretty_name: "Randomize Question Order",
43
- default: false,
44
- },
45
- /** HTML-formatted string to display at top of the page above all of the questions. */
46
- preamble: {
47
- type: ParameterType.HTML_STRING,
48
- pretty_name: "Preamble",
49
- default: null,
50
- },
51
- /** Width of the likert scales in pixels. */
52
- scale_width: {
53
- type: ParameterType.INT,
54
- pretty_name: "Scale width",
55
- default: null,
56
- },
57
- /** Label of the button to submit responses. */
58
- button_label: {
59
- type: ParameterType.STRING,
60
- pretty_name: "Button label",
61
- default: "Continue",
62
- },
63
- /** Setting this to true will enable browser auto-complete or auto-fill for the form. */
64
- autocomplete: {
65
- type: ParameterType.BOOL,
66
- pretty_name: "Allow autocomplete",
67
- default: false,
68
- },
69
- },
70
- };
71
- /**
72
- * **survey-likert**
73
- *
74
- * jsPsych plugin for gathering responses to questions on a likert scale
75
- *
76
- * @author Josh de Leeuw
77
- * @see {@link https://www.jspsych.org/plugins/jspsych-survey-likert/ survey-likert plugin documentation on jspsych.org}
78
- */
79
- class SurveyLikertPlugin {
80
- constructor(jsPsych) {
81
- this.jsPsych = jsPsych;
82
- }
83
- trial(display_element, trial) {
84
- if (trial.scale_width !== null) {
85
- var w = trial.scale_width + "px";
86
- }
87
- else {
88
- var w = "100%";
89
- }
90
- var html = "";
91
- // inject CSS for trial
92
- html += '<style id="jspsych-survey-likert-css">';
93
- html +=
94
- ".jspsych-survey-likert-statement { display:block; font-size: 16px; padding-top: 40px; margin-bottom:10px; }" +
95
- ".jspsych-survey-likert-opts { list-style:none; width:" +
96
- w +
97
- "; margin:auto; padding:0 0 35px; display:block; font-size: 14px; line-height:1.1em; }" +
98
- ".jspsych-survey-likert-opt-label { line-height: 1.1em; color: #444; }" +
99
- ".jspsych-survey-likert-opts:before { content: ''; position:relative; top:11px; /*left:9.5%;*/ display:block; background-color:#efefef; height:4px; width:100%; }" +
100
- ".jspsych-survey-likert-opts:last-of-type { border-bottom: 0; }" +
101
- ".jspsych-survey-likert-opts li { display:inline-block; /*width:19%;*/ text-align:center; vertical-align: top; }" +
102
- ".jspsych-survey-likert-opts li input[type=radio] { display:block; position:relative; top:0; left:50%; margin-left:-6px; }";
103
- html += "</style>";
104
- // show preamble text
105
- if (trial.preamble !== null) {
106
- html +=
107
- '<div id="jspsych-survey-likert-preamble" class="jspsych-survey-likert-preamble">' +
108
- trial.preamble +
109
- "</div>";
110
- }
111
- if (trial.autocomplete) {
112
- html += '<form id="jspsych-survey-likert-form">';
113
- }
114
- else {
115
- html += '<form id="jspsych-survey-likert-form" autocomplete="off">';
116
- }
117
- // add likert scale questions ///
118
- // generate question order. this is randomized here as opposed to randomizing the order of trial.questions
119
- // so that the data are always associated with the same question regardless of order
120
- var question_order = [];
121
- for (var i = 0; i < trial.questions.length; i++) {
122
- question_order.push(i);
123
- }
124
- if (trial.randomize_question_order) {
125
- question_order = this.jsPsych.randomization.shuffle(question_order);
126
- }
127
- for (var i = 0; i < trial.questions.length; i++) {
128
- var question = trial.questions[question_order[i]];
129
- // add question
130
- html += '<label class="jspsych-survey-likert-statement">' + question.prompt + "</label>";
131
- // add options
132
- var width = 100 / question.labels.length;
133
- var options_string = '<ul class="jspsych-survey-likert-opts" data-name="' +
134
- question.name +
135
- '" data-radio-group="Q' +
136
- question_order[i] +
137
- '">';
138
- for (var j = 0; j < question.labels.length; j++) {
139
- options_string +=
140
- '<li style="width:' +
141
- width +
142
- '%"><label class="jspsych-survey-likert-opt-label"><input type="radio" name="Q' +
143
- question_order[i] +
144
- '" value="' +
145
- j +
146
- '"';
147
- if (question.required) {
148
- options_string += " required";
149
- }
150
- options_string += ">" + question.labels[j] + "</label></li>";
151
- }
152
- options_string += "</ul>";
153
- html += options_string;
154
- }
155
- // add submit button
156
- html +=
157
- '<input type="submit" id="jspsych-survey-likert-next" class="jspsych-survey-likert jspsych-btn" value="' +
158
- trial.button_label +
159
- '"></input>';
160
- html += "</form>";
161
- display_element.innerHTML = html;
162
- display_element.querySelector("#jspsych-survey-likert-form").addEventListener("submit", (e) => {
163
- e.preventDefault();
164
- // measure response time
165
- var endTime = performance.now();
166
- var response_time = Math.round(endTime - startTime);
167
- // create object to hold responses
168
- var question_data = {};
169
- var matches = display_element.querySelectorAll("#jspsych-survey-likert-form .jspsych-survey-likert-opts");
170
- for (var index = 0; index < matches.length; index++) {
171
- var id = matches[index].dataset["radioGroup"];
172
- var el = display_element.querySelector('input[name="' + id + '"]:checked');
173
- if (el === null) {
174
- var response = "";
175
- }
176
- else {
177
- var response = parseInt(el.value);
178
- }
179
- var obje = {};
180
- if (matches[index].attributes["data-name"].value !== "") {
181
- var name = matches[index].attributes["data-name"].value;
182
- }
183
- else {
184
- var name = id;
185
- }
186
- obje[name] = response;
187
- Object.assign(question_data, obje);
188
- }
189
- // save data
190
- var trial_data = {
191
- rt: response_time,
192
- response: question_data,
193
- question_order: question_order,
194
- };
195
- display_element.innerHTML = "";
196
- // next trial
197
- this.jsPsych.finishTrial(trial_data);
198
- });
199
- var startTime = performance.now();
200
- }
201
- simulate(trial, simulation_mode, simulation_options, load_callback) {
202
- if (simulation_mode == "data-only") {
203
- load_callback();
204
- this.simulate_data_only(trial, simulation_options);
205
- }
206
- if (simulation_mode == "visual") {
207
- this.simulate_visual(trial, simulation_options, load_callback);
208
- }
209
- }
210
- create_simulation_data(trial, simulation_options) {
211
- const question_data = {};
212
- let rt = 1000;
213
- for (const q of trial.questions) {
214
- const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;
215
- question_data[name] = this.jsPsych.randomization.randomInt(0, q.labels.length - 1);
216
- rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
217
- }
218
- const default_data = {
219
- response: question_data,
220
- rt: rt,
221
- question_order: trial.randomize_question_order
222
- ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])
223
- : [...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(display_element.querySelector(`input[type="radio"][name="${answers[i][0]}"][value="${answers[i][1]}"]`), ((data.rt - 1000) / answers.length) * (i + 1));
241
- }
242
- this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("#jspsych-survey-likert-next"), data.rt);
243
- }
244
- }
3
+ const info = {
4
+ name: "survey-likert",
5
+ parameters: {
6
+ /** Array containing one or more objects with parameters for the question(s) that should be shown on the page. */
7
+ questions: {
8
+ type: ParameterType.COMPLEX,
9
+ array: true,
10
+ pretty_name: "Questions",
11
+ nested: {
12
+ /** Question prompt. */
13
+ prompt: {
14
+ type: ParameterType.HTML_STRING,
15
+ pretty_name: "Prompt",
16
+ default: undefined,
17
+ },
18
+ /** Array of likert labels to display for this question. */
19
+ labels: {
20
+ type: ParameterType.STRING,
21
+ array: true,
22
+ pretty_name: "Labels",
23
+ default: undefined,
24
+ },
25
+ /** Whether or not a response to this question must be given in order to continue. */
26
+ required: {
27
+ type: ParameterType.BOOL,
28
+ pretty_name: "Required",
29
+ default: false,
30
+ },
31
+ /** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */
32
+ name: {
33
+ type: ParameterType.STRING,
34
+ pretty_name: "Question Name",
35
+ default: "",
36
+ },
37
+ },
38
+ },
39
+ /** If true, the order of the questions in the 'questions' array will be randomized. */
40
+ randomize_question_order: {
41
+ type: ParameterType.BOOL,
42
+ pretty_name: "Randomize Question Order",
43
+ default: false,
44
+ },
45
+ /** HTML-formatted string to display at top of the page above all of the questions. */
46
+ preamble: {
47
+ type: ParameterType.HTML_STRING,
48
+ pretty_name: "Preamble",
49
+ default: null,
50
+ },
51
+ /** Width of the likert scales in pixels. */
52
+ scale_width: {
53
+ type: ParameterType.INT,
54
+ pretty_name: "Scale width",
55
+ default: null,
56
+ },
57
+ /** Label of the button to submit responses. */
58
+ button_label: {
59
+ type: ParameterType.STRING,
60
+ pretty_name: "Button label",
61
+ default: "Continue",
62
+ },
63
+ /** Setting this to true will enable browser auto-complete or auto-fill for the form. */
64
+ autocomplete: {
65
+ type: ParameterType.BOOL,
66
+ pretty_name: "Allow autocomplete",
67
+ default: false,
68
+ },
69
+ },
70
+ };
71
+ /**
72
+ * **survey-likert**
73
+ *
74
+ * jsPsych plugin for gathering responses to questions on a likert scale
75
+ *
76
+ * @author Josh de Leeuw
77
+ * @see {@link https://www.jspsych.org/plugins/jspsych-survey-likert/ survey-likert plugin documentation on jspsych.org}
78
+ */
79
+ class SurveyLikertPlugin {
80
+ constructor(jsPsych) {
81
+ this.jsPsych = jsPsych;
82
+ }
83
+ trial(display_element, trial) {
84
+ if (trial.scale_width !== null) {
85
+ var w = trial.scale_width + "px";
86
+ }
87
+ else {
88
+ var w = "100%";
89
+ }
90
+ var html = "";
91
+ // inject CSS for trial
92
+ html += '<style id="jspsych-survey-likert-css">';
93
+ html +=
94
+ ".jspsych-survey-likert-statement { display:block; font-size: 16px; padding-top: 40px; margin-bottom:10px; }" +
95
+ ".jspsych-survey-likert-opts { list-style:none; width:" +
96
+ w +
97
+ "; margin:auto; padding:0 0 35px; display:block; font-size: 14px; line-height:1.1em; }" +
98
+ ".jspsych-survey-likert-opt-label { line-height: 1.1em; color: #444; }" +
99
+ ".jspsych-survey-likert-opts:before { content: ''; position:relative; top:11px; /*left:9.5%;*/ display:block; background-color:#efefef; height:4px; width:100%; }" +
100
+ ".jspsych-survey-likert-opts:last-of-type { border-bottom: 0; }" +
101
+ ".jspsych-survey-likert-opts li { display:inline-block; /*width:19%;*/ text-align:center; vertical-align: top; }" +
102
+ ".jspsych-survey-likert-opts li input[type=radio] { display:block; position:relative; top:0; left:50%; margin-left:-6px; }";
103
+ html += "</style>";
104
+ // show preamble text
105
+ if (trial.preamble !== null) {
106
+ html +=
107
+ '<div id="jspsych-survey-likert-preamble" class="jspsych-survey-likert-preamble">' +
108
+ trial.preamble +
109
+ "</div>";
110
+ }
111
+ if (trial.autocomplete) {
112
+ html += '<form id="jspsych-survey-likert-form">';
113
+ }
114
+ else {
115
+ html += '<form id="jspsych-survey-likert-form" autocomplete="off">';
116
+ }
117
+ // add likert scale questions ///
118
+ // generate question order. this is randomized here as opposed to randomizing the order of trial.questions
119
+ // so that the data are always associated with the same question regardless of order
120
+ var question_order = [];
121
+ for (var i = 0; i < trial.questions.length; i++) {
122
+ question_order.push(i);
123
+ }
124
+ if (trial.randomize_question_order) {
125
+ question_order = this.jsPsych.randomization.shuffle(question_order);
126
+ }
127
+ for (var i = 0; i < trial.questions.length; i++) {
128
+ var question = trial.questions[question_order[i]];
129
+ // add question
130
+ html += '<label class="jspsych-survey-likert-statement">' + question.prompt + "</label>";
131
+ // add options
132
+ var width = 100 / question.labels.length;
133
+ var options_string = '<ul class="jspsych-survey-likert-opts" data-name="' +
134
+ question.name +
135
+ '" data-radio-group="Q' +
136
+ question_order[i] +
137
+ '">';
138
+ for (var j = 0; j < question.labels.length; j++) {
139
+ options_string +=
140
+ '<li style="width:' +
141
+ width +
142
+ '%"><label class="jspsych-survey-likert-opt-label"><input type="radio" name="Q' +
143
+ question_order[i] +
144
+ '" value="' +
145
+ j +
146
+ '"';
147
+ if (question.required) {
148
+ options_string += " required";
149
+ }
150
+ options_string += ">" + question.labels[j] + "</label></li>";
151
+ }
152
+ options_string += "</ul>";
153
+ html += options_string;
154
+ }
155
+ // add submit button
156
+ html +=
157
+ '<input type="submit" id="jspsych-survey-likert-next" class="jspsych-survey-likert jspsych-btn" value="' +
158
+ trial.button_label +
159
+ '"></input>';
160
+ html += "</form>";
161
+ display_element.innerHTML = html;
162
+ display_element.querySelector("#jspsych-survey-likert-form").addEventListener("submit", (e) => {
163
+ e.preventDefault();
164
+ // measure response time
165
+ var endTime = performance.now();
166
+ var response_time = Math.round(endTime - startTime);
167
+ // create object to hold responses
168
+ var question_data = {};
169
+ var matches = display_element.querySelectorAll("#jspsych-survey-likert-form .jspsych-survey-likert-opts");
170
+ for (var index = 0; index < matches.length; index++) {
171
+ var id = matches[index].dataset["radioGroup"];
172
+ var el = display_element.querySelector('input[name="' + id + '"]:checked');
173
+ if (el === null) {
174
+ var response = "";
175
+ }
176
+ else {
177
+ var response = parseInt(el.value);
178
+ }
179
+ var obje = {};
180
+ if (matches[index].attributes["data-name"].value !== "") {
181
+ var name = matches[index].attributes["data-name"].value;
182
+ }
183
+ else {
184
+ var name = id;
185
+ }
186
+ obje[name] = response;
187
+ Object.assign(question_data, obje);
188
+ }
189
+ // save data
190
+ var trial_data = {
191
+ rt: response_time,
192
+ response: question_data,
193
+ question_order: question_order,
194
+ };
195
+ display_element.innerHTML = "";
196
+ // next trial
197
+ this.jsPsych.finishTrial(trial_data);
198
+ });
199
+ var startTime = performance.now();
200
+ }
201
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
202
+ if (simulation_mode == "data-only") {
203
+ load_callback();
204
+ this.simulate_data_only(trial, simulation_options);
205
+ }
206
+ if (simulation_mode == "visual") {
207
+ this.simulate_visual(trial, simulation_options, load_callback);
208
+ }
209
+ }
210
+ create_simulation_data(trial, simulation_options) {
211
+ const question_data = {};
212
+ let rt = 1000;
213
+ for (const q of trial.questions) {
214
+ const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;
215
+ question_data[name] = this.jsPsych.randomization.randomInt(0, q.labels.length - 1);
216
+ rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
217
+ }
218
+ const default_data = {
219
+ response: question_data,
220
+ rt: rt,
221
+ question_order: trial.randomize_question_order
222
+ ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])
223
+ : [...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(display_element.querySelector(`input[type="radio"][name="${answers[i][0]}"][value="${answers[i][1]}"]`), ((data.rt - 1000) / answers.length) * (i + 1));
241
+ }
242
+ this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("#jspsych-survey-likert-next"), data.rt);
243
+ }
244
+ }
245
245
  SurveyLikertPlugin.info = info;
246
246
 
247
247
  export { SurveyLikertPlugin as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jspsych/plugin-survey-likert",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "a jspsych plugin for measuring items on a likert scale",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -37,7 +37,7 @@
37
37
  "jspsych": ">=7.1.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@jspsych/config": "^1.3.0",
40
+ "@jspsych/config": "^2.0.0",
41
41
  "@jspsych/test-utils": "^1.1.2"
42
42
  }
43
43
  }
package/src/index.spec.ts CHANGED
@@ -62,7 +62,7 @@ describe("survey-likert plugin simulation", () => {
62
62
 
63
63
  await expectFinished();
64
64
 
65
- const surveyData = getData().values()[0].response;
65
+ const surveyData = getData().values()[0].response as Record<string, number>;
66
66
  const all_valid = Object.entries(surveyData).every((x) => {
67
67
  return x[1] <= 4 && x[1] >= 0;
68
68
  });
@@ -94,7 +94,7 @@ describe("survey-likert plugin simulation", () => {
94
94
 
95
95
  await expectFinished();
96
96
 
97
- const surveyData = getData().values()[0].response;
97
+ const surveyData = getData().values()[0].response as Record<string, number>;
98
98
  const all_valid = Object.entries(surveyData).every((x) => {
99
99
  return x[1] <= 4 && x[1] >= 0;
100
100
  });