UNPKG

@jspsych/plugin-survey-text

Version:

a jspsych plugin for free response survey questions

3 lines (2 loc) 5.14 kB
var jsPsychSurveyText=function(r){"use strict";var v={name:"@jspsych/plugin-survey-text",version:"2.0.0",description:"a jspsych plugin for free response survey questions",type:"module",main:"dist/index.cjs",exports:{import:"./dist/index.js",require:"./dist/index.cjs"},typings:"dist/index.d.ts",unpkg:"dist/index.browser.min.js",files:["src","dist"],source:"src/index.ts",scripts:{test:"jest","test:watch":"npm test -- --watch",tsc:"tsc",build:"rollup --config","build:watch":"npm run build -- --watch"},repository:{type:"git",url:"git+https://github.com/jspsych/jsPsych.git",directory:"packages/plugin-survey-text"},author:"Josh de Leeuw",license:"MIT",bugs:{url:"https://github.com/jspsych/jsPsych/issues"},homepage:"https://www.jspsych.org/latest/plugins/survey-text",peerDependencies:{jspsych:">=7.1.0"},devDependencies:{"@jspsych/config":"^3.0.0","@jspsych/test-utils":"^1.2.0"}};const f={name:"survey-text",version:v.version,parameters:{questions:{type:r.ParameterType.COMPLEX,array:!0,default:void 0,nested:{prompt:{type:r.ParameterType.HTML_STRING,default:void 0},placeholder:{type:r.ParameterType.STRING,default:""},rows:{type:r.ParameterType.INT,default:1},columns:{type:r.ParameterType.INT,default:40},required:{type:r.ParameterType.BOOL,default:!1},name:{type:r.ParameterType.STRING,default:""}}},randomize_question_order:{type:r.ParameterType.BOOL,default:!1},preamble:{type:r.ParameterType.HTML_STRING,default:null},button_label:{type:r.ParameterType.STRING,default:"Continue"},autocomplete:{type:r.ParameterType.BOOL,default:!1}},data:{response:{type:r.ParameterType.COMPLEX,nested:{identifier:{type:r.ParameterType.STRING},response:{type:r.ParameterType.STRING|r.ParameterType.INT|r.ParameterType.FLOAT|r.ParameterType.BOOL|r.ParameterType.OBJECT}}},rt:{type:r.ParameterType.INT},question_order:{type:r.ParameterType.INT,array:!0}}};class m{constructor(i){this.jsPsych=i}trial(i,t){for(var e=0;e<t.questions.length;e++)typeof t.questions[e].rows=="undefined"&&(t.questions[e].rows=1);for(var e=0;e<t.questions.length;e++)typeof t.questions[e].columns=="undefined"&&(t.questions[e].columns=40);for(var e=0;e<t.questions.length;e++)typeof t.questions[e].value=="undefined"&&(t.questions[e].value="");var s="";t.preamble!==null&&(s+='<div id="jspsych-survey-text-preamble" class="jspsych-survey-text-preamble">'+t.preamble+"</div>"),t.autocomplete?s+='<form id="jspsych-survey-text-form">':s+='<form id="jspsych-survey-text-form" autocomplete="off">';for(var o=[],e=0;e<t.questions.length;e++)o.push(e);t.randomize_question_order&&(o=this.jsPsych.randomization.shuffle(o));for(var e=0;e<t.questions.length;e++){var n=t.questions[o[e]],a=o[e];s+='<div id="jspsych-survey-text-'+a+'" class="jspsych-survey-text-question" style="margin: 2em 0em;">',s+='<p class="jspsych-survey-text">'+n.prompt+"</p>";var u=e==0?"autofocus":"",p=n.required?"required":"";n.rows==1?s+='<input type="text" id="input-'+a+'" name="#jspsych-survey-text-response-'+a+'" data-name="'+n.name+'" size="'+n.columns+'" '+u+" "+p+' placeholder="'+n.placeholder+'"></input>':s+='<textarea id="input-'+a+'" name="#jspsych-survey-text-response-'+a+'" data-name="'+n.name+'" cols="'+n.columns+'" rows="'+n.rows+'" '+u+" "+p+' placeholder="'+n.placeholder+'"></textarea>',s+="</div>"}s+='<input type="submit" id="jspsych-survey-text-next" class="jspsych-btn jspsych-survey-text" value="'+t.button_label+'"></input>',s+="</form>",i.innerHTML=s,i.querySelector("#input-"+o[0]).focus(),i.querySelector("#jspsych-survey-text-form").addEventListener("submit",T=>{T.preventDefault();for(var x=performance.now(),g=Math.round(x-P),c={},l=0;l<t.questions.length;l++){var q="Q"+l,d=document.querySelector("#jspsych-survey-text-"+l).querySelector("textarea, input"),j=d.value,y=d.attributes["data-name"].value;y==""&&(y=q);var h={};h[y]=j,Object.assign(c,h)}var b={rt:g,response:c};this.jsPsych.finishTrial(b)});var P=performance.now()}simulate(i,t,e,s){t=="data-only"&&(s(),this.simulate_data_only(i,e)),t=="visual"&&this.simulate_visual(i,e,s)}create_simulation_data(i,t){const e={};let s=1e3;for(const a of i.questions){const u=a.name?a.name:`Q${i.questions.indexOf(a)}`,p=a.rows==1?this.jsPsych.randomization.sampleExponential(.25):this.jsPsych.randomization.randomInt(1,10)*a.rows;e[u]=this.jsPsych.randomization.randomWords({exactly:p,join:" "}),s+=this.jsPsych.randomization.sampleExGaussian(2e3,400,.004,!0)}const o={response:e,rt:s},n=this.jsPsych.pluginAPI.mergeSimulationData(o,t);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(i,n),n}simulate_data_only(i,t){const e=this.create_simulation_data(i,t);this.jsPsych.finishTrial(e)}simulate_visual(i,t,e){const s=this.create_simulation_data(i,t),o=this.jsPsych.getDisplayElement();this.trial(o,i),e();const n=Object.entries(s.response).map(a=>a[1]);for(let a=0;a<n.length;a++)this.jsPsych.pluginAPI.fillTextInput(o.querySelector(`#input-${a}`),n[a],(s.rt-1e3)/n.length*(a+1));this.jsPsych.pluginAPI.clickTarget(o.querySelector("#jspsych-survey-text-next"),s.rt)}}return m.info=f,m}(jsPsychModule); //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-survey-text@2.0.0/dist/index.browser.min.js.map