@ministryofjustice/frontend 4.0.1 → 5.0.0

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.
Files changed (244) hide show
  1. package/govuk-prototype-kit.config.json +19 -4
  2. package/moj/_base.scss +2 -0
  3. package/moj/_base.scss.map +1 -0
  4. package/moj/all.bundle.js +2523 -0
  5. package/moj/all.bundle.js.map +1 -0
  6. package/moj/all.bundle.mjs +2502 -0
  7. package/moj/all.bundle.mjs.map +1 -0
  8. package/moj/all.mjs +59 -69
  9. package/moj/all.mjs.map +1 -1
  10. package/moj/all.scss +2 -0
  11. package/moj/all.scss.map +1 -0
  12. package/moj/components/_all.scss +2 -0
  13. package/moj/components/_all.scss.map +1 -0
  14. package/moj/components/action-bar/_action-bar.scss +2 -0
  15. package/moj/components/action-bar/_action-bar.scss.map +1 -0
  16. package/moj/components/add-another/_add-another.scss +2 -0
  17. package/moj/components/add-another/_add-another.scss.map +1 -0
  18. package/moj/components/add-another/add-another.bundle.js +128 -0
  19. package/moj/components/add-another/add-another.bundle.js.map +1 -0
  20. package/moj/components/add-another/add-another.bundle.mjs +120 -0
  21. package/moj/components/add-another/add-another.bundle.mjs.map +1 -0
  22. package/moj/components/add-another/add-another.mjs +112 -99
  23. package/moj/components/add-another/add-another.mjs.map +1 -1
  24. package/moj/components/alert/_alert.scss +4 -0
  25. package/moj/components/alert/_alert.scss.map +1 -0
  26. package/moj/components/alert/alert.bundle.js +330 -0
  27. package/moj/components/alert/alert.bundle.js.map +1 -0
  28. package/moj/components/alert/alert.bundle.mjs +322 -0
  29. package/moj/components/alert/alert.bundle.mjs.map +1 -0
  30. package/moj/components/alert/alert.mjs +181 -217
  31. package/moj/components/alert/alert.mjs.map +1 -1
  32. package/moj/components/alert/{alert.spec.helper.js → alert.spec.helper.bundle.js} +1 -1
  33. package/moj/components/alert/alert.spec.helper.bundle.js.map +1 -0
  34. package/moj/components/alert/alert.spec.helper.bundle.mjs +67 -0
  35. package/moj/components/alert/alert.spec.helper.bundle.mjs.map +1 -0
  36. package/moj/components/alert/alert.spec.helper.mjs.map +1 -1
  37. package/moj/components/badge/_badge.scss +2 -0
  38. package/moj/components/badge/_badge.scss.map +1 -0
  39. package/moj/components/banner/_banner.scss +2 -0
  40. package/moj/components/banner/_banner.scss.map +1 -0
  41. package/moj/components/button-menu/README.md +10 -6
  42. package/moj/components/button-menu/_button-menu.scss +4 -1
  43. package/moj/components/button-menu/_button-menu.scss.map +1 -0
  44. package/moj/components/button-menu/button-menu.bundle.js +299 -0
  45. package/moj/components/button-menu/button-menu.bundle.js.map +1 -0
  46. package/moj/components/button-menu/{button-menu.js → button-menu.bundle.mjs} +74 -121
  47. package/moj/components/button-menu/button-menu.bundle.mjs.map +1 -0
  48. package/moj/components/button-menu/button-menu.mjs +246 -285
  49. package/moj/components/button-menu/button-menu.mjs.map +1 -1
  50. package/moj/components/cookie-banner/_cookie-banner.scss +2 -0
  51. package/moj/components/cookie-banner/_cookie-banner.scss.map +1 -0
  52. package/moj/components/currency-input/_currency-input.scss +2 -0
  53. package/moj/components/currency-input/_currency-input.scss.map +1 -0
  54. package/moj/components/date-picker/_date-picker.scss +2 -0
  55. package/moj/components/date-picker/_date-picker.scss.map +1 -0
  56. package/moj/components/date-picker/date-picker.bundle.js +784 -0
  57. package/moj/components/date-picker/date-picker.bundle.js.map +1 -0
  58. package/moj/components/date-picker/{date-picker.js → date-picker.bundle.mjs} +245 -439
  59. package/moj/components/date-picker/date-picker.bundle.mjs.map +1 -0
  60. package/moj/components/date-picker/date-picker.mjs +654 -840
  61. package/moj/components/date-picker/date-picker.mjs.map +1 -1
  62. package/moj/components/filter/_filter.scss +2 -0
  63. package/moj/components/filter/_filter.scss.map +1 -0
  64. package/moj/components/filter-toggle-button/filter-toggle-button.bundle.js +96 -0
  65. package/moj/components/filter-toggle-button/filter-toggle-button.bundle.js.map +1 -0
  66. package/moj/components/filter-toggle-button/filter-toggle-button.bundle.mjs +88 -0
  67. package/moj/components/filter-toggle-button/filter-toggle-button.bundle.mjs.map +1 -0
  68. package/moj/components/filter-toggle-button/filter-toggle-button.mjs +78 -84
  69. package/moj/components/filter-toggle-button/filter-toggle-button.mjs.map +1 -1
  70. package/moj/components/form-validator/form-validator.bundle.js +198 -0
  71. package/moj/components/form-validator/form-validator.bundle.js.map +1 -0
  72. package/moj/components/form-validator/form-validator.bundle.mjs +190 -0
  73. package/moj/components/form-validator/form-validator.bundle.mjs.map +1 -0
  74. package/moj/components/form-validator/form-validator.mjs +149 -152
  75. package/moj/components/form-validator/form-validator.mjs.map +1 -1
  76. package/moj/components/header/_header.scss +2 -0
  77. package/moj/components/header/_header.scss.map +1 -0
  78. package/moj/components/identity-bar/_identity-bar.scss +2 -0
  79. package/moj/components/identity-bar/_identity-bar.scss.map +1 -0
  80. package/moj/components/interruption-card/_interruption-card.scss +2 -0
  81. package/moj/components/interruption-card/_interruption-card.scss.map +1 -0
  82. package/moj/components/messages/_messages.scss +2 -0
  83. package/moj/components/messages/_messages.scss.map +1 -0
  84. package/moj/components/multi-file-upload/_multi-file-upload.scss +2 -0
  85. package/moj/components/multi-file-upload/_multi-file-upload.scss.map +1 -0
  86. package/moj/components/multi-file-upload/multi-file-upload.bundle.js +223 -0
  87. package/moj/components/multi-file-upload/multi-file-upload.bundle.js.map +1 -0
  88. package/moj/components/multi-file-upload/multi-file-upload.bundle.mjs +215 -0
  89. package/moj/components/multi-file-upload/multi-file-upload.bundle.mjs.map +1 -0
  90. package/moj/components/multi-file-upload/multi-file-upload.mjs +193 -209
  91. package/moj/components/multi-file-upload/multi-file-upload.mjs.map +1 -1
  92. package/moj/components/multi-select/_multi-select.scss +2 -0
  93. package/moj/components/multi-select/_multi-select.scss.map +1 -0
  94. package/moj/components/multi-select/multi-select.bundle.js +78 -0
  95. package/moj/components/multi-select/multi-select.bundle.js.map +1 -0
  96. package/moj/components/multi-select/multi-select.bundle.mjs +70 -0
  97. package/moj/components/multi-select/multi-select.bundle.mjs.map +1 -0
  98. package/moj/components/multi-select/multi-select.mjs +59 -67
  99. package/moj/components/multi-select/multi-select.mjs.map +1 -1
  100. package/moj/components/notification-badge/_notification-badge.scss +2 -0
  101. package/moj/components/notification-badge/_notification-badge.scss.map +1 -0
  102. package/moj/components/organisation-switcher/_organisation-switcher.scss +2 -0
  103. package/moj/components/organisation-switcher/_organisation-switcher.scss.map +1 -0
  104. package/moj/components/page-header-actions/_page-header-actions.scss +2 -0
  105. package/moj/components/page-header-actions/_page-header-actions.scss.map +1 -0
  106. package/moj/components/pagination/_pagination.scss +2 -2
  107. package/moj/components/pagination/_pagination.scss.map +1 -0
  108. package/moj/components/password-reveal/_password-reveal.scss +2 -0
  109. package/moj/components/password-reveal/_password-reveal.scss.map +1 -0
  110. package/moj/components/password-reveal/password-reveal.bundle.js +49 -0
  111. package/moj/components/password-reveal/password-reveal.bundle.js.map +1 -0
  112. package/moj/components/password-reveal/password-reveal.bundle.mjs +41 -0
  113. package/moj/components/password-reveal/password-reveal.bundle.mjs.map +1 -0
  114. package/moj/components/password-reveal/password-reveal.mjs +36 -31
  115. package/moj/components/password-reveal/password-reveal.mjs.map +1 -1
  116. package/moj/components/primary-navigation/_primary-navigation.scss +2 -0
  117. package/moj/components/primary-navigation/_primary-navigation.scss.map +1 -0
  118. package/moj/components/progress-bar/_progress-bar.scss +2 -0
  119. package/moj/components/progress-bar/_progress-bar.scss.map +1 -0
  120. package/moj/components/rich-text-editor/README.md +15 -9
  121. package/moj/components/rich-text-editor/_rich-text-editor.scss +2 -0
  122. package/moj/components/rich-text-editor/_rich-text-editor.scss.map +1 -0
  123. package/moj/components/rich-text-editor/rich-text-editor.bundle.js +145 -0
  124. package/moj/components/rich-text-editor/rich-text-editor.bundle.js.map +1 -0
  125. package/moj/components/rich-text-editor/rich-text-editor.bundle.mjs +137 -0
  126. package/moj/components/rich-text-editor/rich-text-editor.bundle.mjs.map +1 -0
  127. package/moj/components/rich-text-editor/rich-text-editor.mjs +124 -145
  128. package/moj/components/rich-text-editor/rich-text-editor.mjs.map +1 -1
  129. package/moj/components/search/_search.scss +2 -0
  130. package/moj/components/search/_search.scss.map +1 -0
  131. package/moj/components/search-toggle/{search-toggle.scss → _search-toggle.scss} +2 -0
  132. package/moj/components/search-toggle/_search-toggle.scss.map +1 -0
  133. package/moj/components/search-toggle/search-toggle.bundle.js +54 -0
  134. package/moj/components/search-toggle/search-toggle.bundle.js.map +1 -0
  135. package/moj/components/search-toggle/search-toggle.bundle.mjs +46 -0
  136. package/moj/components/search-toggle/search-toggle.bundle.mjs.map +1 -0
  137. package/moj/components/search-toggle/search-toggle.mjs +40 -49
  138. package/moj/components/search-toggle/search-toggle.mjs.map +1 -1
  139. package/moj/components/side-navigation/_side-navigation.scss +2 -0
  140. package/moj/components/side-navigation/_side-navigation.scss.map +1 -0
  141. package/moj/components/sortable-table/_sortable-table.scss +2 -2
  142. package/moj/components/sortable-table/_sortable-table.scss.map +1 -0
  143. package/moj/components/sortable-table/sortable-table.bundle.js +134 -0
  144. package/moj/components/sortable-table/sortable-table.bundle.js.map +1 -0
  145. package/moj/components/sortable-table/sortable-table.bundle.mjs +126 -0
  146. package/moj/components/sortable-table/sortable-table.bundle.mjs.map +1 -0
  147. package/moj/components/sortable-table/sortable-table.mjs +117 -130
  148. package/moj/components/sortable-table/sortable-table.mjs.map +1 -1
  149. package/moj/components/sub-navigation/_sub-navigation.scss +2 -0
  150. package/moj/components/sub-navigation/_sub-navigation.scss.map +1 -0
  151. package/moj/components/tag/_tag.scss +2 -0
  152. package/moj/components/tag/_tag.scss.map +1 -0
  153. package/moj/components/task-list/_task-list.scss +2 -0
  154. package/moj/components/task-list/_task-list.scss.map +1 -0
  155. package/moj/components/ticket-panel/_ticket-panel.scss +2 -0
  156. package/moj/components/ticket-panel/_ticket-panel.scss.map +1 -0
  157. package/moj/components/timeline/_timeline.scss +2 -0
  158. package/moj/components/timeline/_timeline.scss.map +1 -0
  159. package/moj/filters/all.js +44 -22
  160. package/moj/helpers/_all.scss +2 -0
  161. package/moj/helpers/_all.scss.map +1 -0
  162. package/moj/helpers/_hidden.scss +2 -0
  163. package/moj/helpers/_hidden.scss.map +1 -0
  164. package/moj/helpers/_links.scss +2 -0
  165. package/moj/helpers/_links.scss.map +1 -0
  166. package/moj/{helpers.js → helpers.bundle.js} +37 -42
  167. package/moj/helpers.bundle.js.map +1 -0
  168. package/moj/helpers.bundle.mjs +179 -0
  169. package/moj/helpers.bundle.mjs.map +1 -0
  170. package/moj/helpers.mjs +52 -28
  171. package/moj/helpers.mjs.map +1 -1
  172. package/moj/init.js +11 -2
  173. package/moj/moj-frontend.min.css +1 -1
  174. package/moj/moj-frontend.min.css.map +1 -1
  175. package/moj/moj-frontend.min.js +1 -1
  176. package/moj/moj-frontend.min.js.map +1 -1
  177. package/moj/objects/_all.scss +2 -0
  178. package/moj/objects/_all.scss.map +1 -0
  179. package/moj/objects/_button-group.scss +2 -0
  180. package/moj/objects/_button-group.scss.map +1 -0
  181. package/moj/objects/_filter-layout.scss +2 -0
  182. package/moj/objects/_filter-layout.scss.map +1 -0
  183. package/moj/objects/_scrollable-pane.scss +2 -0
  184. package/moj/objects/_scrollable-pane.scss.map +1 -0
  185. package/moj/objects/_width-container.scss +2 -0
  186. package/moj/objects/_width-container.scss.map +1 -0
  187. package/moj/settings/_all.scss +2 -0
  188. package/moj/settings/_all.scss.map +1 -0
  189. package/moj/settings/_assets.scss +2 -0
  190. package/moj/settings/_assets.scss.map +1 -0
  191. package/moj/settings/_colours.scss +2 -0
  192. package/moj/settings/_colours.scss.map +1 -0
  193. package/moj/settings/_measurements.scss +2 -0
  194. package/moj/settings/_measurements.scss.map +1 -0
  195. package/moj/settings/_typography.scss +2 -0
  196. package/moj/settings/_typography.scss.map +1 -0
  197. package/moj/template.njk +13 -0
  198. package/moj/utilities/_all.scss +2 -0
  199. package/moj/utilities/_all.scss.map +1 -0
  200. package/moj/utilities/_hidden.scss +2 -0
  201. package/moj/utilities/_hidden.scss.map +1 -0
  202. package/moj/utilities/_width-container.scss +2 -0
  203. package/moj/utilities/_width-container.scss.map +1 -0
  204. package/moj/vendor/govuk-frontend/_base.scss +2 -0
  205. package/moj/vendor/govuk-frontend/_base.scss.map +1 -0
  206. package/moj/vendor/govuk-frontend/_index.scss +2 -0
  207. package/moj/vendor/govuk-frontend/_index.scss.map +1 -0
  208. package/moj/{version.js → version.bundle.js} +1 -1
  209. package/moj/version.bundle.js.map +1 -0
  210. package/moj/version.bundle.mjs +4 -0
  211. package/moj/version.bundle.mjs.map +1 -0
  212. package/moj/version.mjs.map +1 -1
  213. package/package.json +5 -6
  214. package/moj/all.jquery.min.js +0 -1
  215. package/moj/all.jquery.min.js.map +0 -1
  216. package/moj/all.js +0 -2662
  217. package/moj/all.js.map +0 -1
  218. package/moj/components/add-another/add-another.js +0 -115
  219. package/moj/components/add-another/add-another.js.map +0 -1
  220. package/moj/components/alert/alert.js +0 -356
  221. package/moj/components/alert/alert.js.map +0 -1
  222. package/moj/components/alert/alert.spec.helper.js.map +0 -1
  223. package/moj/components/button-menu/button-menu.js.map +0 -1
  224. package/moj/components/date-picker/date-picker.js.map +0 -1
  225. package/moj/components/filter-toggle-button/filter-toggle-button.js +0 -102
  226. package/moj/components/filter-toggle-button/filter-toggle-button.js.map +0 -1
  227. package/moj/components/form-validator/form-validator.js +0 -205
  228. package/moj/components/form-validator/form-validator.js.map +0 -1
  229. package/moj/components/multi-file-upload/multi-file-upload.js +0 -241
  230. package/moj/components/multi-file-upload/multi-file-upload.js.map +0 -1
  231. package/moj/components/multi-select/multi-select.js +0 -86
  232. package/moj/components/multi-select/multi-select.js.map +0 -1
  233. package/moj/components/password-reveal/password-reveal.js +0 -44
  234. package/moj/components/password-reveal/password-reveal.js.map +0 -1
  235. package/moj/components/rich-text-editor/rich-text-editor.js +0 -166
  236. package/moj/components/rich-text-editor/rich-text-editor.js.map +0 -1
  237. package/moj/components/search-toggle/search-toggle.js +0 -63
  238. package/moj/components/search-toggle/search-toggle.js.map +0 -1
  239. package/moj/components/sortable-table/sortable-table.js +0 -147
  240. package/moj/components/sortable-table/sortable-table.js.map +0 -1
  241. package/moj/helpers.js.map +0 -1
  242. package/moj/vendor/html5shiv.js +0 -326
  243. package/moj/vendor/jquery.js +0 -9300
  244. package/moj/version.js.map +0 -1
