katalyst-govuk-formbuilder 1.20.0 → 1.21.1

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.
@@ -1,12 +1,5 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
2
 
3
- function getFragmentFromUrl(url) {
4
- if (!url.includes("#")) {
5
- return undefined;
6
- }
7
- return url.split("#").pop();
8
- }
9
-
10
3
  function setFocus($element, options = {}) {
11
4
  var _options$onBeforeFocu;
12
5
  const isFocusable = $element.getAttribute("tabindex");
@@ -80,11 +73,13 @@ class ConfigError extends GOVUKFrontendError {
80
73
  class ElementError extends GOVUKFrontendError {
81
74
  constructor(messageOrOptions) {
82
75
  let message = typeof messageOrOptions === "string" ? messageOrOptions : "";
83
- if (typeof messageOrOptions === "object") {
76
+ if (isObject(messageOrOptions)) {
84
77
  const {component: component, identifier: identifier, element: element, expectedType: expectedType} = messageOrOptions;
85
78
  message = identifier;
86
79
  message += element ? ` is not of type ${expectedType != null ? expectedType : "HTMLElement"}` : " not found";
87
- message = formatErrorMessage(component, message);
80
+ if (component) {
81
+ message = formatErrorMessage(component, message);
82
+ }
88
83
  }
89
84
  super(message);
90
85
  this.name = "ElementError";
@@ -281,7 +276,7 @@ class I18n {
281
276
  throw new Error("i18n: lookup key missing");
282
277
  }
283
278
  let translation = this.translations[lookupKey];
284
- if (typeof (options == null ? void 0 : options.count) === "number" && typeof translation === "object") {
279
+ if (typeof (options == null ? void 0 : options.count) === "number" && isObject(translation)) {
285
280
  const translationPluralForm = translation[this.getPluralSuffix(lookupKey, options.count)];
286
281
  if (translationPluralForm) {
287
282
  translation = translationPluralForm;
@@ -324,7 +319,7 @@ class I18n {
324
319
  }
325
320
  const translation = this.translations[lookupKey];
326
321
  const preferredForm = this.hasIntlPluralRulesSupport() ? new Intl.PluralRules(this.locale).select(count) : this.selectPluralFormUsingFallbackRules(count);
327
- if (typeof translation === "object") {
322
+ if (isObject(translation)) {
328
323
  if (preferredForm in translation) {
329
324
  return preferredForm;
330
325
  } else if ("other" in translation) {
@@ -582,7 +577,7 @@ function closestAttributeValue($element, attributeName) {
582
577
  });
583
578
  }
584
579
  this.$errorMessage = this.$root.querySelector(".govuk-error-message");
585
- if (`${$textareaDescription.textContent}`.match(/^\s*$/)) {
580
+ if ($textareaDescription.textContent.match(/^\s*$/)) {
586
581
  $textareaDescription.textContent = this.i18n.t("textareaDescription", {
587
582
  count: this.maxLength
588
583
  });
@@ -859,7 +854,7 @@ Checkboxes.moduleName = "govuk-checkboxes";
859
854
  if (!($target instanceof HTMLAnchorElement)) {
860
855
  return false;
861
856
  }
862
- const inputId = getFragmentFromUrl($target.href);
857
+ const inputId = $target.hash.replace("#", "");
863
858
  if (!inputId) {
864
859
  return false;
865
860
  }
@@ -915,6 +910,219 @@ ErrorSummary.schema = Object.freeze({
915
910
  }
916
911
  });
917
912
 
913
+ /**
914
+ * File upload component
915
+ *
916
+ * @preserve
917
+ * @augments ConfigurableComponent<FileUploadConfig>
918
+ */ class FileUpload extends ConfigurableComponent {
919
+ constructor($root, config = {}) {
920
+ super($root, config);
921
+ this.$input = void 0;
922
+ this.$button = void 0;
923
+ this.$status = void 0;
924
+ this.i18n = void 0;
925
+ this.id = void 0;
926
+ this.$announcements = void 0;
927
+ this.enteredAnotherElement = void 0;
928
+ const $input = this.$root.querySelector("input");
929
+ if ($input === null) {
930
+ throw new ElementError({
931
+ component: FileUpload,
932
+ identifier: 'File inputs (`<input type="file">`)'
933
+ });
934
+ }
935
+ if ($input.type !== "file") {
936
+ throw new ElementError(formatErrorMessage(FileUpload, 'File input (`<input type="file">`) attribute (`type`) is not `file`'));
937
+ }
938
+ this.$input = $input;
939
+ if (!this.$input.id) {
940
+ throw new ElementError({
941
+ component: FileUpload,
942
+ identifier: 'File input (`<input type="file">`) attribute (`id`)'
943
+ });
944
+ }
945
+ this.id = this.$input.id;
946
+ this.i18n = new I18n(this.config.i18n, {
947
+ locale: closestAttributeValue(this.$root, "lang")
948
+ });
949
+ const $label = this.findLabel();
950
+ if (!$label.id) {
951
+ $label.id = `${this.id}-label`;
952
+ }
953
+ this.$input.id = `${this.id}-input`;
954
+ this.$input.setAttribute("hidden", "true");
955
+ const $button = document.createElement("button");
956
+ $button.classList.add("govuk-file-upload-button");
957
+ $button.type = "button";
958
+ $button.id = this.id;
959
+ $button.classList.add("govuk-file-upload-button--empty");
960
+ const ariaDescribedBy = this.$input.getAttribute("aria-describedby");
961
+ if (ariaDescribedBy) {
962
+ $button.setAttribute("aria-describedby", ariaDescribedBy);
963
+ }
964
+ const $status = document.createElement("span");
965
+ $status.className = "govuk-body govuk-file-upload-button__status";
966
+ $status.setAttribute("aria-live", "polite");
967
+ $status.innerText = this.i18n.t("noFileChosen");
968
+ $button.appendChild($status);
969
+ const commaSpan = document.createElement("span");
970
+ commaSpan.className = "govuk-visually-hidden";
971
+ commaSpan.innerText = ", ";
972
+ commaSpan.id = `${this.id}-comma`;
973
+ $button.appendChild(commaSpan);
974
+ const containerSpan = document.createElement("span");
975
+ containerSpan.className = "govuk-file-upload-button__pseudo-button-container";
976
+ const buttonSpan = document.createElement("span");
977
+ buttonSpan.className = "govuk-button govuk-button--secondary govuk-file-upload-button__pseudo-button";
978
+ buttonSpan.innerText = this.i18n.t("chooseFilesButton");
979
+ containerSpan.appendChild(buttonSpan);
980
+ containerSpan.insertAdjacentText("beforeend", " ");
981
+ const instructionSpan = document.createElement("span");
982
+ instructionSpan.className = "govuk-body govuk-file-upload-button__instruction";
983
+ instructionSpan.innerText = this.i18n.t("dropInstruction");
984
+ containerSpan.appendChild(instructionSpan);
985
+ $button.appendChild(containerSpan);
986
+ $button.setAttribute("aria-labelledby", `${$label.id} ${commaSpan.id} ${$button.id}`);
987
+ $button.addEventListener("click", this.onClick.bind(this));
988
+ $button.addEventListener("dragover", event => {
989
+ event.preventDefault();
990
+ });
991
+ this.$root.insertAdjacentElement("afterbegin", $button);
992
+ this.$input.setAttribute("tabindex", "-1");
993
+ this.$input.setAttribute("aria-hidden", "true");
994
+ this.$button = $button;
995
+ this.$status = $status;
996
+ this.$input.addEventListener("change", this.onChange.bind(this));
997
+ this.updateDisabledState();
998
+ this.observeDisabledState();
999
+ this.$announcements = document.createElement("span");
1000
+ this.$announcements.classList.add("govuk-file-upload-announcements");
1001
+ this.$announcements.classList.add("govuk-visually-hidden");
1002
+ this.$announcements.setAttribute("aria-live", "assertive");
1003
+ this.$root.insertAdjacentElement("afterend", this.$announcements);
1004
+ this.$button.addEventListener("drop", this.onDrop.bind(this));
1005
+ document.addEventListener("dragenter", this.updateDropzoneVisibility.bind(this));
1006
+ document.addEventListener("dragenter", () => {
1007
+ this.enteredAnotherElement = true;
1008
+ });
1009
+ document.addEventListener("dragleave", () => {
1010
+ if (!this.enteredAnotherElement && !this.$button.disabled) {
1011
+ this.hideDraggingState();
1012
+ this.$announcements.innerText = this.i18n.t("leftDropZone");
1013
+ }
1014
+ this.enteredAnotherElement = false;
1015
+ });
1016
+ }
1017
+ updateDropzoneVisibility(event) {
1018
+ if (this.$button.disabled) return;
1019
+ if (event.target instanceof Node) {
1020
+ if (this.$root.contains(event.target)) {
1021
+ if (event.dataTransfer && isContainingFiles(event.dataTransfer)) {
1022
+ if (!this.$button.classList.contains("govuk-file-upload-button--dragging")) {
1023
+ this.showDraggingState();
1024
+ this.$announcements.innerText = this.i18n.t("enteredDropZone");
1025
+ }
1026
+ }
1027
+ } else {
1028
+ if (this.$button.classList.contains("govuk-file-upload-button--dragging")) {
1029
+ this.hideDraggingState();
1030
+ this.$announcements.innerText = this.i18n.t("leftDropZone");
1031
+ }
1032
+ }
1033
+ }
1034
+ }
1035
+ showDraggingState() {
1036
+ this.$button.classList.add("govuk-file-upload-button--dragging");
1037
+ }
1038
+ hideDraggingState() {
1039
+ this.$button.classList.remove("govuk-file-upload-button--dragging");
1040
+ }
1041
+ onDrop(event) {
1042
+ event.preventDefault();
1043
+ if (event.dataTransfer && isContainingFiles(event.dataTransfer)) {
1044
+ this.$input.files = event.dataTransfer.files;
1045
+ this.$input.dispatchEvent(new CustomEvent("change"));
1046
+ this.hideDraggingState();
1047
+ }
1048
+ }
1049
+ onChange() {
1050
+ const fileCount = this.$input.files.length;
1051
+ if (fileCount === 0) {
1052
+ this.$status.innerText = this.i18n.t("noFileChosen");
1053
+ this.$button.classList.add("govuk-file-upload-button--empty");
1054
+ } else {
1055
+ if (fileCount === 1) {
1056
+ this.$status.innerText = this.$input.files[0].name;
1057
+ } else {
1058
+ this.$status.innerText = this.i18n.t("multipleFilesChosen", {
1059
+ count: fileCount
1060
+ });
1061
+ }
1062
+ this.$button.classList.remove("govuk-file-upload-button--empty");
1063
+ }
1064
+ }
1065
+ findLabel() {
1066
+ const $label = document.querySelector(`label[for="${this.$input.id}"]`);
1067
+ if (!$label) {
1068
+ throw new ElementError({
1069
+ component: FileUpload,
1070
+ identifier: `Field label (\`<label for=${this.$input.id}>\`)`
1071
+ });
1072
+ }
1073
+ return $label;
1074
+ }
1075
+ onClick() {
1076
+ this.$input.click();
1077
+ }
1078
+ observeDisabledState() {
1079
+ const observer = new MutationObserver(mutationList => {
1080
+ for (const mutation of mutationList) {
1081
+ if (mutation.type === "attributes" && mutation.attributeName === "disabled") {
1082
+ this.updateDisabledState();
1083
+ }
1084
+ }
1085
+ });
1086
+ observer.observe(this.$input, {
1087
+ attributes: true
1088
+ });
1089
+ }
1090
+ updateDisabledState() {
1091
+ this.$button.disabled = this.$input.disabled;
1092
+ this.$root.classList.toggle("govuk-drop-zone--disabled", this.$button.disabled);
1093
+ }
1094
+ }
1095
+
1096
+ FileUpload.moduleName = "govuk-file-upload";
1097
+
1098
+ FileUpload.defaults = Object.freeze({
1099
+ i18n: {
1100
+ chooseFilesButton: "Choose file",
1101
+ dropInstruction: "or drop file",
1102
+ noFileChosen: "No file chosen",
1103
+ multipleFilesChosen: {
1104
+ one: "%{count} file chosen",
1105
+ other: "%{count} files chosen"
1106
+ },
1107
+ enteredDropZone: "Entered drop zone",
1108
+ leftDropZone: "Left drop zone"
1109
+ }
1110
+ });
1111
+
1112
+ FileUpload.schema = Object.freeze({
1113
+ properties: {
1114
+ i18n: {
1115
+ type: "object"
1116
+ }
1117
+ }
1118
+ });
1119
+
1120
+ function isContainingFiles(dataTransfer) {
1121
+ const hasNoTypesInfo = dataTransfer.types.length === 0;
1122
+ const isDraggingFiles = dataTransfer.types.some(type => type === "Files");
1123
+ return hasNoTypesInfo || isDraggingFiles;
1124
+ }
1125
+
918
1126
  /**
919
1127
  * Password input component
920
1128
  *
@@ -1259,7 +1467,7 @@ function initAll(config) {
1259
1467
  console.log(new SupportError);
1260
1468
  return;
1261
1469
  }
1262
- const components = [ [ Button, config.button ], [ CharacterCount, config.characterCount ], [ Checkboxes ], [ ErrorSummary, config.errorSummary ], [ Radios ], [ PasswordInput, config.passwordInput ] ];
1470
+ const components = [ [ Button, config.button ], [ CharacterCount, config.characterCount ], [ Checkboxes ], [ ErrorSummary, config.errorSummary ], [ FileUpload, config.fileUpload ], [ Radios ], [ PasswordInput, config.passwordInput ] ];
1263
1471
  const $scope = (_config$scope = config.scope) != null ? _config$scope : document;
1264
1472
  components.forEach(([Component, config]) => {
1265
1473
  const $elements = $scope.querySelectorAll(`[data-module="${Component.moduleName}"]`);
@@ -1,11 +1,11 @@
1
- import{Controller as e}from"@hotwired/stimulus";function t(e=document.body){return!!e&&e.classList.contains("govuk-frontend-supported")}function n(e){return!!e&&"object"==typeof e&&!function(e){return Array.isArray(e)}(e)}function s(e,t){return`${e.moduleName}: ${t}`}class i extends Error{constructor(...e){super(...e),this.name="GOVUKFrontendError"}}class o extends i{constructor(e=document.body){const t="noModule"in HTMLScriptElement.prototype?'GOV.UK Frontend initialised without `<body class="govuk-frontend-supported">` from template `<script>` snippet':"GOV.UK Frontend is not supported in this browser";super(e?t:'GOV.UK Frontend initialised without `<script type="module">`'),this.name="SupportError"}}class r extends i{constructor(...e){super(...e),this.name="ConfigError"}}class a extends i{constructor(e){let t="string"==typeof e?e:"";if("object"==typeof e){const{component:n,identifier:i,element:o,expectedType:r}=e;t=i,t+=o?` is not of type ${null!=r?r:"HTMLElement"}`:" not found",t=s(n,t)}super(t),this.name="ElementError"}}class l extends i{constructor(e){super("string"==typeof e?e:s(e,"Root element (`$root`) already initialised")),this.name="InitError"}}class u{get $root(){return this._$root}constructor(e){this._$root=void 0;const t=this.constructor;if("string"!=typeof t.moduleName)throw new l("`moduleName` not defined in component");if(!(e instanceof t.elementType))throw new a({element:e,component:t,identifier:"Root element (`$root`)",expectedType:t.elementType.name});this._$root=e,t.checkSupport(),this.checkInitialised();const n=t.moduleName;this.$root.setAttribute(`data-${n}-init`,"")}checkInitialised(){const e=this.constructor,t=e.moduleName;if(t&&function(e,t){return e instanceof HTMLElement&&e.hasAttribute(`data-${t}-init`)}(this.$root,t))throw new l(e)}static checkSupport(){if(!t())throw new o}}u.elementType=HTMLElement;const c=Symbol.for("configOverride");class h extends u{[c](e){return{}}get config(){return this._config}constructor(e,t){super(e),this._config=void 0;const i=this.constructor;if(!n(i.defaults))throw new r(s(i,"Config passed as parameter into constructor but no defaults defined"));const o=function(e,t){if(!n(e.schema))throw new r(s(e,"Config passed as parameter into constructor but no schema defined"));const i={},o=Object.entries(e.schema.properties);for(const n of o){const[s,o]=n,r=s.toString();r in t&&(i[r]=d(t[r],o)),"object"===(null==o?void 0:o.type)&&(i[r]=m(e.schema,t,s))}return i}(i,this._$root.dataset);this._config=p(i.defaults,null!=t?t:{},this[c](o),o)}}function d(e,t){const n=e?e.trim():"";let s,i=null==t?void 0:t.type;switch(i||(["true","false"].includes(n)&&(i="boolean"),n.length>0&&isFinite(Number(n))&&(i="number")),i){case"boolean":s="true"===n;break;case"number":s=Number(n);break;default:s=e}return s}function p(...e){const t={};for(const s of e)for(const e of Object.keys(s)){const i=t[e],o=s[e];n(i)&&n(o)?t[e]=p(i,o):t[e]=o}return t}function m(e,t,s){const i=e.properties[s];if("object"!==(null==i?void 0:i.type))return;const o={[s]:{}};for(const[e,i]of Object.entries(t)){let t=o;const r=e.split(".");for(const[o,a]of r.entries())n(t)&&(o<r.length-1?(n(t[a])||(t[a]={}),t=t[a]):e!==s&&(t[a]=d(i)))}return o[s]}class f{constructor(e={},t={}){var n;this.translations=void 0,this.locale=void 0,this.translations=e,this.locale=null!=(n=t.locale)?n:document.documentElement.lang||"en"}t(e,t){if(!e)throw new Error("i18n: lookup key missing");let n=this.translations[e];if("number"==typeof(null==t?void 0:t.count)&&"object"==typeof n){const s=n[this.getPluralSuffix(e,t.count)];s&&(n=s)}if("string"==typeof n){if(n.match(/%{(.\S+)}/)){if(!t)throw new Error("i18n: cannot replace placeholders in string if no option data provided");return this.replacePlaceholders(n,t)}return n}return e}replacePlaceholders(e,t){const n=Intl.NumberFormat.supportedLocalesOf(this.locale).length?new Intl.NumberFormat(this.locale):void 0;return e.replace(/%{(.\S+)}/g,function(e,s){if(Object.prototype.hasOwnProperty.call(t,s)){const e=t[s];return!1===e||"number"!=typeof e&&"string"!=typeof e?"":"number"==typeof e?n?n.format(e):`${e}`:e}throw new Error(`i18n: no data found to replace ${e} placeholder in string`)})}hasIntlPluralRulesSupport(){return Boolean("PluralRules"in window.Intl&&Intl.PluralRules.supportedLocalesOf(this.locale).length)}getPluralSuffix(e,t){if(t=Number(t),!isFinite(t))return"other";const n=this.translations[e],s=this.hasIntlPluralRulesSupport()?new Intl.PluralRules(this.locale).select(t):this.selectPluralFormUsingFallbackRules(t);if("object"==typeof n){if(s in n)return s;if("other"in n)return console.warn(`i18n: Missing plural form ".${s}" for "${this.locale}" locale. Falling back to ".other".`),"other"}throw new Error(`i18n: Plural form ".other" is required for "${this.locale}" locale`)}selectPluralFormUsingFallbackRules(e){e=Math.abs(Math.floor(e));const t=this.getPluralRulesForLocale();return t?f.pluralRules[t](e):"other"}getPluralRulesForLocale(){const e=this.locale.split("-")[0];for(const t in f.pluralRulesMap){const n=f.pluralRulesMap[t];if(n.includes(this.locale)||n.includes(e))return t}}}f.pluralRulesMap={arabic:["ar"],chinese:["my","zh","id","ja","jv","ko","ms","th","vi"],french:["hy","bn","fr","gu","hi","fa","pa","zu"],german:["af","sq","az","eu","bg","ca","da","nl","en","et","fi","ka","de","el","hu","lb","no","so","sw","sv","ta","te","tr","ur"],irish:["ga"],russian:["ru","uk"],scottish:["gd"],spanish:["pt-PT","it","es"],welsh:["cy"]},f.pluralRules={arabic:e=>0===e?"zero":1===e?"one":2===e?"two":e%100>=3&&e%100<=10?"few":e%100>=11&&e%100<=99?"many":"other",chinese:()=>"other",french:e=>0===e||1===e?"one":"other",german:e=>1===e?"one":"other",irish:e=>1===e?"one":2===e?"two":e>=3&&e<=6?"few":e>=7&&e<=10?"many":"other",russian(e){const t=e%100,n=t%10;return 1===n&&11!==t?"one":n>=2&&n<=4&&!(t>=12&&t<=14)?"few":0===n||n>=5&&n<=9||t>=11&&t<=14?"many":"other"},scottish:e=>1===e||11===e?"one":2===e||12===e?"two":e>=3&&e<=10||e>=13&&e<=19?"few":"other",spanish:e=>1===e?"one":e%1e6==0&&0!==e?"many":"other",welsh:e=>0===e?"zero":1===e?"one":2===e?"two":3===e?"few":6===e?"many":"other"};
1
+ import{Controller as e}from"@hotwired/stimulus";function t(e=document.body){return!!e&&e.classList.contains("govuk-frontend-supported")}function n(e){return!!e&&"object"==typeof e&&!function(e){return Array.isArray(e)}(e)}function i(e,t){return`${e.moduleName}: ${t}`}class s extends Error{constructor(...e){super(...e),this.name="GOVUKFrontendError"}}class o extends s{constructor(e=document.body){const t="noModule"in HTMLScriptElement.prototype?'GOV.UK Frontend initialised without `<body class="govuk-frontend-supported">` from template `<script>` snippet':"GOV.UK Frontend is not supported in this browser";super(e?t:'GOV.UK Frontend initialised without `<script type="module">`'),this.name="SupportError"}}class r extends s{constructor(...e){super(...e),this.name="ConfigError"}}class a extends s{constructor(e){let t="string"==typeof e?e:"";if(n(e)){const{component:n,identifier:s,element:o,expectedType:r}=e;t=s,t+=o?` is not of type ${null!=r?r:"HTMLElement"}`:" not found",n&&(t=i(n,t))}super(t),this.name="ElementError"}}class l extends s{constructor(e){super("string"==typeof e?e:i(e,"Root element (`$root`) already initialised")),this.name="InitError"}}class u{get $root(){return this._$root}constructor(e){this._$root=void 0;const t=this.constructor;if("string"!=typeof t.moduleName)throw new l("`moduleName` not defined in component");if(!(e instanceof t.elementType))throw new a({element:e,component:t,identifier:"Root element (`$root`)",expectedType:t.elementType.name});this._$root=e,t.checkSupport(),this.checkInitialised();const n=t.moduleName;this.$root.setAttribute(`data-${n}-init`,"")}checkInitialised(){const e=this.constructor,t=e.moduleName;if(t&&function(e,t){return e instanceof HTMLElement&&e.hasAttribute(`data-${t}-init`)}(this.$root,t))throw new l(e)}static checkSupport(){if(!t())throw new o}}u.elementType=HTMLElement;const c=Symbol.for("configOverride");class d extends u{[c](e){return{}}get config(){return this._config}constructor(e,t){super(e),this._config=void 0;const s=this.constructor;if(!n(s.defaults))throw new r(i(s,"Config passed as parameter into constructor but no defaults defined"));const o=function(e,t){if(!n(e.schema))throw new r(i(e,"Config passed as parameter into constructor but no schema defined"));const s={},o=Object.entries(e.schema.properties);for(const n of o){const[i,o]=n,r=i.toString();r in t&&(s[r]=h(t[r],o)),"object"===(null==o?void 0:o.type)&&(s[r]=m(e.schema,t,i))}return s}(s,this._$root.dataset);this._config=p(s.defaults,null!=t?t:{},this[c](o),o)}}function h(e,t){const n=e?e.trim():"";let i,s=null==t?void 0:t.type;switch(s||(["true","false"].includes(n)&&(s="boolean"),n.length>0&&isFinite(Number(n))&&(s="number")),s){case"boolean":i="true"===n;break;case"number":i=Number(n);break;default:i=e}return i}function p(...e){const t={};for(const i of e)for(const e of Object.keys(i)){const s=t[e],o=i[e];n(s)&&n(o)?t[e]=p(s,o):t[e]=o}return t}function m(e,t,i){const s=e.properties[i];if("object"!==(null==s?void 0:s.type))return;const o={[i]:{}};for(const[e,s]of Object.entries(t)){let t=o;const r=e.split(".");for(const[o,a]of r.entries())n(t)&&(o<r.length-1?(n(t[a])||(t[a]={}),t=t[a]):e!==i&&(t[a]=h(s)))}return o[i]}class g{constructor(e={},t={}){var n;this.translations=void 0,this.locale=void 0,this.translations=e,this.locale=null!=(n=t.locale)?n:document.documentElement.lang||"en"}t(e,t){if(!e)throw new Error("i18n: lookup key missing");let i=this.translations[e];if("number"==typeof(null==t?void 0:t.count)&&n(i)){const n=i[this.getPluralSuffix(e,t.count)];n&&(i=n)}if("string"==typeof i){if(i.match(/%{(.\S+)}/)){if(!t)throw new Error("i18n: cannot replace placeholders in string if no option data provided");return this.replacePlaceholders(i,t)}return i}return e}replacePlaceholders(e,t){const n=Intl.NumberFormat.supportedLocalesOf(this.locale).length?new Intl.NumberFormat(this.locale):void 0;return e.replace(/%{(.\S+)}/g,function(e,i){if(Object.prototype.hasOwnProperty.call(t,i)){const e=t[i];return!1===e||"number"!=typeof e&&"string"!=typeof e?"":"number"==typeof e?n?n.format(e):`${e}`:e}throw new Error(`i18n: no data found to replace ${e} placeholder in string`)})}hasIntlPluralRulesSupport(){return Boolean("PluralRules"in window.Intl&&Intl.PluralRules.supportedLocalesOf(this.locale).length)}getPluralSuffix(e,t){if(t=Number(t),!isFinite(t))return"other";const i=this.translations[e],s=this.hasIntlPluralRulesSupport()?new Intl.PluralRules(this.locale).select(t):this.selectPluralFormUsingFallbackRules(t);if(n(i)){if(s in i)return s;if("other"in i)return console.warn(`i18n: Missing plural form ".${s}" for "${this.locale}" locale. Falling back to ".other".`),"other"}throw new Error(`i18n: Plural form ".other" is required for "${this.locale}" locale`)}selectPluralFormUsingFallbackRules(e){e=Math.abs(Math.floor(e));const t=this.getPluralRulesForLocale();return t?g.pluralRules[t](e):"other"}getPluralRulesForLocale(){const e=this.locale.split("-")[0];for(const t in g.pluralRulesMap){const n=g.pluralRulesMap[t];if(n.includes(this.locale)||n.includes(e))return t}}}g.pluralRulesMap={arabic:["ar"],chinese:["my","zh","id","ja","jv","ko","ms","th","vi"],french:["hy","bn","fr","gu","hi","fa","pa","zu"],german:["af","sq","az","eu","bg","ca","da","nl","en","et","fi","ka","de","el","hu","lb","no","so","sw","sv","ta","te","tr","ur"],irish:["ga"],russian:["ru","uk"],scottish:["gd"],spanish:["pt-PT","it","es"],welsh:["cy"]},g.pluralRules={arabic:e=>0===e?"zero":1===e?"one":2===e?"two":e%100>=3&&e%100<=10?"few":e%100>=11&&e%100<=99?"many":"other",chinese:()=>"other",french:e=>0===e||1===e?"one":"other",german:e=>1===e?"one":"other",irish:e=>1===e?"one":2===e?"two":e>=3&&e<=6?"few":e>=7&&e<=10?"many":"other",russian(e){const t=e%100,n=t%10;return 1===n&&11!==t?"one":n>=2&&n<=4&&!(t>=12&&t<=14)?"few":0===n||n>=5&&n<=9||t>=11&&t<=14?"many":"other"},scottish:e=>1===e||11===e?"one":2===e||12===e?"two":e>=3&&e<=10||e>=13&&e<=19?"few":"other",spanish:e=>1===e?"one":e%1e6==0&&0!==e?"many":"other",welsh:e=>0===e?"zero":1===e?"one":2===e?"two":3===e?"few":6===e?"many":"other"};
2
2
  /**
3
3
  * JavaScript enhancements for the Button component
4
4
  *
5
5
  * @preserve
6
6
  * @augments ConfigurableComponent<ButtonConfig>
7
7
  */
8
- class g extends h{constructor(e,t={}){super(e,t),this.debounceFormSubmitTimer=null,this.$root.addEventListener("keydown",e=>this.handleKeyDown(e)),this.$root.addEventListener("click",e=>this.debounce(e))}handleKeyDown(e){const t=e.target;" "===e.key&&t instanceof HTMLElement&&"button"===t.getAttribute("role")&&(e.preventDefault(),t.click())}debounce(e){if(this.config.preventDoubleClick)return this.debounceFormSubmitTimer?(e.preventDefault(),!1):void(this.debounceFormSubmitTimer=window.setTimeout(()=>{this.debounceFormSubmitTimer=null},1e3))}}function v(e,t){const n=e.closest(`[${t}]`);return n?n.getAttribute(t):null}
8
+ class f extends d{constructor(e,t={}){super(e,t),this.debounceFormSubmitTimer=null,this.$root.addEventListener("keydown",e=>this.handleKeyDown(e)),this.$root.addEventListener("click",e=>this.debounce(e))}handleKeyDown(e){const t=e.target;" "===e.key&&t instanceof HTMLElement&&"button"===t.getAttribute("role")&&(e.preventDefault(),t.click())}debounce(e){if(this.config.preventDoubleClick)return this.debounceFormSubmitTimer?(e.preventDefault(),!1):void(this.debounceFormSubmitTimer=window.setTimeout(()=>{this.debounceFormSubmitTimer=null},1e3))}}function v(e,t){const n=e.closest(`[${t}]`);return n?n.getAttribute(t):null}
9
9
  /**
10
10
  * Character count component
11
11
  *
@@ -18,13 +18,13 @@ class g extends h{constructor(e,t={}){super(e,t),this.debounceFormSubmitTimer=nu
18
18
  *
19
19
  * @preserve
20
20
  * @augments ConfigurableComponent<CharacterCountConfig>
21
- */g.moduleName="govuk-button",g.defaults=Object.freeze({preventDoubleClick:!1}),g.schema=Object.freeze({properties:{preventDoubleClick:{type:"boolean"}}});class w extends h{[c](e){let t={};return("maxwords"in e||"maxlength"in e)&&(t={maxlength:void 0,maxwords:void 0}),t}constructor(e,t={}){var n,i;super(e,t),this.$textarea=void 0,this.$visibleCountMessage=void 0,this.$screenReaderCountMessage=void 0,this.lastInputTimestamp=null,this.lastInputValue="",this.valueChecker=null,this.i18n=void 0,this.maxLength=void 0;const o=this.$root.querySelector(".govuk-js-character-count");if(!(o instanceof HTMLTextAreaElement||o instanceof HTMLInputElement))throw new a({component:w,element:o,expectedType:"HTMLTextareaElement or HTMLInputElement",identifier:"Form field (`.govuk-js-character-count`)"});const l=function(e,t){const n=[];for(const[s,i]of Object.entries(e)){const e=[];if(Array.isArray(i)){for(const{required:n,errorMessage:s}of i)n.every(e=>!!t[e])||e.push(s);"anyOf"!==s||i.length-e.length>=1||n.push(...e)}}return n}(w.schema,this.config);if(l[0])throw new r(s(w,l[0]));this.i18n=new f(this.config.i18n,{locale:v(this.$root,"lang")}),this.maxLength=null!=(n=null!=(i=this.config.maxwords)?i:this.config.maxlength)?n:1/0,this.$textarea=o;const u=`${this.$textarea.id}-info`,c=document.getElementById(u);if(!c)throw new a({component:w,element:c,identifier:`Count message (\`id="${u}"\`)`});this.$errorMessage=this.$root.querySelector(".govuk-error-message"),`${c.textContent}`.match(/^\s*$/)&&(c.textContent=this.i18n.t("textareaDescription",{count:this.maxLength})),this.$textarea.insertAdjacentElement("afterend",c);const h=document.createElement("div");h.className="govuk-character-count__sr-status govuk-visually-hidden",h.setAttribute("aria-live","polite"),this.$screenReaderCountMessage=h,c.insertAdjacentElement("afterend",h);const d=document.createElement("div");d.className=c.className,d.classList.add("govuk-character-count__status"),d.setAttribute("aria-hidden","true"),this.$visibleCountMessage=d,c.insertAdjacentElement("afterend",d),c.classList.add("govuk-visually-hidden"),this.$textarea.removeAttribute("maxlength"),this.bindChangeEvents(),window.addEventListener("pageshow",()=>this.updateCountMessage()),this.updateCountMessage()}bindChangeEvents(){this.$textarea.addEventListener("keyup",()=>this.handleKeyUp()),this.$textarea.addEventListener("focus",()=>this.handleFocus()),this.$textarea.addEventListener("blur",()=>this.handleBlur())}handleKeyUp(){this.updateVisibleCountMessage(),this.lastInputTimestamp=Date.now()}handleFocus(){this.valueChecker=window.setInterval(()=>{(!this.lastInputTimestamp||Date.now()-500>=this.lastInputTimestamp)&&this.updateIfValueChanged()},1e3)}handleBlur(){this.valueChecker&&window.clearInterval(this.valueChecker)}updateIfValueChanged(){this.$textarea.value!==this.lastInputValue&&(this.lastInputValue=this.$textarea.value,this.updateCountMessage())}updateCountMessage(){this.updateVisibleCountMessage(),this.updateScreenReaderCountMessage()}updateVisibleCountMessage(){const e=this.maxLength-this.count(this.$textarea.value)<0;this.$visibleCountMessage.classList.toggle("govuk-character-count__message--disabled",!this.isOverThreshold()),this.$errorMessage||this.$textarea.classList.toggle("govuk-textarea--error",e),this.$visibleCountMessage.classList.toggle("govuk-error-message",e),this.$visibleCountMessage.classList.toggle("govuk-hint",!e),this.$visibleCountMessage.textContent=this.getCountMessage()}updateScreenReaderCountMessage(){this.isOverThreshold()?this.$screenReaderCountMessage.removeAttribute("aria-hidden"):this.$screenReaderCountMessage.setAttribute("aria-hidden","true"),this.$screenReaderCountMessage.textContent=this.getCountMessage()}count(e){if(this.config.maxwords){var t;return(null!=(t=e.match(/\S+/g))?t:[]).length}return e.length}getCountMessage(){const e=this.maxLength-this.count(this.$textarea.value),t=this.config.maxwords?"words":"characters";return this.formatCountMessage(e,t)}formatCountMessage(e,t){if(0===e)return this.i18n.t(`${t}AtLimit`);const n=e<0?"OverLimit":"UnderLimit";return this.i18n.t(`${t}${n}`,{count:Math.abs(e)})}isOverThreshold(){if(!this.config.threshold)return!0;const e=this.count(this.$textarea.value);return this.maxLength*this.config.threshold/100<=e}}w.moduleName="govuk-character-count",w.defaults=Object.freeze({threshold:0,i18n:{charactersUnderLimit:{one:"You have %{count} character remaining",other:"You have %{count} characters remaining"},charactersAtLimit:"You have 0 characters remaining",charactersOverLimit:{one:"You have %{count} character too many",other:"You have %{count} characters too many"},wordsUnderLimit:{one:"You have %{count} word remaining",other:"You have %{count} words remaining"},wordsAtLimit:"You have 0 words remaining",wordsOverLimit:{one:"You have %{count} word too many",other:"You have %{count} words too many"},textareaDescription:{other:""}}}),w.schema=Object.freeze({properties:{i18n:{type:"object"},maxwords:{type:"number"},maxlength:{type:"number"},threshold:{type:"number"}},anyOf:[{required:["maxwords"],errorMessage:'Either "maxlength" or "maxwords" must be provided'},{required:["maxlength"],errorMessage:'Either "maxlength" or "maxwords" must be provided'}]});
21
+ */f.moduleName="govuk-button",f.defaults=Object.freeze({preventDoubleClick:!1}),f.schema=Object.freeze({properties:{preventDoubleClick:{type:"boolean"}}});class b extends d{[c](e){let t={};return("maxwords"in e||"maxlength"in e)&&(t={maxlength:void 0,maxwords:void 0}),t}constructor(e,t={}){var n,s;super(e,t),this.$textarea=void 0,this.$visibleCountMessage=void 0,this.$screenReaderCountMessage=void 0,this.lastInputTimestamp=null,this.lastInputValue="",this.valueChecker=null,this.i18n=void 0,this.maxLength=void 0;const o=this.$root.querySelector(".govuk-js-character-count");if(!(o instanceof HTMLTextAreaElement||o instanceof HTMLInputElement))throw new a({component:b,element:o,expectedType:"HTMLTextareaElement or HTMLInputElement",identifier:"Form field (`.govuk-js-character-count`)"});const l=function(e,t){const n=[];for(const[i,s]of Object.entries(e)){const e=[];if(Array.isArray(s)){for(const{required:n,errorMessage:i}of s)n.every(e=>!!t[e])||e.push(i);"anyOf"!==i||s.length-e.length>=1||n.push(...e)}}return n}(b.schema,this.config);if(l[0])throw new r(i(b,l[0]));this.i18n=new g(this.config.i18n,{locale:v(this.$root,"lang")}),this.maxLength=null!=(n=null!=(s=this.config.maxwords)?s:this.config.maxlength)?n:1/0,this.$textarea=o;const u=`${this.$textarea.id}-info`,c=document.getElementById(u);if(!c)throw new a({component:b,element:c,identifier:`Count message (\`id="${u}"\`)`});this.$errorMessage=this.$root.querySelector(".govuk-error-message"),c.textContent.match(/^\s*$/)&&(c.textContent=this.i18n.t("textareaDescription",{count:this.maxLength})),this.$textarea.insertAdjacentElement("afterend",c);const d=document.createElement("div");d.className="govuk-character-count__sr-status govuk-visually-hidden",d.setAttribute("aria-live","polite"),this.$screenReaderCountMessage=d,c.insertAdjacentElement("afterend",d);const h=document.createElement("div");h.className=c.className,h.classList.add("govuk-character-count__status"),h.setAttribute("aria-hidden","true"),this.$visibleCountMessage=h,c.insertAdjacentElement("afterend",h),c.classList.add("govuk-visually-hidden"),this.$textarea.removeAttribute("maxlength"),this.bindChangeEvents(),window.addEventListener("pageshow",()=>this.updateCountMessage()),this.updateCountMessage()}bindChangeEvents(){this.$textarea.addEventListener("keyup",()=>this.handleKeyUp()),this.$textarea.addEventListener("focus",()=>this.handleFocus()),this.$textarea.addEventListener("blur",()=>this.handleBlur())}handleKeyUp(){this.updateVisibleCountMessage(),this.lastInputTimestamp=Date.now()}handleFocus(){this.valueChecker=window.setInterval(()=>{(!this.lastInputTimestamp||Date.now()-500>=this.lastInputTimestamp)&&this.updateIfValueChanged()},1e3)}handleBlur(){this.valueChecker&&window.clearInterval(this.valueChecker)}updateIfValueChanged(){this.$textarea.value!==this.lastInputValue&&(this.lastInputValue=this.$textarea.value,this.updateCountMessage())}updateCountMessage(){this.updateVisibleCountMessage(),this.updateScreenReaderCountMessage()}updateVisibleCountMessage(){const e=this.maxLength-this.count(this.$textarea.value)<0;this.$visibleCountMessage.classList.toggle("govuk-character-count__message--disabled",!this.isOverThreshold()),this.$errorMessage||this.$textarea.classList.toggle("govuk-textarea--error",e),this.$visibleCountMessage.classList.toggle("govuk-error-message",e),this.$visibleCountMessage.classList.toggle("govuk-hint",!e),this.$visibleCountMessage.textContent=this.getCountMessage()}updateScreenReaderCountMessage(){this.isOverThreshold()?this.$screenReaderCountMessage.removeAttribute("aria-hidden"):this.$screenReaderCountMessage.setAttribute("aria-hidden","true"),this.$screenReaderCountMessage.textContent=this.getCountMessage()}count(e){if(this.config.maxwords){var t;return(null!=(t=e.match(/\S+/g))?t:[]).length}return e.length}getCountMessage(){const e=this.maxLength-this.count(this.$textarea.value),t=this.config.maxwords?"words":"characters";return this.formatCountMessage(e,t)}formatCountMessage(e,t){if(0===e)return this.i18n.t(`${t}AtLimit`);const n=e<0?"OverLimit":"UnderLimit";return this.i18n.t(`${t}${n}`,{count:Math.abs(e)})}isOverThreshold(){if(!this.config.threshold)return!0;const e=this.count(this.$textarea.value);return this.maxLength*this.config.threshold/100<=e}}b.moduleName="govuk-character-count",b.defaults=Object.freeze({threshold:0,i18n:{charactersUnderLimit:{one:"You have %{count} character remaining",other:"You have %{count} characters remaining"},charactersAtLimit:"You have 0 characters remaining",charactersOverLimit:{one:"You have %{count} character too many",other:"You have %{count} characters too many"},wordsUnderLimit:{one:"You have %{count} word remaining",other:"You have %{count} words remaining"},wordsAtLimit:"You have 0 words remaining",wordsOverLimit:{one:"You have %{count} word too many",other:"You have %{count} words too many"},textareaDescription:{other:""}}}),b.schema=Object.freeze({properties:{i18n:{type:"object"},maxwords:{type:"number"},maxlength:{type:"number"},threshold:{type:"number"}},anyOf:[{required:["maxwords"],errorMessage:'Either "maxlength" or "maxwords" must be provided'},{required:["maxlength"],errorMessage:'Either "maxlength" or "maxwords" must be provided'}]});
22
22
  /**
23
23
  * Checkboxes component
24
24
  *
25
25
  * @preserve
26
26
  */
27
- class b extends u{constructor(e){super(e),this.$inputs=void 0;const t=this.$root.querySelectorAll('input[type="checkbox"]');if(!t.length)throw new a({component:b,identifier:'Form inputs (`<input type="checkbox">`)'});this.$inputs=t,this.$inputs.forEach(e=>{const t=e.getAttribute("data-aria-controls");if(t){if(!document.getElementById(t))throw new a({component:b,identifier:`Conditional reveal (\`id="${t}"\`)`});e.setAttribute("aria-controls",t),e.removeAttribute("data-aria-controls")}}),window.addEventListener("pageshow",()=>this.syncAllConditionalReveals()),this.syncAllConditionalReveals(),this.$root.addEventListener("click",e=>this.handleClick(e))}syncAllConditionalReveals(){this.$inputs.forEach(e=>this.syncConditionalRevealWithInputState(e))}syncConditionalRevealWithInputState(e){const t=e.getAttribute("aria-controls");if(!t)return;const n=document.getElementById(t);if(null!=n&&n.classList.contains("govuk-checkboxes__conditional")){const t=e.checked;e.setAttribute("aria-expanded",t.toString()),n.classList.toggle("govuk-checkboxes__conditional--hidden",!t)}}unCheckAllInputsExcept(e){document.querySelectorAll(`input[type="checkbox"][name="${e.name}"]`).forEach(t=>{e.form===t.form&&t!==e&&(t.checked=!1,this.syncConditionalRevealWithInputState(t))})}unCheckExclusiveInputs(e){document.querySelectorAll(`input[data-behaviour="exclusive"][type="checkbox"][name="${e.name}"]`).forEach(t=>{e.form===t.form&&(t.checked=!1,this.syncConditionalRevealWithInputState(t))})}handleClick(e){const t=e.target;if(!(t instanceof HTMLInputElement)||"checkbox"!==t.type)return;if(t.getAttribute("aria-controls")&&this.syncConditionalRevealWithInputState(t),!t.checked)return;"exclusive"===t.getAttribute("data-behaviour")?this.unCheckAllInputsExcept(t):this.unCheckExclusiveInputs(t)}}b.moduleName="govuk-checkboxes";
27
+ class w extends u{constructor(e){super(e),this.$inputs=void 0;const t=this.$root.querySelectorAll('input[type="checkbox"]');if(!t.length)throw new a({component:w,identifier:'Form inputs (`<input type="checkbox">`)'});this.$inputs=t,this.$inputs.forEach(e=>{const t=e.getAttribute("data-aria-controls");if(t){if(!document.getElementById(t))throw new a({component:w,identifier:`Conditional reveal (\`id="${t}"\`)`});e.setAttribute("aria-controls",t),e.removeAttribute("data-aria-controls")}}),window.addEventListener("pageshow",()=>this.syncAllConditionalReveals()),this.syncAllConditionalReveals(),this.$root.addEventListener("click",e=>this.handleClick(e))}syncAllConditionalReveals(){this.$inputs.forEach(e=>this.syncConditionalRevealWithInputState(e))}syncConditionalRevealWithInputState(e){const t=e.getAttribute("aria-controls");if(!t)return;const n=document.getElementById(t);if(null!=n&&n.classList.contains("govuk-checkboxes__conditional")){const t=e.checked;e.setAttribute("aria-expanded",t.toString()),n.classList.toggle("govuk-checkboxes__conditional--hidden",!t)}}unCheckAllInputsExcept(e){document.querySelectorAll(`input[type="checkbox"][name="${e.name}"]`).forEach(t=>{e.form===t.form&&t!==e&&(t.checked=!1,this.syncConditionalRevealWithInputState(t))})}unCheckExclusiveInputs(e){document.querySelectorAll(`input[data-behaviour="exclusive"][type="checkbox"][name="${e.name}"]`).forEach(t=>{e.form===t.form&&(t.checked=!1,this.syncConditionalRevealWithInputState(t))})}handleClick(e){const t=e.target;if(!(t instanceof HTMLInputElement)||"checkbox"!==t.type)return;if(t.getAttribute("aria-controls")&&this.syncConditionalRevealWithInputState(t),!t.checked)return;"exclusive"===t.getAttribute("data-behaviour")?this.unCheckAllInputsExcept(t):this.unCheckExclusiveInputs(t)}}w.moduleName="govuk-checkboxes";
28
28
  /**
29
29
  * Error summary component
30
30
  *
@@ -34,18 +34,24 @@ class b extends u{constructor(e){super(e),this.$inputs=void 0;const t=this.$root
34
34
  * @preserve
35
35
  * @augments ConfigurableComponent<ErrorSummaryConfig>
36
36
  */
37
- class y extends h{constructor(e,t={}){super(e,t),this.config.disableAutoFocus||function(e,t={}){var n;const s=e.getAttribute("tabindex");function i(){var n;null==(n=t.onBlur)||n.call(e),s||e.removeAttribute("tabindex")}s||e.setAttribute("tabindex","-1"),e.addEventListener("focus",function(){e.addEventListener("blur",i,{once:!0})},{once:!0}),null==(n=t.onBeforeFocus)||n.call(e),e.focus()}(this.$root),this.$root.addEventListener("click",e=>this.handleClick(e))}handleClick(e){const t=e.target;t&&this.focusTarget(t)&&e.preventDefault()}focusTarget(e){if(!(e instanceof HTMLAnchorElement))return!1;const t=function(e){if(e.includes("#"))return e.split("#").pop()}(e.href);if(!t)return!1;const n=document.getElementById(t);if(!n)return!1;const s=this.getAssociatedLegendOrLabel(n);return!!s&&(s.scrollIntoView(),n.focus({preventScroll:!0}),!0)}getAssociatedLegendOrLabel(e){var t;const n=e.closest("fieldset");if(n){const t=n.getElementsByTagName("legend");if(t.length){const n=t[0];if(e instanceof HTMLInputElement&&("checkbox"===e.type||"radio"===e.type))return n;const s=n.getBoundingClientRect().top,i=e.getBoundingClientRect();if(i.height&&window.innerHeight){if(i.top+i.height-s<window.innerHeight/2)return n}}}return null!=(t=document.querySelector(`label[for='${e.getAttribute("id")}']`))?t:e.closest("label")}}y.moduleName="govuk-error-summary",y.defaults=Object.freeze({disableAutoFocus:!1}),y.schema=Object.freeze({properties:{disableAutoFocus:{type:"boolean"}}});
37
+ class y extends d{constructor(e,t={}){super(e,t),this.config.disableAutoFocus||function(e,t={}){var n;const i=e.getAttribute("tabindex");function s(){var n;null==(n=t.onBlur)||n.call(e),i||e.removeAttribute("tabindex")}i||e.setAttribute("tabindex","-1"),e.addEventListener("focus",function(){e.addEventListener("blur",s,{once:!0})},{once:!0}),null==(n=t.onBeforeFocus)||n.call(e),e.focus()}(this.$root),this.$root.addEventListener("click",e=>this.handleClick(e))}handleClick(e){const t=e.target;t&&this.focusTarget(t)&&e.preventDefault()}focusTarget(e){if(!(e instanceof HTMLAnchorElement))return!1;const t=e.hash.replace("#","");if(!t)return!1;const n=document.getElementById(t);if(!n)return!1;const i=this.getAssociatedLegendOrLabel(n);return!!i&&(i.scrollIntoView(),n.focus({preventScroll:!0}),!0)}getAssociatedLegendOrLabel(e){var t;const n=e.closest("fieldset");if(n){const t=n.getElementsByTagName("legend");if(t.length){const n=t[0];if(e instanceof HTMLInputElement&&("checkbox"===e.type||"radio"===e.type))return n;const i=n.getBoundingClientRect().top,s=e.getBoundingClientRect();if(s.height&&window.innerHeight){if(s.top+s.height-i<window.innerHeight/2)return n}}}return null!=(t=document.querySelector(`label[for='${e.getAttribute("id")}']`))?t:e.closest("label")}}y.moduleName="govuk-error-summary",y.defaults=Object.freeze({disableAutoFocus:!1}),y.schema=Object.freeze({properties:{disableAutoFocus:{type:"boolean"}}});
38
+ /**
39
+ * File upload component
40
+ *
41
+ * @preserve
42
+ * @augments ConfigurableComponent<FileUploadConfig>
43
+ */
44
+ class $ extends d{constructor(e,t={}){super(e,t),this.$input=void 0,this.$button=void 0,this.$status=void 0,this.i18n=void 0,this.id=void 0,this.$announcements=void 0,this.enteredAnotherElement=void 0;const n=this.$root.querySelector("input");if(null===n)throw new a({component:$,identifier:'File inputs (`<input type="file">`)'});if("file"!==n.type)throw new a(i($,'File input (`<input type="file">`) attribute (`type`) is not `file`'));if(this.$input=n,!this.$input.id)throw new a({component:$,identifier:'File input (`<input type="file">`) attribute (`id`)'});this.id=this.$input.id,this.i18n=new g(this.config.i18n,{locale:v(this.$root,"lang")});const s=this.findLabel();s.id||(s.id=`${this.id}-label`),this.$input.id=`${this.id}-input`,this.$input.setAttribute("hidden","true");const o=document.createElement("button");o.classList.add("govuk-file-upload-button"),o.type="button",o.id=this.id,o.classList.add("govuk-file-upload-button--empty");const r=this.$input.getAttribute("aria-describedby");r&&o.setAttribute("aria-describedby",r);const l=document.createElement("span");l.className="govuk-body govuk-file-upload-button__status",l.setAttribute("aria-live","polite"),l.innerText=this.i18n.t("noFileChosen"),o.appendChild(l);const u=document.createElement("span");u.className="govuk-visually-hidden",u.innerText=", ",u.id=`${this.id}-comma`,o.appendChild(u);const c=document.createElement("span");c.className="govuk-file-upload-button__pseudo-button-container";const d=document.createElement("span");d.className="govuk-button govuk-button--secondary govuk-file-upload-button__pseudo-button",d.innerText=this.i18n.t("chooseFilesButton"),c.appendChild(d),c.insertAdjacentText("beforeend"," ");const h=document.createElement("span");h.className="govuk-body govuk-file-upload-button__instruction",h.innerText=this.i18n.t("dropInstruction"),c.appendChild(h),o.appendChild(c),o.setAttribute("aria-labelledby",`${s.id} ${u.id} ${o.id}`),o.addEventListener("click",this.onClick.bind(this)),o.addEventListener("dragover",e=>{e.preventDefault()}),this.$root.insertAdjacentElement("afterbegin",o),this.$input.setAttribute("tabindex","-1"),this.$input.setAttribute("aria-hidden","true"),this.$button=o,this.$status=l,this.$input.addEventListener("change",this.onChange.bind(this)),this.updateDisabledState(),this.observeDisabledState(),this.$announcements=document.createElement("span"),this.$announcements.classList.add("govuk-file-upload-announcements"),this.$announcements.classList.add("govuk-visually-hidden"),this.$announcements.setAttribute("aria-live","assertive"),this.$root.insertAdjacentElement("afterend",this.$announcements),this.$button.addEventListener("drop",this.onDrop.bind(this)),document.addEventListener("dragenter",this.updateDropzoneVisibility.bind(this)),document.addEventListener("dragenter",()=>{this.enteredAnotherElement=!0}),document.addEventListener("dragleave",()=>{this.enteredAnotherElement||this.$button.disabled||(this.hideDraggingState(),this.$announcements.innerText=this.i18n.t("leftDropZone")),this.enteredAnotherElement=!1})}updateDropzoneVisibility(e){this.$button.disabled||e.target instanceof Node&&(this.$root.contains(e.target)?e.dataTransfer&&k(e.dataTransfer)&&(this.$button.classList.contains("govuk-file-upload-button--dragging")||(this.showDraggingState(),this.$announcements.innerText=this.i18n.t("enteredDropZone"))):this.$button.classList.contains("govuk-file-upload-button--dragging")&&(this.hideDraggingState(),this.$announcements.innerText=this.i18n.t("leftDropZone")))}showDraggingState(){this.$button.classList.add("govuk-file-upload-button--dragging")}hideDraggingState(){this.$button.classList.remove("govuk-file-upload-button--dragging")}onDrop(e){e.preventDefault(),e.dataTransfer&&k(e.dataTransfer)&&(this.$input.files=e.dataTransfer.files,this.$input.dispatchEvent(new CustomEvent("change")),this.hideDraggingState())}onChange(){const e=this.$input.files.length;0===e?(this.$status.innerText=this.i18n.t("noFileChosen"),this.$button.classList.add("govuk-file-upload-button--empty")):(this.$status.innerText=1===e?this.$input.files[0].name:this.i18n.t("multipleFilesChosen",{count:e}),this.$button.classList.remove("govuk-file-upload-button--empty"))}findLabel(){const e=document.querySelector(`label[for="${this.$input.id}"]`);if(!e)throw new a({component:$,identifier:`Field label (\`<label for=${this.$input.id}>\`)`});return e}onClick(){this.$input.click()}observeDisabledState(){new MutationObserver(e=>{for(const t of e)"attributes"===t.type&&"disabled"===t.attributeName&&this.updateDisabledState()}).observe(this.$input,{attributes:!0})}updateDisabledState(){this.$button.disabled=this.$input.disabled,this.$root.classList.toggle("govuk-drop-zone--disabled",this.$button.disabled)}}function k(e){const t=0===e.types.length,n=e.types.some(e=>"Files"===e);return t||n}
38
45
  /**
39
46
  * Password input component
40
47
  *
41
48
  * @preserve
42
49
  * @augments ConfigurableComponent<PasswordInputConfig>
43
- */
44
- class $ extends h{constructor(e,t={}){super(e,t),this.i18n=void 0,this.$input=void 0,this.$showHideButton=void 0,this.$screenReaderStatusMessage=void 0;const n=this.$root.querySelector(".govuk-js-password-input-input");if(!(n instanceof HTMLInputElement))throw new a({component:$,element:n,expectedType:"HTMLInputElement",identifier:"Form field (`.govuk-js-password-input-input`)"});if("password"!==n.type)throw new a("Password input: Form field (`.govuk-js-password-input-input`) must be of type `password`.");const s=this.$root.querySelector(".govuk-js-password-input-toggle");if(!(s instanceof HTMLButtonElement))throw new a({component:$,element:s,expectedType:"HTMLButtonElement",identifier:"Button (`.govuk-js-password-input-toggle`)"});if("button"!==s.type)throw new a("Password input: Button (`.govuk-js-password-input-toggle`) must be of type `button`.");this.$input=n,this.$showHideButton=s,this.i18n=new f(this.config.i18n,{locale:v(this.$root,"lang")}),this.$showHideButton.removeAttribute("hidden");const i=document.createElement("div");i.className="govuk-password-input__sr-status govuk-visually-hidden",i.setAttribute("aria-live","polite"),this.$screenReaderStatusMessage=i,this.$input.insertAdjacentElement("afterend",i),this.$showHideButton.addEventListener("click",this.toggle.bind(this)),this.$input.form&&this.$input.form.addEventListener("submit",()=>this.hide()),window.addEventListener("pageshow",e=>{e.persisted&&"password"!==this.$input.type&&this.hide()}),this.hide()}toggle(e){e.preventDefault(),"password"!==this.$input.type?this.hide():this.show()}show(){this.setType("text")}hide(){this.setType("password")}setType(e){if(e===this.$input.type)return;this.$input.setAttribute("type",e);const t="password"===e,n=t?"show":"hide",s=t?"passwordHidden":"passwordShown";this.$showHideButton.innerText=this.i18n.t(`${n}Password`),this.$showHideButton.setAttribute("aria-label",this.i18n.t(`${n}PasswordAriaLabel`)),this.$screenReaderStatusMessage.innerText=this.i18n.t(`${s}Announcement`)}}$.moduleName="govuk-password-input",$.defaults=Object.freeze({i18n:{showPassword:"Show",hidePassword:"Hide",showPasswordAriaLabel:"Show password",hidePasswordAriaLabel:"Hide password",passwordShownAnnouncement:"Your password is visible",passwordHiddenAnnouncement:"Your password is hidden"}}),$.schema=Object.freeze({properties:{i18n:{type:"object"}}});
50
+ */$.moduleName="govuk-file-upload",$.defaults=Object.freeze({i18n:{chooseFilesButton:"Choose file",dropInstruction:"or drop file",noFileChosen:"No file chosen",multipleFilesChosen:{one:"%{count} file chosen",other:"%{count} files chosen"},enteredDropZone:"Entered drop zone",leftDropZone:"Left drop zone"}}),$.schema=Object.freeze({properties:{i18n:{type:"object"}}});class x extends d{constructor(e,t={}){super(e,t),this.i18n=void 0,this.$input=void 0,this.$showHideButton=void 0,this.$screenReaderStatusMessage=void 0;const n=this.$root.querySelector(".govuk-js-password-input-input");if(!(n instanceof HTMLInputElement))throw new a({component:x,element:n,expectedType:"HTMLInputElement",identifier:"Form field (`.govuk-js-password-input-input`)"});if("password"!==n.type)throw new a("Password input: Form field (`.govuk-js-password-input-input`) must be of type `password`.");const i=this.$root.querySelector(".govuk-js-password-input-toggle");if(!(i instanceof HTMLButtonElement))throw new a({component:x,element:i,expectedType:"HTMLButtonElement",identifier:"Button (`.govuk-js-password-input-toggle`)"});if("button"!==i.type)throw new a("Password input: Button (`.govuk-js-password-input-toggle`) must be of type `button`.");this.$input=n,this.$showHideButton=i,this.i18n=new g(this.config.i18n,{locale:v(this.$root,"lang")}),this.$showHideButton.removeAttribute("hidden");const s=document.createElement("div");s.className="govuk-password-input__sr-status govuk-visually-hidden",s.setAttribute("aria-live","polite"),this.$screenReaderStatusMessage=s,this.$input.insertAdjacentElement("afterend",s),this.$showHideButton.addEventListener("click",this.toggle.bind(this)),this.$input.form&&this.$input.form.addEventListener("submit",()=>this.hide()),window.addEventListener("pageshow",e=>{e.persisted&&"password"!==this.$input.type&&this.hide()}),this.hide()}toggle(e){e.preventDefault(),"password"!==this.$input.type?this.hide():this.show()}show(){this.setType("text")}hide(){this.setType("password")}setType(e){if(e===this.$input.type)return;this.$input.setAttribute("type",e);const t="password"===e,n=t?"show":"hide",i=t?"passwordHidden":"passwordShown";this.$showHideButton.innerText=this.i18n.t(`${n}Password`),this.$showHideButton.setAttribute("aria-label",this.i18n.t(`${n}PasswordAriaLabel`)),this.$screenReaderStatusMessage.innerText=this.i18n.t(`${i}Announcement`)}}x.moduleName="govuk-password-input",x.defaults=Object.freeze({i18n:{showPassword:"Show",hidePassword:"Hide",showPasswordAriaLabel:"Show password",hidePasswordAriaLabel:"Hide password",passwordShownAnnouncement:"Your password is visible",passwordHiddenAnnouncement:"Your password is hidden"}}),x.schema=Object.freeze({properties:{i18n:{type:"object"}}});
45
51
  /**
46
52
  * Radios component
47
53
  *
48
54
  * @preserve
49
55
  */
50
- class x extends u{constructor(e){super(e),this.$inputs=void 0;const t=this.$root.querySelectorAll('input[type="radio"]');if(!t.length)throw new a({component:x,identifier:'Form inputs (`<input type="radio">`)'});this.$inputs=t,this.$inputs.forEach(e=>{const t=e.getAttribute("data-aria-controls");if(t){if(!document.getElementById(t))throw new a({component:x,identifier:`Conditional reveal (\`id="${t}"\`)`});e.setAttribute("aria-controls",t),e.removeAttribute("data-aria-controls")}}),window.addEventListener("pageshow",()=>this.syncAllConditionalReveals()),this.syncAllConditionalReveals(),this.$root.addEventListener("click",e=>this.handleClick(e))}syncAllConditionalReveals(){this.$inputs.forEach(e=>this.syncConditionalRevealWithInputState(e))}syncConditionalRevealWithInputState(e){const t=e.getAttribute("aria-controls");if(!t)return;const n=document.getElementById(t);if(null!=n&&n.classList.contains("govuk-radios__conditional")){const t=e.checked;e.setAttribute("aria-expanded",t.toString()),n.classList.toggle("govuk-radios__conditional--hidden",!t)}}handleClick(e){const t=e.target;if(!(t instanceof HTMLInputElement)||"radio"!==t.type)return;const n=document.querySelectorAll('input[type="radio"][aria-controls]'),s=t.form,i=t.name;n.forEach(e=>{const t=e.form===s;e.name===i&&t&&this.syncConditionalRevealWithInputState(e)})}}x.moduleName="govuk-radios";class k extends e{static targets=["preview","destroy"];static values={mimeTypes:Array};connect(){this.counter=0,this.initialPreviewContent=null,this.onUploadFlag=!1}onUpload(e){this.onUploadFlag=!0,this.hasDestroyTarget&&(this.destroyTarget.value=!1),this.previewTarget.removeAttribute("hidden"),this.hasPreviewTarget&&(e.currentTarget.files.length>0?this.showPreview(e.currentTarget.files[0]):this.setPreviewContent(this.initialPreviewContent))}setDestroy(e){e.preventDefault(),this.initialPreviewContent&&this.onUploadFlag?(this.onUploadFlag=!1,this.setPreviewContent(this.initialPreviewContent)):(this.hasDestroyTarget&&(this.destroyTarget.value=!0),this.hasPreviewTarget&&(this.previewTarget.setAttribute("hidden",""),this.setPreviewContent("")),this.previousInput&&this.previousInput.toggleAttribute("disabled",!0)),this.fileInput.value=""}setPreviewContent(e){this.filenameTag&&(this.filenameTag.innerText=text)}drop(e){e.preventDefault();const t=this.fileForEvent(e,this.mimeTypesValue);if(t){const e=new DataTransfer;e.items.add(t),this.fileInput.files=e.files,this.fileInput.dispatchEvent(new Event("change"))}this.counter=0,this.element.classList.remove("droppable")}dragover(e){e.preventDefault()}dragenter(e){e.preventDefault(),0===this.counter&&this.element.classList.add("droppable"),this.counter++}dragleave(e){e.preventDefault(),this.counter--,0===this.counter&&this.element.classList.remove("droppable")}get fileInput(){return this.element.querySelector("input[type='file']")}get previousInput(){return this.element.querySelector(`input[type='hidden'][name='${this.fileInput.name}']`)}get filenameTag(){return this.hasPreviewTarget?this.previewTarget.querySelector("p.preview-filename"):null}showPreview(e){const t=new FileReader;t.onload=t=>{this.filenameTag&&(this.filenameTag.innerText=e.name)},t.readAsDataURL(e)}fileForEvent(e,t){const n=e=>t.indexOf(e.type)>-1;let s;if(e.dataTransfer.items){const t=[...e.dataTransfer.items].find(n);t&&(s=t.getAsFile())}else s=[...e.dataTransfer.files].find(n);return s}}const C=[{identifier:"govuk-document-field",controllerConstructor:class extends k{connect(){super.connect(),this.initialPreviewContent=this.filenameTag.text}setPreviewContent(e){this.filenameTag.innerText=e}showPreview(e){const t=new FileReader;t.onload=t=>{this.filenameTag&&(this.filenameTag.innerText=e.name)},t.readAsDataURL(e)}get filenameTag(){return this.previewTarget.querySelector("p.preview-filename")}}},{identifier:"govuk-image-field",controllerConstructor:class extends k{connect(){super.connect(),this.initialPreviewContent=this.imageTag.getAttribute("src")}setPreviewContent(e){this.imageTag.src=e}showPreview(e){const t=new FileReader;t.onload=e=>{this.imageTag.src=e.target.result},t.readAsDataURL(e)}get imageTag(){return this.previewTarget.querySelector("img")}}}];function T(e){let n;if(e=void 0!==e?e:{},!t())return void console.log(new o);const s=[[g,e.button],[w,e.characterCount],[b],[y,e.errorSummary],[x],[$,e.passwordInput]],i=null!=(n=e.scope)?n:document;s.forEach(([e,t])=>{i.querySelectorAll(`[data-module="${e.moduleName}"]`).forEach(n=>{try{"defaults"in e?new e(n,t):new e(n)}catch(e){console.log(e)}})})}export{g as Button,w as CharacterCount,b as Checkboxes,y as ErrorSummary,$ as PasswordInput,x as Radios,C as default,T as initAll};
56
+ class C extends u{constructor(e){super(e),this.$inputs=void 0;const t=this.$root.querySelectorAll('input[type="radio"]');if(!t.length)throw new a({component:C,identifier:'Form inputs (`<input type="radio">`)'});this.$inputs=t,this.$inputs.forEach(e=>{const t=e.getAttribute("data-aria-controls");if(t){if(!document.getElementById(t))throw new a({component:C,identifier:`Conditional reveal (\`id="${t}"\`)`});e.setAttribute("aria-controls",t),e.removeAttribute("data-aria-controls")}}),window.addEventListener("pageshow",()=>this.syncAllConditionalReveals()),this.syncAllConditionalReveals(),this.$root.addEventListener("click",e=>this.handleClick(e))}syncAllConditionalReveals(){this.$inputs.forEach(e=>this.syncConditionalRevealWithInputState(e))}syncConditionalRevealWithInputState(e){const t=e.getAttribute("aria-controls");if(!t)return;const n=document.getElementById(t);if(null!=n&&n.classList.contains("govuk-radios__conditional")){const t=e.checked;e.setAttribute("aria-expanded",t.toString()),n.classList.toggle("govuk-radios__conditional--hidden",!t)}}handleClick(e){const t=e.target;if(!(t instanceof HTMLInputElement)||"radio"!==t.type)return;const n=document.querySelectorAll('input[type="radio"][aria-controls]'),i=t.form,s=t.name;n.forEach(e=>{const t=e.form===i;e.name===s&&t&&this.syncConditionalRevealWithInputState(e)})}}C.moduleName="govuk-radios";class E extends e{static targets=["preview","destroy"];static values={mimeTypes:Array};connect(){this.counter=0,this.initialPreviewContent=null,this.onUploadFlag=!1}onUpload(e){this.onUploadFlag=!0,this.hasDestroyTarget&&(this.destroyTarget.value=!1),this.previewTarget.removeAttribute("hidden"),this.hasPreviewTarget&&(e.currentTarget.files.length>0?this.showPreview(e.currentTarget.files[0]):this.setPreviewContent(this.initialPreviewContent))}setDestroy(e){e.preventDefault(),this.initialPreviewContent&&this.onUploadFlag?(this.onUploadFlag=!1,this.setPreviewContent(this.initialPreviewContent)):(this.hasDestroyTarget&&(this.destroyTarget.value=!0),this.hasPreviewTarget&&(this.previewTarget.setAttribute("hidden",""),this.setPreviewContent("")),this.previousInput&&this.previousInput.toggleAttribute("disabled",!0)),this.fileInput.value=""}setPreviewContent(e){this.filenameTag&&(this.filenameTag.innerText=text)}drop(e){e.preventDefault();const t=this.fileForEvent(e,this.mimeTypesValue);if(t){const e=new DataTransfer;e.items.add(t),this.fileInput.files=e.files,this.fileInput.dispatchEvent(new Event("change"))}this.counter=0,this.element.classList.remove("droppable")}dragover(e){e.preventDefault()}dragenter(e){e.preventDefault(),0===this.counter&&this.element.classList.add("droppable"),this.counter++}dragleave(e){e.preventDefault(),this.counter--,0===this.counter&&this.element.classList.remove("droppable")}get fileInput(){return this.element.querySelector("input[type='file']")}get previousInput(){return this.element.querySelector(`input[type='hidden'][name='${this.fileInput.name}']`)}get filenameTag(){return this.hasPreviewTarget?this.previewTarget.querySelector("p.preview-filename"):null}showPreview(e){const t=new FileReader;t.onload=t=>{this.filenameTag&&(this.filenameTag.innerText=e.name)},t.readAsDataURL(e)}fileForEvent(e,t){const n=e=>t.indexOf(e.type)>-1;let i;if(e.dataTransfer.items){const t=[...e.dataTransfer.items].find(n);t&&(i=t.getAsFile())}else i=[...e.dataTransfer.files].find(n);return i}}const T=[{identifier:"govuk-document-field",controllerConstructor:class extends E{connect(){super.connect(),this.initialPreviewContent=this.filenameTag.text}setPreviewContent(e){this.filenameTag.innerText=e}showPreview(e){const t=new FileReader;t.onload=t=>{this.filenameTag&&(this.filenameTag.innerText=e.name)},t.readAsDataURL(e)}get filenameTag(){return this.previewTarget.querySelector("p.preview-filename")}}},{identifier:"govuk-image-field",controllerConstructor:class extends E{connect(){super.connect(),this.initialPreviewContent=this.imageTag.getAttribute("src")}setPreviewContent(e){this.imageTag.src=e}showPreview(e){const t=new FileReader;t.onload=e=>{this.imageTag.src=e.target.result},t.readAsDataURL(e)}get imageTag(){return this.previewTarget.querySelector("img")}}}];function L(e){let n;if(e=void 0!==e?e:{},!t())return void console.log(new o);const i=[[f,e.button],[b,e.characterCount],[w],[y,e.errorSummary],[$,e.fileUpload],[C],[x,e.passwordInput]],s=null!=(n=e.scope)?n:document;i.forEach(([e,t])=>{s.querySelectorAll(`[data-module="${e.moduleName}"]`).forEach(n=>{try{"defaults"in e?new e(n,t):new e(n)}catch(e){console.log(e)}})})}export{f as Button,b as CharacterCount,w as Checkboxes,y as ErrorSummary,x as PasswordInput,C as Radios,T as default,L as initAll};
51
57
  //# sourceMappingURL=formbuilder.min.js.map