@ministryofjustice/frontend 3.5.0 → 3.6.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 (36) hide show
  1. package/moj/all.jquery.js +13378 -0
  2. package/moj/all.jquery.min.js +1 -81
  3. package/moj/all.js +2577 -2853
  4. package/moj/all.mjs +126 -0
  5. package/moj/components/add-another/add-another.js +111 -132
  6. package/moj/components/add-another/add-another.mjs +106 -0
  7. package/moj/components/alert/alert.js +352 -479
  8. package/moj/components/alert/alert.mjs +251 -0
  9. package/moj/components/alert/alert.spec.helper.js +6 -24
  10. package/moj/components/alert/alert.spec.helper.mjs +66 -0
  11. package/moj/components/button-menu/button-menu.js +326 -343
  12. package/moj/components/button-menu/button-menu.mjs +329 -0
  13. package/moj/components/date-picker/date-picker.js +905 -922
  14. package/moj/components/date-picker/date-picker.mjs +961 -0
  15. package/moj/components/filter-toggle-button/filter-toggle-button.js +98 -119
  16. package/moj/components/filter-toggle-button/filter-toggle-button.mjs +93 -0
  17. package/moj/components/form-validator/form-validator.js +201 -396
  18. package/moj/components/form-validator/form-validator.mjs +168 -0
  19. package/moj/components/multi-file-upload/multi-file-upload.js +227 -441
  20. package/moj/components/multi-file-upload/multi-file-upload.mjs +219 -0
  21. package/moj/components/multi-select/multi-select.js +82 -103
  22. package/moj/components/multi-select/multi-select.mjs +77 -0
  23. package/moj/components/password-reveal/password-reveal.js +40 -61
  24. package/moj/components/password-reveal/password-reveal.mjs +35 -0
  25. package/moj/components/rich-text-editor/rich-text-editor.js +162 -183
  26. package/moj/components/rich-text-editor/rich-text-editor.mjs +157 -0
  27. package/moj/components/search-toggle/search-toggle.js +52 -73
  28. package/moj/components/search-toggle/search-toggle.mjs +54 -0
  29. package/moj/components/sortable-table/sortable-table.js +143 -164
  30. package/moj/components/sortable-table/sortable-table.mjs +138 -0
  31. package/moj/helpers.js +196 -215
  32. package/moj/helpers.mjs +123 -0
  33. package/moj/moj-frontend.min.js +1 -81
  34. package/moj/version.js +6 -23
  35. package/moj/version.mjs +3 -0
  36. package/package.json +13 -1