@@ -0,0 +1,2502 @@
1
+ class AddAnother {
2
+ constructor(container) {
3
+ this.container = container;
4
+ if (this.container.hasAttribute('data-moj-add-another-init')) {
5
+ return this;
6
+ }
7
+ this.container.setAttribute('data-moj-add-another-init', '');
8
+ this.container.addEventListener('click', this.onRemoveButtonClick.bind(this));
9
+ this.container.addEventListener('click', this.onAddButtonClick.bind(this));
10
+ const buttons = this.container.querySelectorAll('.moj-add-another__add-button, moj-add-another__remove-button');
11
+ buttons.forEach(button => {
12
+ if (!(button instanceof HTMLButtonElement)) {
13
+ return;
14
+ }
15
+ button.type = 'button';
16
+ });
17
+ }
18
+ onAddButtonClick(event) {
19
+ const button = event.target;
20
+ if (!button || !(button instanceof HTMLButtonElement) || !button.classList.contains('moj-add-another__add-button')) {
21
+ return;
22
+ }
23
+ const items = this.getItems();
24
+ const item = this.getNewItem();
25
+ if (!item || !(item instanceof HTMLElement)) {
26
+ return;
27
+ }
28
+ this.updateAttributes(item, items.length);
29
+ this.resetItem(item);
30
+ const firstItem = items[0];
31
+ if (!this.hasRemoveButton(firstItem)) {
32
+ this.createRemoveButton(firstItem);
33
+ }
34
+ items[items.length - 1].after(item);
35
+ const input = item.querySelector('input, textarea, select');
36
+ if (input && input instanceof HTMLInputElement) {
37
+ input.focus();
38
+ }
39
+ }
40
+ hasRemoveButton(item) {
41
+ return item.querySelectorAll('.moj-add-another__remove-button').length;
42
+ }
43
+ getItems() {
44
+ if (!this.container) {
45
+ return [];
46
+ }
47
+ const items = Array.from(this.container.querySelectorAll('.moj-add-another__item'));
48
+ return items.filter(item => item instanceof HTMLElement);
49
+ }
50
+ getNewItem() {
51
+ const items = this.getItems();
52
+ const item = items[0].cloneNode(true);
53
+ if (!item || !(item instanceof HTMLElement)) {
54
+ return;
55
+ }
56
+ if (!this.hasRemoveButton(item)) {
57
+ this.createRemoveButton(item);
58
+ }
59
+ return item;
60
+ }
61
+ updateAttributes(item, index) {
62
+ item.querySelectorAll('[data-name]').forEach(el => {
63
+ if (!(el instanceof HTMLInputElement)) {
64
+ return;
65
+ }
66
+ const name = el.getAttribute('data-name') || '';
67
+ const id = el.getAttribute('data-id') || '';
68
+ const originalId = el.id;
69
+ el.name = name.replace(/%index%/, `${index}`);
70
+ el.id = id.replace(/%index%/, `${index}`);
71
+ const label = el.parentElement.querySelector('label') || el.closest('label') || item.querySelector(`[for="${originalId}"]`);
72
+ if (label && label instanceof HTMLLabelElement) {
73
+ label.htmlFor = el.id;
74
+ }
75
+ });
76
+ }
77
+ createRemoveButton(item) {
78
+ const button = document.createElement('button');
79
+ button.type = 'button';
80
+ button.classList.add('govuk-button', 'govuk-button--secondary', 'moj-add-another__remove-button');
81
+ button.textContent = 'Remove';
82
+ item.append(button);
83
+ }
84
+ resetItem(item) {
85
+ item.querySelectorAll('[data-name], [data-id]').forEach(el => {
86
+ if (!(el instanceof HTMLInputElement)) {
87
+ return;
88
+ }
89
+ if (el.type === 'checkbox' || el.type === 'radio') {
90
+ el.checked = false;
91
+ } else {
92
+ el.value = '';
93
+ }
94
+ });
95
+ }
96
+ onRemoveButtonClick(event) {
97
+ const button = event.target;
98
+ if (!button || !(button instanceof HTMLButtonElement) || !button.classList.contains('moj-add-another__remove-button')) {
99
+ return;
100
+ }
101
+ button.closest('.moj-add-another__item').remove();
102
+ const items = this.getItems();
103
+ if (items.length === 1) {
104
+ items[0].querySelector('.moj-add-another__remove-button').remove();
105
+ }
106
+ items.forEach((el, index) => {
107
+ this.updateAttributes(el, index);
108
+ });
109
+ this.focusHeading();
110
+ }
111
+ focusHeading() {
112
+ const heading = this.container.querySelector('.moj-add-another__heading');
113
+ if (heading && heading instanceof HTMLElement) {
114
+ heading.focus();
115
+ }
116
+ }
117
+ }
118
+
119
+ function removeAttributeValue(el, attr, value) {
120
+ let re, m;
121
+ if (el.getAttribute(attr)) {
122
+ if (el.getAttribute(attr) === value) {
123
+ el.removeAttribute(attr);
124
+ } else {
125
+ re = new RegExp(`(^|\\s)${value}(\\s|$)`);
126
+ m = el.getAttribute(attr).match(re);
127
+ if (m && m.length === 3) {
128
+ el.setAttribute(attr, el.getAttribute(attr).replace(re, m[1] && m[2] ? ' ' : ''));
129
+ }
130
+ }
131
+ }
132
+ }
133
+ function addAttributeValue(el, attr, value) {
134
+ let re;
135
+ if (!el.getAttribute(attr)) {
136
+ el.setAttribute(attr, value);
137
+ } else {
138
+ re = new RegExp(`(^|\\s)${value}(\\s|$)`);
139
+ if (!re.test(el.getAttribute(attr))) {
140
+ el.setAttribute(attr, `${el.getAttribute(attr)} ${value}`);
141
+ }
142
+ }
143
+ }
144
+ function dragAndDropSupported() {
145
+ const div = document.createElement('div');
146
+ return typeof div.ondrop !== 'undefined';
147
+ }
148
+ function formDataSupported() {
149
+ return typeof FormData === 'function';
150
+ }
151
+ function fileApiSupported() {
152
+ const input = document.createElement('input');
153
+ input.type = 'file';
154
+ return typeof input.files !== 'undefined';
155
+ }
156
+
157
+ /**
158
+ * Find an elements preceding sibling
159
+ *
160
+ * Utility function to find an elements previous sibling matching the provided
161
+ * selector.
162
+ *
163
+ * @param {Element | null} $element - Element to find siblings for
164
+ * @param {string} [selector] - selector for required sibling
165
+ */
166
+ function getPreviousSibling($element, selector) {
167
+ if (!$element || !($element instanceof HTMLElement)) {
168
+ return;
169
+ }
170
+
171
+ // Get the previous sibling element
172
+ let $sibling = $element.previousElementSibling;
173
+
174
+ // If the sibling matches our selector, use it
175
+ // If not, jump to the next sibling and continue the loop
176
+ while ($sibling) {
177
+ if ($sibling.matches(selector)) return $sibling;
178
+ $sibling = $sibling.previousElementSibling;
179
+ }
180
+ }
181
+
182
+ /**
183
+ * @param {Element | null} $element
184
+ * @param {string} [selector]
185
+ */
186
+ function findNearestMatchingElement($element, selector) {
187
+ // If no element or selector is provided, return
188
+ if (!$element || !($element instanceof HTMLElement) || false) {
189
+ return;
190
+ }
191
+
192
+ // Start with the current element
193
+ let $currentElement = $element;
194
+ while ($currentElement) {
195
+ // First check the current element
196
+ if ($currentElement.matches(selector)) {
197
+ return $currentElement;
198
+ }
199
+
200
+ // Check all previous siblings
201
+ let $sibling = $currentElement.previousElementSibling;
202
+ while ($sibling) {
203
+ // Check if the sibling itself is a heading
204
+ if ($sibling.matches(selector)) {
205
+ return $sibling;
206
+ }
207
+ $sibling = $sibling.previousElementSibling;
208
+ }
209
+
210
+ // If no match found in siblings, move up to parent
211
+ $currentElement = $currentElement.parentElement;
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Move focus to element
217
+ *
218
+ * Sets tabindex to -1 to make the element programmatically focusable,
219
+ * but removes it on blur as the element doesn't need to be focused again.
220
+ *
221
+ * @param {HTMLElement} $element - HTML element
222
+ * @param {object} [options] - Handler options
223
+ * @param {function(this: HTMLElement): void} [options.onBeforeFocus] - Callback before focus
224
+ * @param {function(this: HTMLElement): void} [options.onBlur] - Callback on blur
225
+ */
226
+ function setFocus($element, options = {}) {
227
+ const isFocusable = $element.getAttribute('tabindex');
228
+ if (!isFocusable) {
229
+ $element.setAttribute('tabindex', '-1');
230
+ }
231
+
232
+ /**
233
+ * Handle element focus
234
+ */
235
+ function onFocus() {
236
+ $element.addEventListener('blur', onBlur, {
237
+ once: true
238
+ });
239
+ }
240
+
241
+ /**
242
+ * Handle element blur
243
+ */
244
+ function onBlur() {
245
+ if (options.onBlur) {
246
+ options.onBlur.call($element);
247
+ }
248
+ if (!isFocusable) {
249
+ $element.removeAttribute('tabindex');
250
+ }
251
+ }
252
+
253
+ // Add listener to reset element on blur, after focus
254
+ $element.addEventListener('focus', onFocus, {
255
+ once: true
256
+ });
257
+
258
+ // Focus element
259
+ if (options.onBeforeFocus) {
260
+ options.onBeforeFocus.call($element);
261
+ }
262
+ $element.focus();
263
+ }
264
+
265
+ class Alert {
266
+ /**
267
+ * @param {Element | null} $module - HTML element to use for alert
268
+ * @param {AlertConfig} [config] - Alert config
269
+ */
270
+ constructor($module, config = {}) {
271
+ if (!$module || !($module instanceof HTMLElement)) {
272
+ return this;
273
+ }
274
+ const schema = Object.freeze({
275
+ properties: {
276
+ dismissible: {
277
+ type: 'boolean'
278
+ },
279
+ dismissText: {
280
+ type: 'string'
281
+ },
282
+ disableAutoFocus: {
283
+ type: 'boolean'
284
+ },
285
+ focusOnDismissSelector: {
286
+ type: 'string'
287
+ }
288
+ }
289
+ });
290
+ const defaults = {
291
+ dismissible: false,
292
+ dismissText: 'Dismiss',
293
+ disableAutoFocus: false
294
+ };
295
+
296
+ // data attributes override JS config, which overrides defaults
297
+ this.config = this.mergeConfigs(defaults, config, this.parseDataset(schema, $module.dataset));
298
+ this.$module = $module;
299
+
300
+ /**
301
+ * Focus the alert
302
+ *
303
+ * If `role="alert"` is set, focus the element to help some assistive
304
+ * technologies prioritise announcing it.
305
+ *
306
+ * You can turn off the auto-focus functionality by setting
307
+ * `data-disable-auto-focus="true"` in the component HTML. You might wish to
308
+ * do this based on user research findings, or to avoid a clash with another
309
+ * element which should be focused when the page loads.
310
+ */
311
+ if (this.$module.getAttribute('role') === 'alert' && !this.config.disableAutoFocus) {
312
+ setFocus(this.$module);
313
+ }
314
+ this.$dismissButton = this.$module.querySelector('.moj-alert__dismiss');
315
+ if (this.config.dismissible && this.$dismissButton) {
316
+ this.$dismissButton.innerHTML = this.config.dismissText;
317
+ this.$dismissButton.removeAttribute('hidden');
318
+ this.$module.addEventListener('click', event => {
319
+ if (event.target instanceof Node && this.$dismissButton.contains(event.target)) {
320
+ this.dimiss();
321
+ }
322
+ });
323
+ }
324
+ }
325
+
326
+ /**
327
+ * Handle dismissing the alert
328
+ */
329
+ dimiss() {
330
+ let $elementToRecieveFocus;
331
+
332
+ // If a selector has been provided, attempt to find that element
333
+ if (this.config.focusOnDismissSelector) {
334
+ $elementToRecieveFocus = document.querySelector(this.config.focusOnDismissSelector);
335
+ }
336
+
337
+ // Is the next sibling another alert
338
+ if (!$elementToRecieveFocus) {
339
+ const $nextSibling = this.$module.nextElementSibling;
340
+ if ($nextSibling && $nextSibling.matches('.moj-alert')) {
341
+ $elementToRecieveFocus = $nextSibling;
342
+ }
343
+ }
344
+
345
+ // Else try to find any preceding sibling alert or heading
346
+ if (!$elementToRecieveFocus) {
347
+ $elementToRecieveFocus = getPreviousSibling(this.$module, '.moj-alert, h1, h2, h3, h4, h5, h6');
348
+ }
349
+
350
+ // Else find the closest ancestor heading, or fallback to main, or last resort
351
+ // use the body element
352
+ if (!$elementToRecieveFocus) {
353
+ $elementToRecieveFocus = findNearestMatchingElement(this.$module, 'h1, h2, h3, h4, h5, h6, main, body');
354
+ }
355
+
356
+ // If we have an element, place focus on it
357
+ if ($elementToRecieveFocus instanceof HTMLElement) {
358
+ setFocus($elementToRecieveFocus);
359
+ }
360
+
361
+ // Remove the alert
362
+ this.$module.remove();
363
+ }
364
+
365
+ /**
366
+ * Normalise string
367
+ *
368
+ * 'If it looks like a duck, and it quacks like a duck…' 🦆
369
+ *
370
+ * If the passed value looks like a boolean or a number, convert it to a boolean
371
+ * or number.
372
+ *
373
+ * Designed to be used to convert config passed via data attributes (which are
374
+ * always strings) into something sensible.
375
+ *
376
+ * @internal
377
+ * @param {DOMStringMap[string]} value - The value to normalise
378
+ * @param {SchemaProperty} [property] - Component schema property
379
+ * @returns {string | boolean | number | undefined} Normalised data
380
+ */
381
+ normaliseString(value, property) {
382
+ const trimmedValue = value ? value.trim() : '';
383
+ let output;
384
+ let outputType;
385
+ if (property && property.type) {
386
+ outputType = property.type;
387
+ }
388
+
389
+ // No schema type set? Determine automatically
390
+ if (!outputType) {
391
+ if (['true', 'false'].includes(trimmedValue)) {
392
+ outputType = 'boolean';
393
+ }
394
+
395
+ // Empty / whitespace-only strings are considered finite so we need to check
396
+ // the length of the trimmed string as well
397
+ if (trimmedValue.length > 0 && Number.isFinite(Number(trimmedValue))) {
398
+ outputType = 'number';
399
+ }
400
+ }
401
+ switch (outputType) {
402
+ case 'boolean':
403
+ output = trimmedValue === 'true';
404
+ break;
405
+ case 'number':
406
+ output = Number(trimmedValue);
407
+ break;
408
+ default:
409
+ output = value;
410
+ }
411
+ return output;
412
+ }
413
+
414
+ /**
415
+ * Parse dataset
416
+ *
417
+ * Loop over an object and normalise each value using {@link normaliseString},
418
+ * optionally expanding nested `i18n.field`
419
+ *
420
+ * @param {Schema} schema - component schema
421
+ * @param {DOMStringMap} dataset - HTML element dataset
422
+ * @returns {object} Normalised dataset
423
+ */
424
+ parseDataset(schema, dataset) {
425
+ const parsed = {};
426
+ for (const [field, property] of Object.entries(schema.properties)) {
427
+ if (field in dataset) {
428
+ if (dataset[field]) {
429
+ parsed[field] = this.normaliseString(dataset[field], property);
430
+ }
431
+ }
432
+ }
433
+ return parsed;
434
+ }
435
+
436
+ /**
437
+ * Config merging function
438
+ *
439
+ * Takes any number of objects and combines them together, with
440
+ * greatest priority on the LAST item passed in.
441
+ *
442
+ * @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
443
+ * @returns {{ [key: string]: unknown }} A merged config object
444
+ */
445
+ mergeConfigs(...configObjects) {
446
+ const formattedConfigObject = {};
447
+
448
+ // Loop through each of the passed objects
449
+ for (const configObject of configObjects) {
450
+ for (const key of Object.keys(configObject)) {
451
+ const option = formattedConfigObject[key];
452
+ const override = configObject[key];
453
+
454
+ // Push their keys one-by-one into formattedConfigObject. Any duplicate
455
+ // keys with object values will be merged, otherwise the new value will
456
+ // override the existing value.
457
+ if (typeof option === 'object' && typeof override === 'object') {
458
+ // @ts-expect-error Index signature for type 'string' is missing
459
+ formattedConfigObject[key] = this.mergeConfigs(option, override);
460
+ } else {
461
+ formattedConfigObject[key] = override;
462
+ }
463
+ }
464
+ }
465
+ return formattedConfigObject;
466
+ }
467
+ }
468
+
469
+ /**
470
+ * @typedef {object} AlertConfig
471
+ * @property {boolean} [dismissible=false] - Can the alert be dismissed by the user
472
+ * @property {string} [dismissText=Dismiss] - the label text for the dismiss button
473
+ * @property {boolean} [disableAutoFocus=false] - whether the alert will be autofocused
474
+ * @property {string} [focusOnDismissSelector] - CSS Selector for element to be focused on dismiss
475
+ */
476
+
477
+ class ButtonMenu {
478
+ /**
479
+ * @param {Element | null} $module - HTML element to use for button menu
480
+ * @param {ButtonMenuConfig} [config] - Button menu config
481
+ */
482
+ constructor($module, config = {}) {
483
+ if (!$module || !($module instanceof HTMLElement)) {
484
+ return this;
485
+ }
486
+ const schema = Object.freeze({
487
+ properties: {
488
+ buttonText: {
489
+ type: 'string'
490
+ },
491
+ buttonClasses: {
492
+ type: 'string'
493
+ },
494
+ alignMenu: {
495
+ type: 'string'
496
+ }
497
+ }
498
+ });
499
+ const defaults = {
500
+ buttonText: 'Actions',
501
+ alignMenu: 'left',
502
+ buttonClasses: ''
503
+ };
504
+ // data attributes override JS config, which overrides defaults
505
+ this.config = this.mergeConfigs(defaults, config, this.parseDataset(schema, $module.dataset));
506
+ this.$module = $module;
507
+
508
+ // If only one button is provided, don't initiate a menu and toggle button
509
+ // if classes have been provided for the toggleButton, apply them to the single item
510
+ if (this.$module.children.length === 1) {
511
+ const button = this.$module.children[0];
512
+ button.classList.forEach(className => {
513
+ if (className.startsWith('govuk-button-')) {
514
+ button.classList.remove(className);
515
+ }
516
+ button.classList.remove('moj-button-menu__item');
517
+ button.classList.add('moj-button-menu__single-button');
518
+ });
519
+ if (this.config.buttonClasses) {
520
+ button.classList.add(...this.config.buttonClasses.split(' '));
521
+ }
522
+ }
523
+ // Otherwise intialise a button menu
524
+ if (this.$module.children.length > 1) {
525
+ this.initMenu();
526
+ }
527
+ }
528
+ initMenu() {
529
+ this.$menu = this.createMenu();
530
+ this.$module.insertAdjacentHTML('afterbegin', this.toggleTemplate());
531
+ this.setupMenuItems();
532
+ this.$menuToggle = this.$module.querySelector(':scope > button');
533
+ this.items = this.$menu.querySelectorAll('a, button');
534
+ this.$menuToggle.addEventListener('click', event => {
535
+ this.toggleMenu(event);
536
+ });
537
+ this.$module.addEventListener('keydown', event => {
538
+ this.handleKeyDown(event);
539
+ });
540
+ document.addEventListener('click', event => {
541
+ if (!this.$module.contains(event.target)) {
542
+ this.closeMenu(false);
543
+ }
544
+ });
545
+ }
546
+ createMenu() {
547
+ const $menu = document.createElement('ul');
548
+ $menu.setAttribute('role', 'list');
549
+ $menu.hidden = true;
550
+ $menu.classList.add('moj-button-menu__wrapper');
551
+ if (this.config.alignMenu === 'right') {
552
+ $menu.classList.add('moj-button-menu__wrapper--right');
553
+ }
554
+ this.$module.appendChild($menu);
555
+ while (this.$module.firstChild !== $menu) {
556
+ $menu.appendChild(this.$module.firstChild);
557
+ }
558
+ return $menu;
559
+ }
560
+ setupMenuItems() {
561
+ Array.from(this.$menu.children).forEach(item => {
562
+ // wrap item in li tag
563
+ const listItem = document.createElement('li');
564
+ this.$menu.insertBefore(listItem, item);
565
+ listItem.appendChild(item);
566
+ item.setAttribute('tabindex', '-1');
567
+ if (item.tagName === 'BUTTON') {
568
+ item.setAttribute('type', 'button');
569
+ }
570
+ item.classList.forEach(className => {
571
+ if (className.startsWith('govuk-button')) {
572
+ item.classList.remove(className);
573
+ }
574
+ });
575
+
576
+ // add a slight delay after click before closing the menu, makes it *feel* better
577
+ item.addEventListener('click', () => {
578
+ setTimeout(() => {
579
+ this.closeMenu(false);
580
+ }, 50);
581
+ });
582
+ });
583
+ }
584
+ toggleTemplate() {
585
+ return `
586
+ <button type="button" class="govuk-button moj-button-menu__toggle-button ${this.config.buttonClasses || ''}" aria-haspopup="true" aria-expanded="false">
587
+ <span>
588
+ ${this.config.buttonText}
589
+ <svg width="11" height="5" viewBox="0 0 11 5" xmlns="http://www.w3.org/2000/svg">
590
+ <path d="M5.5 0L11 5L0 5L5.5 0Z" fill="currentColor"/>
591
+ </svg>
592
+ </span>
593
+ </button>`;
594
+ }
595
+
596
+ /**
597
+ * @returns {boolean}
598
+ */
599
+ isOpen() {
600
+ return this.$menuToggle.getAttribute('aria-expanded') === 'true';
601
+ }
602
+ toggleMenu(event) {
603
+ event.preventDefault();
604
+
605
+ // If menu is triggered with mouse don't move focus to first item
606
+ const keyboardEvent = event.detail === 0;
607
+ const focusIndex = keyboardEvent ? 0 : -1;
608
+ if (this.isOpen()) {
609
+ this.closeMenu();
610
+ } else {
611
+ this.openMenu(focusIndex);
612
+ }
613
+ }
614
+
615
+ /**
616
+ * Opens the menu and optionally sets the focus to the item with given index
617
+ *
618
+ * @param {number} focusIndex - The index of the item to focus
619
+ */
620
+ openMenu(focusIndex = 0) {
621
+ this.$menu.hidden = false;
622
+ this.$menuToggle.setAttribute('aria-expanded', 'true');
623
+ if (focusIndex !== -1) {
624
+ this.focusItem(focusIndex);
625
+ }
626
+ }
627
+
628
+ /**
629
+ * Closes the menu and optionally returns focus back to menuToggle
630
+ *
631
+ * @param {boolean} moveFocus - whether to return focus to the toggle button
632
+ */
633
+ closeMenu(moveFocus = true) {
634
+ this.$menu.hidden = true;
635
+ this.$menuToggle.setAttribute('aria-expanded', 'false');
636
+ if (moveFocus) {
637
+ this.$menuToggle.focus();
638
+ }
639
+ }
640
+
641
+ /**
642
+ * Focuses the menu item at the specified index
643
+ *
644
+ * @param {number} index - the index of the item to focus
645
+ */
646
+ focusItem(index) {
647
+ if (index >= this.items.length) index = 0;
648
+ if (index < 0) index = this.items.length - 1;
649
+ const menuItem = this.items.item(index);
650
+ if (menuItem) {
651
+ menuItem.focus();
652
+ }
653
+ }
654
+ currentFocusIndex() {
655
+ const activeElement = document.activeElement;
656
+ const menuItems = Array.from(this.items);
657
+ return menuItems.indexOf(activeElement);
658
+ }
659
+ handleKeyDown(event) {
660
+ if (event.target === this.$menuToggle) {
661
+ switch (event.key) {
662
+ case 'ArrowDown':
663
+ event.preventDefault();
664
+ this.openMenu();
665
+ break;
666
+ case 'ArrowUp':
667
+ event.preventDefault();
668
+ this.openMenu(this.items.length - 1);
669
+ break;
670
+ }
671
+ }
672
+ if (this.$menu.contains(event.target) && this.isOpen()) {
673
+ switch (event.key) {
674
+ case 'ArrowDown':
675
+ event.preventDefault();
676
+ if (this.currentFocusIndex() !== -1) {
677
+ this.focusItem(this.currentFocusIndex() + 1);
678
+ }
679
+ break;
680
+ case 'ArrowUp':
681
+ event.preventDefault();
682
+ if (this.currentFocusIndex() !== -1) {
683
+ this.focusItem(this.currentFocusIndex() - 1);
684
+ }
685
+ break;
686
+ case 'Home':
687
+ event.preventDefault();
688
+ this.focusItem(0);
689
+ break;
690
+ case 'End':
691
+ event.preventDefault();
692
+ this.focusItem(this.items.length - 1);
693
+ break;
694
+ }
695
+ }
696
+ if (event.key === 'Escape' && this.isOpen()) {
697
+ this.closeMenu();
698
+ }
699
+ if (event.key === 'Tab' && this.isOpen()) {
700
+ this.closeMenu(false);
701
+ }
702
+ }
703
+
704
+ /**
705
+ * Parse dataset
706
+ *
707
+ * Loop over an object and normalise each value using {@link normaliseString},
708
+ * optionally expanding nested `i18n.field`
709
+ *
710
+ * @param {Schema} schema - component schema
711
+ * @param {DOMStringMap} dataset - HTML element dataset
712
+ * @returns {object} Normalised dataset
713
+ */
714
+ parseDataset(schema, dataset) {
715
+ const parsed = {};
716
+ for (const [field,,] of Object.entries(schema.properties)) {
717
+ if (field in dataset) {
718
+ if (dataset[field]) {
719
+ parsed[field] = dataset[field];
720
+ }
721
+ }
722
+ }
723
+ return parsed;
724
+ }
725
+
726
+ /**
727
+ * Config merging function
728
+ *
729
+ * Takes any number of objects and combines them together, with
730
+ * greatest priority on the LAST item passed in.
731
+ *
732
+ * @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
733
+ * @returns {{ [key: string]: unknown }} A merged config object
734
+ */
735
+ mergeConfigs(...configObjects) {
736
+ const formattedConfigObject = {};
737
+
738
+ // Loop through each of the passed objects
739
+ for (const configObject of configObjects) {
740
+ for (const key of Object.keys(configObject)) {
741
+ const option = formattedConfigObject[key];
742
+ const override = configObject[key];
743
+
744
+ // Push their keys one-by-one into formattedConfigObject. Any duplicate
745
+ // keys with object values will be merged, otherwise the new value will
746
+ // override the existing value.
747
+ if (typeof option === 'object' && typeof override === 'object') {
748
+ // @ts-expect-error Index signature for type 'string' is missing
749
+ formattedConfigObject[key] = this.mergeConfigs(option, override);
750
+ } else {
751
+ formattedConfigObject[key] = override;
752
+ }
753
+ }
754
+ }
755
+ return formattedConfigObject;
756
+ }
757
+ }
758
+
759
+ /**
760
+ * @typedef {object} ButtonMenuConfig
761
+ * @property {string} [buttonText='Actions'] - Label for the toggle button
762
+ * @property {"left" | "right"} [alignMenu='left'] - the alignment of the menu
763
+ * @property {string} [buttonClasses='govuk-button--secondary'] - css classes applied to the toggle button
764
+ */
765
+
766
+ class DatePicker {
767
+ /**
768
+ * @param {Element | null} $module - HTML element to use for date picker
769
+ * @param {DatePickerConfig} [config] - Date picker config
770
+ */
771
+ constructor($module, config = {}) {
772
+ if (!$module || !($module instanceof HTMLElement)) {
773
+ return this;
774
+ }
775
+ const $input = $module.querySelector('.moj-js-datepicker-input');
776
+
777
+ // Check that required elements are present
778
+ if (!$input || !($input instanceof HTMLInputElement)) {
779
+ return this;
780
+ }
781
+ this.$module = $module;
782
+ this.$input = $input;
783
+ const schema = Object.freeze({
784
+ properties: {
785
+ excludedDates: {
786
+ type: 'string'
787
+ },
788
+ excludedDays: {
789
+ type: 'string'
790
+ },
791
+ leadingZeros: {
792
+ type: 'string'
793
+ },
794
+ maxDate: {
795
+ type: 'string'
796
+ },
797
+ minDate: {
798
+ type: 'string'
799
+ },
800
+ weekStartDay: {
801
+ type: 'string'
802
+ }
803
+ }
804
+ });
805
+ const defaults = {
806
+ leadingZeros: false,
807
+ weekStartDay: 'monday'
808
+ };
809
+
810
+ // data attributes override JS config, which overrides defaults
811
+ this.config = this.mergeConfigs(defaults, config, this.parseDataset(schema, $module.dataset));
812
+ this.dayLabels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
813
+ this.monthLabels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
814
+ this.currentDate = new Date();
815
+ this.currentDate.setHours(0, 0, 0, 0);
816
+ this.calendarDays = [];
817
+ this.excludedDates = [];
818
+ this.excludedDays = [];
819
+ this.buttonClass = 'moj-datepicker__button';
820
+ this.selectedDayButtonClass = 'moj-datepicker__button--selected';
821
+ this.currentDayButtonClass = 'moj-datepicker__button--current';
822
+ this.todayButtonClass = 'moj-datepicker__button--today';
823
+ if (this.$module.dataset.initialized) {
824
+ return this;
825
+ }
826
+ this.setOptions();
827
+ this.initControls();
828
+ this.$module.setAttribute('data-initialized', 'true');
829
+ }
830
+ initControls() {
831
+ this.id = `datepicker-${this.$input.id}`;
832
+ this.$dialog = this.createDialog();
833
+ this.createCalendarHeaders();
834
+ const $componentWrapper = document.createElement('div');
835
+ const $inputWrapper = document.createElement('div');
836
+ $componentWrapper.classList.add('moj-datepicker__wrapper');
837
+ $inputWrapper.classList.add('govuk-input__wrapper');
838
+ this.$input.parentElement.insertBefore($componentWrapper, this.$input);
839
+ $componentWrapper.appendChild($inputWrapper);
840
+ $inputWrapper.appendChild(this.$input);
841
+ $inputWrapper.insertAdjacentHTML('beforeend', this.toggleTemplate());
842
+ $componentWrapper.insertAdjacentElement('beforeend', this.$dialog);
843
+ this.$calendarButton = this.$module.querySelector('.moj-js-datepicker-toggle');
844
+ this.$dialogTitle = this.$dialog.querySelector('.moj-js-datepicker-month-year');
845
+ this.createCalendar();
846
+ this.$prevMonthButton = this.$dialog.querySelector('.moj-js-datepicker-prev-month');
847
+ this.$prevYearButton = this.$dialog.querySelector('.moj-js-datepicker-prev-year');
848
+ this.$nextMonthButton = this.$dialog.querySelector('.moj-js-datepicker-next-month');
849
+ this.$nextYearButton = this.$dialog.querySelector('.moj-js-datepicker-next-year');
850
+ this.$cancelButton = this.$dialog.querySelector('.moj-js-datepicker-cancel');
851
+ this.$okButton = this.$dialog.querySelector('.moj-js-datepicker-ok');
852
+
853
+ // add event listeners
854
+ this.$prevMonthButton.addEventListener('click', event => this.focusPreviousMonth(event, false));
855
+ this.$prevYearButton.addEventListener('click', event => this.focusPreviousYear(event, false));
856
+ this.$nextMonthButton.addEventListener('click', event => this.focusNextMonth(event, false));
857
+ this.$nextYearButton.addEventListener('click', event => this.focusNextYear(event, false));
858
+ this.$cancelButton.addEventListener('click', event => {
859
+ event.preventDefault();
860
+ this.closeDialog();
861
+ });
862
+ this.$okButton.addEventListener('click', () => {
863
+ this.selectDate(this.currentDate);
864
+ });
865
+ const dialogButtons = this.$dialog.querySelectorAll('button:not([disabled="true"])');
866
+ this.$firstButtonInDialog = dialogButtons[0];
867
+ this.$lastButtonInDialog = dialogButtons[dialogButtons.length - 1];
868
+ this.$firstButtonInDialog.addEventListener('keydown', event => this.firstButtonKeydown(event));
869
+ this.$lastButtonInDialog.addEventListener('keydown', event => this.lastButtonKeydown(event));
870
+ this.$calendarButton.addEventListener('click', event => this.toggleDialog(event));
871
+ this.$dialog.addEventListener('keydown', event => {
872
+ if (event.key === 'Escape') {
873
+ this.closeDialog();
874
+ event.preventDefault();
875
+ event.stopPropagation();
876
+ }
877
+ });
878
+ document.body.addEventListener('mouseup', event => this.backgroundClick(event));
879
+
880
+ // populates calendar with initial dates, avoids Wave errors about null buttons
881
+ this.updateCalendar();
882
+ }
883
+ createDialog() {
884
+ const titleId = `datepicker-title-${this.$input.id}`;
885
+ const $dialog = document.createElement('div');
886
+ $dialog.id = this.id;
887
+ $dialog.setAttribute('class', 'moj-datepicker__dialog');
888
+ $dialog.setAttribute('role', 'dialog');
889
+ $dialog.setAttribute('aria-modal', 'true');
890
+ $dialog.setAttribute('aria-labelledby', titleId);
891
+ $dialog.innerHTML = this.dialogTemplate(titleId);
892
+ $dialog.hidden = true;
893
+ return $dialog;
894
+ }
895
+ createCalendar() {
896
+ const $tbody = this.$dialog.querySelector('tbody');
897
+ let dayCount = 0;
898
+ for (let i = 0; i < 6; i++) {
899
+ // create row
900
+ const $row = $tbody.insertRow(i);
901
+ for (let j = 0; j < 7; j++) {
902
+ // create cell (day)
903
+ const $cell = document.createElement('td');
904
+ const $dateButton = document.createElement('button');
905
+ $cell.appendChild($dateButton);
906
+ $row.appendChild($cell);
907
+ const calendarDay = new DSCalendarDay($dateButton, dayCount, i, j, this);
908
+ this.calendarDays.push(calendarDay);
909
+ dayCount++;
910
+ }
911
+ }
912
+ }
913
+ toggleTemplate() {
914
+ return `<button class="moj-datepicker__toggle moj-js-datepicker-toggle" type="button" aria-haspopup="dialog" aria-controls="${this.id}" aria-expanded="false">
915
+ <span class="govuk-visually-hidden">Choose date</span>
916
+ <svg width="32" height="24" focusable="false" class="moj-datepicker-icon" aria-hidden="true" role="img" viewBox="0 0 22 22">
917
+ <path
918
+ fill="currentColor"
919
+ fill-rule="evenodd"
920
+ clip-rule="evenodd"
921
+ d="M16.1333 2.93333H5.86668V4.4C5.86668 5.21002 5.21003 5.86667 4.40002 5.86667C3.59 5.86667 2.93335 5.21002 2.93335 4.4V2.93333H2C0.895431 2.93333 0 3.82877 0 4.93334V19.2667C0 20.3712 0.89543 21.2667 2 21.2667H20C21.1046 21.2667 22 20.3712 22 19.2667V4.93333C22 3.82876 21.1046 2.93333 20 2.93333H19.0667V4.4C19.0667 5.21002 18.41 5.86667 17.6 5.86667C16.79 5.86667 16.1333 5.21002 16.1333 4.4V2.93333ZM20.5333 8.06667H1.46665V18.8C1.46665 19.3523 1.91436 19.8 2.46665 19.8H19.5333C20.0856 19.8 20.5333 19.3523 20.5333 18.8V8.06667Z"
922
+ ></path>
923
+ <rect x="3.66669" width="1.46667" height="5.13333" rx="0.733333" fill="currentColor"></rect>
924
+ <rect x="16.8667" width="1.46667" height="5.13333" rx="0.733333" fill="currentColor"></rect>
925
+ </svg>
926
+ </button>`;
927
+ }
928
+
929
+ /**
930
+ * HTML template for calendar dialog
931
+ *
932
+ * @param {string} [titleId] - Id attribute for dialog title
933
+ * @returns {string}
934
+ */
935
+ dialogTemplate(titleId) {
936
+ return `<div class="moj-datepicker__dialog-header">
937
+ <div class="moj-datepicker__dialog-navbuttons">
938
+ <button class="moj-datepicker__button moj-js-datepicker-prev-year">
939
+ <span class="govuk-visually-hidden">Previous year</span>
940
+ <svg width="44" height="40" viewBox="0 0 44 40" fill="none" fill="none" focusable="false" aria-hidden="true" role="img">
941
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M23.1643 20L28.9572 14.2071L27.5429 12.7929L20.3358 20L27.5429 27.2071L28.9572 25.7929L23.1643 20Z" fill="currentColor"/>
942
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M17.1643 20L22.9572 14.2071L21.5429 12.7929L14.3358 20L21.5429 27.2071L22.9572 25.7929L17.1643 20Z" fill="currentColor"/>
943
+ </svg>
944
+ </button>
945
+
946
+ <button class="moj-datepicker__button moj-js-datepicker-prev-month">
947
+ <span class="govuk-visually-hidden">Previous month</span>
948
+ <svg width="44" height="40" viewBox="0 0 44 40" fill="none" focusable="false" aria-hidden="true" role="img">
949
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M20.5729 20L25.7865 14.2071L24.5137 12.7929L18.0273 20L24.5137 27.2071L25.7865 25.7929L20.5729 20Z" fill="currentColor"/>
950
+ </svg>
951
+ </button>
952
+ </div>
953
+
954
+ <h2 id="${titleId}" class="moj-datepicker__dialog-title moj-js-datepicker-month-year" aria-live="polite">June 2020</h2>
955
+
956
+ <div class="moj-datepicker__dialog-navbuttons">
957
+ <button class="moj-datepicker__button moj-js-datepicker-next-month">
958
+ <span class="govuk-visually-hidden">Next month</span>
959
+ <svg width="44" height="40" viewBox="0 0 44 40" fill="none" focusable="false" aria-hidden="true" role="img">
960
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M23.4271 20L18.2135 14.2071L19.4863 12.7929L25.9727 20L19.4863 27.2071L18.2135 25.7929L23.4271 20Z" fill="currentColor"/>
961
+ </svg>
962
+ </button>
963
+
964
+ <button class="moj-datepicker__button moj-js-datepicker-next-year">
965
+ <span class="govuk-visually-hidden">Next year</span>
966
+ <svg width="44" height="40" viewBox="0 0 44 40" fill="none" fill="none" focusable="false" aria-hidden="true" role="img">
967
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M20.8357 20L15.0428 14.2071L16.4571 12.7929L23.6642 20L16.4571 27.2071L15.0428 25.7929L20.8357 20Z" fill="currentColor"/>
968
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M26.8357 20L21.0428 14.2071L22.4571 12.7929L29.6642 20L22.4571 27.2071L21.0428 25.7929L26.8357 20Z" fill="currentColor"/>
969
+ </svg>
970
+ </button>
971
+ </div>
972
+ </div>
973
+
974
+ <table class="moj-datepicker__calendar moj-js-datepicker-grid" role="grid" aria-labelledby="${titleId}">
975
+ <thead>
976
+ <tr></tr>
977
+ </thead>
978
+
979
+ <tbody></tbody>
980
+ </table>
981
+
982
+ <div class="govuk-button-group">
983
+ <button type="button" class="govuk-button moj-js-datepicker-ok">Select</button>
984
+ <button type="button" class="govuk-button govuk-button--secondary moj-js-datepicker-cancel">Close</button>
985
+ </div>`;
986
+ }
987
+ createCalendarHeaders() {
988
+ this.dayLabels.forEach(day => {
989
+ const html = `<th scope="col"><span aria-hidden="true">${day.substring(0, 3)}</span><span class="govuk-visually-hidden">${day}</span></th>`;
990
+ const $headerRow = this.$dialog.querySelector('thead > tr');
991
+ $headerRow.insertAdjacentHTML('beforeend', html);
992
+ });
993
+ }
994
+
995
+ /**
996
+ * Pads given number with leading zeros
997
+ *
998
+ * @param {number} value - The value to be padded
999
+ * @param {number} length - The length in characters of the output
1000
+ * @returns {string}
1001
+ */
1002
+ leadingZeros(value, length = 2) {
1003
+ let ret = value.toString();
1004
+ while (ret.length < length) {
1005
+ ret = `0${ret}`;
1006
+ }
1007
+ return ret;
1008
+ }
1009
+ setOptions() {
1010
+ this.setMinAndMaxDatesOnCalendar();
1011
+ this.setExcludedDates();
1012
+ this.setExcludedDays();
1013
+ this.setLeadingZeros();
1014
+ this.setWeekStartDay();
1015
+ }
1016
+ setMinAndMaxDatesOnCalendar() {
1017
+ if (this.config.minDate) {
1018
+ this.minDate = this.formattedDateFromString(this.config.minDate, null);
1019
+ if (this.minDate && this.currentDate < this.minDate) {
1020
+ this.currentDate = this.minDate;
1021
+ }
1022
+ }
1023
+ if (this.config.maxDate) {
1024
+ this.maxDate = this.formattedDateFromString(this.config.maxDate, null);
1025
+ if (this.maxDate && this.currentDate > this.maxDate) {
1026
+ this.currentDate = this.maxDate;
1027
+ }
1028
+ }
1029
+ }
1030
+ setExcludedDates() {
1031
+ if (this.config.excludedDates) {
1032
+ this.excludedDates = this.config.excludedDates.replace(/\s+/, ' ').split(' ').map(item => {
1033
+ return item.includes('-') ? this.parseDateRangeString(item) : [this.formattedDateFromString(item)];
1034
+ }).reduce((dates, items) => dates.concat(items)).filter(date => date);
1035
+ }
1036
+ }
1037
+
1038
+ /*
1039
+ * Parses a daterange string into an array of dates
1040
+ * @param {String} datestring - A daterange string in the format "dd/mm/yyyy-dd/mm/yyyy"
1041
+ * @returns {Date[]}
1042
+ */
1043
+ parseDateRangeString(datestring) {
1044
+ const dates = [];
1045
+ const [startDate, endDate] = datestring.split('-').map(d => this.formattedDateFromString(d, null));
1046
+ if (startDate && endDate) {
1047
+ const date = new Date(startDate.getTime());
1048
+ /* eslint-disable no-unmodified-loop-condition */
1049
+ while (date <= endDate) {
1050
+ dates.push(new Date(date));
1051
+ date.setDate(date.getDate() + 1);
1052
+ }
1053
+ /* eslint-enable no-unmodified-loop-condition */
1054
+ }
1055
+ return dates;
1056
+ }
1057
+ setExcludedDays() {
1058
+ if (this.config.excludedDays) {
1059
+ // lowercase and arrange dayLabels to put indexOf sunday == 0 for comparison
1060
+ // with getDay() function
1061
+ const weekDays = this.dayLabels.map(item => item.toLowerCase());
1062
+ if (this.config.weekStartDay === 'monday') {
1063
+ weekDays.unshift(weekDays.pop());
1064
+ }
1065
+ this.excludedDays = this.config.excludedDays.replace(/\s+/, ' ').toLowerCase().split(' ').map(item => weekDays.indexOf(item)).filter(item => item !== -1);
1066
+ }
1067
+ }
1068
+ setLeadingZeros() {
1069
+ if (typeof this.config.leadingZeros !== 'boolean') {
1070
+ if (this.config.leadingZeros.toLowerCase() === 'true') {
1071
+ this.config.leadingZeros = true;
1072
+ return;
1073
+ }
1074
+ if (this.config.leadingZeros.toLowerCase() === 'false') {
1075
+ this.config.leadingZeros = false;
1076
+ }
1077
+ }
1078
+ }
1079
+ setWeekStartDay() {
1080
+ const weekStartDayParam = this.config.weekStartDay;
1081
+ if (weekStartDayParam && weekStartDayParam.toLowerCase() === 'sunday') {
1082
+ this.config.weekStartDay = 'sunday';
1083
+ // Rotate dayLabels array to put Sunday as the first item
1084
+ this.dayLabels.unshift(this.dayLabels.pop());
1085
+ } else {
1086
+ this.config.weekStartDay = 'monday';
1087
+ }
1088
+ }
1089
+
1090
+ /**
1091
+ * Determine if a date is selecteable
1092
+ *
1093
+ * @param {Date} date - the date to check
1094
+ * @returns {boolean}
1095
+ */
1096
+ isExcludedDate(date) {
1097
+ // This comparison does not work correctly - it will exclude the mindate itself
1098
+ // see: https://github.com/ministryofjustice/moj-frontend/issues/923
1099
+ if (this.minDate && this.minDate > date) {
1100
+ return true;
1101
+ }
1102
+
1103
+ // This comparison works as expected - the maxdate will not be excluded
1104
+ if (this.maxDate && this.maxDate < date) {
1105
+ return true;
1106
+ }
1107
+ for (const excludedDate of this.excludedDates) {
1108
+ if (date.toDateString() === excludedDate.toDateString()) {
1109
+ return true;
1110
+ }
1111
+ }
1112
+ if (this.excludedDays.includes(date.getDay())) {
1113
+ return true;
1114
+ }
1115
+ return false;
1116
+ }
1117
+
1118
+ /**
1119
+ * Get a Date object from a string
1120
+ *
1121
+ * @param {string} dateString - string in the format d/m/yyyy dd/mm/yyyy
1122
+ * @param {Date} fallback - date object to return if formatting fails
1123
+ * @returns {Date}
1124
+ */
1125
+ formattedDateFromString(dateString, fallback = new Date()) {
1126
+ let formattedDate = null;
1127
+ // Accepts d/m/yyyy and dd/mm/yyyy
1128
+ const dateFormatPattern = /(\d{1,2})([-/,. ])(\d{1,2})\2(\d{4})/;
1129
+ if (!dateFormatPattern.test(dateString)) return fallback;
1130
+ const match = dateFormatPattern.exec(dateString);
1131
+ const day = match[1];
1132
+ const month = match[3];
1133
+ const year = match[4];
1134
+ formattedDate = new Date(`${year}-${month}-${day}`);
1135
+ if (formattedDate instanceof Date && Number.isFinite(formattedDate.getTime())) {
1136
+ return formattedDate;
1137
+ }
1138
+ return fallback;
1139
+ }
1140
+
1141
+ /**
1142
+ * Get a formatted date string from a Date object
1143
+ *
1144
+ * @param {Date} date - date to format to a string
1145
+ * @returns {string}
1146
+ */
1147
+ formattedDateFromDate(date) {
1148
+ if (this.config.leadingZeros) {
1149
+ return `${this.leadingZeros(date.getDate())}/${this.leadingZeros(date.getMonth() + 1)}/${date.getFullYear()}`;
1150
+ }
1151
+ return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
1152
+ }
1153
+
1154
+ /**
1155
+ * Get a human readable date in the format Monday 2 March 2024
1156
+ *
1157
+ * @param {Date} date - date to format
1158
+ * @returns {string}
1159
+ */
1160
+ formattedDateHuman(date) {
1161
+ return `${this.dayLabels[(date.getDay() + 6) % 7]} ${date.getDate()} ${this.monthLabels[date.getMonth()]} ${date.getFullYear()}`;
1162
+ }
1163
+ backgroundClick(event) {
1164
+ if (this.isOpen() && event.target instanceof Node && !this.$dialog.contains(event.target) && !this.$input.contains(event.target) && !this.$calendarButton.contains(event.target)) {
1165
+ event.preventDefault();
1166
+ this.closeDialog();
1167
+ }
1168
+ }
1169
+ firstButtonKeydown(event) {
1170
+ if (event.key === 'Tab' && event.shiftKey) {
1171
+ this.$lastButtonInDialog.focus();
1172
+ event.preventDefault();
1173
+ }
1174
+ }
1175
+ lastButtonKeydown(event) {
1176
+ if (event.key === 'Tab' && !event.shiftKey) {
1177
+ this.$firstButtonInDialog.focus();
1178
+ event.preventDefault();
1179
+ }
1180
+ }
1181
+
1182
+ // render calendar
1183
+ updateCalendar() {
1184
+ this.$dialogTitle.innerHTML = `${this.monthLabels[this.currentDate.getMonth()]} ${this.currentDate.getFullYear()}`;
1185
+ const day = this.currentDate;
1186
+ const firstOfMonth = new Date(day.getFullYear(), day.getMonth(), 1);
1187
+ let dayOfWeek;
1188
+ if (this.config.weekStartDay === 'monday') {
1189
+ dayOfWeek = firstOfMonth.getDay() === 0 ? 6 : firstOfMonth.getDay() - 1; // Change logic to make Monday first day of week, i.e. 0
1190
+ } else {
1191
+ dayOfWeek = firstOfMonth.getDay();
1192
+ }
1193
+ firstOfMonth.setDate(firstOfMonth.getDate() - dayOfWeek);
1194
+ const thisDay = new Date(firstOfMonth);
1195
+
1196
+ // loop through our days
1197
+ for (const calendarDay of this.calendarDays) {
1198
+ const hidden = thisDay.getMonth() !== day.getMonth();
1199
+ const disabled = this.isExcludedDate(thisDay);
1200
+ calendarDay.update(thisDay, hidden, disabled);
1201
+ thisDay.setDate(thisDay.getDate() + 1);
1202
+ }
1203
+ }
1204
+ setCurrentDate(focus = true) {
1205
+ const {
1206
+ currentDate
1207
+ } = this;
1208
+ this.calendarDays.forEach(calendarDay => {
1209
+ calendarDay.button.classList.add('moj-datepicker__button');
1210
+ calendarDay.button.classList.add('moj-datepicker__calendar-day');
1211
+ calendarDay.button.setAttribute('tabindex', '-1');
1212
+ calendarDay.button.classList.remove(this.selectedDayButtonClass);
1213
+ const calendarDayDate = calendarDay.date;
1214
+ calendarDayDate.setHours(0, 0, 0, 0);
1215
+ const today = new Date();
1216
+ today.setHours(0, 0, 0, 0);
1217
+ if (calendarDayDate.getTime() === currentDate.getTime() /* && !calendarDay.button.disabled */) {
1218
+ if (focus) {
1219
+ calendarDay.button.setAttribute('tabindex', '0');
1220
+ calendarDay.button.focus();
1221
+ calendarDay.button.classList.add(this.selectedDayButtonClass);
1222
+ }
1223
+ }
1224
+ if (this.inputDate && calendarDayDate.getTime() === this.inputDate.getTime()) {
1225
+ calendarDay.button.classList.add(this.currentDayButtonClass);
1226
+ calendarDay.button.setAttribute('aria-current', 'date');
1227
+ } else {
1228
+ calendarDay.button.classList.remove(this.currentDayButtonClass);
1229
+ calendarDay.button.removeAttribute('aria-current');
1230
+ }
1231
+ if (calendarDayDate.getTime() === today.getTime()) {
1232
+ calendarDay.button.classList.add(this.todayButtonClass);
1233
+ } else {
1234
+ calendarDay.button.classList.remove(this.todayButtonClass);
1235
+ }
1236
+ });
1237
+
1238
+ // if no date is tab-able, make the first non-disabled date tab-able
1239
+ if (!focus) {
1240
+ const enabledDays = this.calendarDays.filter(calendarDay => {
1241
+ return window.getComputedStyle(calendarDay.button).display === 'block' && !calendarDay.button.disabled;
1242
+ });
1243
+ enabledDays[0].button.setAttribute('tabindex', '0');
1244
+ this.currentDate = enabledDays[0].date;
1245
+ }
1246
+ }
1247
+ selectDate(date) {
1248
+ if (this.isExcludedDate(date)) {
1249
+ return;
1250
+ }
1251
+ this.$calendarButton.querySelector('span').innerText = `Choose date. Selected date is ${this.formattedDateHuman(date)}`;
1252
+ this.$input.value = this.formattedDateFromDate(date);
1253
+ const changeEvent = new Event('change', {
1254
+ bubbles: true,
1255
+ cancelable: true
1256
+ });
1257
+ this.$input.dispatchEvent(changeEvent);
1258
+ this.closeDialog();
1259
+ }
1260
+ isOpen() {
1261
+ return this.$dialog.classList.contains('moj-datepicker__dialog--open');
1262
+ }
1263
+ toggleDialog(event) {
1264
+ event.preventDefault();
1265
+ if (this.isOpen()) {
1266
+ this.closeDialog();
1267
+ } else {
1268
+ this.setMinAndMaxDatesOnCalendar();
1269
+ this.openDialog();
1270
+ }
1271
+ }
1272
+ openDialog() {
1273
+ this.$dialog.hidden = false;
1274
+ this.$dialog.classList.add('moj-datepicker__dialog--open');
1275
+ this.$calendarButton.setAttribute('aria-expanded', 'true');
1276
+
1277
+ // position the dialog
1278
+ // if input is wider than dialog pin it to the right
1279
+ if (this.$input.offsetWidth > this.$dialog.offsetWidth) {
1280
+ this.$dialog.style.right = `0px`;
1281
+ }
1282
+ this.$dialog.style.top = `${this.$input.offsetHeight + 3}px`;
1283
+
1284
+ // get the date from the input element
1285
+ this.inputDate = this.formattedDateFromString(this.$input.value);
1286
+ this.currentDate = this.inputDate;
1287
+ this.currentDate.setHours(0, 0, 0, 0);
1288
+ this.updateCalendar();
1289
+ this.setCurrentDate();
1290
+ }
1291
+ closeDialog() {
1292
+ this.$dialog.hidden = true;
1293
+ this.$dialog.classList.remove('moj-datepicker__dialog--open');
1294
+ this.$calendarButton.setAttribute('aria-expanded', 'false');
1295
+ this.$calendarButton.focus();
1296
+ }
1297
+ goToDate(date, focus) {
1298
+ const current = this.currentDate;
1299
+ this.currentDate = date;
1300
+ if (current.getMonth() !== this.currentDate.getMonth() || current.getFullYear() !== this.currentDate.getFullYear()) {
1301
+ this.updateCalendar();
1302
+ }
1303
+ this.setCurrentDate(focus);
1304
+ }
1305
+
1306
+ // day navigation
1307
+ focusNextDay() {
1308
+ const date = new Date(this.currentDate);
1309
+ date.setDate(date.getDate() + 1);
1310
+ this.goToDate(date);
1311
+ }
1312
+ focusPreviousDay() {
1313
+ const date = new Date(this.currentDate);
1314
+ date.setDate(date.getDate() - 1);
1315
+ this.goToDate(date);
1316
+ }
1317
+
1318
+ // week navigation
1319
+ focusNextWeek() {
1320
+ const date = new Date(this.currentDate);
1321
+ date.setDate(date.getDate() + 7);
1322
+ this.goToDate(date);
1323
+ }
1324
+ focusPreviousWeek() {
1325
+ const date = new Date(this.currentDate);
1326
+ date.setDate(date.getDate() - 7);
1327
+ this.goToDate(date);
1328
+ }
1329
+ focusFirstDayOfWeek() {
1330
+ const date = new Date(this.currentDate);
1331
+ const firstDayOfWeekIndex = this.config.weekStartDay === 'sunday' ? 0 : 1;
1332
+ const dayOfWeek = date.getDay();
1333
+ const diff = dayOfWeek >= firstDayOfWeekIndex ? dayOfWeek - firstDayOfWeekIndex : 6 - dayOfWeek;
1334
+ date.setDate(date.getDate() - diff);
1335
+ date.setHours(0, 0, 0, 0);
1336
+ this.goToDate(date);
1337
+ }
1338
+ focusLastDayOfWeek() {
1339
+ const date = new Date(this.currentDate);
1340
+ const lastDayOfWeekIndex = this.config.weekStartDay === 'sunday' ? 6 : 0;
1341
+ const dayOfWeek = date.getDay();
1342
+ const diff = dayOfWeek <= lastDayOfWeekIndex ? lastDayOfWeekIndex - dayOfWeek : 7 - dayOfWeek;
1343
+ date.setDate(date.getDate() + diff);
1344
+ date.setHours(0, 0, 0, 0);
1345
+ this.goToDate(date);
1346
+ }
1347
+
1348
+ // month navigation
1349
+ focusNextMonth(event, focus = true) {
1350
+ event.preventDefault();
1351
+ const date = new Date(this.currentDate);
1352
+ date.setMonth(date.getMonth() + 1, 1);
1353
+ this.goToDate(date, focus);
1354
+ }
1355
+ focusPreviousMonth(event, focus = true) {
1356
+ event.preventDefault();
1357
+ const date = new Date(this.currentDate);
1358
+ date.setMonth(date.getMonth() - 1, 1);
1359
+ this.goToDate(date, focus);
1360
+ }
1361
+
1362
+ // year navigation
1363
+ focusNextYear(event, focus = true) {
1364
+ event.preventDefault();
1365
+ const date = new Date(this.currentDate);
1366
+ date.setFullYear(date.getFullYear() + 1, date.getMonth(), 1);
1367
+ this.goToDate(date, focus);
1368
+ }
1369
+ focusPreviousYear(event, focus = true) {
1370
+ event.preventDefault();
1371
+ const date = new Date(this.currentDate);
1372
+ date.setFullYear(date.getFullYear() - 1, date.getMonth(), 1);
1373
+ this.goToDate(date, focus);
1374
+ }
1375
+
1376
+ /**
1377
+ * Parse dataset
1378
+ *
1379
+ * @param {Schema} schema - Component class
1380
+ * @param {DOMStringMap} dataset - HTML element dataset
1381
+ * @returns {object} Normalised dataset
1382
+ */
1383
+ parseDataset(schema, dataset) {
1384
+ const parsed = {};
1385
+ for (const [field,,] of Object.entries(schema.properties)) {
1386
+ if (field in dataset) {
1387
+ parsed[field] = dataset[field];
1388
+ }
1389
+ }
1390
+ return parsed;
1391
+ }
1392
+
1393
+ /**
1394
+ * Config merging function
1395
+ *
1396
+ * Takes any number of objects and combines them together, with
1397
+ * greatest priority on the LAST item passed in.
1398
+ *
1399
+ * @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
1400
+ * @returns {{ [key: string]: unknown }} A merged config object
1401
+ */
1402
+ mergeConfigs(...configObjects) {
1403
+ const formattedConfigObject = {};
1404
+
1405
+ // Loop through each of the passed objects
1406
+ for (const configObject of configObjects) {
1407
+ for (const key of Object.keys(configObject)) {
1408
+ const option = formattedConfigObject[key];
1409
+ const override = configObject[key];
1410
+
1411
+ // Push their keys one-by-one into formattedConfigObject. Any duplicate
1412
+ // keys with object values will be merged, otherwise the new value will
1413
+ // override the existing value.
1414
+ if (typeof option === 'object' && typeof override === 'object') {
1415
+ // @ts-expect-error Index signature for type 'string' is missing
1416
+ formattedConfigObject[key] = this.mergeConfigs(option, override);
1417
+ } else {
1418
+ formattedConfigObject[key] = override;
1419
+ }
1420
+ }
1421
+ }
1422
+ return formattedConfigObject;
1423
+ }
1424
+ }
1425
+ class DSCalendarDay {
1426
+ /**
1427
+ *
1428
+ * @param {HTMLElement} button
1429
+ * @param {number} index
1430
+ * @param {number} row
1431
+ * @param {number} column
1432
+ * @param {DatePicker} picker
1433
+ */
1434
+ constructor(button, index, row, column, picker) {
1435
+ this.index = index;
1436
+ this.row = row;
1437
+ this.column = column;
1438
+ this.button = button;
1439
+ this.picker = picker;
1440
+ this.date = new Date();
1441
+ this.button.addEventListener('keydown', this.keyPress.bind(this));
1442
+ this.button.addEventListener('click', this.click.bind(this));
1443
+ }
1444
+
1445
+ /**
1446
+ * @param {Date} day - the Date for the calendar day
1447
+ * @param {boolean} hidden - visibility of the day
1448
+ * @param {boolean} disabled - is the day selectable or excluded
1449
+ */
1450
+ update(day, hidden, disabled) {
1451
+ const label = day.getDate();
1452
+ let accessibleLabel = this.picker.formattedDateHuman(day);
1453
+ if (disabled) {
1454
+ this.button.setAttribute('aria-disabled', 'true');
1455
+ accessibleLabel = `Excluded date, ${accessibleLabel}`;
1456
+ } else {
1457
+ this.button.removeAttribute('aria-disabled');
1458
+ }
1459
+ if (hidden) {
1460
+ this.button.style.display = 'none';
1461
+ } else {
1462
+ this.button.style.display = 'block';
1463
+ }
1464
+ this.button.setAttribute('data-testid', this.picker.formattedDateFromDate(day));
1465
+ this.button.innerHTML = `<span class="govuk-visually-hidden">${accessibleLabel}</span><span aria-hidden="true">${label}</span>`;
1466
+ this.date = new Date(day);
1467
+ }
1468
+ click(event) {
1469
+ this.picker.goToDate(this.date);
1470
+ this.picker.selectDate(this.date);
1471
+ event.stopPropagation();
1472
+ event.preventDefault();
1473
+ }
1474
+ keyPress(event) {
1475
+ let calendarNavKey = true;
1476
+ switch (event.key) {
1477
+ case 'ArrowLeft':
1478
+ this.picker.focusPreviousDay();
1479
+ break;
1480
+ case 'ArrowRight':
1481
+ this.picker.focusNextDay();
1482
+ break;
1483
+ case 'ArrowUp':
1484
+ this.picker.focusPreviousWeek();
1485
+ break;
1486
+ case 'ArrowDown':
1487
+ this.picker.focusNextWeek();
1488
+ break;
1489
+ case 'Home':
1490
+ this.picker.focusFirstDayOfWeek();
1491
+ break;
1492
+ case 'End':
1493
+ this.picker.focusLastDayOfWeek();
1494
+ break;
1495
+ case 'PageUp':
1496
+ {
1497
+ if (event.shiftKey) {
1498
+ this.picker.focusPreviousYear(event);
1499
+ } else {
1500
+ this.picker.focusPreviousMonth(event);
1501
+ }
1502
+ break;
1503
+ }
1504
+ case 'PageDown':
1505
+ {
1506
+ if (event.shiftKey) {
1507
+ this.picker.focusNextYear(event);
1508
+ } else {
1509
+ this.picker.focusNextMonth(event);
1510
+ }
1511
+ break;
1512
+ }
1513
+ default:
1514
+ calendarNavKey = false;
1515
+ break;
1516
+ }
1517
+ if (calendarNavKey) {
1518
+ event.preventDefault();
1519
+ event.stopPropagation();
1520
+ }
1521
+ }
1522
+ }
1523
+
1524
+ /**
1525
+ * Date picker config
1526
+ *
1527
+ * @typedef {object} DatePickerConfig
1528
+ * @property {string} [excludedDates] - Dates that cannot be selected
1529
+ * @property {string} [excludedDays] - Days that cannot be selected
1530
+ * @property {boolean} [leadingZeroes] - Whether to add leading zeroes when populating the field
1531
+ * @property {string} [minDate] - The earliest available date
1532
+ * @property {string} [maxDate] - The latest available date
1533
+ * @property {string} [weekStartDay] - First day of the week in calendar view
1534
+ */
1535
+
1536
+ /**
1537
+ * @import { Schema } from '../../all.mjs'
1538
+ */
1539
+
1540
+ class FilterToggleButton {
1541
+ constructor(options) {
1542
+ this.options = options;
1543
+ this.container = this.options.toggleButton.container;
1544
+ this.filterContainer = this.options.filter.container;
1545
+ this.createToggleButton();
1546
+ this.setupResponsiveChecks();
1547
+ this.filterContainer.setAttribute('tabindex', '-1');
1548
+ if (this.options.startHidden) {
1549
+ this.hideMenu();
1550
+ }
1551
+ }
1552
+ setupResponsiveChecks() {
1553
+ this.mq = window.matchMedia(this.options.bigModeMediaQuery);
1554
+ this.mq.addListener(this.checkMode.bind(this));
1555
+ this.checkMode(this.mq);
1556
+ }
1557
+ createToggleButton() {
1558
+ this.menuButton = document.createElement('button');
1559
+ this.menuButton.setAttribute('type', 'button');
1560
+ this.menuButton.setAttribute('aria-haspopup', 'true');
1561
+ this.menuButton.setAttribute('aria-expanded', 'false');
1562
+ this.menuButton.className = `govuk-button ${this.options.toggleButton.classes}`;
1563
+ this.menuButton.textContent = this.options.toggleButton.showText;
1564
+ this.menuButton.addEventListener('click', this.onMenuButtonClick.bind(this));
1565
+ this.container.append(this.menuButton);
1566
+ }
1567
+ checkMode(mq) {
1568
+ if (mq.matches) {
1569
+ this.enableBigMode();
1570
+ } else {
1571
+ this.enableSmallMode();
1572
+ }
1573
+ }
1574
+ enableBigMode() {
1575
+ this.showMenu();
1576
+ this.removeCloseButton();
1577
+ }
1578
+ enableSmallMode() {
1579
+ this.hideMenu();
1580
+ this.addCloseButton();
1581
+ }
1582
+ addCloseButton() {
1583
+ if (!this.options.closeButton) {
1584
+ return;
1585
+ }
1586
+ this.closeButton = document.createElement('button');
1587
+ this.closeButton.setAttribute('type', 'button');
1588
+ this.closeButton.className = 'moj-filter__close';
1589
+ this.closeButton.textContent = this.options.closeButton.text;
1590
+ this.closeButton.addEventListener('click', this.onCloseClick.bind(this));
1591
+ this.options.closeButton.container.append(this.closeButton);
1592
+ }
1593
+ onCloseClick() {
1594
+ this.hideMenu();
1595
+ this.menuButton.focus();
1596
+ }
1597
+ removeCloseButton() {
1598
+ if (this.closeButton) {
1599
+ this.closeButton.remove();
1600
+ this.closeButton = null;
1601
+ }
1602
+ }
1603
+ hideMenu() {
1604
+ this.menuButton.setAttribute('aria-expanded', 'false');
1605
+ this.filterContainer.classList.add('moj-js-hidden');
1606
+ this.menuButton.textContent = this.options.toggleButton.showText;
1607
+ }
1608
+ showMenu() {
1609
+ this.menuButton.setAttribute('aria-expanded', 'true');
1610
+ this.filterContainer.classList.remove('moj-js-hidden');
1611
+ this.menuButton.textContent = this.options.toggleButton.hideText;
1612
+ }
1613
+ onMenuButtonClick() {
1614
+ this.toggle();
1615
+ }
1616
+ toggle() {
1617
+ if (this.menuButton.getAttribute('aria-expanded') === 'false') {
1618
+ this.showMenu();
1619
+ this.filterContainer.focus();
1620
+ } else {
1621
+ this.hideMenu();
1622
+ }
1623
+ }
1624
+ }
1625
+
1626
+ class FormValidator {
1627
+ /**
1628
+ * @param {Element | null} form - HTML element to use for form validator
1629
+ * @param {FormValidatorConfig} [config] - Button menu config
1630
+ */
1631
+ constructor(form, config = {}) {
1632
+ if (!form || !(form instanceof HTMLFormElement)) {
1633
+ return this;
1634
+ }
1635
+ this.form = form;
1636
+ this.errors = [];
1637
+ this.validators = [];
1638
+ this.form.addEventListener('submit', this.onSubmit.bind(this));
1639
+ this.summary = config.summary || document.querySelector('.govuk-error-summary');
1640
+ this.originalTitle = document.title;
1641
+ }
1642
+ escapeHtml(string) {
1643
+ return String(string).replace(/[&<>"'`=/]/g, function fromEntityMap(s) {
1644
+ return FormValidator.entityMap[s];
1645
+ });
1646
+ }
1647
+ resetTitle() {
1648
+ document.title = this.originalTitle;
1649
+ }
1650
+ updateTitle() {
1651
+ document.title = `${this.errors.length} errors - ${document.title}`;
1652
+ }
1653
+ showSummary() {
1654
+ this.summary.innerHTML = this.getSummaryHtml();
1655
+ this.summary.classList.remove('moj-hidden');
1656
+ this.summary.setAttribute('aria-labelledby', 'errorSummary-heading');
1657
+ this.summary.focus();
1658
+ }
1659
+ getSummaryHtml() {
1660
+ let html = '<h2 id="error-summary-title" class="govuk-error-summary__title">There is a problem</h2>';
1661
+ html += '<div class="govuk-error-summary__body">';
1662
+ html += '<ul class="govuk-list govuk-error-summary__list">';
1663
+ for (const error of this.errors) {
1664
+ html += '<li>';
1665
+ html += `<a href="#${this.escapeHtml(error.fieldName)}">`;
1666
+ html += this.escapeHtml(error.message);
1667
+ html += '</a>';
1668
+ html += '</li>';
1669
+ }
1670
+ html += '</ul>';
1671
+ html += '</div>';
1672
+ return html;
1673
+ }
1674
+ hideSummary() {
1675
+ this.summary.classList.add('moj-hidden');
1676
+ this.summary.removeAttribute('aria-labelledby');
1677
+ }
1678
+ onSubmit(event) {
1679
+ this.removeInlineErrors();
1680
+ this.hideSummary();
1681
+ this.resetTitle();
1682
+ if (!this.validate()) {
1683
+ event.preventDefault();
1684
+ this.updateTitle();
1685
+ this.showSummary();
1686
+ this.showInlineErrors();
1687
+ }
1688
+ }
1689
+ showInlineErrors() {
1690
+ for (const error of this.errors) {
1691
+ this.showInlineError(error);
1692
+ }
1693
+ }
1694
+ showInlineError(error) {
1695
+ const errorSpan = document.createElement('span');
1696
+ errorSpan.id = `${error.fieldName}-error`;
1697
+ errorSpan.classList.add('govuk-error-message');
1698
+ errorSpan.innerHTML = this.escapeHtml(error.message);
1699
+ const control = document.querySelector(`#${error.fieldName}`);
1700
+ const fieldset = control.closest('.govuk-fieldset');
1701
+ const fieldContainer = (fieldset || control).closest('.govuk-form-group');
1702
+ const label = fieldContainer.querySelector('label');
1703
+ const legend = fieldContainer.querySelector('legend');
1704
+ fieldContainer.classList.add('govuk-form-group--error');
1705
+ if (fieldset && legend) {
1706
+ legend.after(errorSpan);
1707
+ fieldContainer.setAttribute('aria-invalid', 'true');
1708
+ addAttributeValue(fieldset, 'aria-describedby', errorSpan.id);
1709
+ } else if (label && control) {
1710
+ label.after(errorSpan);
1711
+ control.setAttribute('aria-invalid', 'true');
1712
+ addAttributeValue(control, 'aria-describedby', errorSpan.id);
1713
+ }
1714
+ }
1715
+ removeInlineErrors() {
1716
+ for (const error of this.errors) {
1717
+ this.removeInlineError(error);
1718
+ }
1719
+ }
1720
+ removeInlineError(error) {
1721
+ const errorSpan = document.querySelector(`#${error.fieldName}-error`);
1722
+ const control = document.querySelector(`#${error.fieldName}`);
1723
+ const fieldset = control.closest('.govuk-fieldset');
1724
+ const fieldContainer = (fieldset || control).closest('.govuk-form-group');
1725
+ const label = fieldContainer.querySelector('label');
1726
+ const legend = fieldContainer.querySelector('legend');
1727
+ errorSpan.remove();
1728
+ fieldContainer.classList.remove('govuk-form-group--error');
1729
+ if (fieldset && legend) {
1730
+ fieldContainer.removeAttribute('aria-invalid');
1731
+ removeAttributeValue(fieldset, 'aria-describedby', errorSpan.id);
1732
+ } else if (label && control) {
1733
+ control.removeAttribute('aria-invalid');
1734
+ removeAttributeValue(control, 'aria-describedby', errorSpan.id);
1735
+ }
1736
+ }
1737
+ addValidator(fieldName, rules) {
1738
+ this.validators.push({
1739
+ fieldName,
1740
+ rules,
1741
+ field: this.form.elements[fieldName]
1742
+ });
1743
+ }
1744
+ validate() {
1745
+ this.errors = [];
1746
+ let validator = null;
1747
+ let validatorReturnValue = true;
1748
+ let i;
1749
+ let j;
1750
+ for (i = 0; i < this.validators.length; i++) {
1751
+ validator = this.validators[i];
1752
+ for (j = 0; j < validator.rules.length; j++) {
1753
+ validatorReturnValue = validator.rules[j].method(validator.field, validator.rules[j].params);
1754
+ if (typeof validatorReturnValue === 'boolean' && !validatorReturnValue) {
1755
+ this.errors.push({
1756
+ fieldName: validator.fieldName,
1757
+ message: validator.rules[j].message
1758
+ });
1759
+ break;
1760
+ } else if (typeof validatorReturnValue === 'string') {
1761
+ this.errors.push({
1762
+ fieldName: validatorReturnValue,
1763
+ message: validator.rules[j].message
1764
+ });
1765
+ break;
1766
+ }
1767
+ }
1768
+ }
1769
+ return this.errors.length === 0;
1770
+ }
1771
+ }
1772
+
1773
+ /**
1774
+ * @typedef {object} FormValidatorConfig
1775
+ * @property {HTMLElement} [summary] - HTML element to use for error summary
1776
+ */
1777
+ FormValidator.entityMap = {
1778
+ '&': '&amp;',
1779
+ '<': '&lt;',
1780
+ '>': '&gt;',
1781
+ '"': '&quot;',
1782
+ "'": '&#39;',
1783
+ '/': '&#x2F;',
1784
+ '`': '&#x60;',
1785
+ '=': '&#x3D;'
1786
+ };
1787
+
1788
+ /* eslint-disable @typescript-eslint/no-empty-function */
1789
+
1790
+ class MultiFileUpload {
1791
+ /**
1792
+ * @param {MultiFileUploadConfig} [params] - Multi file upload config
1793
+ */
1794
+ constructor(params = {}) {
1795
+ const {
1796
+ container
1797
+ } = params;
1798
+ if (!container || !(container instanceof HTMLElement) || !(dragAndDropSupported() && formDataSupported() && fileApiSupported())) {
1799
+ return this;
1800
+ }
1801
+ this.container = container;
1802
+ this.container.classList.add('moj-multi-file-upload--enhanced');
1803
+ this.defaultParams = {
1804
+ uploadFileEntryHook: () => {},
1805
+ uploadFileExitHook: () => {},
1806
+ uploadFileErrorHook: () => {},
1807
+ fileDeleteHook: () => {},
1808
+ uploadStatusText: 'Uploading files, please wait',
1809
+ dropzoneHintText: 'Drag and drop files here or',
1810
+ dropzoneButtonText: 'Choose files'
1811
+ };
1812
+ this.params = Object.assign({}, this.defaultParams, params);
1813
+ this.feedbackContainer = /** @type {HTMLDivElement} */
1814
+ this.container.querySelector('.moj-multi-file__uploaded-files');
1815
+ this.setupFileInput();
1816
+ this.setupDropzone();
1817
+ this.setupLabel();
1818
+ this.setupStatusBox();
1819
+ this.container.addEventListener('click', this.onFileDeleteClick.bind(this));
1820
+ }
1821
+ setupDropzone() {
1822
+ this.dropzone = document.createElement('div');
1823
+ this.dropzone.classList.add('moj-multi-file-upload__dropzone');
1824
+ this.dropzone.addEventListener('dragover', this.onDragOver.bind(this));
1825
+ this.dropzone.addEventListener('dragleave', this.onDragLeave.bind(this));
1826
+ this.dropzone.addEventListener('drop', this.onDrop.bind(this));
1827
+ this.fileInput.replaceWith(this.dropzone);
1828
+ this.dropzone.appendChild(this.fileInput);
1829
+ }
1830
+ setupLabel() {
1831
+ const label = document.createElement('label');
1832
+ label.setAttribute('for', this.fileInput.id);
1833
+ label.classList.add('govuk-button', 'govuk-button--secondary');
1834
+ label.textContent = this.params.dropzoneButtonText;
1835
+ const hint = document.createElement('p');
1836
+ hint.classList.add('govuk-body');
1837
+ hint.textContent = this.params.dropzoneHintText;
1838
+ this.label = label;
1839
+ this.dropzone.append(hint);
1840
+ this.dropzone.append(label);
1841
+ }
1842
+ setupFileInput() {
1843
+ this.fileInput = /** @type {HTMLInputElement} */
1844
+ this.container.querySelector('.moj-multi-file-upload__input');
1845
+ this.fileInput.addEventListener('change', this.onFileChange.bind(this));
1846
+ this.fileInput.addEventListener('focus', this.onFileFocus.bind(this));
1847
+ this.fileInput.addEventListener('blur', this.onFileBlur.bind(this));
1848
+ }
1849
+ setupStatusBox() {
1850
+ this.status = document.createElement('div');
1851
+ this.status.classList.add('govuk-visually-hidden');
1852
+ this.status.setAttribute('aria-live', 'polite');
1853
+ this.status.setAttribute('role', 'status');
1854
+ this.dropzone.append(this.status);
1855
+ }
1856
+ onDragOver(event) {
1857
+ event.preventDefault();
1858
+ this.dropzone.classList.add('moj-multi-file-upload--dragover');
1859
+ }
1860
+ onDragLeave() {
1861
+ this.dropzone.classList.remove('moj-multi-file-upload--dragover');
1862
+ }
1863
+ onDrop(event) {
1864
+ event.preventDefault();
1865
+ this.dropzone.classList.remove('moj-multi-file-upload--dragover');
1866
+ this.feedbackContainer.classList.remove('moj-hidden');
1867
+ this.status.textContent = this.params.uploadStatusText;
1868
+ this.uploadFiles(event.dataTransfer.files);
1869
+ }
1870
+ uploadFiles(files) {
1871
+ for (const file of Array.from(files)) {
1872
+ this.uploadFile(file);
1873
+ }
1874
+ }
1875
+ onFileChange() {
1876
+ this.feedbackContainer.classList.remove('moj-hidden');
1877
+ this.status.textContent = this.params.uploadStatusText;
1878
+ this.uploadFiles(this.fileInput.files);
1879
+ const fileInput = this.fileInput.cloneNode(true);
1880
+ if (!fileInput || !(fileInput instanceof HTMLInputElement)) {
1881
+ return;
1882
+ }
1883
+ fileInput.value = '';
1884
+ this.fileInput.replaceWith(fileInput);
1885
+ this.setupFileInput();
1886
+ this.fileInput.focus();
1887
+ }
1888
+ onFileFocus() {
1889
+ this.label.classList.add('moj-multi-file-upload--focused');
1890
+ }
1891
+ onFileBlur() {
1892
+ this.label.classList.remove('moj-multi-file-upload--focused');
1893
+ }
1894
+ getSuccessHtml(success) {
1895
+ return `<span class="moj-multi-file-upload__success"> <svg class="moj-banner__icon" fill="currentColor" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 25 25" height="25" width="25"><path d="M25,6.2L8.7,23.2L0,14.1l4-4.2l4.7,4.9L21,2L25,6.2z"/></svg>${success.messageHtml}</span>`;
1896
+ }
1897
+ getErrorHtml(error) {
1898
+ return `<span class="moj-multi-file-upload__error"> <svg class="moj-banner__icon" fill="currentColor" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 25 25" height="25" width="25"><path d="M13.6,15.4h-2.3v-4.5h2.3V15.4z M13.6,19.8h-2.3v-2.2h2.3V19.8z M0,23.2h25L12.5,2L0,23.2z"/></svg>${error.message}</span>`;
1899
+ }
1900
+ getFileRow(file) {
1901
+ const row = document.createElement('div');
1902
+ row.classList.add('govuk-summary-list__row', 'moj-multi-file-upload__row');
1903
+ row.innerHTML = `
1904
+ <div class="govuk-summary-list__value moj-multi-file-upload__message">
1905
+ <span class="moj-multi-file-upload__filename">${file.name}</span>
1906
+ <span class="moj-multi-file-upload__progress">0%</span>
1907
+ </div>
1908
+ <div class="govuk-summary-list__actions moj-multi-file-upload__actions"></div>
1909
+ `;
1910
+ return row;
1911
+ }
1912
+ getDeleteButton(file) {
1913
+ const button = document.createElement('button');
1914
+ button.setAttribute('type', 'button');
1915
+ button.setAttribute('name', 'delete');
1916
+ button.setAttribute('value', file.filename);
1917
+ button.classList.add('moj-multi-file-upload__delete', 'govuk-button', 'govuk-button--secondary', 'govuk-!-margin-bottom-0');
1918
+ button.innerHTML = `Delete <span class="govuk-visually-hidden">${file.originalname}</span>`;
1919
+ return button;
1920
+ }
1921
+ uploadFile(file) {
1922
+ this.params.uploadFileEntryHook(this, file);
1923
+ const item = this.getFileRow(file);
1924
+ const message = item.querySelector('.moj-multi-file-upload__message');
1925
+ const actions = item.querySelector('.moj-multi-file-upload__actions');
1926
+ const progress = item.querySelector('.moj-multi-file-upload__progress');
1927
+ const formData = new FormData();
1928
+ formData.append('documents', file);
1929
+ this.feedbackContainer.querySelector('.moj-multi-file-upload__list').append(item);
1930
+ const xhr = new XMLHttpRequest();
1931
+ const onLoad = () => {
1932
+ if (xhr.status < 200 || xhr.status >= 300 || !('success' in xhr.response)) {
1933
+ return onError();
1934
+ }
1935
+ message.innerHTML = this.getSuccessHtml(xhr.response.success);
1936
+ this.status.textContent = xhr.response.success.messageText;
1937
+ actions.append(this.getDeleteButton(xhr.response.file));
1938
+ this.params.uploadFileExitHook(this, file, xhr, xhr.responseText);
1939
+ };
1940
+ const onError = () => {
1941
+ const error = new Error(xhr.response && 'error' in xhr.response ? xhr.response.error.message : xhr.statusText || 'Upload failed');
1942
+ message.innerHTML = this.getErrorHtml(error);
1943
+ this.status.textContent = error.message;
1944
+ this.params.uploadFileErrorHook(this, file, xhr, xhr.responseText, error);
1945
+ };
1946
+ xhr.addEventListener('load', onLoad);
1947
+ xhr.addEventListener('error', onError);
1948
+ xhr.upload.addEventListener('progress', event => {
1949
+ if (!event.lengthComputable) {
1950
+ return;
1951
+ }
1952
+ const percentComplete = Math.round(event.loaded / event.total * 100);
1953
+ progress.textContent = ` ${percentComplete}%`;
1954
+ });
1955
+ xhr.open('POST', this.params.uploadUrl);
1956
+ xhr.responseType = 'json';
1957
+ xhr.send(formData);
1958
+ }
1959
+ onFileDeleteClick(event) {
1960
+ const button = event.target;
1961
+ if (!button || !(button instanceof HTMLButtonElement) || !button.classList.contains('moj-multi-file-upload__delete')) {
1962
+ return;
1963
+ }
1964
+ event.preventDefault(); // if user refreshes page and then deletes
1965
+
1966
+ const xhr = new XMLHttpRequest();
1967
+ xhr.addEventListener('load', () => {
1968
+ if (xhr.status < 200 || xhr.status >= 300) {
1969
+ return;
1970
+ }
1971
+ const rows = Array.from(this.feedbackContainer.querySelectorAll('.moj-multi-file-upload__row'));
1972
+ if (rows.length === 1) {
1973
+ this.feedbackContainer.classList.add('moj-hidden');
1974
+ }
1975
+ const row = rows.find(row => row.contains(button));
1976
+ if (row) row.remove();
1977
+ this.params.fileDeleteHook(this, undefined, xhr, xhr.responseText);
1978
+ });
1979
+ xhr.open('POST', this.params.deleteUrl);
1980
+ xhr.setRequestHeader('Content-Type', 'application/json');
1981
+ xhr.responseType = 'json';
1982
+ xhr.send(JSON.stringify({
1983
+ [button.name]: button.value
1984
+ }));
1985
+ }
1986
+ }
1987
+
1988
+ class MultiSelect {
1989
+ constructor(options) {
1990
+ this.container = options.container;
1991
+ if (this.container.hasAttribute('data-moj-multi-select-init')) {
1992
+ return this;
1993
+ }
1994
+ this.container.setAttribute('data-moj-multi-select-init', '');
1995
+ const idPrefix = options.id_prefix;
1996
+ this.setupToggle(idPrefix);
1997
+ this.toggleButton = this.toggle.querySelector('input');
1998
+ this.toggleButton.addEventListener('click', this.onButtonClick.bind(this));
1999
+ this.container.append(this.toggle);
2000
+ this.checkboxes = Array.from(options.checkboxes);
2001
+ this.checkboxes.forEach(el => el.addEventListener('click', this.onCheckboxClick.bind(this)));
2002
+ this.checked = options.checked || false;
2003
+ }
2004
+ setupToggle(idPrefix = '') {
2005
+ const id = `${idPrefix}checkboxes-all`;
2006
+ const toggle = document.createElement('div');
2007
+ const label = document.createElement('label');
2008
+ const input = document.createElement('input');
2009
+ const span = document.createElement('span');
2010
+ toggle.classList.add('govuk-checkboxes__item', 'govuk-checkboxes--small', 'moj-multi-select__checkbox');
2011
+ input.id = id;
2012
+ input.type = 'checkbox';
2013
+ input.classList.add('govuk-checkboxes__input');
2014
+ label.setAttribute('for', id);
2015
+ label.classList.add('govuk-label', 'govuk-checkboxes__label', 'moj-multi-select__toggle-label');
2016
+ span.classList.add('govuk-visually-hidden');
2017
+ span.textContent = 'Select all';
2018
+ label.append(span);
2019
+ toggle.append(input, label);
2020
+ this.toggle = toggle;
2021
+ }
2022
+ onButtonClick() {
2023
+ if (this.checked) {
2024
+ this.uncheckAll();
2025
+ this.toggleButton.checked = false;
2026
+ } else {
2027
+ this.checkAll();
2028
+ this.toggleButton.checked = true;
2029
+ }
2030
+ }
2031
+ checkAll() {
2032
+ this.checkboxes.forEach(el => {
2033
+ el.checked = true;
2034
+ });
2035
+ this.checked = true;
2036
+ }
2037
+ uncheckAll() {
2038
+ this.checkboxes.forEach(el => {
2039
+ el.checked = false;
2040
+ });
2041
+ this.checked = false;
2042
+ }
2043
+ onCheckboxClick(event) {
2044
+ if (!event.target.checked) {
2045
+ this.toggleButton.checked = false;
2046
+ this.checked = false;
2047
+ } else {
2048
+ if (this.checkboxes.filter(el => el.checked).length === this.checkboxes.length) {
2049
+ this.toggleButton.checked = true;
2050
+ this.checked = true;
2051
+ }
2052
+ }
2053
+ }
2054
+ }
2055
+
2056
+ class PasswordReveal {
2057
+ /**
2058
+ * @param {Element | null} element - HTML element to use for password reveal
2059
+ */
2060
+ constructor(element) {
2061
+ if (!element || !(element instanceof HTMLInputElement)) {
2062
+ return this;
2063
+ }
2064
+ this.el = element;
2065
+ this.container = element.parentElement;
2066
+ if (this.container.hasAttribute('data-moj-password-reveal-init')) {
2067
+ return this;
2068
+ }
2069
+ this.container.setAttribute('data-moj-password-reveal-init', '');
2070
+ this.el.setAttribute('spellcheck', 'false');
2071
+ this.createButton();
2072
+ }
2073
+ createButton() {
2074
+ this.group = document.createElement('div');
2075
+ this.button = document.createElement('button');
2076
+ this.button.setAttribute('type', 'button');
2077
+ this.group.className = 'moj-password-reveal';
2078
+ this.button.className = 'govuk-button govuk-button--secondary moj-password-reveal__button';
2079
+ this.button.innerHTML = 'Show <span class="govuk-visually-hidden">password</span>';
2080
+ this.button.addEventListener('click', this.onButtonClick.bind(this));
2081
+ this.group.append(this.el, this.button);
2082
+ this.container.append(this.group);
2083
+ }
2084
+ onButtonClick() {
2085
+ if (this.el.type === 'password') {
2086
+ this.el.type = 'text';
2087
+ this.button.innerHTML = 'Hide <span class="govuk-visually-hidden">password</span>';
2088
+ } else {
2089
+ this.el.type = 'password';
2090
+ this.button.innerHTML = 'Show <span class="govuk-visually-hidden">password</span>';
2091
+ }
2092
+ }
2093
+ }
2094
+
2095
+ class RichTextEditor {
2096
+ /**
2097
+ * @param {RichTextEditorConfig} options
2098
+ */
2099
+ constructor(options = {}) {
2100
+ const {
2101
+ textarea
2102
+ } = options;
2103
+ if (!textarea || !textarea.parentElement || !(textarea instanceof HTMLTextAreaElement) || !('contentEditable' in document.documentElement)) {
2104
+ return this;
2105
+ }
2106
+ options.toolbar = options.toolbar || {
2107
+ bold: false,
2108
+ italic: false,
2109
+ underline: false,
2110
+ bullets: true,
2111
+ numbers: true
2112
+ };
2113
+ this.textarea = textarea;
2114
+ this.container = this.textarea.parentElement;
2115
+ this.options = options;
2116
+ if (this.container.hasAttribute('data-rich-text-editor-init')) {
2117
+ return this;
2118
+ }
2119
+ this.container.setAttribute('data-rich-text-editor-init', '');
2120
+ this.createToolbar();
2121
+ this.hideDefault();
2122
+ this.configureToolbar();
2123
+ this.keys = {
2124
+ left: 37,
2125
+ right: 39,
2126
+ up: 38,
2127
+ down: 40
2128
+ };
2129
+ this.content.addEventListener('input', this.onEditorInput.bind(this));
2130
+ this.container.querySelector('label').addEventListener('click', this.onLabelClick.bind(this));
2131
+ this.toolbar.addEventListener('keydown', this.onToolbarKeydown.bind(this));
2132
+ }
2133
+ onToolbarKeydown(event) {
2134
+ let focusableButton;
2135
+ switch (event.keyCode) {
2136
+ case this.keys.right:
2137
+ case this.keys.down:
2138
+ {
2139
+ focusableButton = this.buttons.find(button => button.getAttribute('tabindex') === '0');
2140
+ const nextButton = focusableButton.nextElementSibling;
2141
+ if (nextButton instanceof HTMLButtonElement) {
2142
+ nextButton.focus();
2143
+ focusableButton.setAttribute('tabindex', '-1');
2144
+ nextButton.setAttribute('tabindex', '0');
2145
+ }
2146
+ break;
2147
+ }
2148
+ case this.keys.left:
2149
+ case this.keys.up:
2150
+ {
2151
+ focusableButton = this.buttons.find(button => button.getAttribute('tabindex') === '0');
2152
+ const previousButton = focusableButton.previousElementSibling;
2153
+ if (previousButton instanceof HTMLButtonElement) {
2154
+ previousButton.focus();
2155
+ focusableButton.setAttribute('tabindex', '-1');
2156
+ previousButton.setAttribute('tabindex', '0');
2157
+ }
2158
+ break;
2159
+ }
2160
+ }
2161
+ }
2162
+ getToolbarHtml() {
2163
+ let html = '';
2164
+ html += '<div class="moj-rich-text-editor__toolbar" role="toolbar">';
2165
+ if (this.options.toolbar.bold) {
2166
+ html += '<button class="moj-rich-text-editor__toolbar-button moj-rich-text-editor__toolbar-button--bold" type="button" data-command="bold"><span class="govuk-visually-hidden">Bold</span></button>';
2167
+ }
2168
+ if (this.options.toolbar.italic) {
2169
+ html += '<button class="moj-rich-text-editor__toolbar-button moj-rich-text-editor__toolbar-button--italic" type="button" data-command="italic"><span class="govuk-visually-hidden">Italic</span></button>';
2170
+ }
2171
+ if (this.options.toolbar.underline) {
2172
+ html += '<button class="moj-rich-text-editor__toolbar-button moj-rich-text-editor__toolbar-button--underline" type="button" data-command="underline"><span class="govuk-visually-hidden">Underline</span></button>';
2173
+ }
2174
+ if (this.options.toolbar.bullets) {
2175
+ html += '<button class="moj-rich-text-editor__toolbar-button moj-rich-text-editor__toolbar-button--unordered-list" type="button" data-command="insertUnorderedList"><span class="govuk-visually-hidden">Unordered list</span></button>';
2176
+ }
2177
+ if (this.options.toolbar.numbers) {
2178
+ html += '<button class="moj-rich-text-editor__toolbar-button moj-rich-text-editor__toolbar-button--ordered-list" type="button" data-command="insertOrderedList"><span class="govuk-visually-hidden">Ordered list</span></button>';
2179
+ }
2180
+ html += '</div>';
2181
+ return html;
2182
+ }
2183
+ getEnhancedHtml() {
2184
+ return `${this.getToolbarHtml()}<div class="govuk-textarea moj-rich-text-editor__content" contenteditable="true" spellcheck="false"></div>`;
2185
+ }
2186
+ hideDefault() {
2187
+ this.textarea.classList.add('govuk-visually-hidden');
2188
+ this.textarea.setAttribute('aria-hidden', 'true');
2189
+ this.textarea.setAttribute('tabindex', '-1');
2190
+ }
2191
+ createToolbar() {
2192
+ this.toolbar = document.createElement('div');
2193
+ this.toolbar.className = 'moj-rich-text-editor';
2194
+ this.toolbar.innerHTML = this.getEnhancedHtml();
2195
+ this.container.append(this.toolbar);
2196
+ this.content = /** @type {HTMLDivElement} */
2197
+ this.container.querySelector('.moj-rich-text-editor__content');
2198
+ this.content.innerHTML = this.$textarea.value;
2199
+ }
2200
+ configureToolbar() {
2201
+ this.buttons = Array.from(/** @type {NodeListOf<HTMLButtonElement>} */
2202
+ this.container.querySelectorAll('.moj-rich-text-editor__toolbar-button'));
2203
+ this.buttons.forEach((button, index) => {
2204
+ button.setAttribute('tabindex', !index ? '0' : '-1');
2205
+ button.addEventListener('click', this.onButtonClick.bind(this));
2206
+ });
2207
+ }
2208
+ onButtonClick(event) {
2209
+ if (!(event.currentTarget instanceof HTMLElement)) {
2210
+ return;
2211
+ }
2212
+ document.execCommand(event.currentTarget.getAttribute('data-command'), false, undefined);
2213
+ }
2214
+ getContent() {
2215
+ return this.content.innerHTML;
2216
+ }
2217
+ onEditorInput() {
2218
+ this.updateTextarea();
2219
+ }
2220
+ updateTextarea() {
2221
+ document.execCommand('defaultParagraphSeparator', false, 'p');
2222
+ this.textarea.value = this.getContent();
2223
+ }
2224
+ onLabelClick(event) {
2225
+ event.preventDefault();
2226
+ this.content.focus();
2227
+ }
2228
+ }
2229
+
2230
+ class SearchToggle {
2231
+ constructor(options) {
2232
+ this.options = options;
2233
+ this.container = this.options.search.container;
2234
+ this.toggleButtonContainer = this.options.toggleButton.container;
2235
+ if (this.container.hasAttribute('data-moj-search-toggle-init')) {
2236
+ return this;
2237
+ }
2238
+ this.container.setAttribute('data-moj-search-toggle-init', '');
2239
+ const svg = '<svg viewBox="0 0 20 20" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="moj-search-toggle__button__icon"><path d="M7.433,12.5790048 C6.06762625,12.5808611 4.75763941,12.0392925 3.79217348,11.0738265 C2.82670755,10.1083606 2.28513891,8.79837375 2.28699522,7.433 C2.28513891,6.06762625 2.82670755,4.75763941 3.79217348,3.79217348 C4.75763941,2.82670755 6.06762625,2.28513891 7.433,2.28699522 C8.79837375,2.28513891 10.1083606,2.82670755 11.0738265,3.79217348 C12.0392925,4.75763941 12.5808611,6.06762625 12.5790048,7.433 C12.5808611,8.79837375 12.0392925,10.1083606 11.0738265,11.0738265 C10.1083606,12.0392925 8.79837375,12.5808611 7.433,12.5790048 L7.433,12.5790048 Z M14.293,12.579 L13.391,12.579 L13.071,12.269 C14.2300759,10.9245158 14.8671539,9.20813198 14.866,7.433 C14.866,3.32786745 11.5381325,-1.65045755e-15 7.433,-1.65045755e-15 C3.32786745,-1.65045755e-15 -1.65045755e-15,3.32786745 -1.65045755e-15,7.433 C-1.65045755e-15,11.5381325 3.32786745,14.866 7.433,14.866 C9.208604,14.8671159 10.9253982,14.2296624 12.27,13.07 L12.579,13.39 L12.579,14.294 L18.296,20 L20,18.296 L14.294,12.579 L14.293,12.579 Z"></path></svg>';
2240
+ this.toggleButton = document.createElement('button');
2241
+ this.toggleButton.setAttribute('class', 'moj-search-toggle__button');
2242
+ this.toggleButton.setAttribute('type', 'button');
2243
+ this.toggleButton.setAttribute('aria-haspopup', 'true');
2244
+ this.toggleButton.setAttribute('aria-expanded', 'false');
2245
+ this.toggleButton.innerHTML = `${this.options.toggleButton.text} ${svg}`;
2246
+ this.toggleButton.addEventListener('click', this.onToggleButtonClick.bind(this));
2247
+ this.toggleButtonContainer.append(this.toggleButton);
2248
+ document.addEventListener('click', this.onDocumentClick.bind(this));
2249
+ document.addEventListener('focusin', this.onDocumentClick.bind(this));
2250
+ }
2251
+ showMenu() {
2252
+ this.toggleButton.setAttribute('aria-expanded', 'true');
2253
+ this.container.classList.remove('moj-js-hidden');
2254
+ this.container.querySelector('input').focus();
2255
+ }
2256
+ hideMenu() {
2257
+ this.container.classList.add('moj-js-hidden');
2258
+ this.toggleButton.setAttribute('aria-expanded', 'false');
2259
+ }
2260
+ onToggleButtonClick() {
2261
+ if (this.toggleButton.getAttribute('aria-expanded') === 'false') {
2262
+ this.showMenu();
2263
+ } else {
2264
+ this.hideMenu();
2265
+ }
2266
+ }
2267
+ onDocumentClick(event) {
2268
+ if (!this.toggleButtonContainer.contains(event.target) && !this.container.contains(event.target)) {
2269
+ this.hideMenu();
2270
+ }
2271
+ }
2272
+ }
2273
+
2274
+ class SortableTable {
2275
+ constructor(params) {
2276
+ const table = params.table;
2277
+ const head = table == null ? void 0 : table.querySelector('thead');
2278
+ const body = table == null ? void 0 : table.querySelector('tbody');
2279
+ if (!table || !(table instanceof HTMLElement) || !head || !body) {
2280
+ return this;
2281
+ }
2282
+ this.table = table;
2283
+ this.head = head;
2284
+ this.body = body;
2285
+ if (this.table.hasAttribute('data-moj-sortable-table-init')) {
2286
+ return this;
2287
+ }
2288
+ this.table.setAttribute('data-moj-sortable-table-init', '');
2289
+ this.headings = this.head ? Array.from(this.head.querySelectorAll('th')) : [];
2290
+ this.setupOptions(params);
2291
+ this.createHeadingButtons();
2292
+ this.createStatusBox();
2293
+ this.initialiseSortedColumn();
2294
+ this.head.addEventListener('click', this.onSortButtonClick.bind(this));
2295
+ }
2296
+ setupOptions(params) {
2297
+ params = params || {};
2298
+ this.statusMessage = params.statusMessage || 'Sort by %heading% (%direction%)';
2299
+ this.ascendingText = params.ascendingText || 'ascending';
2300
+ this.descendingText = params.descendingText || 'descending';
2301
+ }
2302
+ createHeadingButtons() {
2303
+ for (const heading of this.headings) {
2304
+ if (heading.hasAttribute('aria-sort')) {
2305
+ this.createHeadingButton(heading);
2306
+ }
2307
+ }
2308
+ }
2309
+ createHeadingButton(heading) {
2310
+ const index = this.headings.indexOf(heading);
2311
+ const button = document.createElement('button');
2312
+ button.setAttribute('type', 'button');
2313
+ button.setAttribute('data-index', `${index}`);
2314
+ button.textContent = heading.textContent;
2315
+ heading.textContent = '';
2316
+ heading.appendChild(button);
2317
+ }
2318
+ createStatusBox() {
2319
+ this.status = document.createElement('div');
2320
+ this.status.setAttribute('aria-atomic', 'true');
2321
+ this.status.setAttribute('aria-live', 'polite');
2322
+ this.status.setAttribute('class', 'govuk-visually-hidden');
2323
+ this.status.setAttribute('role', 'status');
2324
+ this.table.insertAdjacentElement('afterend', this.status);
2325
+ }
2326
+ initialiseSortedColumn() {
2327
+ var _sortButton$getAttrib;
2328
+ const rows = this.getTableRowsArray();
2329
+ const heading = this.table.querySelector('th[aria-sort]');
2330
+ const sortButton = heading == null ? void 0 : heading.querySelector('button');
2331
+ const sortDirection = heading == null ? void 0 : heading.getAttribute('aria-sort');
2332
+ const columnNumber = Number.parseInt((_sortButton$getAttrib = sortButton == null ? void 0 : sortButton.getAttribute('data-index')) != null ? _sortButton$getAttrib : '0', 10);
2333
+ if (!heading || !sortButton || !(sortDirection === 'ascending' || sortDirection === 'descending')) {
2334
+ return;
2335
+ }
2336
+ const sortedRows = this.sort(rows, columnNumber, sortDirection);
2337
+ this.addRows(sortedRows);
2338
+ }
2339
+ onSortButtonClick(event) {
2340
+ var _button$getAttribute;
2341
+ const button = event.target;
2342
+ if (!button || !(button instanceof HTMLButtonElement) || !button.parentElement) {
2343
+ return;
2344
+ }
2345
+ const heading = button.parentElement;
2346
+ const sortDirection = heading.getAttribute('aria-sort');
2347
+ const columnNumber = Number.parseInt((_button$getAttribute = button == null ? void 0 : button.getAttribute('data-index')) != null ? _button$getAttribute : '0', 10);
2348
+ const newSortDirection = sortDirection === 'none' || sortDirection === 'descending' ? 'ascending' : 'descending';
2349
+ const rows = this.getTableRowsArray();
2350
+ const sortedRows = this.sort(rows, columnNumber, newSortDirection);
2351
+ this.addRows(sortedRows);
2352
+ this.removeButtonStates();
2353
+ this.updateButtonState(button, newSortDirection);
2354
+ }
2355
+ updateButtonState(button, direction) {
2356
+ if (!(direction === 'ascending' || direction === 'descending')) {
2357
+ return;
2358
+ }
2359
+ button.parentElement.setAttribute('aria-sort', direction);
2360
+ let message = this.statusMessage;
2361
+ message = message.replace(/%heading%/, button.textContent);
2362
+ message = message.replace(/%direction%/, this[`${direction}Text`]);
2363
+ this.status.textContent = message;
2364
+ }
2365
+ removeButtonStates() {
2366
+ for (const heading of this.headings) {
2367
+ heading.setAttribute('aria-sort', 'none');
2368
+ }
2369
+ }
2370
+ addRows(rows) {
2371
+ for (const row of rows) {
2372
+ this.body.append(row);
2373
+ }
2374
+ }
2375
+ getTableRowsArray() {
2376
+ return Array.from(this.body.querySelectorAll('tr'));
2377
+ }
2378
+ sort(rows, columnNumber, sortDirection) {
2379
+ return rows.sort((rowA, rowB) => {
2380
+ const tdA = rowA.querySelectorAll('td, th')[columnNumber];
2381
+ const tdB = rowB.querySelectorAll('td, th')[columnNumber];
2382
+ if (!tdA || !tdB || !(tdA instanceof HTMLElement) || !(tdB instanceof HTMLElement)) {
2383
+ return 0;
2384
+ }
2385
+ const valueA = sortDirection === 'ascending' ? this.getCellValue(tdA) : this.getCellValue(tdB);
2386
+ const valueB = sortDirection === 'ascending' ? this.getCellValue(tdB) : this.getCellValue(tdA);
2387
+ return !(typeof valueA === 'number' && typeof valueB === 'number') ? valueA.toString().localeCompare(valueB.toString()) : valueA - valueB;
2388
+ });
2389
+ }
2390
+ getCellValue(cell) {
2391
+ const val = cell.getAttribute('data-sort-value') || cell.innerHTML;
2392
+ const valAsNumber = Number(val);
2393
+ return Number.isFinite(valAsNumber) ? valAsNumber // Exclude invalid numbers, infinity etc
2394
+ : val;
2395
+ }
2396
+ }
2397
+
2398
+ const version = '0.0.0-development';
2399
+
2400
+ /* eslint-disable no-new */
2401
+
2402
+
2403
+ /**
2404
+ * @param {Config} [config]
2405
+ */
2406
+ function initAll(config) {
2407
+ // Set the config to an empty object by default if no config is passed.
2408
+ config = typeof config !== 'undefined' ? config : {};
2409
+
2410
+ // Allow the user to initialise MOJ Frontend in only certain sections of the page
2411
+ // Defaults to the entire document if nothing is set.
2412
+ const scope = typeof config.scope !== 'undefined' ? config.scope : document;
2413
+ const $addAnothers = scope.querySelectorAll('[data-module="moj-add-another"]');
2414
+ $addAnothers.forEach($addAnother => {
2415
+ new AddAnother($addAnother);
2416
+ });
2417
+ const $multiSelects = scope.querySelectorAll('[data-module="moj-multi-select"]');
2418
+ $multiSelects.forEach($multiSelect => {
2419
+ const containerSelector = $multiSelect.getAttribute('data-multi-select-checkbox');
2420
+ if (!($multiSelect instanceof HTMLElement) || !containerSelector) {
2421
+ return;
2422
+ }
2423
+ new MultiSelect({
2424
+ container: $multiSelect.querySelector(containerSelector),
2425
+ checkboxes: $multiSelect.querySelectorAll('tbody .govuk-checkboxes__input'),
2426
+ id_prefix: $multiSelect.getAttribute('data-multi-select-idprefix')
2427
+ });
2428
+ });
2429
+ const $passwordReveals = scope.querySelectorAll('[data-module="moj-password-reveal"]');
2430
+ $passwordReveals.forEach($passwordReveal => {
2431
+ new PasswordReveal($passwordReveal);
2432
+ });
2433
+ const $richTextEditors = scope.querySelectorAll('[data-module="moj-rich-text-editor"]');
2434
+ $richTextEditors.forEach($richTextEditor => {
2435
+ const options = {
2436
+ textarea: $richTextEditor
2437
+ };
2438
+ const toolbarAttr = $richTextEditor.getAttribute('data-moj-rich-text-editor-toolbar');
2439
+ if (toolbarAttr) {
2440
+ const toolbar = toolbarAttr.split(',');
2441
+ options.toolbar = {};
2442
+ for (const option of toolbar) {
2443
+ if (option === 'bold' || option === 'italic' || option === 'underline' || option === 'bullets' || option === 'numbers') {
2444
+ options.toolbar[option] = true;
2445
+ }
2446
+ }
2447
+ }
2448
+ new RichTextEditor(options);
2449
+ });
2450
+ const $searchToggles = scope.querySelectorAll('[data-module="moj-search-toggle"]');
2451
+ $searchToggles.forEach($searchToggle => {
2452
+ new SearchToggle({
2453
+ toggleButton: {
2454
+ container: $searchToggle.querySelector('.moj-search-toggle__toggle'),
2455
+ text: $searchToggle.getAttribute('data-moj-search-toggle-text')
2456
+ },
2457
+ search: {
2458
+ container: $searchToggle.querySelector('.moj-search')
2459
+ }
2460
+ });
2461
+ });
2462
+ const $sortableTables = scope.querySelectorAll('[data-module="moj-sortable-table"]');
2463
+ $sortableTables.forEach($table => {
2464
+ new SortableTable({
2465
+ table: $table
2466
+ });
2467
+ });
2468
+ const $datePickers = scope.querySelectorAll('[data-module="moj-date-picker"]');
2469
+ $datePickers.forEach($datePicker => {
2470
+ new DatePicker($datePicker);
2471
+ });
2472
+ const $buttonMenus = scope.querySelectorAll('[data-module="moj-button-menu"]');
2473
+ $buttonMenus.forEach($buttonmenu => {
2474
+ new ButtonMenu($buttonmenu);
2475
+ });
2476
+ const $alerts = scope.querySelectorAll('[data-module="moj-alert"]');
2477
+ $alerts.forEach($alert => {
2478
+ new Alert($alert);
2479
+ });
2480
+ }
2481
+
2482
+ /**
2483
+ * @typedef {object} Config
2484
+ * @property {Element} [scope=document] - Scope to query for components
2485
+ */
2486
+
2487
+ /**
2488
+ * Schema for component config
2489
+ *
2490
+ * @typedef {object} Schema
2491
+ * @property {{ [field: string]: SchemaProperty | undefined }} properties - Schema properties
2492
+ */
2493
+
2494
+ /**
2495
+ * Schema property for component config
2496
+ *
2497
+ * @typedef {object} SchemaProperty
2498
+ * @property {'string' | 'boolean' | 'number' | 'object'} type - Property type
2499
+ */
2500
+
2501
+ export { AddAnother, Alert, ButtonMenu, DatePicker, FilterToggleButton, FormValidator, MultiFileUpload, MultiSelect, PasswordReveal, RichTextEditor, SearchToggle, SortableTable, initAll, version };
2502
+ //# sourceMappingURL=all.bundle.mjs.map