@ministryofjustice/frontend 3.4.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.
- package/moj/all.jquery.js +13378 -0
- package/moj/all.jquery.min.js +1 -144
- package/moj/all.js +2266 -2551
- package/moj/all.mjs +126 -0
- package/moj/components/add-another/add-another.js +110 -100
- package/moj/components/add-another/add-another.mjs +106 -0
- package/moj/components/alert/alert.js +319 -211
- package/moj/components/alert/alert.mjs +251 -0
- package/moj/components/alert/alert.spec.helper.js +12 -5
- package/moj/components/alert/alert.spec.helper.mjs +66 -0
- package/moj/components/button-menu/button-menu.js +302 -292
- package/moj/components/button-menu/button-menu.mjs +329 -0
- package/moj/components/date-picker/date-picker.js +850 -842
- package/moj/components/date-picker/date-picker.mjs +961 -0
- package/moj/components/filter-toggle-button/filter-toggle-button.js +98 -88
- package/moj/components/filter-toggle-button/filter-toggle-button.mjs +93 -0
- package/moj/components/form-validator/form-validator.js +195 -155
- package/moj/components/form-validator/form-validator.mjs +168 -0
- package/moj/components/multi-file-upload/multi-file-upload.js +158 -137
- package/moj/components/multi-file-upload/multi-file-upload.mjs +219 -0
- package/moj/components/multi-select/multi-select.js +75 -65
- package/moj/components/multi-select/multi-select.mjs +77 -0
- package/moj/components/password-reveal/password-reveal.js +40 -30
- package/moj/components/password-reveal/password-reveal.mjs +35 -0
- package/moj/components/rich-text-editor/rich-text-editor.js +92 -80
- package/moj/components/rich-text-editor/rich-text-editor.mjs +157 -0
- package/moj/components/search-toggle/search-toggle.js +55 -45
- package/moj/components/search-toggle/search-toggle.mjs +54 -0
- package/moj/components/sortable-table/sortable-table.js +141 -141
- package/moj/components/sortable-table/sortable-table.mjs +138 -0
- package/moj/helpers/_links.scss +1 -1
- package/moj/helpers.js +171 -152
- package/moj/helpers.mjs +123 -0
- package/moj/moj-frontend.min.js +1 -144
- package/moj/version.js +11 -1
- package/moj/version.mjs +3 -0
- package/package.json +13 -1
- package/moj/all.spec.js +0 -24
- package/moj/components/add-another/add-another.spec.js +0 -165
- package/moj/components/alert/alert.spec.js +0 -229
- package/moj/components/button-menu/button-menu.spec.js +0 -360
- package/moj/components/date-picker/date-picker.spec.js +0 -1178
- package/moj/components/filter-toggle-button/filter-toggle-button.spec.js +0 -302
- package/moj/components/multi-file-upload/multi-file-upload.spec.js +0 -510
- package/moj/components/multi-select/multi-select.spec.js +0 -128
- package/moj/components/password-reveal/password-reveal.spec.js +0 -57
- package/moj/components/search-toggle/search-toggle.spec.js +0 -129
- package/moj/components/sortable-table/sortable-table.spec.js +0 -362
- package/moj/helpers.spec.js +0 -235
- package/moj/namespace.js +0 -2
|
@@ -1,91 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.createToggleButton()
|
|
7
|
-
this.setupResponsiveChecks()
|
|
8
|
-
this.filterContainer.attr('tabindex', '-1')
|
|
9
|
-
if (this.options.startHidden) {
|
|
10
|
-
this.hideMenu()
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
MOJFrontend.FilterToggleButton.prototype.setupResponsiveChecks = function () {
|
|
15
|
-
this.mq = window.matchMedia(this.options.bigModeMediaQuery)
|
|
16
|
-
this.mq.addListener($.proxy(this, 'checkMode'))
|
|
17
|
-
this.checkMode(this.mq)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
MOJFrontend.FilterToggleButton.prototype.createToggleButton = function () {
|
|
21
|
-
this.menuButton = $(
|
|
22
|
-
`<button class="govuk-button ${this.options.toggleButton.classes}" type="button" aria-haspopup="true" aria-expanded="false">${this.options.toggleButton.showText}</button>`
|
|
23
|
-
)
|
|
24
|
-
this.menuButton.on('click', $.proxy(this, 'onMenuButtonClick'))
|
|
25
|
-
this.container.append(this.menuButton)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
MOJFrontend.FilterToggleButton.prototype.checkMode = function (mq) {
|
|
29
|
-
if (mq.matches) {
|
|
30
|
-
this.enableBigMode()
|
|
31
|
-
} else {
|
|
32
|
-
this.enableSmallMode()
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
MOJFrontend.FilterToggleButton.prototype.enableBigMode = function () {
|
|
37
|
-
this.showMenu()
|
|
38
|
-
this.removeCloseButton()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
MOJFrontend.FilterToggleButton.prototype.enableSmallMode = function () {
|
|
42
|
-
this.hideMenu()
|
|
43
|
-
this.addCloseButton()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
MOJFrontend.FilterToggleButton.prototype.addCloseButton = function () {
|
|
47
|
-
if (this.options.closeButton) {
|
|
48
|
-
this.closeButton = $(
|
|
49
|
-
`<button class="moj-filter__close" type="button">${this.options.closeButton.text}</button>`
|
|
50
|
-
)
|
|
51
|
-
this.closeButton.on('click', $.proxy(this, 'onCloseClick'))
|
|
52
|
-
$(this.options.closeButton.container).append(this.closeButton)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
1
|
+
(function (global, factory) {
|
|
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';
|
|
55
6
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
7
|
+
function FilterToggleButton(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
this.container = window.jQuery(this.options.toggleButton.container);
|
|
10
|
+
this.filterContainer = window.jQuery(this.options.filter.container);
|
|
60
11
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this.
|
|
64
|
-
this.
|
|
12
|
+
this.createToggleButton();
|
|
13
|
+
this.setupResponsiveChecks();
|
|
14
|
+
this.filterContainer.attr('tabindex', '-1');
|
|
15
|
+
if (this.options.startHidden) {
|
|
16
|
+
this.hideMenu();
|
|
17
|
+
}
|
|
65
18
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
19
|
+
|
|
20
|
+
FilterToggleButton.prototype.setupResponsiveChecks = function () {
|
|
21
|
+
this.mq = window.matchMedia(this.options.bigModeMediaQuery);
|
|
22
|
+
this.mq.addListener(window.jQuery.proxy(this, 'checkMode'));
|
|
23
|
+
this.checkMode(this.mq);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
FilterToggleButton.prototype.createToggleButton = function () {
|
|
27
|
+
this.menuButton = window.jQuery(
|
|
28
|
+
`<button class="govuk-button ${this.options.toggleButton.classes}" type="button" aria-haspopup="true" aria-expanded="false">${this.options.toggleButton.showText}</button>`
|
|
29
|
+
);
|
|
30
|
+
this.menuButton.on('click', window.jQuery.proxy(this, 'onMenuButtonClick'));
|
|
31
|
+
this.container.append(this.menuButton);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
FilterToggleButton.prototype.checkMode = function (mq) {
|
|
35
|
+
if (mq.matches) {
|
|
36
|
+
this.enableBigMode();
|
|
37
|
+
} else {
|
|
38
|
+
this.enableSmallMode();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
FilterToggleButton.prototype.enableBigMode = function () {
|
|
43
|
+
this.showMenu();
|
|
44
|
+
this.removeCloseButton();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
FilterToggleButton.prototype.enableSmallMode = function () {
|
|
48
|
+
this.hideMenu();
|
|
49
|
+
this.addCloseButton();
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
FilterToggleButton.prototype.addCloseButton = function () {
|
|
53
|
+
if (this.options.closeButton) {
|
|
54
|
+
this.closeButton = window.jQuery(
|
|
55
|
+
`<button class="moj-filter__close" type="button">${this.options.closeButton.text}</button>`
|
|
56
|
+
);
|
|
57
|
+
this.closeButton.on('click', window.jQuery.proxy(this, 'onCloseClick'));
|
|
58
|
+
window.jQuery(this.options.closeButton.container).append(this.closeButton);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
FilterToggleButton.prototype.onCloseClick = function () {
|
|
63
|
+
this.hideMenu();
|
|
64
|
+
this.menuButton.focus();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
FilterToggleButton.prototype.removeCloseButton = function () {
|
|
68
|
+
if (this.closeButton) {
|
|
69
|
+
this.closeButton.remove();
|
|
70
|
+
this.closeButton = null;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
FilterToggleButton.prototype.hideMenu = function () {
|
|
75
|
+
this.menuButton.attr('aria-expanded', 'false');
|
|
76
|
+
this.filterContainer.addClass('moj-js-hidden');
|
|
77
|
+
this.menuButton.text(this.options.toggleButton.showText);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
FilterToggleButton.prototype.showMenu = function () {
|
|
81
|
+
this.menuButton.attr('aria-expanded', 'true');
|
|
82
|
+
this.filterContainer.removeClass('moj-js-hidden');
|
|
83
|
+
this.menuButton.text(this.options.toggleButton.hideText);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
FilterToggleButton.prototype.onMenuButtonClick = function () {
|
|
87
|
+
this.toggle();
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
FilterToggleButton.prototype.toggle = function () {
|
|
91
|
+
if (this.menuButton.attr('aria-expanded') === 'false') {
|
|
92
|
+
this.showMenu();
|
|
93
|
+
this.filterContainer.get(0).focus();
|
|
94
|
+
} else {
|
|
95
|
+
this.hideMenu();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
exports.FilterToggleButton = FilterToggleButton;
|
|
100
|
+
|
|
101
|
+
}));
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
function FilterToggleButton(options) {
|
|
2
|
+
this.options = options;
|
|
3
|
+
this.container = window.jQuery(this.options.toggleButton.container);
|
|
4
|
+
this.filterContainer = window.jQuery(this.options.filter.container);
|
|
5
|
+
|
|
6
|
+
this.createToggleButton();
|
|
7
|
+
this.setupResponsiveChecks();
|
|
8
|
+
this.filterContainer.attr('tabindex', '-1');
|
|
9
|
+
if (this.options.startHidden) {
|
|
10
|
+
this.hideMenu();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
FilterToggleButton.prototype.setupResponsiveChecks = function () {
|
|
15
|
+
this.mq = window.matchMedia(this.options.bigModeMediaQuery);
|
|
16
|
+
this.mq.addListener(window.jQuery.proxy(this, 'checkMode'));
|
|
17
|
+
this.checkMode(this.mq);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
FilterToggleButton.prototype.createToggleButton = function () {
|
|
21
|
+
this.menuButton = window.jQuery(
|
|
22
|
+
`<button class="govuk-button ${this.options.toggleButton.classes}" type="button" aria-haspopup="true" aria-expanded="false">${this.options.toggleButton.showText}</button>`
|
|
23
|
+
);
|
|
24
|
+
this.menuButton.on('click', window.jQuery.proxy(this, 'onMenuButtonClick'));
|
|
25
|
+
this.container.append(this.menuButton);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
FilterToggleButton.prototype.checkMode = function (mq) {
|
|
29
|
+
if (mq.matches) {
|
|
30
|
+
this.enableBigMode();
|
|
31
|
+
} else {
|
|
32
|
+
this.enableSmallMode();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
FilterToggleButton.prototype.enableBigMode = function () {
|
|
37
|
+
this.showMenu();
|
|
38
|
+
this.removeCloseButton();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
FilterToggleButton.prototype.enableSmallMode = function () {
|
|
42
|
+
this.hideMenu();
|
|
43
|
+
this.addCloseButton();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
FilterToggleButton.prototype.addCloseButton = function () {
|
|
47
|
+
if (this.options.closeButton) {
|
|
48
|
+
this.closeButton = window.jQuery(
|
|
49
|
+
`<button class="moj-filter__close" type="button">${this.options.closeButton.text}</button>`
|
|
50
|
+
);
|
|
51
|
+
this.closeButton.on('click', window.jQuery.proxy(this, 'onCloseClick'));
|
|
52
|
+
window.jQuery(this.options.closeButton.container).append(this.closeButton);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
FilterToggleButton.prototype.onCloseClick = function () {
|
|
57
|
+
this.hideMenu();
|
|
58
|
+
this.menuButton.focus();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
FilterToggleButton.prototype.removeCloseButton = function () {
|
|
62
|
+
if (this.closeButton) {
|
|
63
|
+
this.closeButton.remove();
|
|
64
|
+
this.closeButton = null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
FilterToggleButton.prototype.hideMenu = function () {
|
|
69
|
+
this.menuButton.attr('aria-expanded', 'false');
|
|
70
|
+
this.filterContainer.addClass('moj-js-hidden');
|
|
71
|
+
this.menuButton.text(this.options.toggleButton.showText);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
FilterToggleButton.prototype.showMenu = function () {
|
|
75
|
+
this.menuButton.attr('aria-expanded', 'true');
|
|
76
|
+
this.filterContainer.removeClass('moj-js-hidden');
|
|
77
|
+
this.menuButton.text(this.options.toggleButton.hideText);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
FilterToggleButton.prototype.onMenuButtonClick = function () {
|
|
81
|
+
this.toggle();
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
FilterToggleButton.prototype.toggle = function () {
|
|
85
|
+
if (this.menuButton.attr('aria-expanded') === 'false') {
|
|
86
|
+
this.showMenu();
|
|
87
|
+
this.filterContainer.get(0).focus();
|
|
88
|
+
} else {
|
|
89
|
+
this.hideMenu();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export { FilterToggleButton };
|
|
@@ -1,164 +1,204 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.summary =
|
|
7
|
-
options && options.summary ? $(options.summary) : $('.govuk-error-summary')
|
|
8
|
-
this.originalTitle = document.title
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
MOJFrontend.FormValidator.entityMap = {
|
|
12
|
-
'&': '&',
|
|
13
|
-
'<': '<',
|
|
14
|
-
'>': '>',
|
|
15
|
-
'"': '"',
|
|
16
|
-
"'": ''',
|
|
17
|
-
'/': '/',
|
|
18
|
-
'`': '`',
|
|
19
|
-
'=': '='
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
MOJFrontend.FormValidator.prototype.escapeHtml = function (string) {
|
|
23
|
-
return String(string).replace(/[&<>"'`=/]/g, function fromEntityMap(s) {
|
|
24
|
-
return MOJFrontend.FormValidator.entityMap[s]
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
MOJFrontend.FormValidator.prototype.resetTitle = function () {
|
|
29
|
-
document.title = this.originalTitle
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
MOJFrontend.FormValidator.prototype.updateTitle = function () {
|
|
33
|
-
document.title = `${this.errors.length} errors - ${document.title}`
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
MOJFrontend.FormValidator.prototype.showSummary = function () {
|
|
37
|
-
this.summary.html(this.getSummaryHtml())
|
|
38
|
-
this.summary.removeClass('moj-hidden')
|
|
39
|
-
this.summary.attr('aria-labelledby', 'errorSummary-heading')
|
|
40
|
-
this.summary.focus()
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
MOJFrontend.FormValidator.prototype.getSummaryHtml = function () {
|
|
44
|
-
let html =
|
|
45
|
-
'<h2 id="error-summary-title" class="govuk-error-summary__title">There is a problem</h2>'
|
|
46
|
-
html += '<div class="govuk-error-summary__body">'
|
|
47
|
-
html += '<ul class="govuk-list govuk-error-summary__list">'
|
|
48
|
-
for (let i = 0, j = this.errors.length; i < j; i++) {
|
|
49
|
-
const error = this.errors[i]
|
|
50
|
-
html += '<li>'
|
|
51
|
-
html += `<a href="#${this.escapeHtml(error.fieldName)}">`
|
|
52
|
-
html += this.escapeHtml(error.message)
|
|
53
|
-
html += '</a>'
|
|
54
|
-
html += '</li>'
|
|
55
|
-
}
|
|
56
|
-
html += '</ul>'
|
|
57
|
-
html += '</div>'
|
|
58
|
-
return html
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
MOJFrontend.FormValidator.prototype.hideSummary = function () {
|
|
62
|
-
this.summary.addClass('moj-hidden')
|
|
63
|
-
this.summary.removeAttr('aria-labelledby')
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
MOJFrontend.FormValidator.prototype.onSubmit = function (e) {
|
|
67
|
-
this.removeInlineErrors()
|
|
68
|
-
this.hideSummary()
|
|
69
|
-
this.resetTitle()
|
|
70
|
-
if (!this.validate()) {
|
|
71
|
-
e.preventDefault()
|
|
72
|
-
this.updateTitle()
|
|
73
|
-
this.showSummary()
|
|
74
|
-
this.showInlineErrors()
|
|
75
|
-
}
|
|
76
|
-
}
|
|
1
|
+
(function (global, factory) {
|
|
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';
|
|
77
6
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
7
|
+
function removeAttributeValue(el, attr, value) {
|
|
8
|
+
let re, m;
|
|
9
|
+
if (el.getAttribute(attr)) {
|
|
10
|
+
if (el.getAttribute(attr) === value) {
|
|
11
|
+
el.removeAttribute(attr);
|
|
12
|
+
} else {
|
|
13
|
+
re = new RegExp(`(^|\\s)${value}(\\s|$)`);
|
|
14
|
+
m = el.getAttribute(attr).match(re);
|
|
15
|
+
if (m && m.length === 3) {
|
|
16
|
+
el.setAttribute(
|
|
17
|
+
attr,
|
|
18
|
+
el.getAttribute(attr).replace(re, m[1] && m[2] ? ' ' : '')
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
81
23
|
}
|
|
82
|
-
}
|
|
83
24
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
fieldContainer.addClass('govuk-form-group--error')
|
|
95
|
-
if (legend.length) {
|
|
96
|
-
legend.after(errorSpan)
|
|
97
|
-
fieldContainer.attr('aria-invalid', 'true')
|
|
98
|
-
MOJFrontend.addAttributeValue(fieldset[0], 'aria-describedby', errorSpanId)
|
|
99
|
-
} else {
|
|
100
|
-
label.after(errorSpan)
|
|
101
|
-
control.attr('aria-invalid', 'true')
|
|
102
|
-
MOJFrontend.addAttributeValue(control[0], 'aria-describedby', errorSpanId)
|
|
25
|
+
function addAttributeValue(el, attr, value) {
|
|
26
|
+
let re;
|
|
27
|
+
if (!el.getAttribute(attr)) {
|
|
28
|
+
el.setAttribute(attr, value);
|
|
29
|
+
} else {
|
|
30
|
+
re = new RegExp(`(^|\\s)${value}(\\s|$)`);
|
|
31
|
+
if (!re.test(el.getAttribute(attr))) {
|
|
32
|
+
el.setAttribute(attr, `${el.getAttribute(attr)} ${value}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
103
35
|
}
|
|
104
|
-
}
|
|
105
36
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this.
|
|
37
|
+
function FormValidator(form, options) {
|
|
38
|
+
this.form = form;
|
|
39
|
+
this.errors = [];
|
|
40
|
+
this.validators = [];
|
|
41
|
+
window.jQuery(this.form).on('submit', window.jQuery.proxy(this, 'onSubmit'));
|
|
42
|
+
this.summary =
|
|
43
|
+
options && options.summary ? window.jQuery(options.summary) : window.jQuery('.govuk-error-summary');
|
|
44
|
+
this.originalTitle = document.title;
|
|
109
45
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
46
|
+
|
|
47
|
+
FormValidator.entityMap = {
|
|
48
|
+
'&': '&',
|
|
49
|
+
'<': '<',
|
|
50
|
+
'>': '>',
|
|
51
|
+
'"': '"',
|
|
52
|
+
"'": ''',
|
|
53
|
+
'/': '/',
|
|
54
|
+
'`': '`',
|
|
55
|
+
'=': '='
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
FormValidator.prototype.escapeHtml = function (string) {
|
|
59
|
+
return String(string).replace(/[&<>"'`=/]/g, function fromEntityMap(s) {
|
|
60
|
+
return FormValidator.entityMap[s]
|
|
61
|
+
})
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
FormValidator.prototype.resetTitle = function () {
|
|
65
|
+
document.title = this.originalTitle;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
FormValidator.prototype.updateTitle = function () {
|
|
69
|
+
document.title = `${this.errors.length} errors - ${document.title}`;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
FormValidator.prototype.showSummary = function () {
|
|
73
|
+
this.summary.html(this.getSummaryHtml());
|
|
74
|
+
this.summary.removeClass('moj-hidden');
|
|
75
|
+
this.summary.attr('aria-labelledby', 'errorSummary-heading');
|
|
76
|
+
this.summary.focus();
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
FormValidator.prototype.getSummaryHtml = function () {
|
|
80
|
+
let html =
|
|
81
|
+
'<h2 id="error-summary-title" class="govuk-error-summary__title">There is a problem</h2>';
|
|
82
|
+
html += '<div class="govuk-error-summary__body">';
|
|
83
|
+
html += '<ul class="govuk-list govuk-error-summary__list">';
|
|
84
|
+
for (let i = 0, j = this.errors.length; i < j; i++) {
|
|
85
|
+
const error = this.errors[i];
|
|
86
|
+
html += '<li>';
|
|
87
|
+
html += `<a href="#${this.escapeHtml(error.fieldName)}">`;
|
|
88
|
+
html += this.escapeHtml(error.message);
|
|
89
|
+
html += '</a>';
|
|
90
|
+
html += '</li>';
|
|
91
|
+
}
|
|
92
|
+
html += '</ul>';
|
|
93
|
+
html += '</div>';
|
|
94
|
+
return html
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
FormValidator.prototype.hideSummary = function () {
|
|
98
|
+
this.summary.addClass('moj-hidden');
|
|
99
|
+
this.summary.removeAttr('aria-labelledby');
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
FormValidator.prototype.onSubmit = function (e) {
|
|
103
|
+
this.removeInlineErrors();
|
|
104
|
+
this.hideSummary();
|
|
105
|
+
this.resetTitle();
|
|
106
|
+
if (!this.validate()) {
|
|
107
|
+
e.preventDefault();
|
|
108
|
+
this.updateTitle();
|
|
109
|
+
this.showSummary();
|
|
110
|
+
this.showInlineErrors();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
FormValidator.prototype.showInlineErrors = function () {
|
|
115
|
+
for (let i = 0, j = this.errors.length; i < j; i++) {
|
|
116
|
+
this.showInlineError(this.errors[i]);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
FormValidator.prototype.showInlineError = function (error) {
|
|
121
|
+
const errorSpanId = `${error.fieldName}-error`;
|
|
122
|
+
const errorSpan = `<span class="govuk-error-message" id="${
|
|
122
123
|
errorSpanId
|
|
123
|
-
)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
for (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
124
|
+
}">${this.escapeHtml(error.message)}</span>`;
|
|
125
|
+
const control = window.jQuery(`#${error.fieldName}`);
|
|
126
|
+
const fieldContainer = control.parents('.govuk-form-group');
|
|
127
|
+
const label = fieldContainer.find('label');
|
|
128
|
+
const legend = fieldContainer.find('legend');
|
|
129
|
+
const fieldset = fieldContainer.find('fieldset');
|
|
130
|
+
fieldContainer.addClass('govuk-form-group--error');
|
|
131
|
+
if (legend.length) {
|
|
132
|
+
legend.after(errorSpan);
|
|
133
|
+
fieldContainer.attr('aria-invalid', 'true');
|
|
134
|
+
addAttributeValue(fieldset[0], 'aria-describedby', errorSpanId);
|
|
135
|
+
} else {
|
|
136
|
+
label.after(errorSpan);
|
|
137
|
+
control.attr('aria-invalid', 'true');
|
|
138
|
+
addAttributeValue(control[0], 'aria-describedby', errorSpanId);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
FormValidator.prototype.removeInlineErrors = function () {
|
|
143
|
+
for (let i = 0; i < this.errors.length; i++) {
|
|
144
|
+
this.removeInlineError(this.errors[i]);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
FormValidator.prototype.removeInlineError = function (error) {
|
|
149
|
+
const control = window.jQuery(`#${error.fieldName}`);
|
|
150
|
+
const fieldContainer = control.parents('.govuk-form-group');
|
|
151
|
+
fieldContainer.find('.govuk-error-message').remove();
|
|
152
|
+
fieldContainer.removeClass('govuk-form-group--error');
|
|
153
|
+
fieldContainer.find('[aria-invalid]').attr('aria-invalid', 'false');
|
|
154
|
+
const errorSpanId = `${error.fieldName}-error`;
|
|
155
|
+
removeAttributeValue(
|
|
156
|
+
fieldContainer.find('[aria-describedby]')[0],
|
|
157
|
+
'aria-describedby',
|
|
158
|
+
errorSpanId
|
|
159
|
+
);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
FormValidator.prototype.addValidator = function (fieldName, rules) {
|
|
163
|
+
this.validators.push({
|
|
164
|
+
fieldName,
|
|
165
|
+
rules,
|
|
166
|
+
field: this.form.elements[fieldName]
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
FormValidator.prototype.validate = function () {
|
|
171
|
+
this.errors = [];
|
|
172
|
+
let validator = null;
|
|
173
|
+
let validatorReturnValue = true;
|
|
174
|
+
let i;
|
|
175
|
+
let j;
|
|
176
|
+
for (i = 0; i < this.validators.length; i++) {
|
|
177
|
+
validator = this.validators[i];
|
|
178
|
+
for (j = 0; j < validator.rules.length; j++) {
|
|
179
|
+
validatorReturnValue = validator.rules[j].method(
|
|
180
|
+
validator.field,
|
|
181
|
+
validator.rules[j].params
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
if (typeof validatorReturnValue === 'boolean' && !validatorReturnValue) {
|
|
185
|
+
this.errors.push({
|
|
186
|
+
fieldName: validator.fieldName,
|
|
187
|
+
message: validator.rules[j].message
|
|
188
|
+
});
|
|
189
|
+
break
|
|
190
|
+
} else if (typeof validatorReturnValue === 'string') {
|
|
191
|
+
this.errors.push({
|
|
192
|
+
fieldName: validatorReturnValue,
|
|
193
|
+
message: validator.rules[j].message
|
|
194
|
+
});
|
|
195
|
+
break
|
|
196
|
+
}
|
|
160
197
|
}
|
|
161
198
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
199
|
+
return this.errors.length === 0
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
exports.FormValidator = FormValidator;
|
|
203
|
+
|
|
204
|
+
}));
|