@@ -0,0 +1,251 @@
1
+ import { setFocus, getPreviousSibling, findNearestMatchingElement } from '../../helpers.mjs';
2
+
3
+ /**
4
+ * @typedef {object} AlertConfig
5
+ * @property {boolean} [dismissible=false] - Can the alert be dismissed by the user
6
+ * @property {string} [dismissText=Dismiss] - the label text for the dismiss button
7
+ * @property {boolean} [disableAutoFocus=false] - whether the alert will be autofocused
8
+ * @property {string} [focusOnDismissSelector] - CSS Selector for element to be focused on dismiss
9
+ */
10
+
11
+ /**
12
+ * @param {HTMLElement} $module - the Alert element
13
+ * @param {AlertConfig} config - configuration options
14
+ * @class
15
+ */
16
+ function Alert($module, config = {}) {
17
+ if (!$module) {
18
+ return this
19
+ }
20
+
21
+ const schema = Object.freeze({
22
+ properties: {
23
+ dismissible: { type: 'boolean' },
24
+ dismissText: { type: 'string' },
25
+ disableAutoFocus: { type: 'boolean' },
26
+ focusOnDismissSelector: { type: 'string' }
27
+ }
28
+ });
29
+
30
+ const defaults = {
31
+ dismissible: false,
32
+ dismissText: 'Dismiss',
33
+ disableAutoFocus: false
34
+ };
35
+
36
+ // data attributes override JS config, which overrides defaults
37
+ this.config = this.mergeConfigs(
38
+ defaults,
39
+ config,
40
+ this.parseDataset(schema, $module.dataset)
41
+ );
42
+
43
+ this.$module = $module;
44
+ }
45
+
46
+ Alert.prototype.init = function () {
47
+ /**
48
+ * Focus the alert
49
+ *
50
+ * If `role="alert"` is set, focus the element to help some assistive
51
+ * technologies prioritise announcing it.
52
+ *
53
+ * You can turn off the auto-focus functionality by setting
54
+ * `data-disable-auto-focus="true"` in the component HTML. You might wish to
55
+ * do this based on user research findings, or to avoid a clash with another
56
+ * element which should be focused when the page loads.
57
+ */
58
+ if (
59
+ this.$module.getAttribute('role') === 'alert' &&
60
+ !this.config.disableAutoFocus
61
+ ) {
62
+ setFocus(this.$module);
63
+ }
64
+
65
+ this.$dismissButton = this.$module.querySelector('.moj-alert__dismiss');
66
+
67
+ if (this.config.dismissible && this.$dismissButton) {
68
+ this.$dismissButton.innerHTML = this.config.dismissText;
69
+ this.$dismissButton.removeAttribute('hidden');
70
+
71
+ this.$module.addEventListener('click', (event) => {
72
+ if (this.$dismissButton.contains(event.target)) {
73
+ this.dimiss();
74
+ }
75
+ });
76
+ }
77
+ };
78
+
79
+ /**
80
+ * Handle dismissing the alert
81
+ */
82
+ Alert.prototype.dimiss = function () {
83
+ let $elementToRecieveFocus;
84
+
85
+ // If a selector has been provided, attempt to find that element
86
+ if (this.config.focusOnDismissSelector) {
87
+ $elementToRecieveFocus = document.querySelector(
88
+ this.config.focusOnDismissSelector
89
+ );
90
+ }
91
+
92
+ // Is the next sibling another alert
93
+ if (!$elementToRecieveFocus) {
94
+ const $nextSibling = this.$module.nextElementSibling;
95
+ if ($nextSibling && $nextSibling.matches('.moj-alert')) {
96
+ $elementToRecieveFocus = $nextSibling;
97
+ }
98
+ }
99
+
100
+ // Else try to find any preceding sibling alert or heading
101
+ if (!$elementToRecieveFocus) {
102
+ $elementToRecieveFocus = getPreviousSibling(
103
+ this.$module,
104
+ '.moj-alert, h1, h2, h3, h4, h5, h6'
105
+ );
106
+ }
107
+
108
+ // Else find the closest ancestor heading, or fallback to main, or last resort
109
+ // use the body element
110
+ if (!$elementToRecieveFocus) {
111
+ $elementToRecieveFocus = findNearestMatchingElement(
112
+ this.$module,
113
+ 'h1, h2, h3, h4, h5, h6, main, body'
114
+ );
115
+ }
116
+
117
+ // If we have an element, place focus on it
118
+ if ($elementToRecieveFocus) {
119
+ setFocus($elementToRecieveFocus);
120
+ }
121
+
122
+ // Remove the alert
123
+ this.$module.remove();
124
+ };
125
+
126
+ /**
127
+ * Normalise string
128
+ *
129
+ * 'If it looks like a duck, and it quacks like a duck…' 🦆
130
+ *
131
+ * If the passed value looks like a boolean or a number, convert it to a boolean
132
+ * or number.
133
+ *
134
+ * Designed to be used to convert config passed via data attributes (which are
135
+ * always strings) into something sensible.
136
+ *
137
+ * @internal
138
+ * @param {DOMStringMap[string]} value - The value to normalise
139
+ * @param {SchemaProperty} [property] - Component schema property
140
+ * @returns {string | boolean | number | undefined} Normalised data
141
+ */
142
+ Alert.prototype.normaliseString = function (value, property) {
143
+ const trimmedValue = value ? value.trim() : '';
144
+
145
+ let output;
146
+ let outputType;
147
+ if (property && property.type) {
148
+ outputType = property.type;
149
+ }
150
+
151
+ // No schema type set? Determine automatically
152
+ if (!outputType) {
153
+ if (['true', 'false'].includes(trimmedValue)) {
154
+ outputType = 'boolean';
155
+ }
156
+
157
+ // Empty / whitespace-only strings are considered finite so we need to check
158
+ // the length of the trimmed string as well
159
+ if (trimmedValue.length > 0 && isFinite(Number(trimmedValue))) {
160
+ outputType = 'number';
161
+ }
162
+ }
163
+
164
+ switch (outputType) {
165
+ case 'boolean':
166
+ output = trimmedValue === 'true';
167
+ break
168
+
169
+ case 'number':
170
+ output = Number(trimmedValue);
171
+ break
172
+
173
+ default:
174
+ output = value;
175
+ }
176
+
177
+ return output
178
+ };
179
+
180
+ /**
181
+ * Parse dataset
182
+ *
183
+ * Loop over an object and normalise each value using {@link normaliseString},
184
+ * optionally expanding nested `i18n.field`
185
+ *
186
+ * @param {Schema} schema - component schema
187
+ * @param {DOMStringMap} dataset - HTML element dataset
188
+ * @returns {object} Normalised dataset
189
+ */
190
+ Alert.prototype.parseDataset = function (schema, dataset) {
191
+ const parsed = {};
192
+
193
+ for (const [field, property] of Object.entries(schema.properties)) {
194
+ if (field in dataset) {
195
+ if (dataset[field]) {
196
+ parsed[field] = this.normaliseString(dataset[field], property);
197
+ }
198
+ }
199
+ }
200
+
201
+ return parsed
202
+ };
203
+
204
+ /**
205
+ * Config merging function
206
+ *
207
+ * Takes any number of objects and combines them together, with
208
+ * greatest priority on the LAST item passed in.
209
+ *
210
+ * @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
211
+ * @returns {{ [key: string]: unknown }} A merged config object
212
+ */
213
+ Alert.prototype.mergeConfigs = function (...configObjects) {
214
+ const formattedConfigObject = {};
215
+
216
+ // Loop through each of the passed objects
217
+ for (const configObject of configObjects) {
218
+ for (const key of Object.keys(configObject)) {
219
+ const option = formattedConfigObject[key];
220
+ const override = configObject[key];
221
+
222
+ // Push their keys one-by-one into formattedConfigObject. Any duplicate
223
+ // keys with object values will be merged, otherwise the new value will
224
+ // override the existing value.
225
+ if (typeof option === 'object' && typeof override === 'object') {
226
+ // @ts-expect-error Index signature for type 'string' is missing
227
+ formattedConfigObject[key] = this.mergeConfigs(option, override);
228
+ } else {
229
+ formattedConfigObject[key] = override;
230
+ }
231
+ }
232
+ }
233
+
234
+ return formattedConfigObject
235
+ };
236
+
237
+ /**
238
+ * Schema for component config
239
+ *
240
+ * @typedef {object} Schema
241
+ * @property {{ [field: string]: SchemaProperty | undefined }} properties - Schema properties
242
+ */
243
+
244
+ /**
245
+ * Schema property for component config
246
+ *
247
+ * @typedef {object} SchemaProperty
248
+ * @property {'string' | 'boolean' | 'number' | 'object'} type - Property type
249
+ */
250
+
251
+ export { Alert };
@@ -1,20 +1,10 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.MOJFrontend = factory());
5
- })(this, (function () { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MOJFrontend = global.MOJFrontend || {}));
5
+ })(this, (function (exports) { 'use strict';
6
6
 
7
- function getDefaultExportFromCjs (x) {
8
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
9
- }
10
-
11
- var alert_spec_helper$1;
12
- var hasRequiredAlert_spec_helper;
13
-
14
- function requireAlert_spec_helper () {
15
- if (hasRequiredAlert_spec_helper) return alert_spec_helper$1;
16
- hasRequiredAlert_spec_helper = 1;
17
- const pageTemplate = `
7
+ const pageTemplate = `
18
8
  <main>
19
9
  <div id="alert-1" role="region" class="moj-alert moj-alert--information moj-alert--with-heading" aria-label="information: This contains information" data-module="moj-alert" data-dismissible="true">
20
10
  <div>
@@ -78,15 +68,7 @@
78
68
  </section>
79
69
  </main>
80
70
  `;
81
- alert_spec_helper$1 = {
82
- pageTemplate
83
- };
84
- return alert_spec_helper$1;
85
- }
86
-
87
- var alert_spec_helperExports = requireAlert_spec_helper();
88
- var alert_spec_helper = /*@__PURE__*/getDefaultExportFromCjs(alert_spec_helperExports);
89
71
 
90
- return alert_spec_helper;
72
+ exports.pageTemplate = pageTemplate;
91
73
 
92
74
  }));
@@ -0,0 +1,66 @@
1
+ const pageTemplate = `
2
+ <main>
3
+ <div id="alert-1" role="region" class="moj-alert moj-alert--information moj-alert--with-heading" aria-label="information: This contains information" data-module="moj-alert" data-dismissible="true">
4
+ <div>
5
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.2165 3.45151C11.733 2.82332 13.3585 2.5 15 2.5C16.6415 2.5 18.267 2.82332 19.7835 3.45151C21.3001 4.07969 22.6781 5.00043 23.8388 6.16117C24.9996 7.3219 25.9203 8.69989 26.5485 10.2165C27.1767 11.733 27.5 13.3585 27.5 15C27.5 18.3152 26.183 21.4946 23.8388 23.8388C21.4946 26.183 18.3152 27.5 15 27.5C13.3585 27.5 11.733 27.1767 10.2165 26.5485C8.69989 25.9203 7.3219 24.9996 6.16117 23.8388C3.81696 21.4946 2.5 18.3152 2.5 15C2.5 11.6848 3.81696 8.50537 6.16117 6.16117C7.3219 5.00043 8.69989 4.07969 10.2165 3.45151ZM16.3574 22.4121H13.6621V12.95H16.3574V22.4121ZM13.3789 9.20898C13.3789 8.98763 13.4212 8.7793 13.5059 8.58398C13.5905 8.38216 13.7044 8.20964 13.8477 8.06641C13.9974 7.91667 14.1699 7.79948 14.3652 7.71484C14.5605 7.63021 14.7721 7.58789 15 7.58789C15.2214 7.58789 15.4297 7.63021 15.625 7.71484C15.8268 7.79948 15.9993 7.91667 16.1426 8.06641C16.2923 8.20964 16.4095 8.38216 16.4941 8.58398C16.5788 8.7793 16.6211 8.98763 16.6211 9.20898C16.6211 9.43685 16.5788 9.64844 16.4941 9.84375C16.4095 10.0391 16.2923 10.2116 16.1426 10.3613C15.9993 10.5046 15.8268 10.6185 15.625 10.7031C15.4297 10.7878 15.2214 10.8301 15 10.8301C14.7721 10.8301 14.5605 10.7878 14.3652 10.7031C14.1699 10.6185 13.9974 10.5046 13.8477 10.3613C13.7044 10.2116 13.5905 10.0391 13.5059 9.84375C13.4212 9.64844 13.3789 9.43685 13.3789 9.20898Z" fill="currentColor"/></svg>
6
+ </div>
7
+ <div class="moj-alert__content">
8
+ <h2 class="govuk-heading-m">This contains information</h2>
9
+ Content that informs you of a thing. It really is so very informative, that&#39;s why it needs so much content and is really, exceedingly verbose.
10
+ </div>
11
+ <div class="moj-alert__action">
12
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
13
+ </div>
14
+ </div>
15
+
16
+ <h1 id="h1">Heading 1</h1>
17
+ <section>
18
+ <h2 id="h2">heading 2</h2>
19
+ <div id="alert-2" role="region" class="moj-alert moj-alert--information" aria-label="information: You might like to know" data-module="moj-alert" data-dismissible="true">
20
+ <div>
21
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.2165 3.45151C11.733 2.82332 13.3585 2.5 15 2.5C16.6415 2.5 18.267 2.82332 19.7835 3.45151C21.3001 4.07969 22.6781 5.00043 23.8388 6.16117C24.9996 7.3219 25.9203 8.69989 26.5485 10.2165C27.1767 11.733 27.5 13.3585 27.5 15C27.5 18.3152 26.183 21.4946 23.8388 23.8388C21.4946 26.183 18.3152 27.5 15 27.5C13.3585 27.5 11.733 27.1767 10.2165 26.5485C8.69989 25.9203 7.3219 24.9996 6.16117 23.8388C3.81696 21.4946 2.5 18.3152 2.5 15C2.5 11.6848 3.81696 8.50537 6.16117 6.16117C7.3219 5.00043 8.69989 4.07969 10.2165 3.45151ZM16.3574 22.4121H13.6621V12.95H16.3574V22.4121ZM13.3789 9.20898C13.3789 8.98763 13.4212 8.7793 13.5059 8.58398C13.5905 8.38216 13.7044 8.20964 13.8477 8.06641C13.9974 7.91667 14.1699 7.79948 14.3652 7.71484C14.5605 7.63021 14.7721 7.58789 15 7.58789C15.2214 7.58789 15.4297 7.63021 15.625 7.71484C15.8268 7.79948 15.9993 7.91667 16.1426 8.06641C16.2923 8.20964 16.4095 8.38216 16.4941 8.58398C16.5788 8.7793 16.6211 8.98763 16.6211 9.20898C16.6211 9.43685 16.5788 9.64844 16.4941 9.84375C16.4095 10.0391 16.2923 10.2116 16.1426 10.3613C15.9993 10.5046 15.8268 10.6185 15.625 10.7031C15.4297 10.7878 15.2214 10.8301 15 10.8301C14.7721 10.8301 14.5605 10.7878 14.3652 10.7031C14.1699 10.6185 13.9974 10.5046 13.8477 10.3613C13.7044 10.2116 13.5905 10.0391 13.5059 9.84375C13.4212 9.64844 13.3789 9.43685 13.3789 9.20898Z" fill="currentColor"/></svg>
22
+ </div>
23
+ <div class="moj-alert__content">Content that informs you of a thing. <a href="#">More information</a></div>
24
+ <div class="moj-alert__action">
25
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
26
+ </div>
27
+ </div>
28
+
29
+
30
+ <div id="alert-3"role="region" class="moj-alert moj-alert--success" aria-label="success: Woohoo!" data-module="moj-alert" data-dismissible="true">
31
+ <div>
32
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path d="M11.2869 24.6726L2.00415 15.3899L4.62189 12.7722L11.2869 19.4186L25.3781 5.32739L27.9958 7.96369L11.2869 24.6726Z" fill="currentColor"/>
33
+ </svg>
34
+ </div>
35
+ <div class="moj-alert__content">That thing just successfully occurred. <a href="#">Celebrate here</a></div>
36
+ <div class="moj-alert__action">
37
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
38
+ </div>
39
+ </div>
40
+
41
+ <div id="alert-4" role="region" class="moj-alert moj-alert--warning" aria-label="warning: Something&#39;s not quite right" data-module="moj-alert" data-dismissible="true">
42
+ <div>
43
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M15 2.44922L28.75 26.1992H1.25L15 2.44922ZM13.5107 9.49579H16.4697L16.2431 17.7678H13.7461L13.5107 9.49579ZM13.1299 21.82C13.1299 21.5661 13.1787 21.3285 13.2764 21.1071C13.374 20.8793 13.5075 20.6807 13.6768 20.5114C13.8525 20.3421 14.0544 20.2087 14.2822 20.111C14.5101 20.0134 14.7542 19.9645 15.0146 19.9645C15.2686 19.9645 15.5062 20.0134 15.7275 20.111C15.9554 20.2087 16.154 20.3421 16.3232 20.5114C16.4925 20.6807 16.626 20.8793 16.7236 21.1071C16.8213 21.3285 16.8701 21.5661 16.8701 21.82C16.8701 22.0804 16.8213 22.3246 16.7236 22.5524C16.626 22.7803 16.4925 22.9789 16.3232 23.1481C16.154 23.3174 15.9554 23.4509 15.7275 23.5485C15.5062 23.6462 15.2686 23.695 15.0146 23.695C14.7542 23.695 14.5101 23.6462 14.2822 23.5485C14.0544 23.4509 13.8525 23.3174 13.6768 23.1481C13.5075 22.9789 13.374 22.7803 13.2764 22.5524C13.1787 22.3246 13.1299 22.0804 13.1299 21.82Z" fill="currentColor"/></svg>
44
+ </div>
45
+ <div class="moj-alert__content">You should be aware of this thing. <a href="#">More information</a></div>
46
+ <div class="moj-alert__action">
47
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
48
+ </div>
49
+ </div>
50
+
51
+ <div id="alert-5" role="region" class="moj-alert moj-alert--error" aria-label="error: Woah, hold up!" data-module="moj-alert" data-dismissible="true" data-dismissible="true" data-focus-on-dismiss-selector="#focusOnMe">
52
+ <div>
53
+ <svg class="moj-alert__icon" role="presentation" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" height="30" width="30"><path fill-rule="evenodd" clip-rule="evenodd" d="M20.1777 2.5H9.82233L2.5 9.82233V20.1777L9.82233 27.5H20.1777L27.5 20.1777V9.82233L20.1777 2.5ZM10.9155 8.87769L15.0001 12.9623L19.0847 8.87771L21.1224 10.9154L17.0378 15L21.1224 19.0846L19.0847 21.1222L15.0001 17.0376L10.9155 21.1223L8.87782 19.0846L12.9624 15L8.87783 10.9153L10.9155 8.87769Z" fill="currentColor"/></svg>
54
+ </div>
55
+ <div class="moj-alert__content">Bad things happened. <a href="#">Contact us</a></div>
56
+ <div class="moj-alert__action">
57
+ <button class="moj-alert__dismiss" hidden>Dismiss</button>
58
+ </div>
59
+ </div>
60
+
61
+ <div id="focusOnMe">I will receive focus</div>
62
+ </section>
63
+ </main>
64
+ `;
65
+
66
+ export { pageTemplate };