@jspsych/plugin-survey-multi-choice 1.1.1 → 1.1.2
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 +41 -0
- package/dist/index.browser.min.js.map +1 -1
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
jsPsych is a JavaScript framework for creating behavioral experiments that run in a web browser.
|
|
4
|
+
|
|
5
|
+
## Plugin Description
|
|
6
|
+
|
|
7
|
+
The survey-multi-choice plugin displays a set of questions with multiple choice responses, where the participant responds by selecting a single answer.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
Several example experiments and plugin demonstrations are available in the `/examples` folder.
|
|
12
|
+
After you've downloaded the [latest release](https://github.com/jspsych/jsPsych/releases), double-click on an example HTML file to run it in your web browser, and open it with a programming-friendly text editor to see how it works.
|
|
13
|
+
|
|
14
|
+
## Documentation
|
|
15
|
+
|
|
16
|
+
Documentation for this plugin is available [here](https://www.jspsych.org/latest/plugins/survery-multi-choice).
|
|
17
|
+
|
|
18
|
+
## Getting help
|
|
19
|
+
|
|
20
|
+
For questions about using the library, please use the GitHub [discussions forum](https://github.com/jspsych/jsPsych/discussions).
|
|
21
|
+
You can also browse through the history of Q&A on the forum to find related questions.
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
We :heart: contributions!
|
|
26
|
+
See the [contributing to jsPsych](https://www.jspsych.org/latest/developers/contributing/) documentation page for more information about how you can help.
|
|
27
|
+
|
|
28
|
+
## Citation
|
|
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):
|
|
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
|
|
33
|
+
|
|
34
|
+
## Contributors
|
|
35
|
+
|
|
36
|
+
jsPsych is open source project with [numerous contributors](https://github.com/jspsych/jsPsych/graphs/contributors).
|
|
37
|
+
The project is currently managed by the core team of Josh de Leeuw ([@jodeleeuw](https://github.com/jodeleeuw)), Becky Gilbert ([@becky-gilbert](https://github.com/becky-gilbert)), and Björn Luchterhandt ([@bjoluc](https://github.com/bjoluc)).
|
|
38
|
+
|
|
39
|
+
jsPsych was created by [Josh de Leeuw](http://www.twitter.com/joshdeleeuw).
|
|
40
|
+
|
|
41
|
+
We're also grateful for the generous support from a [Mozilla Open Source Support award](https://www.mozilla.org/en-US/moss/), which funded development of the library from 2020-2021.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"survey-multi-choice\",\n parameters: {\n /** Array containing one or more objects with parameters for the question(s) that should be shown on the page. */\n questions: {\n type: ParameterType.COMPLEX,\n array: true,\n pretty_name: \"Questions\",\n nested: {\n /** Question prompt. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: undefined,\n },\n /** Array of multiple choice options for this question. */\n options: {\n type: ParameterType.STRING,\n pretty_name: \"Options\",\n array: true,\n default: undefined,\n },\n /** Whether or not a response to this question must be given in order to continue. */\n required: {\n type: ParameterType.BOOL,\n pretty_name: \"Required\",\n default: false,\n },\n /** If true, then the question will be centered and options will be displayed horizontally. */\n horizontal: {\n type: ParameterType.BOOL,\n pretty_name: \"Horizontal\",\n default: false,\n },\n /** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */\n name: {\n type: ParameterType.STRING,\n pretty_name: \"Question Name\",\n default: \"\",\n },\n },\n },\n /** If true, the order of the questions in the 'questions' array will be randomized. */\n randomize_question_order: {\n type: ParameterType.BOOL,\n pretty_name: \"Randomize Question Order\",\n default: false,\n },\n /** HTML-formatted string to display at top of the page above all of the questions. */\n preamble: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Preamble\",\n default: null,\n },\n /** Label of the button to submit responses. */\n button_label: {\n type: ParameterType.STRING,\n pretty_name: \"Button label\",\n default: \"Continue\",\n },\n /** Setting this to true will enable browser auto-complete or auto-fill for the form. */\n autocomplete: {\n type: ParameterType.BOOL,\n pretty_name: \"Allow autocomplete\",\n default: false,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **survey-multi-choice**\n *\n * jsPsych plugin for presenting multiple choice survey questions\n *\n * @author Shane Martin\n * @see {@link https://www.jspsych.org/plugins/jspsych-survey-multi-choice/ survey-multi-choice plugin documentation on jspsych.org}\n */\nclass SurveyMultiChoicePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var plugin_id_name = \"jspsych-survey-multi-choice\";\n\n var html = \"\";\n\n // inject CSS for trial\n html += '<style id=\"jspsych-survey-multi-choice-css\">';\n html +=\n \".jspsych-survey-multi-choice-question { margin-top: 2em; margin-bottom: 2em; text-align: left; }\" +\n \".jspsych-survey-multi-choice-text span.required {color: darkred;}\" +\n \".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-text { text-align: center;}\" +\n \".jspsych-survey-multi-choice-option { line-height: 2; }\" +\n \".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-option { display: inline-block; margin-left: 1em; margin-right: 1em; vertical-align: top;}\" +\n \"label.jspsych-survey-multi-choice-text input[type='radio'] {margin-right: 1em;}\";\n html += \"</style>\";\n\n // show preamble text\n if (trial.preamble !== null) {\n html +=\n '<div id=\"jspsych-survey-multi-choice-preamble\" class=\"jspsych-survey-multi-choice-preamble\">' +\n trial.preamble +\n \"</div>\";\n }\n\n // form element\n if (trial.autocomplete) {\n html += '<form id=\"jspsych-survey-multi-choice-form\">';\n } else {\n html += '<form id=\"jspsych-survey-multi-choice-form\" autocomplete=\"off\">';\n }\n // generate question order. this is randomized here as opposed to randomizing the order of trial.questions\n // so that the data are always associated with the same question regardless of order\n var question_order = [];\n for (var i = 0; i < trial.questions.length; i++) {\n question_order.push(i);\n }\n if (trial.randomize_question_order) {\n question_order = this.jsPsych.randomization.shuffle(question_order);\n }\n\n // add multiple-choice questions\n for (var i = 0; i < trial.questions.length; i++) {\n // get question based on question_order\n var question = trial.questions[question_order[i]];\n var question_id = question_order[i];\n\n // create question container\n var question_classes = [\"jspsych-survey-multi-choice-question\"];\n if (question.horizontal) {\n question_classes.push(\"jspsych-survey-multi-choice-horizontal\");\n }\n\n html +=\n '<div id=\"jspsych-survey-multi-choice-' +\n question_id +\n '\" class=\"' +\n question_classes.join(\" \") +\n '\" data-name=\"' +\n question.name +\n '\">';\n\n // add question text\n html += '<p class=\"jspsych-survey-multi-choice-text survey-multi-choice\">' + question.prompt;\n if (question.required) {\n html += \"<span class='required'>*</span>\";\n }\n html += \"</p>\";\n\n // create option radio buttons\n for (var j = 0; j < question.options.length; j++) {\n // add label and question text\n var option_id_name = \"jspsych-survey-multi-choice-option-\" + question_id + \"-\" + j;\n var input_name = \"jspsych-survey-multi-choice-response-\" + question_id;\n var input_id = \"jspsych-survey-multi-choice-response-\" + question_id + \"-\" + j;\n\n var required_attr = question.required ? \"required\" : \"\";\n\n // add radio button container\n html += '<div id=\"' + option_id_name + '\" class=\"jspsych-survey-multi-choice-option\">';\n html += '<label class=\"jspsych-survey-multi-choice-text\" for=\"' + input_id + '\">';\n html +=\n '<input type=\"radio\" name=\"' +\n input_name +\n '\" id=\"' +\n input_id +\n '\" value=\"' +\n question.options[j] +\n '\" ' +\n required_attr +\n \"></input>\";\n html += question.options[j] + \"</label>\";\n html += \"</div>\";\n }\n\n html += \"</div>\";\n }\n\n // add submit button\n html +=\n '<input type=\"submit\" id=\"' +\n plugin_id_name +\n '-next\" class=\"' +\n plugin_id_name +\n ' jspsych-btn\"' +\n (trial.button_label ? ' value=\"' + trial.button_label + '\"' : \"\") +\n \"></input>\";\n html += \"</form>\";\n\n // render\n display_element.innerHTML = html;\n\n document.querySelector(\"form\").addEventListener(\"submit\", (event) => {\n event.preventDefault();\n // measure response time\n var endTime = performance.now();\n var response_time = Math.round(endTime - startTime);\n\n // create object to hold responses\n var question_data = {};\n for (var i = 0; i < trial.questions.length; i++) {\n var match = display_element.querySelector(\"#jspsych-survey-multi-choice-\" + i);\n var id = \"Q\" + i;\n var val: String;\n if (match.querySelector(\"input[type=radio]:checked\") !== null) {\n val = match.querySelector<HTMLInputElement>(\"input[type=radio]:checked\").value;\n } else {\n val = \"\";\n }\n var obje = {};\n var name = id;\n if (match.attributes[\"data-name\"].value !== \"\") {\n name = match.attributes[\"data-name\"].value;\n }\n obje[name] = val;\n Object.assign(question_data, obje);\n }\n // save data\n var trial_data = {\n rt: response_time,\n response: question_data,\n question_order: question_order,\n };\n display_element.innerHTML = \"\";\n\n // next trial\n this.jsPsych.finishTrial(trial_data);\n });\n\n var startTime = performance.now();\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const question_data = {};\n let rt = 1000;\n\n for (const q of trial.questions) {\n const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;\n question_data[name] = this.jsPsych.randomization.sampleWithoutReplacement(q.options, 1)[0];\n rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);\n }\n\n const default_data = {\n response: question_data,\n rt: rt,\n question_order: trial.randomize_question_order\n ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])\n : [...Array(trial.questions.length).keys()],\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n const answers = Object.entries(data.response);\n for (let i = 0; i < answers.length; i++) {\n this.jsPsych.pluginAPI.clickTarget(\n display_element.querySelector(\n `#jspsych-survey-multi-choice-response-${i}-${trial.questions[i].options.indexOf(\n answers[i][1]\n )}`\n ),\n ((data.rt - 1000) / answers.length) * (i + 1)\n );\n }\n\n this.jsPsych.pluginAPI.clickTarget(\n display_element.querySelector(\"#jspsych-survey-multi-choice-next\"),\n data.rt\n );\n }\n}\n\nexport default SurveyMultiChoicePlugin;\n"],"names":["info","name","parameters","questions","type","ParameterType","COMPLEX","array","pretty_name","nested","prompt","HTML_STRING","default","undefined","options","STRING","required","BOOL","horizontal","randomize_question_order","preamble","button_label","autocomplete","SurveyMultiChoicePlugin","jsPsych","_classCallCheck","this","display_element","trial","_this","plugin_id_name","html","question_order","i","length","push","randomization","shuffle","question","question_id","question_classes","join","j","option_id_name","input_name","input_id","required_attr","innerHTML","document","querySelector","addEventListener","event","preventDefault","endTime","performance","now","response_time","Math","round","startTime","question_data","val","match","id","value","obje","attributes","Object","assign","trial_data","rt","response","finishTrial","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","_step","_iterator","s","n","done","q","indexOf","sampleWithoutReplacement","sampleExGaussian","err","e","f","default_data","_toConsumableArray","Array","keys","data","pluginAPI","mergeSimulationData","ensureSimulationDataConsistency","create_simulation_data","getDisplayElement","answers","entries","clickTarget","concat"],"mappings":"+8BAEA,IAAMA,EAAc,CAClBC,KAAM,sBACNC,WAAY,CAEVC,UAAW,CACTC,KAAMC,EAAaA,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AAQgD,YAAYsB,4BAGnB9B,MAAA,SAAgBpC,EAAwB8C,EAAoBC,GAClE,IAAMmB,EAAOpE,KAAKwE,uBAAuBtE,EAAO8C,GAE1C/C,EAAkBD,KAAKF,QAAQ2E,oBAErCzE,KAAKE,MAAMD,EAAiBC,GAC5B+C,IAGA,IADA,IAAMyB,EAAUjC,OAAOkC,QAAQP,EAAKvB,UAC3BtC,EAAI,EAAGA,EAAImE,EAAQlE,OAAQD,IAClCP,KAAKF,QAAQuE,UAAUO,YACrB3E,EAAgBsB,cAAhB,yCAAAsD,OAC2CtE,EAAKL,KAAAA,OAAAA,EAAMzB,UAAU8B,GAAGnB,QAAQsE,QACvEgB,EAAQnE,GAAG,OAGb6D,EAAKxB,GAAK,KAAQ8B,EAAQlE,QAAWD,EAAI,IAI/CP,KAAKF,QAAQuE,UAAUO,YACrB3E,EAAgBsB,cAAc,qCAC9B6C,EAAKxB,mGA/NF/C,EAAIvB,KAAGA"}
|
|
1
|
+
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"survey-multi-choice\",\n parameters: {\n /** Array containing one or more objects with parameters for the question(s) that should be shown on the page. */\n questions: {\n type: ParameterType.COMPLEX,\n array: true,\n pretty_name: \"Questions\",\n nested: {\n /** Question prompt. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: undefined,\n },\n /** Array of multiple choice options for this question. */\n options: {\n type: ParameterType.STRING,\n pretty_name: \"Options\",\n array: true,\n default: undefined,\n },\n /** Whether or not a response to this question must be given in order to continue. */\n required: {\n type: ParameterType.BOOL,\n pretty_name: \"Required\",\n default: false,\n },\n /** If true, then the question will be centered and options will be displayed horizontally. */\n horizontal: {\n type: ParameterType.BOOL,\n pretty_name: \"Horizontal\",\n default: false,\n },\n /** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */\n name: {\n type: ParameterType.STRING,\n pretty_name: \"Question Name\",\n default: \"\",\n },\n },\n },\n /** If true, the order of the questions in the 'questions' array will be randomized. */\n randomize_question_order: {\n type: ParameterType.BOOL,\n pretty_name: \"Randomize Question Order\",\n default: false,\n },\n /** HTML-formatted string to display at top of the page above all of the questions. */\n preamble: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Preamble\",\n default: null,\n },\n /** Label of the button to submit responses. */\n button_label: {\n type: ParameterType.STRING,\n pretty_name: \"Button label\",\n default: \"Continue\",\n },\n /** Setting this to true will enable browser auto-complete or auto-fill for the form. */\n autocomplete: {\n type: ParameterType.BOOL,\n pretty_name: \"Allow autocomplete\",\n default: false,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **survey-multi-choice**\n *\n * jsPsych plugin for presenting multiple choice survey questions\n *\n * @author Shane Martin\n * @see {@link https://www.jspsych.org/plugins/jspsych-survey-multi-choice/ survey-multi-choice plugin documentation on jspsych.org}\n */\nclass SurveyMultiChoicePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var plugin_id_name = \"jspsych-survey-multi-choice\";\n\n var html = \"\";\n\n // inject CSS for trial\n html += '<style id=\"jspsych-survey-multi-choice-css\">';\n html +=\n \".jspsych-survey-multi-choice-question { margin-top: 2em; margin-bottom: 2em; text-align: left; }\" +\n \".jspsych-survey-multi-choice-text span.required {color: darkred;}\" +\n \".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-text { text-align: center;}\" +\n \".jspsych-survey-multi-choice-option { line-height: 2; }\" +\n \".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-option { display: inline-block; margin-left: 1em; margin-right: 1em; vertical-align: top;}\" +\n \"label.jspsych-survey-multi-choice-text input[type='radio'] {margin-right: 1em;}\";\n html += \"</style>\";\n\n // show preamble text\n if (trial.preamble !== null) {\n html +=\n '<div id=\"jspsych-survey-multi-choice-preamble\" class=\"jspsych-survey-multi-choice-preamble\">' +\n trial.preamble +\n \"</div>\";\n }\n\n // form element\n if (trial.autocomplete) {\n html += '<form id=\"jspsych-survey-multi-choice-form\">';\n } else {\n html += '<form id=\"jspsych-survey-multi-choice-form\" autocomplete=\"off\">';\n }\n // generate question order. this is randomized here as opposed to randomizing the order of trial.questions\n // so that the data are always associated with the same question regardless of order\n var question_order = [];\n for (var i = 0; i < trial.questions.length; i++) {\n question_order.push(i);\n }\n if (trial.randomize_question_order) {\n question_order = this.jsPsych.randomization.shuffle(question_order);\n }\n\n // add multiple-choice questions\n for (var i = 0; i < trial.questions.length; i++) {\n // get question based on question_order\n var question = trial.questions[question_order[i]];\n var question_id = question_order[i];\n\n // create question container\n var question_classes = [\"jspsych-survey-multi-choice-question\"];\n if (question.horizontal) {\n question_classes.push(\"jspsych-survey-multi-choice-horizontal\");\n }\n\n html +=\n '<div id=\"jspsych-survey-multi-choice-' +\n question_id +\n '\" class=\"' +\n question_classes.join(\" \") +\n '\" data-name=\"' +\n question.name +\n '\">';\n\n // add question text\n html += '<p class=\"jspsych-survey-multi-choice-text survey-multi-choice\">' + question.prompt;\n if (question.required) {\n html += \"<span class='required'>*</span>\";\n }\n html += \"</p>\";\n\n // create option radio buttons\n for (var j = 0; j < question.options.length; j++) {\n // add label and question text\n var option_id_name = \"jspsych-survey-multi-choice-option-\" + question_id + \"-\" + j;\n var input_name = \"jspsych-survey-multi-choice-response-\" + question_id;\n var input_id = \"jspsych-survey-multi-choice-response-\" + question_id + \"-\" + j;\n\n var required_attr = question.required ? \"required\" : \"\";\n\n // add radio button container\n html += '<div id=\"' + option_id_name + '\" class=\"jspsych-survey-multi-choice-option\">';\n html += '<label class=\"jspsych-survey-multi-choice-text\" for=\"' + input_id + '\">';\n html +=\n '<input type=\"radio\" name=\"' +\n input_name +\n '\" id=\"' +\n input_id +\n '\" value=\"' +\n question.options[j] +\n '\" ' +\n required_attr +\n \"></input>\";\n html += question.options[j] + \"</label>\";\n html += \"</div>\";\n }\n\n html += \"</div>\";\n }\n\n // add submit button\n html +=\n '<input type=\"submit\" id=\"' +\n plugin_id_name +\n '-next\" class=\"' +\n plugin_id_name +\n ' jspsych-btn\"' +\n (trial.button_label ? ' value=\"' + trial.button_label + '\"' : \"\") +\n \"></input>\";\n html += \"</form>\";\n\n // render\n display_element.innerHTML = html;\n\n document.querySelector(\"form\").addEventListener(\"submit\", (event) => {\n event.preventDefault();\n // measure response time\n var endTime = performance.now();\n var response_time = Math.round(endTime - startTime);\n\n // create object to hold responses\n var question_data = {};\n for (var i = 0; i < trial.questions.length; i++) {\n var match = display_element.querySelector(\"#jspsych-survey-multi-choice-\" + i);\n var id = \"Q\" + i;\n var val: String;\n if (match.querySelector(\"input[type=radio]:checked\") !== null) {\n val = match.querySelector<HTMLInputElement>(\"input[type=radio]:checked\").value;\n } else {\n val = \"\";\n }\n var obje = {};\n var name = id;\n if (match.attributes[\"data-name\"].value !== \"\") {\n name = match.attributes[\"data-name\"].value;\n }\n obje[name] = val;\n Object.assign(question_data, obje);\n }\n // save data\n var trial_data = {\n rt: response_time,\n response: question_data,\n question_order: question_order,\n };\n display_element.innerHTML = \"\";\n\n // next trial\n this.jsPsych.finishTrial(trial_data);\n });\n\n var startTime = performance.now();\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const question_data = {};\n let rt = 1000;\n\n for (const q of trial.questions) {\n const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;\n question_data[name] = this.jsPsych.randomization.sampleWithoutReplacement(q.options, 1)[0];\n rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);\n }\n\n const default_data = {\n response: question_data,\n rt: rt,\n question_order: trial.randomize_question_order\n ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])\n : [...Array(trial.questions.length).keys()],\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n const answers = Object.entries(data.response);\n for (let i = 0; i < answers.length; i++) {\n this.jsPsych.pluginAPI.clickTarget(\n display_element.querySelector(\n `#jspsych-survey-multi-choice-response-${i}-${trial.questions[i].options.indexOf(\n answers[i][1]\n )}`\n ),\n ((data.rt - 1000) / answers.length) * (i + 1)\n );\n }\n\n this.jsPsych.pluginAPI.clickTarget(\n display_element.querySelector(\"#jspsych-survey-multi-choice-next\"),\n data.rt\n );\n }\n}\n\nexport default SurveyMultiChoicePlugin;\n"],"names":["info","name","parameters","questions","type","ParameterType","COMPLEX","array","pretty_name","nested","prompt","HTML_STRING","default","undefined","options","STRING","required","BOOL","horizontal","randomize_question_order","preamble","button_label","autocomplete","SurveyMultiChoicePlugin","jsPsych","_classCallCheck","this","display_element","trial","_this","plugin_id_name","html","question_order","i","length","push","randomization","shuffle","question","question_id","question_classes","join","j","option_id_name","input_name","input_id","required_attr","innerHTML","document","querySelector","addEventListener","event","preventDefault","endTime","performance","now","response_time","Math","round","startTime","question_data","val","match","id","value","obje","attributes","Object","assign","trial_data","rt","response","finishTrial","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","_step","_iterator","s","n","done","q","indexOf","sampleWithoutReplacement","sampleExGaussian","err","e","f","default_data","_toConsumableArray","Array","keys","data","pluginAPI","mergeSimulationData","ensureSimulationDataConsistency","create_simulation_data","getDisplayElement","answers","entries","clickTarget","concat"],"mappings":"+8BAEA,IAAMA,EAAc,CAClBC,KAAM,sBACNC,WAAY,CAEVC,UAAW,CACTC,KAAMC,EAAaA,cAACC,QACpBC,OAAO,EACPC,YAAa,YACbC,OAAQ,CAENC,OAAQ,CACNN,KAAMC,EAAaA,cAACM,YACpBH,YAAa,SACbI,aAASC,GAGXC,QAAS,CACPV,KAAMC,EAAaA,cAACU,OACpBP,YAAa,UACbD,OAAO,EACPK,aAASC,GAGXG,SAAU,CACRZ,KAAMC,EAAaA,cAACY,KACpBT,YAAa,WACbI,SAAS,GAGXM,WAAY,CACVd,KAAMC,EAAaA,cAACY,KACpBT,YAAa,aACbI,SAAS,GAGXX,KAAM,CACJG,KAAMC,EAAaA,cAACU,OACpBP,YAAa,gBACbI,QAAS,MAKfO,yBAA0B,CACxBf,KAAMC,EAAaA,cAACY,KACpBT,YAAa,2BACbI,SAAS,GAGXQ,SAAU,CACRhB,KAAMC,EAAaA,cAACM,YACpBH,YAAa,WACbI,QAAS,MAGXS,aAAc,CACZjB,KAAMC,EAAaA,cAACU,OACpBP,YAAa,eACbI,QAAS,YAGXU,aAAc,CACZlB,KAAMC,EAAaA,cAACY,KACpBT,YAAa,qBACbI,SAAS,KAeTW,aAGJ,SAAAA,EAAoBC,gGAAgBC,CAAAC,KAAAH,GAAhBG,KAAOF,QAAPA,CAAoB,6CAExC,SAAMG,EAA8BC,GAAsB,IAAAC,EAAAH,KACpDI,EAAiB,8BAEjBC,EAAO,GAGXA,GAAQ,+CACRA,GACE,8iBAMFA,GAAQ,WAGe,OAAnBH,EAAMR,WACRW,GACE,+FACAH,EAAMR,SACN,UAIAQ,EAAMN,aACRS,GAAQ,+CAERA,GAAQ,kEAKV,IADA,IAAIC,EAAiB,GACZC,EAAI,EAAGA,EAAIL,EAAMzB,UAAU+B,OAAQD,IAC1CD,EAAeG,KAAKF,GAOtB,IALIL,EAAMT,2BACRa,EAAiBN,K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B,QAAQ2D,SAGlCC,EAAOpE,KAAKF,QAAQuE,UAAUC,oBAAoBN,EAAchB,GAItE,OAFAhD,KAAKF,QAAQuE,UAAUE,gCAAgCrE,EAAOkE,GAEvDA,CACR,mCAEO,SAAmBlE,EAAwB8C,GACjD,IAAMoB,EAAOpE,KAAKwE,uBAAuBtE,EAAO8C,GAEhDhD,KAAKF,QAAQgD,YAAYsB,EAC1B,0BAEO9B,MAAA,SAAgBpC,EAAwB8C,EAAoBC,GAClE,IAAMmB,EAAOpE,KAAKwE,uBAAuBtE,EAAO8C,GAE1C/C,EAAkBD,KAAKF,QAAQ2E,oBAErCzE,KAAKE,MAAMD,EAAiBC,GAC5B+C,IAGA,IADA,IAAMyB,EAAUjC,OAAOkC,QAAQP,EAAKvB,UAC3BtC,EAAI,EAAGA,EAAImE,EAAQlE,OAAQD,IAClCP,KAAKF,QAAQuE,UAAUO,YACrB3E,EAAgBsB,cAAhB,yCAAAsD,OAC2CtE,EAAKL,KAAAA,OAAAA,EAAMzB,UAAU8B,GAAGnB,QAAQsE,QACvEgB,EAAQnE,GAAG,OAGb6D,EAAKxB,GAAK,KAAQ8B,EAAQlE,QAAWD,EAAI,IAI/CP,KAAKF,QAAQuE,UAAUO,YACrB3E,EAAgBsB,cAAc,qCAC9B6C,EAAKxB,GAER,gGAjOM/C,EAAIvB,KAAGA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jspsych/plugin-survey-multi-choice",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "a jspsych plugin for multiple choice survey questions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@jspsych/config": "^1.3.0",
|
|
41
|
-
"@jspsych/test-utils": "^1.1.
|
|
41
|
+
"@jspsych/test-utils": "^1.1.2"
|
|
42
42
|
}
|
|
43
43
|
}
|