@ministryofjustice/frontend 3.5.0 → 3.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/moj/all.jquery.min.js +1 -81
  2. package/moj/all.js +2577 -2853
  3. package/moj/all.mjs +126 -0
  4. package/moj/all.scss +1 -1
  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/cookie-banner/_cookie-banner.scss +1 -1
  14. package/moj/components/date-picker/date-picker.js +905 -922
  15. package/moj/components/date-picker/date-picker.mjs +961 -0
  16. package/moj/components/filter-toggle-button/filter-toggle-button.js +98 -119
  17. package/moj/components/filter-toggle-button/filter-toggle-button.mjs +93 -0
  18. package/moj/components/form-validator/form-validator.js +201 -396
  19. package/moj/components/form-validator/form-validator.mjs +168 -0
  20. package/moj/components/multi-file-upload/multi-file-upload.js +227 -441
  21. package/moj/components/multi-file-upload/multi-file-upload.mjs +219 -0
  22. package/moj/components/multi-select/multi-select.js +82 -103
  23. package/moj/components/multi-select/multi-select.mjs +77 -0
  24. package/moj/components/password-reveal/password-reveal.js +40 -61
  25. package/moj/components/password-reveal/password-reveal.mjs +35 -0
  26. package/moj/components/rich-text-editor/rich-text-editor.js +162 -183
  27. package/moj/components/rich-text-editor/rich-text-editor.mjs +157 -0
  28. package/moj/components/search-toggle/search-toggle.js +52 -73
  29. package/moj/components/search-toggle/search-toggle.mjs +54 -0
  30. package/moj/components/sortable-table/sortable-table.js +143 -164
  31. package/moj/components/sortable-table/sortable-table.mjs +138 -0
  32. package/moj/helpers.js +196 -215
  33. package/moj/helpers.mjs +123 -0
  34. package/moj/moj-frontend.min.js +1 -81
  35. package/moj/version.js +6 -23
  36. package/moj/version.mjs +3 -0
  37. package/package.json +24 -6
