@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/README.md +8 -2
- package/dist/index.browser.js +243 -243
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +242 -242
- package/dist/index.d.ts +156 -156
- package/dist/index.js +242 -242
- package/package.json +2 -2
- package/src/index.spec.ts +2 -2
|
@@ -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-likert\",\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 likert labels to display for this question. */\n labels: {\n type: ParameterType.STRING,\n array: true,\n pretty_name: \"Labels\",\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 /** 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 /** Width of the likert scales in pixels. */\n scale_width: {\n type: ParameterType.INT,\n pretty_name: \"Scale width\",\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-likert**\n *\n * jsPsych plugin for gathering responses to questions on a likert scale\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-survey-likert/ survey-likert plugin documentation on jspsych.org}\n */\nclass SurveyLikertPlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n if (trial.scale_width !== null) {\n var w = trial.scale_width + \"px\";\n } else {\n var w = \"100%\";\n }\n\n var html = \"\";\n // inject CSS for trial\n html += '<style id=\"jspsych-survey-likert-css\">';\n html +=\n \".jspsych-survey-likert-statement { display:block; font-size: 16px; padding-top: 40px; margin-bottom:10px; }\" +\n \".jspsych-survey-likert-opts { list-style:none; width:\" +\n w +\n \"; margin:auto; padding:0 0 35px; display:block; font-size: 14px; line-height:1.1em; }\" +\n \".jspsych-survey-likert-opt-label { line-height: 1.1em; color: #444; }\" +\n \".jspsych-survey-likert-opts:before { content: ''; position:relative; top:11px; /*left:9.5%;*/ display:block; background-color:#efefef; height:4px; width:100%; }\" +\n \".jspsych-survey-likert-opts:last-of-type { border-bottom: 0; }\" +\n \".jspsych-survey-likert-opts li { display:inline-block; /*width:19%;*/ text-align:center; vertical-align: top; }\" +\n \".jspsych-survey-likert-opts li input[type=radio] { display:block; position:relative; top:0; left:50%; margin-left:-6px; }\";\n html += \"</style>\";\n\n // show preamble text\n if (trial.preamble !== null) {\n html +=\n '<div id=\"jspsych-survey-likert-preamble\" class=\"jspsych-survey-likert-preamble\">' +\n trial.preamble +\n \"</div>\";\n }\n\n if (trial.autocomplete) {\n html += '<form id=\"jspsych-survey-likert-form\">';\n } else {\n html += '<form id=\"jspsych-survey-likert-form\" autocomplete=\"off\">';\n }\n\n // add likert scale questions ///\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 for (var i = 0; i < trial.questions.length; i++) {\n var question = trial.questions[question_order[i]];\n // add question\n html += '<label class=\"jspsych-survey-likert-statement\">' + question.prompt + \"</label>\";\n // add options\n var width = 100 / question.labels.length;\n var options_string =\n '<ul class=\"jspsych-survey-likert-opts\" data-name=\"' +\n question.name +\n '\" data-radio-group=\"Q' +\n question_order[i] +\n '\">';\n for (var j = 0; j < question.labels.length; j++) {\n options_string +=\n '<li style=\"width:' +\n width +\n '%\"><label class=\"jspsych-survey-likert-opt-label\"><input type=\"radio\" name=\"Q' +\n question_order[i] +\n '\" value=\"' +\n j +\n '\"';\n if (question.required) {\n options_string += \" required\";\n }\n options_string += \">\" + question.labels[j] + \"</label></li>\";\n }\n options_string += \"</ul>\";\n html += options_string;\n }\n\n // add submit button\n html +=\n '<input type=\"submit\" id=\"jspsych-survey-likert-next\" class=\"jspsych-survey-likert jspsych-btn\" value=\"' +\n trial.button_label +\n '\"></input>';\n\n html += \"</form>\";\n\n display_element.innerHTML = html;\n\n display_element.querySelector(\"#jspsych-survey-likert-form\").addEventListener(\"submit\", (e) => {\n e.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 var matches = display_element.querySelectorAll<HTMLFormElement>(\n \"#jspsych-survey-likert-form .jspsych-survey-likert-opts\"\n );\n for (var index = 0; index < matches.length; index++) {\n var id = matches[index].dataset[\"radioGroup\"];\n var el = display_element.querySelector<HTMLInputElement>(\n 'input[name=\"' + id + '\"]:checked'\n );\n if (el === null) {\n var response: string | number = \"\";\n } else {\n var response: string | number = parseInt(el.value);\n }\n var obje = {};\n if (matches[index].attributes[\"data-name\"].value !== \"\") {\n var name = matches[index].attributes[\"data-name\"].value;\n } else {\n var name = id;\n }\n obje[name] = response;\n Object.assign(question_data, obje);\n }\n\n // save data\n var trial_data = {\n rt: response_time,\n response: question_data,\n question_order: question_order,\n };\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.randomInt(0, q.labels.length - 1);\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 `input[type=\"radio\"][name=\"${answers[i][0]}\"][value=\"${answers[i][1]}\"]`\n ),\n ((data.rt - 1000) / answers.length) * (i + 1)\n );\n }\n\n this.jsPsych.pluginAPI.clickTarget(\n display_element.querySelector(\"#jspsych-survey-likert-next\"),\n data.rt\n );\n }\n}\n\nexport default SurveyLikertPlugin;\n"],"names":["info","name","parameters","questions","type","ParameterType","COMPLEX","array","pretty_name","nested","prompt","HTML_STRING","default","undefined","labels","STRING","required","BOOL","randomize_question_order","preamble","scale_width","INT","button_label","autocomplete","SurveyLikertPlugin","jsPsych","_classCallCheck","this","display_element","trial","_this","w","html","question_order","i","length","push","randomization","shuffle","question","width","options_string","j","innerHTML","querySelector","addEventListener","e","preventDefault","endTime","performance","now","response_time","Math","round","startTime","question_data","matches","querySelectorAll","index","id","dataset","el","response","parseInt","value","obje","attributes","Object","assign","trial_data","rt","finishTrial","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","_step","_iterator","s","n","done","q","indexOf","randomInt","sampleExGaussian","err","f","default_data","_toConsumableArray","Array","keys","data","pluginAPI","mergeSimulationData","ensureSimulationDataConsistency","create_simulation_data","getDisplayElement","answers","entries","clickTarget"],"mappings":"08BAEA,IAAMA,EAAc,CAClBC,KAAM,gBACNC,WAAY,CAEVC,UAAW,CACTC,KAAMC,EAAaA,cAACC,QACpBC,OAAO,EACPC,YAAa,YACbC,OAAQ,CAENC,OAAQ,CACNN,KAAMC,EAAaA,cAACM,YACpBH,YAAa,SACbI,aAASC,GAGXC,OAAQ,CACNV,KAAMC,EAAaA,cAACU,OACpBR,OAAO,EACPC,YAAa,SACbI,aAASC,GAGXG,SAAU,CACRZ,KAAMC,EAAaA,cAACY,KACpBT,YAAa,WACbI,SAAS,GAGXX,KAAM,CACJG,KAAMC,EAAaA,cAACU,OACpBP,YAAa,gBACbI,QAAS,MAKfM,yBAA0B,CACxBd,KAAMC,EAAaA,cAACY,KACpBT,YAAa,2BACbI,SAAS,GAGXO,SAAU,CACRf,KAAMC,EAAaA,cAACM,YACpBH,YAAa,WACbI,Q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|
+
{"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-likert\",\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 likert labels to display for this question. */\n labels: {\n type: ParameterType.STRING,\n array: true,\n pretty_name: \"Labels\",\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 /** 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 /** Width of the likert scales in pixels. */\n scale_width: {\n type: ParameterType.INT,\n pretty_name: \"Scale width\",\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-likert**\n *\n * jsPsych plugin for gathering responses to questions on a likert scale\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-survey-likert/ survey-likert plugin documentation on jspsych.org}\n */\nclass SurveyLikertPlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n if (trial.scale_width !== null) {\n var w = trial.scale_width + \"px\";\n } else {\n var w = \"100%\";\n }\n\n var html = \"\";\n // inject CSS for trial\n html += '<style id=\"jspsych-survey-likert-css\">';\n html +=\n \".jspsych-survey-likert-statement { display:block; font-size: 16px; padding-top: 40px; margin-bottom:10px; }\" +\n \".jspsych-survey-likert-opts { list-style:none; width:\" +\n w +\n \"; margin:auto; padding:0 0 35px; display:block; font-size: 14px; line-height:1.1em; }\" +\n \".jspsych-survey-likert-opt-label { line-height: 1.1em; color: #444; }\" +\n \".jspsych-survey-likert-opts:before { content: ''; position:relative; top:11px; /*left:9.5%;*/ display:block; background-color:#efefef; height:4px; width:100%; }\" +\n \".jspsych-survey-likert-opts:last-of-type { border-bottom: 0; }\" +\n \".jspsych-survey-likert-opts li { display:inline-block; /*width:19%;*/ text-align:center; vertical-align: top; }\" +\n \".jspsych-survey-likert-opts li input[type=radio] { display:block; position:relative; top:0; left:50%; margin-left:-6px; }\";\n html += \"</style>\";\n\n // show preamble text\n if (trial.preamble !== null) {\n html +=\n '<div id=\"jspsych-survey-likert-preamble\" class=\"jspsych-survey-likert-preamble\">' +\n trial.preamble +\n \"</div>\";\n }\n\n if (trial.autocomplete) {\n html += '<form id=\"jspsych-survey-likert-form\">';\n } else {\n html += '<form id=\"jspsych-survey-likert-form\" autocomplete=\"off\">';\n }\n\n // add likert scale questions ///\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 for (var i = 0; i < trial.questions.length; i++) {\n var question = trial.questions[question_order[i]];\n // add question\n html += '<label class=\"jspsych-survey-likert-statement\">' + question.prompt + \"</label>\";\n // add options\n var width = 100 / question.labels.length;\n var options_string =\n '<ul class=\"jspsych-survey-likert-opts\" data-name=\"' +\n question.name +\n '\" data-radio-group=\"Q' +\n question_order[i] +\n '\">';\n for (var j = 0; j < question.labels.length; j++) {\n options_string +=\n '<li style=\"width:' +\n width +\n '%\"><label class=\"jspsych-survey-likert-opt-label\"><input type=\"radio\" name=\"Q' +\n question_order[i] +\n '\" value=\"' +\n j +\n '\"';\n if (question.required) {\n options_string += \" required\";\n }\n options_string += \">\" + question.labels[j] + \"</label></li>\";\n }\n options_string += \"</ul>\";\n html += options_string;\n }\n\n // add submit button\n html +=\n '<input type=\"submit\" id=\"jspsych-survey-likert-next\" class=\"jspsych-survey-likert jspsych-btn\" value=\"' +\n trial.button_label +\n '\"></input>';\n\n html += \"</form>\";\n\n display_element.innerHTML = html;\n\n display_element.querySelector(\"#jspsych-survey-likert-form\").addEventListener(\"submit\", (e) => {\n e.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 var matches = display_element.querySelectorAll<HTMLFormElement>(\n \"#jspsych-survey-likert-form .jspsych-survey-likert-opts\"\n );\n for (var index = 0; index < matches.length; index++) {\n var id = matches[index].dataset[\"radioGroup\"];\n var el = display_element.querySelector<HTMLInputElement>(\n 'input[name=\"' + id + '\"]:checked'\n );\n if (el === null) {\n var response: string | number = \"\";\n } else {\n var response: string | number = parseInt(el.value);\n }\n var obje = {};\n if (matches[index].attributes[\"data-name\"].value !== \"\") {\n var name = matches[index].attributes[\"data-name\"].value;\n } else {\n var name = id;\n }\n obje[name] = response;\n Object.assign(question_data, obje);\n }\n\n // save data\n var trial_data = {\n rt: response_time,\n response: question_data,\n question_order: question_order,\n };\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.randomInt(0, q.labels.length - 1);\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 `input[type=\"radio\"][name=\"${answers[i][0]}\"][value=\"${answers[i][1]}\"]`\n ),\n ((data.rt - 1000) / answers.length) * (i + 1)\n );\n }\n\n this.jsPsych.pluginAPI.clickTarget(\n display_element.querySelector(\"#jspsych-survey-likert-next\"),\n data.rt\n );\n }\n}\n\nexport default SurveyLikertPlugin;\n"],"names":["info","name","parameters","questions","type","ParameterType","COMPLEX","array","pretty_name","nested","prompt","HTML_STRING","default","undefined","labels","STRING","required","BOOL","randomize_question_order","preamble","scale_width","INT","button_label","autocomplete","SurveyLikertPlugin","jsPsych","_classCallCheck","this","key","value","display_element","trial","_this","w","html","question_order","i","length","push","randomization","shuffle","question","width","options_string","j","innerHTML","querySelector","addEventListener","e","preventDefault","endTime","performance","now","response_time","Math","round","startTime","question_data","matches","querySelectorAll","index","id","dataset","el","response","parseInt","obje","attributes","Object","assign","trial_data","rt","finishTrial","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","_step","_iterator","_createForOfIteratorHelper","s","n","done","q","concat","indexOf","randomInt","sampleExGaussian","err","f","default_data","_toConsumableArray","Array","keys","data","pluginAPI","mergeSimulationData","ensureSimulationDataConsistency","create_simulation_data","getDisplayElement","answers","entries","clickTarget"],"mappings":"mxCAEA,IAAMA,EAAc,CAClBC,KAAM,gBACNC,WAAY,CAEVC,UAAW,CACTC,KAAMC,EAAaA,cAACC,QACpBC,OAAO,EACPC,YAAa,YACbC,OAAQ,CAENC,OAAQ,CACNN,KAAMC,EAAaA,cAACM,YACpBH,YAAa,SACbI,aAASC,GAGXC,OAAQ,CACNV,KAAMC,EAAaA,cAACU,OACpBR,OAAO,EACPC,YAAa,SACbI,aAASC,GAGXG,SAAU,CACRZ,KAAMC,EAAaA,cAACY,KACpBT,YAAa,WACbI,SAAS,GAGXX,KAAM,CACJG,KAAMC,EAAaA,cAACU,OACpBP,YAAa,gBACbI,QAAS,MAKfM,yBAA0B,CACxBd,KAAMC,EAAaA,cAACY,KACpBT,YAAa,2BACbI,SAAS,GAGXO,SAAU,CACRf,KAAMC,EAAaA,cAACM,YACpBH,YAAa,WACbI,QAAS,MAGXQ,YAAa,CACXhB,KAAMC,EAAaA,cAACgB,IACpBb,YAAa,cACbI,QAAS,MAGXU,aAAc,CACZlB,KAAMC,EAAaA,cAACU,OACpBP,YAAa,eACbI,QAAS,YAGXW,aAAc,CACZnB,KAAMC,EAAaA,cAACY,KACpBT,YAAa,qBACbI,SAAS,KAeTY,EAAkB,WAGtB,SAAAA,EAAoBC,gGAAgBC,MAAAF,GAAhBG,KAAOF,QAAPA,CAAmB,WA4MtC,SA5Mu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
-
name: "survey-likert",
|
|
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 likert labels to display for this question. */
|
|
21
|
-
labels: {
|
|
22
|
-
type: jspsych.ParameterType.STRING,
|
|
23
|
-
array: true,
|
|
24
|
-
pretty_name: "Labels",
|
|
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
|
-
/** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */
|
|
34
|
-
name: {
|
|
35
|
-
type: jspsych.ParameterType.STRING,
|
|
36
|
-
pretty_name: "Question Name",
|
|
37
|
-
default: "",
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
/** If true, the order of the questions in the 'questions' array will be randomized. */
|
|
42
|
-
randomize_question_order: {
|
|
43
|
-
type: jspsych.ParameterType.BOOL,
|
|
44
|
-
pretty_name: "Randomize Question Order",
|
|
45
|
-
default: false,
|
|
46
|
-
},
|
|
47
|
-
/** HTML-formatted string to display at top of the page above all of the questions. */
|
|
48
|
-
preamble: {
|
|
49
|
-
type: jspsych.ParameterType.HTML_STRING,
|
|
50
|
-
pretty_name: "Preamble",
|
|
51
|
-
default: null,
|
|
52
|
-
},
|
|
53
|
-
/** Width of the likert scales in pixels. */
|
|
54
|
-
scale_width: {
|
|
55
|
-
type: jspsych.ParameterType.INT,
|
|
56
|
-
pretty_name: "Scale width",
|
|
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-likert**
|
|
75
|
-
*
|
|
76
|
-
* jsPsych plugin for gathering responses to questions on a likert scale
|
|
77
|
-
*
|
|
78
|
-
* @author Josh de Leeuw
|
|
79
|
-
* @see {@link https://www.jspsych.org/plugins/jspsych-survey-likert/ survey-likert plugin documentation on jspsych.org}
|
|
80
|
-
*/
|
|
81
|
-
class SurveyLikertPlugin {
|
|
82
|
-
constructor(jsPsych) {
|
|
83
|
-
this.jsPsych = jsPsych;
|
|
84
|
-
}
|
|
85
|
-
trial(display_element, trial) {
|
|
86
|
-
if (trial.scale_width !== null) {
|
|
87
|
-
var w = trial.scale_width + "px";
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
var w = "100%";
|
|
91
|
-
}
|
|
92
|
-
var html = "";
|
|
93
|
-
// inject CSS for trial
|
|
94
|
-
html += '<style id="jspsych-survey-likert-css">';
|
|
95
|
-
html +=
|
|
96
|
-
".jspsych-survey-likert-statement { display:block; font-size: 16px; padding-top: 40px; margin-bottom:10px; }" +
|
|
97
|
-
".jspsych-survey-likert-opts { list-style:none; width:" +
|
|
98
|
-
w +
|
|
99
|
-
"; margin:auto; padding:0 0 35px; display:block; font-size: 14px; line-height:1.1em; }" +
|
|
100
|
-
".jspsych-survey-likert-opt-label { line-height: 1.1em; color: #444; }" +
|
|
101
|
-
".jspsych-survey-likert-opts:before { content: ''; position:relative; top:11px; /*left:9.5%;*/ display:block; background-color:#efefef; height:4px; width:100%; }" +
|
|
102
|
-
".jspsych-survey-likert-opts:last-of-type { border-bottom: 0; }" +
|
|
103
|
-
".jspsych-survey-likert-opts li { display:inline-block; /*width:19%;*/ text-align:center; vertical-align: top; }" +
|
|
104
|
-
".jspsych-survey-likert-opts li input[type=radio] { display:block; position:relative; top:0; left:50%; margin-left:-6px; }";
|
|
105
|
-
html += "</style>";
|
|
106
|
-
// show preamble text
|
|
107
|
-
if (trial.preamble !== null) {
|
|
108
|
-
html +=
|
|
109
|
-
'<div id="jspsych-survey-likert-preamble" class="jspsych-survey-likert-preamble">' +
|
|
110
|
-
trial.preamble +
|
|
111
|
-
"</div>";
|
|
112
|
-
}
|
|
113
|
-
if (trial.autocomplete) {
|
|
114
|
-
html += '<form id="jspsych-survey-likert-form">';
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
html += '<form id="jspsych-survey-likert-form" autocomplete="off">';
|
|
118
|
-
}
|
|
119
|
-
// add likert scale questions ///
|
|
120
|
-
// generate question order. this is randomized here as opposed to randomizing the order of trial.questions
|
|
121
|
-
// so that the data are always associated with the same question regardless of order
|
|
122
|
-
var question_order = [];
|
|
123
|
-
for (var i = 0; i < trial.questions.length; i++) {
|
|
124
|
-
question_order.push(i);
|
|
125
|
-
}
|
|
126
|
-
if (trial.randomize_question_order) {
|
|
127
|
-
question_order = this.jsPsych.randomization.shuffle(question_order);
|
|
128
|
-
}
|
|
129
|
-
for (var i = 0; i < trial.questions.length; i++) {
|
|
130
|
-
var question = trial.questions[question_order[i]];
|
|
131
|
-
// add question
|
|
132
|
-
html += '<label class="jspsych-survey-likert-statement">' + question.prompt + "</label>";
|
|
133
|
-
// add options
|
|
134
|
-
var width = 100 / question.labels.length;
|
|
135
|
-
var options_string = '<ul class="jspsych-survey-likert-opts" data-name="' +
|
|
136
|
-
question.name +
|
|
137
|
-
'" data-radio-group="Q' +
|
|
138
|
-
question_order[i] +
|
|
139
|
-
'">';
|
|
140
|
-
for (var j = 0; j < question.labels.length; j++) {
|
|
141
|
-
options_string +=
|
|
142
|
-
'<li style="width:' +
|
|
143
|
-
width +
|
|
144
|
-
'%"><label class="jspsych-survey-likert-opt-label"><input type="radio" name="Q' +
|
|
145
|
-
question_order[i] +
|
|
146
|
-
'" value="' +
|
|
147
|
-
j +
|
|
148
|
-
'"';
|
|
149
|
-
if (question.required) {
|
|
150
|
-
options_string += " required";
|
|
151
|
-
}
|
|
152
|
-
options_string += ">" + question.labels[j] + "</label></li>";
|
|
153
|
-
}
|
|
154
|
-
options_string += "</ul>";
|
|
155
|
-
html += options_string;
|
|
156
|
-
}
|
|
157
|
-
// add submit button
|
|
158
|
-
html +=
|
|
159
|
-
'<input type="submit" id="jspsych-survey-likert-next" class="jspsych-survey-likert jspsych-btn" value="' +
|
|
160
|
-
trial.button_label +
|
|
161
|
-
'"></input>';
|
|
162
|
-
html += "</form>";
|
|
163
|
-
display_element.innerHTML = html;
|
|
164
|
-
display_element.querySelector("#jspsych-survey-likert-form").addEventListener("submit", (e) => {
|
|
165
|
-
e.preventDefault();
|
|
166
|
-
// measure response time
|
|
167
|
-
var endTime = performance.now();
|
|
168
|
-
var response_time = Math.round(endTime - startTime);
|
|
169
|
-
// create object to hold responses
|
|
170
|
-
var question_data = {};
|
|
171
|
-
var matches = display_element.querySelectorAll("#jspsych-survey-likert-form .jspsych-survey-likert-opts");
|
|
172
|
-
for (var index = 0; index < matches.length; index++) {
|
|
173
|
-
var id = matches[index].dataset["radioGroup"];
|
|
174
|
-
var el = display_element.querySelector('input[name="' + id + '"]:checked');
|
|
175
|
-
if (el === null) {
|
|
176
|
-
var response = "";
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
var response = parseInt(el.value);
|
|
180
|
-
}
|
|
181
|
-
var obje = {};
|
|
182
|
-
if (matches[index].attributes["data-name"].value !== "") {
|
|
183
|
-
var name = matches[index].attributes["data-name"].value;
|
|
184
|
-
}
|
|
185
|
-
else {
|
|
186
|
-
var name = id;
|
|
187
|
-
}
|
|
188
|
-
obje[name] = response;
|
|
189
|
-
Object.assign(question_data, obje);
|
|
190
|
-
}
|
|
191
|
-
// save data
|
|
192
|
-
var trial_data = {
|
|
193
|
-
rt: response_time,
|
|
194
|
-
response: question_data,
|
|
195
|
-
question_order: question_order,
|
|
196
|
-
};
|
|
197
|
-
display_element.innerHTML = "";
|
|
198
|
-
// next trial
|
|
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 = 1000;
|
|
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.randomInt(0, q.labels.length - 1);
|
|
218
|
-
rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
|
|
219
|
-
}
|
|
220
|
-
const default_data = {
|
|
221
|
-
response: question_data,
|
|
222
|
-
rt: rt,
|
|
223
|
-
question_order: trial.randomize_question_order
|
|
224
|
-
? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])
|
|
225
|
-
: [...Array(trial.questions.length).keys()],
|
|
226
|
-
};
|
|
227
|
-
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
228
|
-
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
229
|
-
return data;
|
|
230
|
-
}
|
|
231
|
-
simulate_data_only(trial, simulation_options) {
|
|
232
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
233
|
-
this.jsPsych.finishTrial(data);
|
|
234
|
-
}
|
|
235
|
-
simulate_visual(trial, simulation_options, load_callback) {
|
|
236
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
237
|
-
const display_element = this.jsPsych.getDisplayElement();
|
|
238
|
-
this.trial(display_element, trial);
|
|
239
|
-
load_callback();
|
|
240
|
-
const answers = Object.entries(data.response);
|
|
241
|
-
for (let i = 0; i < answers.length; i++) {
|
|
242
|
-
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));
|
|
243
|
-
}
|
|
244
|
-
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("#jspsych-survey-likert-next"), data.rt);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
5
|
+
const info = {
|
|
6
|
+
name: "survey-likert",
|
|
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 likert labels to display for this question. */
|
|
21
|
+
labels: {
|
|
22
|
+
type: jspsych.ParameterType.STRING,
|
|
23
|
+
array: true,
|
|
24
|
+
pretty_name: "Labels",
|
|
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
|
+
/** Name of the question in the trial data. If no name is given, the questions are named Q0, Q1, etc. */
|
|
34
|
+
name: {
|
|
35
|
+
type: jspsych.ParameterType.STRING,
|
|
36
|
+
pretty_name: "Question Name",
|
|
37
|
+
default: "",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
/** If true, the order of the questions in the 'questions' array will be randomized. */
|
|
42
|
+
randomize_question_order: {
|
|
43
|
+
type: jspsych.ParameterType.BOOL,
|
|
44
|
+
pretty_name: "Randomize Question Order",
|
|
45
|
+
default: false,
|
|
46
|
+
},
|
|
47
|
+
/** HTML-formatted string to display at top of the page above all of the questions. */
|
|
48
|
+
preamble: {
|
|
49
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
50
|
+
pretty_name: "Preamble",
|
|
51
|
+
default: null,
|
|
52
|
+
},
|
|
53
|
+
/** Width of the likert scales in pixels. */
|
|
54
|
+
scale_width: {
|
|
55
|
+
type: jspsych.ParameterType.INT,
|
|
56
|
+
pretty_name: "Scale width",
|
|
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-likert**
|
|
75
|
+
*
|
|
76
|
+
* jsPsych plugin for gathering responses to questions on a likert scale
|
|
77
|
+
*
|
|
78
|
+
* @author Josh de Leeuw
|
|
79
|
+
* @see {@link https://www.jspsych.org/plugins/jspsych-survey-likert/ survey-likert plugin documentation on jspsych.org}
|
|
80
|
+
*/
|
|
81
|
+
class SurveyLikertPlugin {
|
|
82
|
+
constructor(jsPsych) {
|
|
83
|
+
this.jsPsych = jsPsych;
|
|
84
|
+
}
|
|
85
|
+
trial(display_element, trial) {
|
|
86
|
+
if (trial.scale_width !== null) {
|
|
87
|
+
var w = trial.scale_width + "px";
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
var w = "100%";
|
|
91
|
+
}
|
|
92
|
+
var html = "";
|
|
93
|
+
// inject CSS for trial
|
|
94
|
+
html += '<style id="jspsych-survey-likert-css">';
|
|
95
|
+
html +=
|
|
96
|
+
".jspsych-survey-likert-statement { display:block; font-size: 16px; padding-top: 40px; margin-bottom:10px; }" +
|
|
97
|
+
".jspsych-survey-likert-opts { list-style:none; width:" +
|
|
98
|
+
w +
|
|
99
|
+
"; margin:auto; padding:0 0 35px; display:block; font-size: 14px; line-height:1.1em; }" +
|
|
100
|
+
".jspsych-survey-likert-opt-label { line-height: 1.1em; color: #444; }" +
|
|
101
|
+
".jspsych-survey-likert-opts:before { content: ''; position:relative; top:11px; /*left:9.5%;*/ display:block; background-color:#efefef; height:4px; width:100%; }" +
|
|
102
|
+
".jspsych-survey-likert-opts:last-of-type { border-bottom: 0; }" +
|
|
103
|
+
".jspsych-survey-likert-opts li { display:inline-block; /*width:19%;*/ text-align:center; vertical-align: top; }" +
|
|
104
|
+
".jspsych-survey-likert-opts li input[type=radio] { display:block; position:relative; top:0; left:50%; margin-left:-6px; }";
|
|
105
|
+
html += "</style>";
|
|
106
|
+
// show preamble text
|
|
107
|
+
if (trial.preamble !== null) {
|
|
108
|
+
html +=
|
|
109
|
+
'<div id="jspsych-survey-likert-preamble" class="jspsych-survey-likert-preamble">' +
|
|
110
|
+
trial.preamble +
|
|
111
|
+
"</div>";
|
|
112
|
+
}
|
|
113
|
+
if (trial.autocomplete) {
|
|
114
|
+
html += '<form id="jspsych-survey-likert-form">';
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
html += '<form id="jspsych-survey-likert-form" autocomplete="off">';
|
|
118
|
+
}
|
|
119
|
+
// add likert scale questions ///
|
|
120
|
+
// generate question order. this is randomized here as opposed to randomizing the order of trial.questions
|
|
121
|
+
// so that the data are always associated with the same question regardless of order
|
|
122
|
+
var question_order = [];
|
|
123
|
+
for (var i = 0; i < trial.questions.length; i++) {
|
|
124
|
+
question_order.push(i);
|
|
125
|
+
}
|
|
126
|
+
if (trial.randomize_question_order) {
|
|
127
|
+
question_order = this.jsPsych.randomization.shuffle(question_order);
|
|
128
|
+
}
|
|
129
|
+
for (var i = 0; i < trial.questions.length; i++) {
|
|
130
|
+
var question = trial.questions[question_order[i]];
|
|
131
|
+
// add question
|
|
132
|
+
html += '<label class="jspsych-survey-likert-statement">' + question.prompt + "</label>";
|
|
133
|
+
// add options
|
|
134
|
+
var width = 100 / question.labels.length;
|
|
135
|
+
var options_string = '<ul class="jspsych-survey-likert-opts" data-name="' +
|
|
136
|
+
question.name +
|
|
137
|
+
'" data-radio-group="Q' +
|
|
138
|
+
question_order[i] +
|
|
139
|
+
'">';
|
|
140
|
+
for (var j = 0; j < question.labels.length; j++) {
|
|
141
|
+
options_string +=
|
|
142
|
+
'<li style="width:' +
|
|
143
|
+
width +
|
|
144
|
+
'%"><label class="jspsych-survey-likert-opt-label"><input type="radio" name="Q' +
|
|
145
|
+
question_order[i] +
|
|
146
|
+
'" value="' +
|
|
147
|
+
j +
|
|
148
|
+
'"';
|
|
149
|
+
if (question.required) {
|
|
150
|
+
options_string += " required";
|
|
151
|
+
}
|
|
152
|
+
options_string += ">" + question.labels[j] + "</label></li>";
|
|
153
|
+
}
|
|
154
|
+
options_string += "</ul>";
|
|
155
|
+
html += options_string;
|
|
156
|
+
}
|
|
157
|
+
// add submit button
|
|
158
|
+
html +=
|
|
159
|
+
'<input type="submit" id="jspsych-survey-likert-next" class="jspsych-survey-likert jspsych-btn" value="' +
|
|
160
|
+
trial.button_label +
|
|
161
|
+
'"></input>';
|
|
162
|
+
html += "</form>";
|
|
163
|
+
display_element.innerHTML = html;
|
|
164
|
+
display_element.querySelector("#jspsych-survey-likert-form").addEventListener("submit", (e) => {
|
|
165
|
+
e.preventDefault();
|
|
166
|
+
// measure response time
|
|
167
|
+
var endTime = performance.now();
|
|
168
|
+
var response_time = Math.round(endTime - startTime);
|
|
169
|
+
// create object to hold responses
|
|
170
|
+
var question_data = {};
|
|
171
|
+
var matches = display_element.querySelectorAll("#jspsych-survey-likert-form .jspsych-survey-likert-opts");
|
|
172
|
+
for (var index = 0; index < matches.length; index++) {
|
|
173
|
+
var id = matches[index].dataset["radioGroup"];
|
|
174
|
+
var el = display_element.querySelector('input[name="' + id + '"]:checked');
|
|
175
|
+
if (el === null) {
|
|
176
|
+
var response = "";
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
var response = parseInt(el.value);
|
|
180
|
+
}
|
|
181
|
+
var obje = {};
|
|
182
|
+
if (matches[index].attributes["data-name"].value !== "") {
|
|
183
|
+
var name = matches[index].attributes["data-name"].value;
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
var name = id;
|
|
187
|
+
}
|
|
188
|
+
obje[name] = response;
|
|
189
|
+
Object.assign(question_data, obje);
|
|
190
|
+
}
|
|
191
|
+
// save data
|
|
192
|
+
var trial_data = {
|
|
193
|
+
rt: response_time,
|
|
194
|
+
response: question_data,
|
|
195
|
+
question_order: question_order,
|
|
196
|
+
};
|
|
197
|
+
display_element.innerHTML = "";
|
|
198
|
+
// next trial
|
|
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 = 1000;
|
|
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.randomInt(0, q.labels.length - 1);
|
|
218
|
+
rt += this.jsPsych.randomization.sampleExGaussian(1500, 400, 1 / 200, true);
|
|
219
|
+
}
|
|
220
|
+
const default_data = {
|
|
221
|
+
response: question_data,
|
|
222
|
+
rt: rt,
|
|
223
|
+
question_order: trial.randomize_question_order
|
|
224
|
+
? this.jsPsych.randomization.shuffle([...Array(trial.questions.length).keys()])
|
|
225
|
+
: [...Array(trial.questions.length).keys()],
|
|
226
|
+
};
|
|
227
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
228
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
229
|
+
return data;
|
|
230
|
+
}
|
|
231
|
+
simulate_data_only(trial, simulation_options) {
|
|
232
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
233
|
+
this.jsPsych.finishTrial(data);
|
|
234
|
+
}
|
|
235
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
236
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
237
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
238
|
+
this.trial(display_element, trial);
|
|
239
|
+
load_callback();
|
|
240
|
+
const answers = Object.entries(data.response);
|
|
241
|
+
for (let i = 0; i < answers.length; i++) {
|
|
242
|
+
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));
|
|
243
|
+
}
|
|
244
|
+
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("#jspsych-survey-likert-next"), data.rt);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
247
|
SurveyLikertPlugin.info = info;
|
|
248
248
|
|
|
249
249
|
module.exports = SurveyLikertPlugin;
|