{"version":3,"file":"diagnostic_form_controller-BHuda4Rh.js","names":[],"sources":["../../../app/javascript/entrypoints/site_controllers/diagnostic_form_controller.js"],"sourcesContent":["import { Controller } from \"@hotwired/stimulus\"\n\n// Diagnostic questionnaire: shows one step at a time with a progress bar, and does not move on\n// while a required question has no answer. Without JavaScript the whole form shows and posts as is;\n// the server checks the answers again.\nexport default class extends Controller {\n static targets = [\"step\", \"bar\", \"nav\", \"error\", \"question\"]\n static values = { required: String }\n\n connect() {\n this.element.classList.add(\"is-stepped\")\n this.navTargets.forEach((button) => { button.hidden = false })\n const withError = this.stepTargets.findIndex((step) => step.dataset.hasError === \"true\")\n this.index = Math.max(withError, 0)\n this.show(false)\n }\n\n next() {\n if (!this.complete(this.stepTargets[this.index])) return\n this.index = Math.min(this.index + 1, this.stepTargets.length - 1)\n this.show(true)\n }\n\n back() {\n this.index = Math.max(this.index - 1, 0)\n this.show(true)\n }\n\n // Last step: the browser would post without checking radio groups, so check them here.\n validate(event) {\n const firstIncomplete = this.stepTargets.findIndex((step) => !this.complete(step, false))\n if (firstIncomplete === -1) return\n event.preventDefault()\n this.index = firstIncomplete\n this.show(true)\n this.complete(this.stepTargets[this.index])\n }\n\n complete(step, report = true) {\n const missing = this.questionTargets\n .filter((question) => step.contains(question) && question.hasAttribute(\"data-required\"))\n .filter((question) => !question.querySelector(\"input:checked\"))\n if (!report) return missing.length === 0\n\n this.questionTargets.forEach((question) => question.classList.toggle(\"is-missing\", missing.includes(question)))\n const error = this.errorTargets.find((target) => step.contains(target))\n if (error) {\n error.textContent = missing.length ? this.requiredValue : \"\"\n error.hidden = missing.length === 0\n }\n if (missing.length) missing[0].querySelector(\"input\")?.focus()\n return missing.length === 0\n }\n\n show(scroll) {\n this.stepTargets.forEach((step, i) => step.classList.toggle(\"is-current\", i === this.index))\n this.barTargets.forEach((bar, i) => bar.classList.toggle(\"on\", i <= this.index))\n if (!scroll) return\n\n const behavior = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches ? \"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