@@ -0,0 +1,54 @@
1
+ function SearchToggle(options) {
2
+ this.options = options;
3
+ this.container = window.jQuery(this.options.search.container);
4
+ this.toggleButtonContainer = window.jQuery(this.options.toggleButton.container);
5
+
6
+ if (this.container.data('moj-search-toggle-initialised')) {
7
+ return
8
+ }
9
+
10
+ this.container.data('moj-search-toggle-initialised', true);
11
+
12
+ const svg =
13
+ '<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>';
14
+
15
+ this.toggleButton = window.jQuery(
16
+ `<button class="moj-search-toggle__button" type="button" aria-haspopup="true" aria-expanded="false">
17
+ ${this.options.toggleButton.text} ${svg}
18
+ </button>`
19
+ );
20
+ this.toggleButton.on('click', window.jQuery.proxy(this, 'onToggleButtonClick'));
21
+ this.toggleButtonContainer.append(this.toggleButton);
22
+ window.jQuery(document).on('click', this.onDocumentClick.bind(this));
23
+ window.jQuery(document).on('focusin', this.onDocumentClick.bind(this));
24
+ }
25
+
26
+ SearchToggle.prototype.showMenu = function () {
27
+ this.toggleButton.attr('aria-expanded', 'true');
28
+ this.container.removeClass('moj-js-hidden');
29
+ this.container.find('input').first().get(0).focus();
30
+ };
31
+
32
+ SearchToggle.prototype.hideMenu = function () {
33
+ this.container.addClass('moj-js-hidden');
34
+ this.toggleButton.attr('aria-expanded', 'false');
35
+ };
36
+
37
+ SearchToggle.prototype.onToggleButtonClick = function () {
38
+ if (this.toggleButton.attr('aria-expanded') === 'false') {
39
+ this.showMenu();
40
+ } else {
41
+ this.hideMenu();
42
+ }
43
+ };
44
+
45
+ SearchToggle.prototype.onDocumentClick = function (e) {
46
+ if (
47
+ !window.jQuery.contains(this.toggleButtonContainer[0], e.target) &&
48
+ !window.jQuery.contains(this.container[0], e.target)
49
+ ) {
50
+ this.hideMenu();
51
+ }
52
+ };
53
+
54
+ export { SearchToggle };
@@ -1,167 +1,146 @@
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';
6
-
7
- function getDefaultExportFromCjs (x) {
8
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
9
- }
10
-
11
- var _global_window_jQuery = window.jQuery;
12
-
13
- var sortableTable$1;
14
- var hasRequiredSortableTable;
15
-
16
- function requireSortableTable () {
17
- if (hasRequiredSortableTable) return sortableTable$1;
18
- hasRequiredSortableTable = 1;
19
- const $ = _global_window_jQuery;
20
-
21
- function SortableTable(params) {
22
- this.table = $(params.table);
23
-
24
- if (this.table.data('moj-search-toggle-initialised')) {
25
- return
26
- }
27
-
28
- this.table.data('moj-search-toggle-initialised', true);
29
-
30
- this.setupOptions(params);
31
- this.body = this.table.find('tbody');
32
- this.createHeadingButtons();
33
- this.createStatusBox();
34
- this.initialiseSortedColumn();
35
- this.table.on('click', 'th button', $.proxy(this, 'onSortButtonClick'));
36
- }
37
-
38
- SortableTable.prototype.setupOptions = function (params) {
39
- params = params || {};
40
- this.statusMessage = params.statusMessage || 'Sort by %heading% (%direction%)';
41
- this.ascendingText = params.ascendingText || 'ascending';
42
- this.descendingText = params.descendingText || 'descending';
43
- };
44
-
45
- SortableTable.prototype.createHeadingButtons = function () {
46
- const headings = this.table.find('thead th');
47
- let heading;
48
- for (let i = 0; i < headings.length; i++) {
49
- heading = $(headings[i]);
50
- if (heading.attr('aria-sort')) {
51
- this.createHeadingButton(heading, i);
52
- }
53
- }
54
- };
55
-
56
- SortableTable.prototype.createHeadingButton = function (heading, i) {
57
- const text = heading.text();
58
- const button = $(`<button type="button" data-index="${i}">${text}</button>`);
59
- heading.text('');
60
- heading.append(button);
61
- };
62
-
63
- SortableTable.prototype.createStatusBox = function () {
64
- this.status = $(
65
- '<div aria-live="polite" role="status" aria-atomic="true" class="govuk-visually-hidden" />'
66
- );
67
- this.table.parent().append(this.status);
68
- };
69
-
70
- SortableTable.prototype.initialiseSortedColumn = function () {
71
- const rows = this.getTableRowsArray();
72
-
73
- this.table
74
- .find('th')
75
- .filter('[aria-sort="ascending"], [aria-sort="descending"]')
76
- .first()
77
- .each((index, el) => {
78
- const sortDirection = $(el).attr('aria-sort');
79
- const columnNumber = $(el).find('button').attr('data-index');
80
- const sortedRows = this.sort(rows, columnNumber, sortDirection);
81
- this.addRows(sortedRows);
82
- });
83
- };
84
-
85
- SortableTable.prototype.onSortButtonClick = function (e) {
86
- const columnNumber = e.currentTarget.getAttribute('data-index');
87
- const sortDirection = $(e.currentTarget).parent().attr('aria-sort');
88
- let newSortDirection;
89
- if (sortDirection === 'none' || sortDirection === 'descending') {
90
- newSortDirection = 'ascending';
91
- } else {
92
- newSortDirection = 'descending';
93
- }
94
- const rows = this.getTableRowsArray();
95
- const sortedRows = this.sort(rows, columnNumber, newSortDirection);
96
- this.addRows(sortedRows);
97
- this.removeButtonStates();
98
- this.updateButtonState($(e.currentTarget), newSortDirection);
99
- };
100
-
101
- SortableTable.prototype.updateButtonState = function (button, direction) {
102
- button.parent().attr('aria-sort', direction);
103
- let message = this.statusMessage;
104
- message = message.replace(/%heading%/, button.text());
105
- message = message.replace(/%direction%/, this[`${direction}Text`]);
106
- this.status.text(message);
107
- };
108
-
109
- SortableTable.prototype.removeButtonStates = function () {
110
- this.table.find('thead th').attr('aria-sort', 'none');
111
- };
112
-
113
- SortableTable.prototype.addRows = function (rows) {
114
- for (let i = 0; i < rows.length; i++) {
115
- this.body.append(rows[i]);
116
- }
117
- };
118
-
119
- SortableTable.prototype.getTableRowsArray = function () {
120
- const rows = [];
121
- const trs = this.body.find('tr');
122
- for (let i = 0; i < trs.length; i++) {
123
- rows.push(trs[i]);
124
- }
125
- return rows
126
- };
127
-
128
- SortableTable.prototype.sort = function (rows, columnNumber, sortDirection) {
129
- const newRows = rows.sort(
130
- function (rowA, rowB) {
131
- const tdA = $(rowA).find('td,th').eq(columnNumber);
132
- const tdB = $(rowB).find('td,th').eq(columnNumber);
133
-
134
- const valueA =
135
- sortDirection === 'ascending'
136
- ? this.getCellValue(tdA)
137
- : this.getCellValue(tdB);
138
- const valueB =
139
- sortDirection === 'ascending'
140
- ? this.getCellValue(tdB)
141
- : this.getCellValue(tdA);
142
-
143
- if (typeof valueA === 'string' || typeof valueB === 'string')
144
- return valueA.toString().localeCompare(valueB.toString())
145
- return valueA - valueB
146
- }.bind(this)
147
- );
148
- return newRows
149
- };
150
-
151
- SortableTable.prototype.getCellValue = function (cell) {
152
- const val = cell.attr('data-sort-value') || cell.html();
153
-
154
- const valAsNumber = Number(val);
155
- return isNaN(valAsNumber) ? val : valAsNumber
156
- };
157
-
158
- sortableTable$1 = { SortableTable };
159
- return sortableTable$1;
160
- }
161
-
162
- var sortableTableExports = requireSortableTable();
163
- var sortableTable = /*@__PURE__*/getDefaultExportFromCjs(sortableTableExports);
164
-
165
- return sortableTable;
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
+
7
+ function SortableTable(params) {
8
+ this.table = window.jQuery(params.table);
9
+
10
+ if (this.table.data('moj-search-toggle-initialised')) {
11
+ return
12
+ }
13
+
14
+ this.table.data('moj-search-toggle-initialised', true);
15
+
16
+ this.setupOptions(params);
17
+ this.body = this.table.find('tbody');
18
+ this.createHeadingButtons();
19
+ this.createStatusBox();
20
+ this.initialiseSortedColumn();
21
+ this.table.on('click', 'th button', window.jQuery.proxy(this, 'onSortButtonClick'));
22
+ }
23
+
24
+ SortableTable.prototype.setupOptions = function (params) {
25
+ params = params || {};
26
+ this.statusMessage = params.statusMessage || 'Sort by %heading% (%direction%)';
27
+ this.ascendingText = params.ascendingText || 'ascending';
28
+ this.descendingText = params.descendingText || 'descending';
29
+ };
30
+
31
+ SortableTable.prototype.createHeadingButtons = function () {
32
+ const headings = this.table.find('thead th');
33
+ let heading;
34
+ for (let i = 0; i < headings.length; i++) {
35
+ heading = window.jQuery(headings[i]);
36
+ if (heading.attr('aria-sort')) {
37
+ this.createHeadingButton(heading, i);
38
+ }
39
+ }
40
+ };
41
+
42
+ SortableTable.prototype.createHeadingButton = function (heading, i) {
43
+ const text = heading.text();
44
+ const button = window.jQuery(`<button type="button" data-index="${i}">${text}</button>`);
45
+ heading.text('');
46
+ heading.append(button);
47
+ };
48
+
49
+ SortableTable.prototype.createStatusBox = function () {
50
+ this.status = window.jQuery(
51
+ '<div aria-live="polite" role="status" aria-atomic="true" class="govuk-visually-hidden" />'
52
+ );
53
+ this.table.parent().append(this.status);
54
+ };
55
+
56
+ SortableTable.prototype.initialiseSortedColumn = function () {
57
+ const rows = this.getTableRowsArray();
58
+
59
+ this.table
60
+ .find('th')
61
+ .filter('[aria-sort="ascending"], [aria-sort="descending"]')
62
+ .first()
63
+ .each((index, el) => {
64
+ const sortDirection = window.jQuery(el).attr('aria-sort');
65
+ const columnNumber = window.jQuery(el).find('button').attr('data-index');
66
+ const sortedRows = this.sort(rows, columnNumber, sortDirection);
67
+ this.addRows(sortedRows);
68
+ });
69
+ };
70
+
71
+ SortableTable.prototype.onSortButtonClick = function (e) {
72
+ const columnNumber = e.currentTarget.getAttribute('data-index');
73
+ const sortDirection = window.jQuery(e.currentTarget).parent().attr('aria-sort');
74
+ let newSortDirection;
75
+ if (sortDirection === 'none' || sortDirection === 'descending') {
76
+ newSortDirection = 'ascending';
77
+ } else {
78
+ newSortDirection = 'descending';
79
+ }
80
+ const rows = this.getTableRowsArray();
81
+ const sortedRows = this.sort(rows, columnNumber, newSortDirection);
82
+ this.addRows(sortedRows);
83
+ this.removeButtonStates();
84
+ this.updateButtonState(window.jQuery(e.currentTarget), newSortDirection);
85
+ };
86
+
87
+ SortableTable.prototype.updateButtonState = function (button, direction) {
88
+ button.parent().attr('aria-sort', direction);
89
+ let message = this.statusMessage;
90
+ message = message.replace(/%heading%/, button.text());
91
+ message = message.replace(/%direction%/, this[`${direction}Text`]);
92
+ this.status.text(message);
93
+ };
94
+
95
+ SortableTable.prototype.removeButtonStates = function () {
96
+ this.table.find('thead th').attr('aria-sort', 'none');
97
+ };
98
+
99
+ SortableTable.prototype.addRows = function (rows) {
100
+ for (let i = 0; i < rows.length; i++) {
101
+ this.body.append(rows[i]);
102
+ }
103
+ };
104
+
105
+ SortableTable.prototype.getTableRowsArray = function () {
106
+ const rows = [];
107
+ const trs = this.body.find('tr');
108
+ for (let i = 0; i < trs.length; i++) {
109
+ rows.push(trs[i]);
110
+ }
111
+ return rows
112
+ };
113
+
114
+ SortableTable.prototype.sort = function (rows, columnNumber, sortDirection) {
115
+ const newRows = rows.sort(
116
+ function (rowA, rowB) {
117
+ const tdA = window.jQuery(rowA).find('td,th').eq(columnNumber);
118
+ const tdB = window.jQuery(rowB).find('td,th').eq(columnNumber);
119
+
120
+ const valueA =
121
+ sortDirection === 'ascending'
122
+ ? this.getCellValue(tdA)
123
+ : this.getCellValue(tdB);
124
+ const valueB =
125
+ sortDirection === 'ascending'
126
+ ? this.getCellValue(tdB)
127
+ : this.getCellValue(tdA);
128
+
129
+ if (typeof valueA === 'string' || typeof valueB === 'string')
130
+ return valueA.toString().localeCompare(valueB.toString())
131
+ return valueA - valueB
132
+ }.bind(this)
133
+ );
134
+ return newRows
135
+ };
136
+
137
+ SortableTable.prototype.getCellValue = function (cell) {
138
+ const val = cell.attr('data-sort-value') || cell.html();
139
+
140
+ const valAsNumber = Number(val);
141
+ return isNaN(valAsNumber) ? val : valAsNumber
142
+ };
143
+
144
+ exports.SortableTable = SortableTable;
166
145
 
