@jspsych/plugin-survey-multi-choice 1.1.1 → 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.
@@ -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,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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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","key","value","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","obje","attributes","Object","assign","trial_data","rt","response","finishTrial","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","_step","_iterator","_createForOfIteratorHelper","s","n","done","q","concat","indexOf","sampleWithoutReplacement","sampleExGaussian","err","e","f","default_data","_toConsumableArray","Array","keys","data","pluginAPI","mergeSimulationData","ensureSimulationDataConsistency","create_simulation_data","getDisplayElement","answers","entries","clickTarget"],"mappings":"wxCAEA,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,O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,EAAOvE,KAAK2E,uBAAuBvE,EAAO6C,GAEhDjD,KAAKF,QAAQiD,YAAYwB,EAC3B,GAAC,CAAAtE,IAAA,kBAAAC,MAEO,SAAgBE,EAAwB6C,EAAoBC,GAClE,IAAMqB,EAAOvE,KAAK2E,uBAAuBvE,EAAO6C,GAE1C9C,EAAkBH,KAAKF,QAAQ8E,oBAErC5E,KAAKI,MAAMD,EAAiBC,GAC5B8C,IAGA,IADA,IAAM2B,EAAUnC,OAAOoC,QAAQP,EAAKzB,UAC3BrC,EAAI,EAAGA,EAAIoE,EAAQnE,OAAQD,IAClCT,KAAKF,QAAQ0E,UAAUO,YACrB5E,EAAgBsB,cAAamC,yCAAAA,OACcnD,EAAC,KAAAmD,OAAIxD,EAAM3B,UAAUgC,GAAGrB,QAAQyE,QACvEgB,EAAQpE,GAAG,OAGb8D,EAAK1B,GAAK,KAAQgC,EAAQnE,QAAWD,EAAI,IAI/CT,KAAKF,QAAQ0E,UAAUO,YACrB5E,EAAgBsB,cAAc,qCAC9B8C,EAAK1B,GAET,oFAAChD,CAAA,CAlO0B,UACpBA,EAAIvB,KAAGA"}
package/dist/index.cjs CHANGED
@@ -2,264 +2,264 @@
2
2
 
3
3
  var jspsych = require('jspsych');
4
4
 
