@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,168 @@
1
+ import { addAttributeValue, removeAttributeValue } from '../../helpers.mjs';
2
+
3
+ function FormValidator(form, options) {
4
+ this.form = form;
5
+ this.errors = [];
6
+ this.validators = [];
7
+ window.jQuery(this.form).on('submit', window.jQuery.proxy(this, 'onSubmit'));
8
+ this.summary =
9
+ options && options.summary ? window.jQuery(options.summary) : window.jQuery('.govuk-error-summary');
10
+ this.originalTitle = document.title;
11
+ }
12
+
13
+ FormValidator.entityMap = {
14
+ '&': '&',
15
+ '<': '&lt;',
16
+ '>': '&gt;',
17
+ '"': '&quot;',
18
+ "'": '&#39;',
19
+ '/': '&#x2F;',
20
+ '`': '&#x60;',
21
+ '=': '&#x3D;'
22
+ };
23
+
24
+ FormValidator.prototype.escapeHtml = function (string) {
25
+ return String(string).replace(/[&<>"'`=/]/g, function fromEntityMap(s) {
26
+ return FormValidator.entityMap[s]
27
+ })
28
+ };
29
+
30
+ FormValidator.prototype.resetTitle = function () {
31
+ document.title = this.originalTitle;
32
+ };
33
+
34
+ FormValidator.prototype.updateTitle = function () {
35
+ document.title = `${this.errors.length} errors - ${document.title}`;
36
+ };
37
+
38
+ FormValidator.prototype.showSummary = function () {
39
+ this.summary.html(this.getSummaryHtml());
40
+ this.summary.removeClass('moj-hidden');
41
+ this.summary.attr('aria-labelledby', 'errorSummary-heading');
42
+ this.summary.focus();
43
+ };
44
+
45
+ FormValidator.prototype.getSummaryHtml = function () {
46
+ let html =
47
+ '<h2 id="error-summary-title" class="govuk-error-summary__title">There is a problem</h2>';
48
+ html += '<div class="govuk-error-summary__body">';
49
+ html += '<ul class="govuk-list govuk-error-summary__list">';
50
+ for (let i = 0, j = this.errors.length; i < j; i++) {
51
+ const error = this.errors[i];
52
+ html += '<li>';
53
+ html += `<a href="#${this.escapeHtml(error.fieldName)}">`;
54
+ html += this.escapeHtml(error.message);
55
+ html += '</a>';
56
+ html += '</li>';
57
+ }
58
+ html += '</ul>';
59
+ html += '</div>';
60
+ return html
61
+ };
62
+
63
+ FormValidator.prototype.hideSummary = function () {
64
+ this.summary.addClass('moj-hidden');
65
+ this.summary.removeAttr('aria-labelledby');
66
+ };
67
+
68
+ FormValidator.prototype.onSubmit = function (e) {
69
+ this.removeInlineErrors();
70
+ this.hideSummary();
71
+ this.resetTitle();
72
+ if (!this.validate()) {
73
+ e.preventDefault();
74
+ this.updateTitle();
75
+ this.showSummary();
76
+ this.showInlineErrors();
77
+ }
78
+ };
79
+
80
+ FormValidator.prototype.showInlineErrors = function () {
81
+ for (let i = 0, j = this.errors.length; i < j; i++) {
82
+ this.showInlineError(this.errors[i]);
83
+ }
84
+ };
85
+
86
+ FormValidator.prototype.showInlineError = function (error) {
87
+ const errorSpanId = `${error.fieldName}-error`;
88
+ const errorSpan = `<span class="govuk-error-message" id="${
89
+ errorSpanId
90
+ }">${this.escapeHtml(error.message)}</span>`;
91
+ const control = window.jQuery(`#${error.fieldName}`);
92
+ const fieldContainer = control.parents('.govuk-form-group');
93
+ const label = fieldContainer.find('label');
94
+ const legend = fieldContainer.find('legend');
95
+ const fieldset = fieldContainer.find('fieldset');
96
+ fieldContainer.addClass('govuk-form-group--error');
97
+ if (legend.length) {
98
+ legend.after(errorSpan);
99
+ fieldContainer.attr('aria-invalid', 'true');
100
+ addAttributeValue(fieldset[0], 'aria-describedby', errorSpanId);
101
+ } else {
102
+ label.after(errorSpan);
103
+ control.attr('aria-invalid', 'true');
104
+ addAttributeValue(control[0], 'aria-describedby', errorSpanId);
105
+ }
106
+ };
107
+
108
+ FormValidator.prototype.removeInlineErrors = function () {
109
+ for (let i = 0; i < this.errors.length; i++) {
110
+ this.removeInlineError(this.errors[i]);
111
+ }
112
+ };
113
+
114
+ FormValidator.prototype.removeInlineError = function (error) {
115
+ const control = window.jQuery(`#${error.fieldName}`);
116
+ const fieldContainer = control.parents('.govuk-form-group');
117
+ fieldContainer.find('.govuk-error-message').remove();
118
+ fieldContainer.removeClass('govuk-form-group--error');
119
+ fieldContainer.find('[aria-invalid]').attr('aria-invalid', 'false');
120
+ const errorSpanId = `${error.fieldName}-error`;
121
+ removeAttributeValue(
122
+ fieldContainer.find('[aria-describedby]')[0],
123
+ 'aria-describedby',
124
+ errorSpanId
125
+ );
126
+ };
127
+
128
+ FormValidator.prototype.addValidator = function (fieldName, rules) {
129
+ this.validators.push({
130
+ fieldName,
131
+ rules,
132
+ field: this.form.elements[fieldName]
133
+ });
134
+ };
135
+
136
+ FormValidator.prototype.validate = function () {
137
+ this.errors = [];
138
+ let validator = null;
139
+ let validatorReturnValue = true;
140
+ let i;
141
+ let j;
142
+ for (i = 0; i < this.validators.length; i++) {
143
+ validator = this.validators[i];
144
+ for (j = 0; j < validator.rules.length; j++) {
145
+ validatorReturnValue = validator.rules[j].method(
146
+ validator.field,
147
+ validator.rules[j].params
148
+ );
149
+
150
+ if (typeof validatorReturnValue === 'boolean' && !validatorReturnValue) {
151
+ this.errors.push({
152
+ fieldName: validator.fieldName,
153
+ message: validator.rules[j].message
154
+ });
155
+ break
156
+ } else if (typeof validatorReturnValue === 'string') {
157
+ this.errors.push({
158
+ fieldName: validatorReturnValue,
159
+ message: validator.rules[j].message
160
+ });
161
+ break
162
+ }
163
+ }
164
+ }
165
+ return this.errors.length === 0
166
+ };
167
+
168
+ export { FormValidator };