UNPKG

@jspsych/plugin-survey-html-form

Version:
1 lines 8.49 kB
{"version":3,"file":"index.cjs","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-survey-html-form\",\n \"version\": \"2.1.0\",\n \"description\": \"a jspsych plugin for free html forms\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-survey-html-form\"\n },\n \"author\": \"Jan Simson\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/survey-html-form\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"survey-html-form\",\n version: version,\n parameters: {\n /** HTML formatted string containing all the input elements to display. Every element has to have its own distinctive name attribute. The <form> tag must not be included and is generated by the plugin. */\n html: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** HTML formatted string to display at the top of the page above all the questions. */\n preamble: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** The text that appears on the button to finish the trial. */\n button_label: {\n type: ParameterType.STRING,\n default: \"Continue\",\n },\n /** The HTML element ID of a form field to autofocus on. */\n autofocus: {\n type: ParameterType.STRING,\n default: \"\",\n },\n /** Retrieve the data as an array e.g. [{name: \"INPUT_NAME\", value: \"INPUT_VALUE\"}, ...] instead of an object e.g. {INPUT_NAME: INPUT_VALUE, ...}. */\n dataAsArray: {\n type: ParameterType.BOOL,\n default: false,\n },\n /** Setting this to true will enable browser auto-complete or auto-fill for the form. */\n autocomplete: {\n type: ParameterType.BOOL,\n default: false,\n },\n },\n data: {\n /** An object containing the response for each input. The object will have a separate key (variable) for the response to each input, with each variable being named after its corresponding input element. Each response is a string containing whatever the participant answered for this particular input. This will be encoded as a JSON string when data is saved using the `.json()` or `.csv()` functions. */\n response: {\n type: ParameterType.OBJECT,\n },\n /** The response time in milliseconds for the participant to make a response. */\n rt: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n *\n * The survey-html-form plugin displays a set of `<inputs>` from a HTML string. The type of input can be freely\n * chosen, for a list of possible input types see the [MDN page on inputs](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).\n * The participant provides answers to the input fields.\n * @author Jan Simson\n * @see {@link https://www.jspsych.org/latest/plugins/survey-html-form/ survey-html-form plugin documentation on jspsych.org}\n */\nclass SurveyHtmlFormPlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var html = \"\";\n // show preamble text\n if (trial.preamble !== null) {\n html +=\n '<div id=\"jspsych-survey-html-form-preamble\" class=\"jspsych-survey-html-form-preamble\">' +\n trial.preamble +\n \"</div>\";\n }\n // start form\n if (trial.autocomplete) {\n html += '<form id=\"jspsych-survey-html-form\">';\n } else {\n html += '<form id=\"jspsych-survey-html-form\" autocomplete=\"off\">';\n }\n\n // add form HTML / input elements\n html += trial.html;\n\n // add submit button\n html +=\n '<input type=\"submit\" id=\"jspsych-survey-html-form-next\" class=\"jspsych-btn jspsych-survey-html-form\" value=\"' +\n trial.button_label +\n '\"></input>';\n\n html += \"</form>\";\n display_element.innerHTML = html;\n\n if (trial.autofocus !== \"\") {\n var focus_elements = display_element.querySelectorAll<HTMLInputElement>(\n \"#\" + trial.autofocus\n );\n if (focus_elements.length === 0) {\n console.warn(\"No element found with id: \" + trial.autofocus);\n } else if (focus_elements.length > 1) {\n console.warn('The id \"' + trial.autofocus + '\" is not unique so autofocus will not work.');\n } else {\n focus_elements[0].focus();\n }\n }\n\n display_element\n .querySelector(\"#jspsych-survey-html-form\")\n .addEventListener(\"submit\", (event) => {\n // don't submit form\n event.preventDefault();\n\n // measure response time\n var endTime = performance.now();\n var response_time = Math.round(endTime - startTime);\n\n var this_form = display_element.querySelector(\"#jspsych-survey-html-form\");\n var question_data = serializeArray(this_form);\n\n if (!trial.dataAsArray) {\n question_data = objectifyForm(question_data);\n }\n\n // save data\n var trialdata = {\n rt: response_time,\n response: question_data,\n };\n\n // next trial\n this.jsPsych.finishTrial(trialdata);\n });\n\n var startTime = performance.now();\n\n /**\n * Serialize all form data into an array\n * @copyright (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com\n * @param {Node} form The form to serialize\n * @return {String} The serialized form data\n */\n function serializeArray(form) {\n // Setup our serialized data\n var serialized = [];\n\n // Loop through each field in the form\n for (var i = 0; i < form.elements.length; i++) {\n var field = form.elements[i];\n\n // Don't serialize fields without a name, submits, buttons, file and reset inputs, and disabled fields\n if (\n !field.name ||\n field.disabled ||\n field.type === \"file\" ||\n field.type === \"reset\" ||\n field.type === \"submit\" ||\n field.type === \"button\"\n )\n continue;\n\n // If a multi-select, get all selections\n if (field.type === \"select-multiple\") {\n for (var n = 0; n < field.options.length; n++) {\n if (!field.options[n].selected) continue;\n serialized.push({\n name: field.name,\n value: field.options[n].value,\n });\n }\n }\n\n // Convert field data to a query string\n else if ((field.type !== \"checkbox\" && field.type !== \"radio\") || field.checked) {\n serialized.push({\n name: field.name,\n value: field.value,\n });\n }\n }\n\n return serialized;\n }\n\n // from https://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery\n function objectifyForm(formArray) {\n //serialize data function\n var returnArray = <any>{};\n for (var i = 0; i < formArray.length; i++) {\n returnArray[formArray[i][\"name\"]] = formArray[i][\"value\"];\n }\n return returnArray;\n }\n }\n}\n\nexport default SurveyHtmlFormPlugin;\n"],"names":[],"mappings":";;;;AAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECgDA,SAAA,EAAA;AAAA;;GAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}