5
- const info = {
6
- name: "survey-multi-choice",
7
- parameters: {
8
- /** Array containing one or more objects with parameters for the question(s) that should be shown on the page. */
9
- questions: {
10
- type: jspsych.ParameterType.COMPLEX,
11
- array: true,
12
- pretty_name: "Questions",
13
- nested: {
14
- /** Question prompt. */
15
- prompt: {
16
- type: jspsych.ParameterType.HTML_STRING,
17
- pretty_name: "Prompt",
18
- default: undefined,
19
- },
20
- /** Array of multiple choice options for this question. */
21
- options: {
22
- type: jspsych.ParameterType.STRING,
23
- pretty_name: "Options",
24
- array: true,
25
- default: undefined,
26
- },
27
- /** Whether or not a response to this question must be given in order to continue. */
28
- required: {
29
- type: jspsych.ParameterType.BOOL,
30
- pretty_name: "Required",
31
- default: false,
32
- },
33
- /** If true, then the question will be centered and options will be displayed horizontally. */
34
- horizontal: {
35
- type: jspsych.ParameterType.BOOL,
36
- pretty_name: "Horizontal",
37
- default: false,
38
- },
39
- /** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */
40
- name: {
41
- type: jspsych.ParameterType.STRING,
42
- pretty_name: "Question Name",
43
- default: "",
44
- },
45
- },
46
- },
47
- /** If true, the order of the questions in the 'questions' array will be randomized. */
48
- randomize_question_order: {
49
- type: jspsych.ParameterType.BOOL,
50
- pretty_name: "Randomize Question Order",
51
- default: false,
52
- },
53
- /** HTML-formatted string to display at top of the page above all of the questions. */
54
- preamble: {
55
- type: jspsych.ParameterType.HTML_STRING,
56
- pretty_name: "Preamble",
57
- default: null,
58
- },
59
- /** Label of the button to submit responses. */
60
- button_label: {
61
- type: jspsych.ParameterType.STRING,
62
- pretty_name: "Button label",
63
- default: "Continue",
64
- },
65
- /** Setting this to true will enable browser auto-complete or auto-fill for the form. */
66
- autocomplete: {
67
- type: jspsych.ParameterType.BOOL,
68
- pretty_name: "Allow autocomplete",
69
- default: false,
70
- },
71
- },
72
- };
73
- /**
74
- * **survey-multi-choice**
75
- *
76
- * jsPsych plugin for presenting multiple choice survey questions
77
- *
78
- * @author Shane Martin
79
- * @see {@link https://www.jspsych.org/plugins/jspsych-survey-multi-choice/ survey-multi-choice plugin documentation on jspsych.org}
80
- */
81
- class SurveyMultiChoicePlugin {
82
- constructor(jsPsych) {
83
- this.jsPsych = jsPsych;
84
- }
85
- trial(display_element, trial) {
86
- var plugin_id_name = "jspsych-survey-multi-choice";
87
- var html = "";
88
- // inject CSS for trial
89
- html += '<style id="jspsych-survey-multi-choice-css">';
90
- html +=
91
- ".jspsych-survey-multi-choice-question { margin-top: 2em; margin-bottom: 2em; text-align: left; }" +
92
- ".jspsych-survey-multi-choice-text span.required {color: darkred;}" +
93
- ".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-text { text-align: center;}" +
94
- ".jspsych-survey-multi-choice-option { line-height: 2; }" +
95
- ".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-option { display: inline-block; margin-left: 1em; margin-right: 1em; vertical-align: top;}" +
96
- "label.jspsych-survey-multi-choice-text input[type='radio'] {margin-right: 1em;}";
97
- html += "</style>";
98
- // show preamble text
99
- if (trial.preamble !== null) {
100
- html +=
101
- '<div id="jspsych-survey-multi-choice-preamble" class="jspsych-survey-multi-choice-preamble">' +
102
- trial.preamble +
103
- "</div>";
104
- }
105
- // form element
106
- if (trial.autocomplete) {
107
- html += '<form id="jspsych-survey-multi-choice-form">';
108
- }
109
- else {
110
- html += '<form id="jspsych-survey-multi-choice-form" autocomplete="off">';
111
- }
112
- // generate question order. this is randomized here as opposed to randomizing the order of trial.questions
113
- // so that the data are always associated with the same question regardless of order
114
- var question_order = [];
115
- for (var i = 0; i < trial.questions.length; i++) {
116
- question_order.push(i);
117
- }
118
- if (trial.randomize_question_order) {
119
- question_order = this.jsPsych.randomization.shuffle(question_order);
120
- }
121
- // add multiple-choice questions
122
- for (var i = 0; i < trial.questions.length; i++) {
123
- // get question based on question_order
124
- var question = trial.questions[question_order[i]];
125
- var question_id = question_order[i];
126
- // create question container
127
- var question_classes = ["jspsych-survey-multi-choice-question"];
128
- if (question.horizontal) {
129
- question_classes.push("jspsych-survey-multi-choice-horizontal");
130
- }
131
- html +=
132
- '<div id="jspsych-survey-multi-choice-' +
133
- question_id +
134
- '" class="' +
135
- question_classes.join(" ") +
136
- '" data-name="' +
137
- question.name +
138
- '">';
139
- // add question text
140
- html += '<p class="jspsych-survey-multi-choice-text survey-multi-choice">' + question.prompt;
141
- if (question.required) {
142
- html += "<span class='required'>*</span>";
143
- }
144
- html += "</p>";
145
- // create option radio buttons
146
- for (var j = 0; j < question.options.length; j++) {
147
- // add label and question text
148
- var option_id_name = "jspsych-survey-multi-choice-option-" + question_id + "-" + j;
149
- var input_name = "jspsych-survey-multi-choice-response-" + question_id;
150
- var input_id = "jspsych-survey-multi-choice-response-" + question_id + "-" + j;
151
- var required_attr = question.required ? "required" : "";
152
- // add radio button container
153
- html += '<div id="' + option_id_name + '" class="jspsych-survey-multi-choice-option">';
154
- html += '<label class="jspsych-survey-multi-choice-text" for="' + input_id + '">';
155
- html +=
156
- '<input type="radio" name="' +
157
- input_name +
158
- '" id="' +
159
- input_id +
160
- '" value="' +
161
- question.options[j] +
162
- '" ' +
163
- required_attr +
164
- "></input>";
165
- html += question.options[j] + "</label>";
166
- html += "</div>";
167
- }
168
- html += "</div>";
169
- }
170
- // add submit button
171
- html +=
172
- '<input type="submit" id="' +
173
- plugin_id_name +
174
- '-next" class="' +
175
- plugin_id_name +
176
- ' jspsych-btn"' +
177
- (trial.button_label ? ' value="' + trial.button_label + '"' : "") +
178
- "></input>";
179
- html += "</form>";
180
- // render
181
- display_element.innerHTML = html;
182
- document.querySelector("form").addEventListener("submit", (event) => {
183
- event.preventDefault();
184
- // measure response time
185
- var endTime = performance.now();
186
- var response_time = Math.round(endTime - startTime);
187
- // create object to hold responses
188
- var question_data = {};
189
- for (var i = 0; i < trial.questions.length; i++) {
190
- var match = display_element.querySelector("#jspsych-survey-multi-choice-" + i);
191
- var id = "Q" + i;
192
- var val;
193
- if (match.querySelector("input[type=radio]:checked") !== null) {
194
- val = match.querySelector("input[type=radio]:checked").value;
195
- }
196
- else {
197
- val = "";
198
- }
199
- var obje = {};
200
- var name = id;
201
- if (match.attributes["data-name"].value !== "") {
202
- name = match.attributes["data-name"].value;
203
- }
204
- obje[name] = val;
205
- Object.assign(question_data, obje);
206
- }
207
- // save data
208
- var trial_data = {
209
- rt: response_time,
210
- response: question_data,
211
- question_order: question_order,
212
- };
213
- display_element.innerHTML = "";
214
- // next trial
215
- this.jsPsych.finishTrial(trial_data);
216
- });
217
- var startTime = performance.now();
218
- }
219
- simulate(trial, simulation_mode, simulation_options, load_callback) {
220
- if (simulation_mode == "data-only") {
221
- load_callback();
222
- this.simulate_data_only(trial, simulation_options);
223
- }
224
- if (simulation_mode == "visual") {
225
- this.simulate_visual(trial, simulation_options, load_callback);
226
- }
227
- }
228
- create_simulation_data(trial, simulation_options) {
229
- const question_data = {};
230
- let rt = 1000;
231
- for (const q of trial.questions) {
232
- const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;
233
- question_data[name] = this.jsPsych.randomization.sampleWithoutReplacement(q.options, 1)[0];
234
- rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
235
- }
236
- const default_data = {
237
- response: question_data,
238
- rt: rt,
239
- question_order: trial.randomize_question_order
240
- ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])
241
- : [...Array(trial.questions.length).keys()],
242
- };
243
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
244
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
245
- return data;
246
- }
247
- simulate_data_only(trial, simulation_options) {
248
- const data = this.create_simulation_data(trial, simulation_options);
249
- this.jsPsych.finishTrial(data);
250
- }
251
- simulate_visual(trial, simulation_options, load_callback) {
252
- const data = this.create_simulation_data(trial, simulation_options);
253
- const display_element = this.jsPsych.getDisplayElement();
254
- this.trial(display_element, trial);
255
- load_callback();
256
- const answers = Object.entries(data.response);
257
- for (let i = 0; i < answers.length; i++) {
258
- 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));
259
- }
260
- this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("#jspsych-survey-multi-choice-next"), data.rt);
261
- }
262
- }
5
+ const info = {
6
+ name: "survey-multi-choice",
7
+ parameters: {
8
+ /** Array containing one or more objects with parameters for the question(s) that should be shown on the page. */
9
+ questions: {
10
+ type: jspsych.ParameterType.COMPLEX,
11
+ array: true,
12
+ pretty_name: "Questions",
13
+ nested: {
14
+ /** Question prompt. */
15
+ prompt: {
16
+ type: jspsych.ParameterType.HTML_STRING,
17
+ pretty_name: "Prompt",
18
+ default: undefined,
19
+ },
20
+ /** Array of multiple choice options for this question. */
21
+ options: {
22
+ type: jspsych.ParameterType.STRING,
23
+ pretty_name: "Options",
24
+ array: true,
25
+ default: undefined,
26
+ },
27
+ /** Whether or not a response to this question must be given in order to continue. */
28
+ required: {
29
+ type: jspsych.ParameterType.BOOL,
30
+ pretty_name: "Required",
31
+ default: false,
32
+ },
33
+ /** If true, then the question will be centered and options will be displayed horizontally. */
34
+ horizontal: {
35
+ type: jspsych.ParameterType.BOOL,
36
+ pretty_name: "Horizontal",
37
+ default: false,
38
+ },
39
+ /** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */
40
+ name: {
41
+ type: jspsych.ParameterType.STRING,
42
+ pretty_name: "Question Name",
43
+ default: "",
44
+ },
45
+ },
46
+ },
47
+ /** If true, the order of the questions in the 'questions' array will be randomized. */
48
+ randomize_question_order: {
49
+ type: jspsych.ParameterType.BOOL,
50
+ pretty_name: "Randomize Question Order",
51
+ default: false,
52
+ },
53
+ /** HTML-formatted string to display at top of the page above all of the questions. */
54
+ preamble: {
55
+ type: jspsych.ParameterType.HTML_STRING,
56
+ pretty_name: "Preamble",
57
+ default: null,
58
+ },
59
+ /** Label of the button to submit responses. */
60
+ button_label: {
61
+ type: jspsych.ParameterType.STRING,
62
+ pretty_name: "Button label",
63
+ default: "Continue",
64
+ },
65
+ /** Setting this to true will enable browser auto-complete or auto-fill for the form. */
66
+ autocomplete: {
67
+ type: jspsych.ParameterType.BOOL,
68
+ pretty_name: "Allow autocomplete",
69
+ default: false,
70
+ },
71
+ },
72
+ };
73
+ /**
74
+ * **survey-multi-choice**
75
+ *
76
+ * jsPsych plugin for presenting multiple choice survey questions
77
+ *
78
+ * @author Shane Martin
79
+ * @see {@link https://www.jspsych.org/plugins/jspsych-survey-multi-choice/ survey-multi-choice plugin documentation on jspsych.org}
80
+ */
81
+ class SurveyMultiChoicePlugin {
82
+ constructor(jsPsych) {
83
+ this.jsPsych = jsPsych;
84
+ }
85
+ trial(display_element, trial) {
86
+ var plugin_id_name = "jspsych-survey-multi-choice";
87
+ var html = "";
88
+ // inject CSS for trial
89
+ html += '<style id="jspsych-survey-multi-choice-css">';
90
+ html +=
91
+ ".jspsych-survey-multi-choice-question { margin-top: 2em; margin-bottom: 2em; text-align: left; }" +
92
+ ".jspsych-survey-multi-choice-text span.required {color: darkred;}" +
93
+ ".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-text { text-align: center;}" +
94
+ ".jspsych-survey-multi-choice-option { line-height: 2; }" +
95
+ ".jspsych-survey-multi-choice-horizontal .jspsych-survey-multi-choice-option { display: inline-block; margin-left: 1em; margin-right: 1em; vertical-align: top;}" +
96
+ "label.jspsych-survey-multi-choice-text input[type='radio'] {margin-right: 1em;}";
97
+ html += "</style>";
98
+ // show preamble text
99
+ if (trial.preamble !== null) {
100
+ html +=
101
+ '<div id="jspsych-survey-multi-choice-preamble" class="jspsych-survey-multi-choice-preamble">' +
102
+ trial.preamble +
103
+ "</div>";
104
+ }
105
+ // form element
106
+ if (trial.autocomplete) {
107
+ html += '<form id="jspsych-survey-multi-choice-form">';
108
+ }
109
+ else {
110
+ html += '<form id="jspsych-survey-multi-choice-form" autocomplete="off">';
111
+ }
112
+ // generate question order. this is randomized here as opposed to randomizing the order of trial.questions
113
+ // so that the data are always associated with the same question regardless of order
114
+ var question_order = [];
115
+ for (var i = 0; i < trial.questions.length; i++) {
116
+ question_order.push(i);
117
+ }
118
+ if (trial.randomize_question_order) {
119
+ question_order = this.jsPsych.randomization.shuffle(question_order);
120
+ }
121
+ // add multiple-choice questions
122
+ for (var i = 0; i < trial.questions.length; i++) {
123
+ // get question based on question_order
124
+ var question = trial.questions[question_order[i]];
125
+ var question_id = question_order[i];
126
+ // create question container
127
+ var question_classes = ["jspsych-survey-multi-choice-question"];
128
+ if (question.horizontal) {
129
+ question_classes.push("jspsych-survey-multi-choice-horizontal");
130
+ }
131
+ html +=
132
+ '<div id="jspsych-survey-multi-choice-' +
133
+ question_id +
134
+ '" class="' +
135
+ question_classes.join(" ") +
136
+ '" data-name="' +
137
+ question.name +
138
+ '">';
139
+ // add question text
140
+ html += '<p class="jspsych-survey-multi-choice-text survey-multi-choice">' + question.prompt;
141
+ if (question.required) {
142
+ html += "<span class='required'>*</span>";
143
+ }
144
+ html += "</p>";
145
+ // create option radio buttons
146
+ for (var j = 0; j < question.options.length; j++) {
147
+ // add label and question text
148
+ var option_id_name = "jspsych-survey-multi-choice-option-" + question_id + "-" + j;
149
+ var input_name = "jspsych-survey-multi-choice-response-" + question_id;
150
+ var input_id = "jspsych-survey-multi-choice-response-" + question_id + "-" + j;
151
+ var required_attr = question.required ? "required" : "";
152
+ // add radio button container
153
+ html += '<div id="' + option_id_name + '" class="jspsych-survey-multi-choice-option">';
154
+ html += '<label class="jspsych-survey-multi-choice-text" for="' + input_id + '">';
155
+ html +=
156
+ '<input type="radio" name="' +
157
+ input_name +
158
+ '" id="' +
159
+ input_id +
160
+ '" value="' +
161
+ question.options[j] +
162
+ '" ' +
163
+ required_attr +
164
+ "></input>";
165
+ html += question.options[j] + "</label>";
166
+ html += "</div>";
167
+ }
168
+ html += "</div>";
169
+ }
170
+ // add submit button
171
+ html +=
172
+ '<input type="submit" id="' +
173
+ plugin_id_name +
174
+ '-next" class="' +
175
+ plugin_id_name +
176
+ ' jspsych-btn"' +
177
+ (trial.button_label ? ' value="' + trial.button_label + '"' : "") +
178
+ "></input>";
179
+ html += "</form>";
180
+ // render
181
+ display_element.innerHTML = html;
182
+ document.querySelector("form").addEventListener("submit", (event) => {
183
+ event.preventDefault();
184
+ // measure response time
185
+ var endTime = performance.now();
186
+ var response_time = Math.round(endTime - startTime);
187
+ // create object to hold responses
188
+ var question_data = {};
189
+ for (var i = 0; i < trial.questions.length; i++) {
190
+ var match = display_element.querySelector("#jspsych-survey-multi-choice-" + i);
191
+ var id = "Q" + i;
192
+ var val;
193
+ if (match.querySelector("input[type=radio]:checked") !== null) {
194
+ val = match.querySelector("input[type=radio]:checked").value;
195
+ }
196
+ else {
197
+ val = "";
198
+ }
199
+ var obje = {};
200
+ var name = id;
201
+ if (match.attributes["data-name"].value !== "") {
202
+ name = match.attributes["data-name"].value;
203
+ }
204
+ obje[name] = val;
205
+ Object.assign(question_data, obje);
206
+ }
207
+ // save data
208
+ var trial_data = {
209
+ rt: response_time,
210
+ response: question_data,
211
+ question_order: question_order,
212
+ };
213
+ display_element.innerHTML = "";
214
+ // next trial
215
+ this.jsPsych.finishTrial(trial_data);
216
+ });
217
+ var startTime = performance.now();
218
+ }
219
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
220
+ if (simulation_mode == "data-only") {
221
+ load_callback();
222
+ this.simulate_data_only(trial, simulation_options);
223
+ }
224
+ if (simulation_mode == "visual") {
225
+ this.simulate_visual(trial, simulation_options, load_callback);
226
+ }
227
+ }
228
+ create_simulation_data(trial, simulation_options) {
229
+ const question_data = {};
230
+ let rt = 1000;
231
+ for (const q of trial.questions) {
232
+ const name = q.name ? q.name : `Q${trial.questions.indexOf(q)}`;
233
+ question_data[name] = this.jsPsych.randomization.sampleWithoutReplacement(q.options, 1)[0];
234
+ rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
235
+ }
236
+ const default_data = {
237
+ response: question_data,
238
+ rt: rt,
239
+ question_order: trial.randomize_question_order
240
+ ? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])
241
+ : [...Array(trial.questions.length).keys()],
242
+ };
243
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
244
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
245
+ return data;
246
+ }
247
+ simulate_data_only(trial, simulation_options) {
248
+ const data = this.create_simulation_data(trial, simulation_options);
249
+ this.jsPsych.finishTrial(data);
250
+ }
251
+ simulate_visual(trial, simulation_options, load_callback) {
252
+ const data = this.create_simulation_data(trial, simulation_options);
253
+ const display_element = this.jsPsych.getDisplayElement();
254
+ this.trial(display_element, trial);
255
+ load_callback();
256
+ const answers = Object.entries(data.response);
257
+ for (let i = 0; i < answers.length; i++) {
258
+ 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));
259
+ }
260
+ this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("#jspsych-survey-multi-choice-next"), data.rt);
261
+ }
262
+ }
263
263
  SurveyMultiChoicePlugin.info = info;
264
264
 
265
265
  module.exports = SurveyMultiChoicePlugin;