167
146
  }));
@@ -0,0 +1,138 @@
1
+ function SortableTable(params) {
2
+ this.table = window.jQuery(params.table);
3
+
4
+ if (this.table.data('moj-search-toggle-initialised')) {
5
+ return
6
+ }
7
+
8
+ this.table.data('moj-search-toggle-initialised', true);
9
+
10
+ this.setupOptions(params);
11
+ this.body = this.table.find('tbody');
12
+ this.createHeadingButtons();
13
+ this.createStatusBox();
14
+ this.initialiseSortedColumn();
15
+ this.table.on('click', 'th button', window.jQuery.proxy(this, 'onSortButtonClick'));
16
+ }
17
+
18
+ SortableTable.prototype.setupOptions = function (params) {
19
+ params = params || {};
20
+ this.statusMessage = params.statusMessage || 'Sort by %heading% (%direction%)';
21
+ this.ascendingText = params.ascendingText || 'ascending';
22
+ this.descendingText = params.descendingText || 'descending';
23
+ };
24
+
25
+ SortableTable.prototype.createHeadingButtons = function () {
26
+ const headings = this.table.find('thead th');
27
+ let heading;
28
+ for (let i = 0; i < headings.length; i++) {
29
+ heading = window.jQuery(headings[i]);
30
+ if (heading.attr('aria-sort')) {
31
+ this.createHeadingButton(heading, i);
32
+ }
33
+ }
34
+ };
35
+
36
+ SortableTable.prototype.createHeadingButton = function (heading, i) {
37
+ const text = heading.text();
38
+ const button = window.jQuery(`<button type="button" data-index="${i}">${text}</button>`);
39
+ heading.text('');
40
+ heading.append(button);
41
+ };
42
+
43
+ SortableTable.prototype.createStatusBox = function () {
44
+ this.status = window.jQuery(
45
+ '<div aria-live="polite" role="status" aria-atomic="true" class="govuk-visually-hidden" />'
46
+ );
47
+ this.table.parent().append(this.status);
48
+ };
49
+
50
+ SortableTable.prototype.initialiseSortedColumn = function () {
51
+ const rows = this.getTableRowsArray();
52
+
53
+ this.table
54
+ .find('th')
55
+ .filter('[aria-sort="ascending"], [aria-sort="descending"]')
56
+ .first()
57
+ .each((index, el) => {
58
+ const sortDirection = window.jQuery(el).attr('aria-sort');
59
+ const columnNumber = window.jQuery(el).find('button').attr('data-index');
60
+ const sortedRows = this.sort(rows, columnNumber, sortDirection);
61
+ this.addRows(sortedRows);
62
+ });
63
+ };
64
+
65
+ SortableTable.prototype.onSortButtonClick = function (e) {
66
+ const columnNumber = e.currentTarget.getAttribute('data-index');
67
+ const sortDirection = window.jQuery(e.currentTarget).parent().attr('aria-sort');
68
+ let newSortDirection;
69
+ if (sortDirection === 'none' || sortDirection === 'descending') {
70
+ newSortDirection = 'ascending';
71
+ } else {
72
+ newSortDirection = 'descending';
73
+ }
74
+ const rows = this.getTableRowsArray();
75
+ const sortedRows = this.sort(rows, columnNumber, newSortDirection);
76
+ this.addRows(sortedRows);
77
+ this.removeButtonStates();
78
+ this.updateButtonState(window.jQuery(e.currentTarget), newSortDirection);
79
+ };
80
+
81
+ SortableTable.prototype.updateButtonState = function (button, direction) {
82
+ button.parent().attr('aria-sort', direction);
83
+ let message = this.statusMessage;
84
+ message = message.replace(/%heading%/, button.text());
85
+ message = message.replace(/%direction%/, this[`${direction}Text`]);
86
+ this.status.text(message);
87
+ };
88
+
89
+ SortableTable.prototype.removeButtonStates = function () {
90
+ this.table.find('thead th').attr('aria-sort', 'none');
91
+ };
92
+
93
+ SortableTable.prototype.addRows = function (rows) {
94
+ for (let i = 0; i < rows.length; i++) {
95
+ this.body.append(rows[i]);
96
+ }
97
+ };
98
+
99
+ SortableTable.prototype.getTableRowsArray = function () {
100
+ const rows = [];
101
+ const trs = this.body.find('tr');
102
+ for (let i = 0; i < trs.length; i++) {
103
+ rows.push(trs[i]);
104
+ }
105
+ return rows
106
+ };
107
+
108
+ SortableTable.prototype.sort = function (rows, columnNumber, sortDirection) {
109
+ const newRows = rows.sort(
110
+ function (rowA, rowB) {
111
+ const tdA = window.jQuery(rowA).find('td,th').eq(columnNumber);
112
+ const tdB = window.jQuery(rowB).find('td,th').eq(columnNumber);
113
+
114
+ const valueA =
115
+ sortDirection === 'ascending'
116
+ ? this.getCellValue(tdA)
117
+ : this.getCellValue(tdB);
118
+ const valueB =
119
+ sortDirection === 'ascending'
120
+ ? this.getCellValue(tdB)
121
+ : this.getCellValue(tdA);
122
+
123
+ if (typeof valueA === 'string' || typeof valueB === 'string')
124
+ return valueA.toString().localeCompare(valueB.toString())
125
+ return valueA - valueB
126
+ }.bind(this)
127
+ );
128
+ return newRows
129
+ };
130
+
131
+ SortableTable.prototype.getCellValue = function (cell) {
132
+ const val = cell.attr('data-sort-value') || cell.html();
133
+
134
+ const valAsNumber = Number(val);
135
+ return isNaN(valAsNumber) ? val : valAsNumber
136
+ };
137
+
138
+ export { SortableTable };