{"version":3,"file":"radios.mjs","sources":["../../../../src/govuk/components/radios/radios.mjs"],"sourcesContent":["/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */\n\nimport { nodeListForEach } from '../../common/index.mjs'\nimport '../../vendor/polyfills/Element/prototype/classList.mjs'\nimport '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded\nimport '../../vendor/polyfills/Function/prototype/bind.mjs'\n\n/**\n * Radios component\n *\n * @class\n * @param {Element} $module - HTML element to use for radios\n */\nfunction Radios ($module) {\n if (!($module instanceof HTMLElement)) {\n return this\n }\n\n var $inputs = $module.querySelectorAll('input[type=\"radio\"]')\n if (!$inputs.length) {\n return this\n }\n\n /** @deprecated Will be made private in v5.0 */\n this.$module = $module\n\n /** @deprecated Will be made private in v5.0 */\n this.$inputs = $inputs\n}\n\n/**\n * Initialise component\n *\n * Radios can be associated with a 'conditionally revealed' content block – for\n * example, a radio for 'Phone' could reveal an additional form field for the\n * user to enter their phone number.\n *\n * These associations are made using a `data-aria-controls` attribute, which is\n * promoted to an aria-controls attribute during initialisation.\n *\n * We also need to restore the state of any conditional reveals on the page (for\n * example if the user has navigated back), and set up event handlers to keep\n * the reveal in sync with the radio state.\n */\nRadios.prototype.init = function () {\n // Check that required elements are present\n if (!this.$module || !this.$inputs) {\n return\n }\n\n var $module = this.$module\n var $inputs = this.$inputs\n\n nodeListForEach($inputs, function ($input) {\n var targetId = $input.getAttribute('data-aria-controls')\n\n // Skip radios without data-aria-controls attributes, or where the\n // target element does not exist.\n if (!targetId || !document.getElementById(targetId)) {\n return\n }\n\n // Promote the data-aria-controls attribute to a aria-controls attribute\n // so that the relationship is exposed in the AOM\n $input.setAttribute('aria-controls', targetId)\n $input.removeAttribute('data-aria-controls')\n })\n\n // When the page is restored after navigating 'back' in some browsers the\n // state of form controls is not restored until *after* the DOMContentLoaded\n // event is fired, so we need to sync after the pageshow event in browsers\n // that support it.\n window.addEventListener(\n 'onpageshow' in window ? 'pageshow' : 'DOMContentLoaded',\n this.syncAllConditionalReveals.bind(this)\n )\n\n // Although we've set up handlers to sync state on the pageshow or\n // DOMContentLoaded event, init could be called after those events have fired,\n // for example if they are added to the page dynamically, so sync now too.\n this.syncAllConditionalReveals()\n\n // Handle events\n $module.addEventListener('click', this.handleClick.bind(this))\n}\n\n/**\n * Sync the conditional reveal states for all radio buttons in this $module.\n *\n * @deprecated Will be made private in v5.0\n */\nRadios.prototype.syncAllConditionalReveals = function () {\n nodeListForEach(this.$inputs, this.syncConditionalRevealWithInputState.bind(this))\n}\n\n/**\n * Sync conditional reveal with the input state\n *\n * Synchronise the visibility of the conditional reveal, and its accessible\n * state, with the input's checked state.\n *\n * @deprecated Will be made private in v5.0\n * @param {HTMLInputElement} $input - Radio input\n */\nRadios.prototype.syncConditionalRevealWithInputState = function ($input) {\n var targetId = $input.getAttribute('aria-controls')\n if (!targetId) {\n return\n }\n\n var $target = document.getElementById(targetId)\n if ($target && $target.classList.contains('govuk-radios__conditional')) {\n var inputIsChecked = $input.checked\n\n $input.setAttribute('aria-expanded', inputIsChecked.toString())\n $target.classList.toggle('govuk-radios__conditional--hidden', !inputIsChecked)\n }\n}\n\n/**\n * Click event handler\n *\n * Handle a click within the $module – if the click occurred on a radio, sync\n * the state of the conditional reveal for all radio buttons in the same form\n * with the same name (because checking one radio could have un-checked a radio\n * in another $module)\n *\n * @deprecated Will be made private in v5.0\n * @param {MouseEvent} event - Click event\n */\nRadios.prototype.handleClick = function (event) {\n var $component = this\n var $clickedInput = event.target\n\n // Ignore clicks on things that aren't radio buttons\n if (!($clickedInput instanceof HTMLInputElement) || $clickedInput.type !== 'radio') {\n return\n }\n\n // We only need to consider radios with conditional reveals, which will have\n // aria-controls attributes.\n var $allInputs = document.querySelectorAll('input[type=\"radio\"][aria-controls]')\n\n var $clickedInputForm = $clickedInput.form\n var $clickedInputName = $clickedInput.name\n\n nodeListForEach($allInputs, function ($input) {\n var hasSameFormOwner = $input.form === $clickedInputForm\n var hasSameName = $input.name === $clickedInputName\n\n if (hasSameName && hasSameFormOwner) {\n $component.syncConditionalRevealWithInputState($input)\n }\n })\n}\n\nexport default Radios\n"],"names":[],"mappings":";;;;;AAAA;AACA,AAKA;;;;;;;AAOA,SAAS,MAAM,EAAE,OAAO,EAAE;EACxB,IAAI,EAAE,OAAO,YAAY,WAAW,CAAC,EAAE;IACrC,OAAO,IAAI;GACZ;;EAED,IAAI,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,qBAAqB,EAAC;EAC7D,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;IACnB,OAAO,IAAI;GACZ;;;EAGD,IAAI,CAAC,OAAO,GAAG,QAAO;;;EAGtB,IAAI,CAAC,OAAO,GAAG,QAAO;CACvB;;;;;;;;;;;;;;;;AAgBD,MAAM,CAAC,SAAS,CAAC,IAAI,GAAG,YAAY;;EAElC,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;IAClC,MAAM;GACP;;EAED,IAAI,OAAO,GAAG,IAAI,CAAC,QAAO;EAC1B,IAAI,OAAO,GAAG,IAAI,CAAC,QAAO;;EAE1B,eAAe,CAAC,OAAO,EAAE,UAAU,MAAM,EAAE;IACzC,IAAI,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,oBAAoB,EAAC;;;;IAIxD,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,QAAQ,CAAC,EAAE;MACnD,MAAM;KACP;;;;IAID,MAAM,CAAC,YAAY,CAAC,eAAe,EAAE,QAAQ,EAAC;IAC9C,MAAM,CAAC,eAAe,CAAC,oBAAoB,EAAC;GAC7C,EAAC;;;;;;EAMF,MAAM,CAAC,gBAAgB;IACrB,YAAY,IAAI,MAAM,GAAG,UAAU,GAAG,kBAAkB;IACxD,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC;IAC1C;;;;;EAKD,IAAI,CAAC,yBAAyB,GAAE;;;EAGhC,OAA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