@ministryofjustice/frontend 4.0.1 → 5.1.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/govuk-prototype-kit.config.json +19 -4
- package/moj/_base.scss +2 -0
- package/moj/_base.scss.map +1 -0
- package/moj/all.bundle.js +3010 -0
- package/moj/all.bundle.js.map +1 -0
- package/moj/all.bundle.mjs +3293 -0
- package/moj/all.bundle.mjs.map +1 -0
- package/moj/all.mjs +17 -110
- package/moj/all.mjs.map +1 -1
- package/moj/all.scss +3 -0
- package/moj/all.scss.map +1 -0
- package/moj/common/index.mjs +57 -0
- package/moj/common/index.mjs.map +1 -0
- package/moj/common/moj-frontend-version.mjs +14 -0
- package/moj/common/moj-frontend-version.mjs.map +1 -0
- package/moj/components/_all.scss +2 -0
- package/moj/components/_all.scss.map +1 -0
- package/moj/components/action-bar/_action-bar.scss +2 -0
- package/moj/components/action-bar/_action-bar.scss.map +1 -0
- package/moj/components/add-another/_add-another.scss +2 -0
- package/moj/components/add-another/_add-another.scss.map +1 -0
- package/moj/components/add-another/add-another.bundle.js +157 -0
- package/moj/components/add-another/add-another.bundle.js.map +1 -0
- package/moj/components/add-another/add-another.bundle.mjs +271 -0
- package/moj/components/add-another/add-another.bundle.mjs.map +1 -0
- package/moj/components/add-another/add-another.mjs +135 -91
- package/moj/components/add-another/add-another.mjs.map +1 -1
- package/moj/components/alert/_alert.scss +4 -0
- package/moj/components/alert/_alert.scss.map +1 -0
- package/moj/components/alert/alert.bundle.js +254 -0
- package/moj/components/alert/alert.bundle.js.map +1 -0
- package/moj/components/alert/alert.bundle.mjs +490 -0
- package/moj/components/alert/alert.bundle.mjs.map +1 -0
- package/moj/components/alert/alert.mjs +97 -218
- package/moj/components/alert/alert.mjs.map +1 -1
- package/moj/components/alert/{alert.spec.helper.js → alert.spec.helper.bundle.js} +1 -1
- package/moj/components/alert/alert.spec.helper.bundle.js.map +1 -0
- package/moj/components/alert/alert.spec.helper.bundle.mjs +67 -0
- package/moj/components/alert/alert.spec.helper.bundle.mjs.map +1 -0
- package/moj/components/alert/alert.spec.helper.mjs.map +1 -1
- package/moj/components/badge/_badge.scss +2 -0
- package/moj/components/badge/_badge.scss.map +1 -0
- package/moj/components/banner/_banner.scss +2 -0
- package/moj/components/banner/_banner.scss.map +1 -0
- package/moj/components/button-menu/README.md +12 -6
- package/moj/components/button-menu/_button-menu.scss +4 -1
- package/moj/components/button-menu/_button-menu.scss.map +1 -0
- package/moj/components/button-menu/button-menu.bundle.js +270 -0
- package/moj/components/button-menu/button-menu.bundle.js.map +1 -0
- package/moj/components/button-menu/button-menu.bundle.mjs +506 -0
- package/moj/components/button-menu/button-menu.bundle.mjs.map +1 -0
- package/moj/components/button-menu/button-menu.mjs +214 -280
- package/moj/components/button-menu/button-menu.mjs.map +1 -1
- package/moj/components/cookie-banner/_cookie-banner.scss +2 -0
- package/moj/components/cookie-banner/_cookie-banner.scss.map +1 -0
- package/moj/components/currency-input/_currency-input.scss +2 -0
- package/moj/components/currency-input/_currency-input.scss.map +1 -0
- package/moj/components/date-picker/_date-picker.scss +2 -0
- package/moj/components/date-picker/_date-picker.scss.map +1 -0
- package/moj/components/date-picker/date-picker.bundle.js +804 -0
- package/moj/components/date-picker/date-picker.bundle.js.map +1 -0
- package/moj/components/date-picker/date-picker.bundle.mjs +1040 -0
- package/moj/components/date-picker/date-picker.bundle.mjs.map +1 -0
- package/moj/components/date-picker/date-picker.mjs +663 -827
- package/moj/components/date-picker/date-picker.mjs.map +1 -1
- package/moj/components/filter/_filter.scss +2 -0
- package/moj/components/filter/_filter.scss.map +1 -0
- package/moj/components/filter/template.njk +1 -1
- package/moj/components/filter-toggle-button/filter-toggle-button.bundle.js +185 -0
- package/moj/components/filter-toggle-button/filter-toggle-button.bundle.js.map +1 -0
- package/moj/components/filter-toggle-button/filter-toggle-button.bundle.mjs +421 -0
- package/moj/components/filter-toggle-button/filter-toggle-button.bundle.mjs.map +1 -0
- package/moj/components/filter-toggle-button/filter-toggle-button.mjs +166 -81
- package/moj/components/filter-toggle-button/filter-toggle-button.mjs.map +1 -1
- package/moj/components/form-validator/form-validator.bundle.js +288 -0
- package/moj/components/form-validator/form-validator.bundle.js.map +1 -0
- package/moj/components/form-validator/form-validator.bundle.mjs +524 -0
- package/moj/components/form-validator/form-validator.bundle.mjs.map +1 -0
- package/moj/components/form-validator/form-validator.mjs +226 -149
- package/moj/components/form-validator/form-validator.mjs.map +1 -1
- package/moj/components/header/_header.scss +2 -0
- package/moj/components/header/_header.scss.map +1 -0
- package/moj/components/identity-bar/_identity-bar.scss +2 -0
- package/moj/components/identity-bar/_identity-bar.scss.map +1 -0
- package/moj/components/interruption-card/_interruption-card.scss +2 -0
- package/moj/components/interruption-card/_interruption-card.scss.map +1 -0
- package/moj/components/messages/_messages.scss +2 -0
- package/moj/components/messages/_messages.scss.map +1 -0
- package/moj/components/multi-file-upload/_multi-file-upload.scss +2 -0
- package/moj/components/multi-file-upload/_multi-file-upload.scss.map +1 -0
- package/moj/components/multi-file-upload/multi-file-upload.bundle.js +397 -0
- package/moj/components/multi-file-upload/multi-file-upload.bundle.js.map +1 -0
- package/moj/components/multi-file-upload/multi-file-upload.bundle.mjs +633 -0
- package/moj/components/multi-file-upload/multi-file-upload.bundle.mjs.map +1 -0
- package/moj/components/multi-file-upload/multi-file-upload.mjs +384 -213
- package/moj/components/multi-file-upload/multi-file-upload.mjs.map +1 -1
- package/moj/components/multi-file-upload/template.njk +1 -1
- package/moj/components/multi-select/_multi-select.scss +2 -0
- package/moj/components/multi-select/_multi-select.scss.map +1 -0
- package/moj/components/multi-select/multi-select.bundle.js +143 -0
- package/moj/components/multi-select/multi-select.bundle.js.map +1 -0
- package/moj/components/multi-select/multi-select.bundle.mjs +379 -0
- package/moj/components/multi-select/multi-select.bundle.mjs.map +1 -0
- package/moj/components/multi-select/multi-select.mjs +123 -64
- package/moj/components/multi-select/multi-select.mjs.map +1 -1
- package/moj/components/notification-badge/_notification-badge.scss +2 -0
- package/moj/components/notification-badge/_notification-badge.scss.map +1 -0
- package/moj/components/organisation-switcher/_organisation-switcher.scss +2 -0
- package/moj/components/organisation-switcher/_organisation-switcher.scss.map +1 -0
- package/moj/components/page-header-actions/_page-header-actions.scss +2 -0
- package/moj/components/page-header-actions/_page-header-actions.scss.map +1 -0
- package/moj/components/pagination/_pagination.scss +2 -2
- package/moj/components/pagination/_pagination.scss.map +1 -0
- package/moj/components/password-reveal/_password-reveal.scss +5 -1
- package/moj/components/password-reveal/_password-reveal.scss.map +1 -0
- package/moj/components/password-reveal/password-reveal.bundle.js +52 -0
- package/moj/components/password-reveal/password-reveal.bundle.js.map +1 -0
- package/moj/components/password-reveal/password-reveal.bundle.mjs +166 -0
- package/moj/components/password-reveal/password-reveal.bundle.mjs.map +1 -0
- package/moj/components/password-reveal/password-reveal.mjs +39 -29
- package/moj/components/password-reveal/password-reveal.mjs.map +1 -1
- package/moj/components/primary-navigation/_primary-navigation.scss +2 -0
- package/moj/components/primary-navigation/_primary-navigation.scss.map +1 -0
- package/moj/components/progress-bar/_progress-bar.scss +2 -0
- package/moj/components/progress-bar/_progress-bar.scss.map +1 -0
- package/moj/components/rich-text-editor/README.md +16 -9
- package/moj/components/rich-text-editor/_rich-text-editor.scss +2 -0
- package/moj/components/rich-text-editor/_rich-text-editor.scss.map +1 -0
- package/moj/components/rich-text-editor/rich-text-editor.bundle.js +210 -0
- package/moj/components/rich-text-editor/rich-text-editor.bundle.js.map +1 -0
- package/moj/components/rich-text-editor/rich-text-editor.bundle.mjs +446 -0
- package/moj/components/rich-text-editor/rich-text-editor.bundle.mjs.map +1 -0
- package/moj/components/rich-text-editor/rich-text-editor.mjs +186 -140
- package/moj/components/rich-text-editor/rich-text-editor.mjs.map +1 -1
- package/moj/components/search/_search.scss +2 -0
- package/moj/components/search/_search.scss.map +1 -0
- package/moj/components/search-toggle/{search-toggle.scss → _search-toggle.scss} +2 -0
- package/moj/components/search-toggle/_search-toggle.scss.map +1 -0
- package/moj/components/search-toggle/search-toggle.bundle.js +122 -0
- package/moj/components/search-toggle/search-toggle.bundle.js.map +1 -0
- package/moj/components/search-toggle/search-toggle.bundle.mjs +358 -0
- package/moj/components/search-toggle/search-toggle.bundle.mjs.map +1 -0
- package/moj/components/search-toggle/search-toggle.mjs +104 -43
- package/moj/components/search-toggle/search-toggle.mjs.map +1 -1
- package/moj/components/side-navigation/_side-navigation.scss +2 -0
- package/moj/components/side-navigation/_side-navigation.scss.map +1 -0
- package/moj/components/sortable-table/_sortable-table.scss +2 -2
- package/moj/components/sortable-table/_sortable-table.scss.map +1 -0
- package/moj/components/sortable-table/sortable-table.bundle.js +202 -0
- package/moj/components/sortable-table/sortable-table.bundle.js.map +1 -0
- package/moj/components/sortable-table/sortable-table.bundle.mjs +438 -0
- package/moj/components/sortable-table/sortable-table.bundle.mjs.map +1 -0
- package/moj/components/sortable-table/sortable-table.mjs +179 -122
- package/moj/components/sortable-table/sortable-table.mjs.map +1 -1
- package/moj/components/sub-navigation/_sub-navigation.scss +2 -0
- package/moj/components/sub-navigation/_sub-navigation.scss.map +1 -0
- package/moj/components/tag/_tag.scss +2 -0
- package/moj/components/tag/_tag.scss.map +1 -0
- package/moj/components/task-list/_task-list.scss +2 -0
- package/moj/components/task-list/_task-list.scss.map +1 -0
- package/moj/components/ticket-panel/_ticket-panel.scss +2 -0
- package/moj/components/ticket-panel/_ticket-panel.scss.map +1 -0
- package/moj/components/timeline/_timeline.scss +2 -0
- package/moj/components/timeline/_timeline.scss.map +1 -0
- package/moj/core/_all.scss +3 -0
- package/moj/core/_all.scss.map +1 -0
- package/moj/core/_moj-frontend-properties.scss +7 -0
- package/moj/core/_moj-frontend-properties.scss.map +1 -0
- package/moj/filters/all.js +44 -22
- package/moj/filters/prototype-kit-13-filters.js +4 -3
- package/moj/helpers/_all.scss +2 -0
- package/moj/helpers/_all.scss.map +1 -0
- package/moj/helpers/_hidden.scss +2 -0
- package/moj/helpers/_hidden.scss.map +1 -0
- package/moj/helpers/_links.scss +2 -0
- package/moj/helpers/_links.scss.map +1 -0
- package/moj/helpers.bundle.js +140 -0
- package/moj/helpers.bundle.js.map +1 -0
- package/moj/helpers.bundle.mjs +128 -0
- package/moj/helpers.bundle.mjs.map +1 -0
- package/moj/helpers.mjs +50 -77
- package/moj/helpers.mjs.map +1 -1
- package/moj/init.js +11 -2
- package/moj/moj-frontend.min.css +1 -1
- package/moj/moj-frontend.min.css.map +1 -1
- package/moj/moj-frontend.min.js +1 -1
- package/moj/moj-frontend.min.js.map +1 -1
- package/moj/objects/_all.scss +2 -0
- package/moj/objects/_all.scss.map +1 -0
- package/moj/objects/_button-group.scss +2 -0
- package/moj/objects/_button-group.scss.map +1 -0
- package/moj/objects/_filter-layout.scss +2 -0
- package/moj/objects/_filter-layout.scss.map +1 -0
- package/moj/objects/_scrollable-pane.scss +2 -0
- package/moj/objects/_scrollable-pane.scss.map +1 -0
- package/moj/objects/_width-container.scss +2 -0
- package/moj/objects/_width-container.scss.map +1 -0
- package/moj/settings/_all.scss +2 -0
- package/moj/settings/_all.scss.map +1 -0
- package/moj/settings/_assets.scss +2 -0
- package/moj/settings/_assets.scss.map +1 -0
- package/moj/settings/_colours.scss +2 -0
- package/moj/settings/_colours.scss.map +1 -0
- package/moj/settings/_measurements.scss +2 -0
- package/moj/settings/_measurements.scss.map +1 -0
- package/moj/settings/_typography.scss +2 -0
- package/moj/settings/_typography.scss.map +1 -0
- package/moj/template.njk +13 -0
- package/moj/utilities/_all.scss +2 -0
- package/moj/utilities/_all.scss.map +1 -0
- package/moj/utilities/_hidden.scss +2 -0
- package/moj/utilities/_hidden.scss.map +1 -0
- package/moj/utilities/_width-container.scss +2 -0
- package/moj/utilities/_width-container.scss.map +1 -0
- package/moj/vendor/govuk-frontend/_base.scss +2 -0
- package/moj/vendor/govuk-frontend/_base.scss.map +1 -0
- package/moj/vendor/govuk-frontend/_index.scss +2 -0
- package/moj/vendor/govuk-frontend/_index.scss.map +1 -0
- package/package.json +5 -6
- package/moj/all.jquery.min.js +0 -1
- package/moj/all.jquery.min.js.map +0 -1
- package/moj/all.js +0 -2662
- package/moj/all.js.map +0 -1
- package/moj/components/add-another/add-another.js +0 -115
- package/moj/components/add-another/add-another.js.map +0 -1
- package/moj/components/alert/alert.js +0 -356
- package/moj/components/alert/alert.js.map +0 -1
- package/moj/components/alert/alert.spec.helper.js.map +0 -1
- package/moj/components/button-menu/button-menu.js +0 -338
- package/moj/components/button-menu/button-menu.js.map +0 -1
- package/moj/components/date-picker/date-picker.js +0 -970
- package/moj/components/date-picker/date-picker.js.map +0 -1
- package/moj/components/filter-toggle-button/filter-toggle-button.js +0 -102
- package/moj/components/filter-toggle-button/filter-toggle-button.js.map +0 -1
- package/moj/components/form-validator/form-validator.js +0 -205
- package/moj/components/form-validator/form-validator.js.map +0 -1
- package/moj/components/multi-file-upload/multi-file-upload.js +0 -241
- package/moj/components/multi-file-upload/multi-file-upload.js.map +0 -1
- package/moj/components/multi-select/multi-select.js +0 -86
- package/moj/components/multi-select/multi-select.js.map +0 -1
- package/moj/components/password-reveal/password-reveal.js +0 -44
- package/moj/components/password-reveal/password-reveal.js.map +0 -1
- package/moj/components/rich-text-editor/rich-text-editor.js +0 -166
- package/moj/components/rich-text-editor/rich-text-editor.js.map +0 -1
- package/moj/components/search-toggle/search-toggle.js +0 -63
- package/moj/components/search-toggle/search-toggle.js.map +0 -1
- package/moj/components/sortable-table/sortable-table.js +0 -147
- package/moj/components/sortable-table/sortable-table.js.map +0 -1
- package/moj/helpers.js +0 -200
- package/moj/helpers.js.map +0 -1
- package/moj/vendor/html5shiv.js +0 -326
- package/moj/vendor/jquery.js +0 -9300
- package/moj/version.js +0 -12
- package/moj/version.js.map +0 -1
- package/moj/version.mjs +0 -4
- package/moj/version.mjs.map +0 -1
|
@@ -0,0 +1,804 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('govuk-frontend')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'govuk-frontend'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MOJFrontend = global.MOJFrontend || {}, global.GOVUKFrontend));
|
|
5
|
+
})(this, (function (exports, govukFrontend) { 'use strict';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @augments {ConfigurableComponent<DatePickerConfig>}
|
|
9
|
+
*/
|
|
10
|
+
class DatePicker extends govukFrontend.ConfigurableComponent {
|
|
11
|
+
/**
|
|
12
|
+
* @param {Element | null} $root - HTML element to use for date picker
|
|
13
|
+
* @param {DatePickerConfig} [config] - Date picker config
|
|
14
|
+
*/
|
|
15
|
+
constructor($root, config = {}) {
|
|
16
|
+
var _this$config$input$el;
|
|
17
|
+
super($root, config);
|
|
18
|
+
const $input = (_this$config$input$el = this.config.input.element) != null ? _this$config$input$el : this.$root.querySelector(this.config.input.selector);
|
|
19
|
+
if (!$input || !($input instanceof HTMLInputElement)) {
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
this.$input = $input;
|
|
23
|
+
this.dayLabels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
|
|
24
|
+
this.monthLabels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
|
|
25
|
+
this.currentDate = new Date();
|
|
26
|
+
this.currentDate.setHours(0, 0, 0, 0);
|
|
27
|
+
this.calendarDays = /** @type {DSCalendarDay[]} */[];
|
|
28
|
+
this.excludedDates = /** @type {Date[]} */[];
|
|
29
|
+
this.excludedDays = /** @type {number[]} */[];
|
|
30
|
+
this.buttonClass = 'moj-datepicker__button';
|
|
31
|
+
this.selectedDayButtonClass = 'moj-datepicker__button--selected';
|
|
32
|
+
this.currentDayButtonClass = 'moj-datepicker__button--current';
|
|
33
|
+
this.todayButtonClass = 'moj-datepicker__button--today';
|
|
34
|
+
this.setOptions();
|
|
35
|
+
this.initControls();
|
|
36
|
+
}
|
|
37
|
+
initControls() {
|
|
38
|
+
this.id = `datepicker-${this.$input.id}`;
|
|
39
|
+
this.$dialog = this.createDialog();
|
|
40
|
+
this.createCalendarHeaders();
|
|
41
|
+
const $componentWrapper = document.createElement('div');
|
|
42
|
+
const $inputWrapper = document.createElement('div');
|
|
43
|
+
$componentWrapper.classList.add('moj-datepicker__wrapper');
|
|
44
|
+
$inputWrapper.classList.add('govuk-input__wrapper');
|
|
45
|
+
this.$input.parentElement.insertBefore($componentWrapper, this.$input);
|
|
46
|
+
$componentWrapper.appendChild($inputWrapper);
|
|
47
|
+
$inputWrapper.appendChild(this.$input);
|
|
48
|
+
$inputWrapper.insertAdjacentHTML('beforeend', this.toggleTemplate());
|
|
49
|
+
$componentWrapper.insertAdjacentElement('beforeend', this.$dialog);
|
|
50
|
+
this.$calendarButton = /** @type {HTMLButtonElement} */
|
|
51
|
+
this.$root.querySelector('.moj-js-datepicker-toggle');
|
|
52
|
+
this.$dialogTitle = /** @type {HTMLHeadingElement} */
|
|
53
|
+
this.$dialog.querySelector('.moj-js-datepicker-month-year');
|
|
54
|
+
this.createCalendar();
|
|
55
|
+
this.$prevMonthButton = /** @type {HTMLButtonElement} */
|
|
56
|
+
this.$dialog.querySelector('.moj-js-datepicker-prev-month');
|
|
57
|
+
this.$prevYearButton = /** @type {HTMLButtonElement} */
|
|
58
|
+
this.$dialog.querySelector('.moj-js-datepicker-prev-year');
|
|
59
|
+
this.$nextMonthButton = /** @type {HTMLButtonElement} */
|
|
60
|
+
this.$dialog.querySelector('.moj-js-datepicker-next-month');
|
|
61
|
+
this.$nextYearButton = /** @type {HTMLButtonElement} */
|
|
62
|
+
this.$dialog.querySelector('.moj-js-datepicker-next-year');
|
|
63
|
+
this.$cancelButton = /** @type {HTMLButtonElement} */
|
|
64
|
+
this.$dialog.querySelector('.moj-js-datepicker-cancel');
|
|
65
|
+
this.$okButton = /** @type {HTMLButtonElement} */
|
|
66
|
+
this.$dialog.querySelector('.moj-js-datepicker-ok');
|
|
67
|
+
|
|
68
|
+
// add event listeners
|
|
69
|
+
this.$prevMonthButton.addEventListener('click', event => this.focusPreviousMonth(event, false));
|
|
70
|
+
this.$prevYearButton.addEventListener('click', event => this.focusPreviousYear(event, false));
|
|
71
|
+
this.$nextMonthButton.addEventListener('click', event => this.focusNextMonth(event, false));
|
|
72
|
+
this.$nextYearButton.addEventListener('click', event => this.focusNextYear(event, false));
|
|
73
|
+
this.$cancelButton.addEventListener('click', event => {
|
|
74
|
+
event.preventDefault();
|
|
75
|
+
this.closeDialog();
|
|
76
|
+
});
|
|
77
|
+
this.$okButton.addEventListener('click', () => {
|
|
78
|
+
this.selectDate(this.currentDate);
|
|
79
|
+
});
|
|
80
|
+
const $dialogButtons = this.$dialog.querySelectorAll('button:not([disabled="true"])');
|
|
81
|
+
this.$firstButtonInDialog = $dialogButtons[0];
|
|
82
|
+
this.$lastButtonInDialog = $dialogButtons[$dialogButtons.length - 1];
|
|
83
|
+
this.$firstButtonInDialog.addEventListener('keydown', event => this.firstButtonKeydown(event));
|
|
84
|
+
this.$lastButtonInDialog.addEventListener('keydown', event => this.lastButtonKeydown(event));
|
|
85
|
+
this.$calendarButton.addEventListener('click', event => this.toggleDialog(event));
|
|
86
|
+
this.$dialog.addEventListener('keydown', event => {
|
|
87
|
+
if (event.key === 'Escape') {
|
|
88
|
+
this.closeDialog();
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
event.stopPropagation();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
document.body.addEventListener('mouseup', event => this.backgroundClick(event));
|
|
94
|
+
|
|
95
|
+
// populates calendar with initial dates, avoids Wave errors about null buttons
|
|
96
|
+
this.updateCalendar();
|
|
97
|
+
}
|
|
98
|
+
createDialog() {
|
|
99
|
+
const titleId = `datepicker-title-${this.$input.id}`;
|
|
100
|
+
const $dialog = document.createElement('div');
|
|
101
|
+
$dialog.id = this.id;
|
|
102
|
+
$dialog.setAttribute('class', 'moj-datepicker__dialog');
|
|
103
|
+
$dialog.setAttribute('role', 'dialog');
|
|
104
|
+
$dialog.setAttribute('aria-modal', 'true');
|
|
105
|
+
$dialog.setAttribute('aria-labelledby', titleId);
|
|
106
|
+
$dialog.innerHTML = this.dialogTemplate(titleId);
|
|
107
|
+
$dialog.hidden = true;
|
|
108
|
+
return $dialog;
|
|
109
|
+
}
|
|
110
|
+
createCalendar() {
|
|
111
|
+
const $tbody = this.$dialog.querySelector('tbody');
|
|
112
|
+
let dayCount = 0;
|
|
113
|
+
for (let i = 0; i < 6; i++) {
|
|
114
|
+
// create row
|
|
115
|
+
const $row = $tbody.insertRow(i);
|
|
116
|
+
for (let j = 0; j < 7; j++) {
|
|
117
|
+
// create cell (day)
|
|
118
|
+
const $cell = document.createElement('td');
|
|
119
|
+
const $dateButton = document.createElement('button');
|
|
120
|
+
$cell.appendChild($dateButton);
|
|
121
|
+
$row.appendChild($cell);
|
|
122
|
+
const calendarDay = new DSCalendarDay($dateButton, dayCount, i, j, this);
|
|
123
|
+
this.calendarDays.push(calendarDay);
|
|
124
|
+
dayCount++;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
toggleTemplate() {
|
|
129
|
+
return `<button class="moj-datepicker__toggle moj-js-datepicker-toggle" type="button" aria-haspopup="dialog" aria-controls="${this.id}" aria-expanded="false">
|
|
130
|
+
<span class="govuk-visually-hidden">Choose date</span>
|
|
131
|
+
<svg width="32" height="24" focusable="false" class="moj-datepicker-icon" aria-hidden="true" role="img" viewBox="0 0 22 22">
|
|
132
|
+
<path
|
|
133
|
+
fill="currentColor"
|
|
134
|
+
fill-rule="evenodd"
|
|
135
|
+
clip-rule="evenodd"
|
|
136
|
+
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"
|
|
137
|
+
></path>
|
|
138
|
+
<rect x="3.66669" width="1.46667" height="5.13333" rx="0.733333" fill="currentColor"></rect>
|
|
139
|
+
<rect x="16.8667" width="1.46667" height="5.13333" rx="0.733333" fill="currentColor"></rect>
|
|
140
|
+
</svg>
|
|
141
|
+
</button>`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* HTML template for calendar dialog
|
|
146
|
+
*
|
|
147
|
+
* @param {string} [titleId] - Id attribute for dialog title
|
|
148
|
+
* @returns {string}
|
|
149
|
+
*/
|
|
150
|
+
dialogTemplate(titleId) {
|
|
151
|
+
return `<div class="moj-datepicker__dialog-header">
|
|
152
|
+
<div class="moj-datepicker__dialog-navbuttons">
|
|
153
|
+
<button class="moj-datepicker__button moj-js-datepicker-prev-year">
|
|
154
|
+
<span class="govuk-visually-hidden">Previous year</span>
|
|
155
|
+
<svg width="44" height="40" viewBox="0 0 44 40" fill="none" fill="none" focusable="false" aria-hidden="true" role="img">
|
|
156
|
+
<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"/>
|
|
157
|
+
<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"/>
|
|
158
|
+
</svg>
|
|
159
|
+
</button>
|
|
160
|
+
|
|
161
|
+
<button class="moj-datepicker__button moj-js-datepicker-prev-month">
|
|
162
|
+
<span class="govuk-visually-hidden">Previous month</span>
|
|
163
|
+
<svg width="44" height="40" viewBox="0 0 44 40" fill="none" focusable="false" aria-hidden="true" role="img">
|
|
164
|
+
<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"/>
|
|
165
|
+
</svg>
|
|
166
|
+
</button>
|
|
167
|
+
</div>
|
|
168
|
+
|
|
169
|
+
<h2 id="${titleId}" class="moj-datepicker__dialog-title moj-js-datepicker-month-year" aria-live="polite">June 2020</h2>
|
|
170
|
+
|
|
171
|
+
<div class="moj-datepicker__dialog-navbuttons">
|
|
172
|
+
<button class="moj-datepicker__button moj-js-datepicker-next-month">
|
|
173
|
+
<span class="govuk-visually-hidden">Next month</span>
|
|
174
|
+
<svg width="44" height="40" viewBox="0 0 44 40" fill="none" focusable="false" aria-hidden="true" role="img">
|
|
175
|
+
<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"/>
|
|
176
|
+
</svg>
|
|
177
|
+
</button>
|
|
178
|
+
|
|
179
|
+
<button class="moj-datepicker__button moj-js-datepicker-next-year">
|
|
180
|
+
<span class="govuk-visually-hidden">Next year</span>
|
|
181
|
+
<svg width="44" height="40" viewBox="0 0 44 40" fill="none" fill="none" focusable="false" aria-hidden="true" role="img">
|
|
182
|
+
<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"/>
|
|
183
|
+
<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"/>
|
|
184
|
+
</svg>
|
|
185
|
+
</button>
|
|
186
|
+
</div>
|
|
187
|
+
</div>
|
|
188
|
+
|
|
189
|
+
<table class="moj-datepicker__calendar moj-js-datepicker-grid" role="grid" aria-labelledby="${titleId}">
|
|
190
|
+
<thead>
|
|
191
|
+
<tr></tr>
|
|
192
|
+
</thead>
|
|
193
|
+
|
|
194
|
+
<tbody></tbody>
|
|
195
|
+
</table>
|
|
196
|
+
|
|
197
|
+
<div class="govuk-button-group">
|
|
198
|
+
<button type="button" class="govuk-button moj-js-datepicker-ok">Select</button>
|
|
199
|
+
<button type="button" class="govuk-button govuk-button--secondary moj-js-datepicker-cancel">Close</button>
|
|
200
|
+
</div>`;
|
|
201
|
+
}
|
|
202
|
+
createCalendarHeaders() {
|
|
203
|
+
this.dayLabels.forEach(day => {
|
|
204
|
+
const html = `<th scope="col"><span aria-hidden="true">${day.substring(0, 3)}</span><span class="govuk-visually-hidden">${day}</span></th>`;
|
|
205
|
+
const $headerRow = this.$dialog.querySelector('thead > tr');
|
|
206
|
+
$headerRow.insertAdjacentHTML('beforeend', html);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Pads given number with leading zeros
|
|
212
|
+
*
|
|
213
|
+
* @param {number} value - The value to be padded
|
|
214
|
+
* @param {number} length - The length in characters of the output
|
|
215
|
+
* @returns {string}
|
|
216
|
+
*/
|
|
217
|
+
leadingZeros(value, length = 2) {
|
|
218
|
+
let ret = value.toString();
|
|
219
|
+
while (ret.length < length) {
|
|
220
|
+
ret = `0${ret}`;
|
|
221
|
+
}
|
|
222
|
+
return ret;
|
|
223
|
+
}
|
|
224
|
+
setOptions() {
|
|
225
|
+
this.setMinAndMaxDatesOnCalendar();
|
|
226
|
+
this.setExcludedDates();
|
|
227
|
+
this.setExcludedDays();
|
|
228
|
+
this.setWeekStartDay();
|
|
229
|
+
}
|
|
230
|
+
setMinAndMaxDatesOnCalendar() {
|
|
231
|
+
if (this.config.minDate) {
|
|
232
|
+
this.minDate = this.formattedDateFromString(this.config.minDate, null);
|
|
233
|
+
if (this.minDate && this.currentDate < this.minDate) {
|
|
234
|
+
this.currentDate = this.minDate;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (this.config.maxDate) {
|
|
238
|
+
this.maxDate = this.formattedDateFromString(this.config.maxDate, null);
|
|
239
|
+
if (this.maxDate && this.currentDate > this.maxDate) {
|
|
240
|
+
this.currentDate = this.maxDate;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
setExcludedDates() {
|
|
245
|
+
if (this.config.excludedDates) {
|
|
246
|
+
this.excludedDates = this.config.excludedDates.replace(/\s+/, ' ').split(' ').map(item => {
|
|
247
|
+
return item.includes('-') ? this.parseDateRangeString(item) : [this.formattedDateFromString(item)];
|
|
248
|
+
}).reduce((dates, items) => dates.concat(items)).filter(date => date);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Parses a daterange string into an array of dates
|
|
254
|
+
*
|
|
255
|
+
* @param {string} datestring - A daterange string in the format "dd/mm/yyyy-dd/mm/yyyy"
|
|
256
|
+
*/
|
|
257
|
+
parseDateRangeString(datestring) {
|
|
258
|
+
const dates = [];
|
|
259
|
+
const [startDate, endDate] = datestring.split('-').map(d => this.formattedDateFromString(d, null));
|
|
260
|
+
if (startDate && endDate) {
|
|
261
|
+
const date = new Date(startDate.getTime());
|
|
262
|
+
/* eslint-disable no-unmodified-loop-condition */
|
|
263
|
+
while (date <= endDate) {
|
|
264
|
+
dates.push(new Date(date));
|
|
265
|
+
date.setDate(date.getDate() + 1);
|
|
266
|
+
}
|
|
267
|
+
/* eslint-enable no-unmodified-loop-condition */
|
|
268
|
+
}
|
|
269
|
+
return dates;
|
|
270
|
+
}
|
|
271
|
+
setExcludedDays() {
|
|
272
|
+
if (this.config.excludedDays) {
|
|
273
|
+
// lowercase and arrange dayLabels to put indexOf sunday == 0 for comparison
|
|
274
|
+
// with getDay() function
|
|
275
|
+
const weekDays = this.dayLabels.map(item => item.toLowerCase());
|
|
276
|
+
if (this.config.weekStartDay === 'monday') {
|
|
277
|
+
weekDays.unshift(weekDays.pop());
|
|
278
|
+
}
|
|
279
|
+
this.excludedDays = this.config.excludedDays.replace(/\s+/, ' ').toLowerCase().split(' ').map(item => weekDays.indexOf(item)).filter(item => item !== -1);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
setWeekStartDay() {
|
|
283
|
+
const weekStartDayParam = this.config.weekStartDay;
|
|
284
|
+
if (weekStartDayParam && weekStartDayParam.toLowerCase() === 'sunday') {
|
|
285
|
+
this.config.weekStartDay = 'sunday';
|
|
286
|
+
// Rotate dayLabels array to put Sunday as the first item
|
|
287
|
+
this.dayLabels.unshift(this.dayLabels.pop());
|
|
288
|
+
} else {
|
|
289
|
+
this.config.weekStartDay = 'monday';
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Determine if a date is selectable
|
|
295
|
+
*
|
|
296
|
+
* @param {Date} date - the date to check
|
|
297
|
+
* @returns {boolean}
|
|
298
|
+
*/
|
|
299
|
+
isExcludedDate(date) {
|
|
300
|
+
// This comparison does not work correctly - it will exclude the mindate itself
|
|
301
|
+
// see: https://github.com/ministryofjustice/moj-frontend/issues/923
|
|
302
|
+
if (this.minDate && this.minDate > date) {
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// This comparison works as expected - the maxdate will not be excluded
|
|
307
|
+
if (this.maxDate && this.maxDate < date) {
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
for (const excludedDate of this.excludedDates) {
|
|
311
|
+
if (date.toDateString() === excludedDate.toDateString()) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (this.excludedDays.includes(date.getDay())) {
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Get a Date object from a string
|
|
323
|
+
*
|
|
324
|
+
* @param {string} dateString - string in the format d/m/yyyy dd/mm/yyyy
|
|
325
|
+
* @param {Date} fallback - date object to return if formatting fails
|
|
326
|
+
* @returns {Date}
|
|
327
|
+
*/
|
|
328
|
+
formattedDateFromString(dateString, fallback = new Date()) {
|
|
329
|
+
let formattedDate = null;
|
|
330
|
+
// Accepts d/m/yyyy and dd/mm/yyyy
|
|
331
|
+
const dateFormatPattern = /(\d{1,2})([-/,. ])(\d{1,2})\2(\d{4})/;
|
|
332
|
+
if (!dateFormatPattern.test(dateString)) return fallback;
|
|
333
|
+
const match = dateFormatPattern.exec(dateString);
|
|
334
|
+
const day = match[1];
|
|
335
|
+
const month = match[3];
|
|
336
|
+
const year = match[4];
|
|
337
|
+
formattedDate = new Date(`${year}-${month}-${day}`);
|
|
338
|
+
if (formattedDate instanceof Date && Number.isFinite(formattedDate.getTime())) {
|
|
339
|
+
return formattedDate;
|
|
340
|
+
}
|
|
341
|
+
return fallback;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Get a formatted date string from a Date object
|
|
346
|
+
*
|
|
347
|
+
* @param {Date} date - date to format to a string
|
|
348
|
+
* @returns {string}
|
|
349
|
+
*/
|
|
350
|
+
formattedDateFromDate(date) {
|
|
351
|
+
if (this.config.leadingZeros) {
|
|
352
|
+
return `${this.leadingZeros(date.getDate())}/${this.leadingZeros(date.getMonth() + 1)}/${date.getFullYear()}`;
|
|
353
|
+
}
|
|
354
|
+
return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Get a human readable date in the format Monday 2 March 2024
|
|
359
|
+
*
|
|
360
|
+
* @param {Date} date - Date to format
|
|
361
|
+
* @returns {string}
|
|
362
|
+
*/
|
|
363
|
+
formattedDateHuman(date) {
|
|
364
|
+
return `${this.dayLabels[(date.getDay() + 6) % 7]} ${date.getDate()} ${this.monthLabels[date.getMonth()]} ${date.getFullYear()}`;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* @param {MouseEvent} event - Click event
|
|
369
|
+
*/
|
|
370
|
+
backgroundClick(event) {
|
|
371
|
+
if (this.isOpen() && event.target instanceof Node && !this.$dialog.contains(event.target) && !this.$input.contains(event.target) && !this.$calendarButton.contains(event.target)) {
|
|
372
|
+
event.preventDefault();
|
|
373
|
+
this.closeDialog();
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* @param {KeyboardEvent} event - Keydown event
|
|
379
|
+
*/
|
|
380
|
+
firstButtonKeydown(event) {
|
|
381
|
+
if (event.key === 'Tab' && event.shiftKey) {
|
|
382
|
+
this.$lastButtonInDialog.focus();
|
|
383
|
+
event.preventDefault();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* @param {KeyboardEvent} event - Keydown event
|
|
389
|
+
*/
|
|
390
|
+
lastButtonKeydown(event) {
|
|
391
|
+
if (event.key === 'Tab' && !event.shiftKey) {
|
|
392
|
+
this.$firstButtonInDialog.focus();
|
|
393
|
+
event.preventDefault();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// render calendar
|
|
398
|
+
updateCalendar() {
|
|
399
|
+
this.$dialogTitle.innerHTML = `${this.monthLabels[this.currentDate.getMonth()]} ${this.currentDate.getFullYear()}`;
|
|
400
|
+
const day = this.currentDate;
|
|
401
|
+
const firstOfMonth = new Date(day.getFullYear(), day.getMonth(), 1);
|
|
402
|
+
let dayOfWeek;
|
|
403
|
+
if (this.config.weekStartDay === 'monday') {
|
|
404
|
+
dayOfWeek = firstOfMonth.getDay() === 0 ? 6 : firstOfMonth.getDay() - 1; // Change logic to make Monday first day of week, i.e. 0
|
|
405
|
+
} else {
|
|
406
|
+
dayOfWeek = firstOfMonth.getDay();
|
|
407
|
+
}
|
|
408
|
+
firstOfMonth.setDate(firstOfMonth.getDate() - dayOfWeek);
|
|
409
|
+
const thisDay = new Date(firstOfMonth);
|
|
410
|
+
|
|
411
|
+
// loop through our days
|
|
412
|
+
for (const calendarDay of this.calendarDays) {
|
|
413
|
+
const hidden = thisDay.getMonth() !== day.getMonth();
|
|
414
|
+
const disabled = this.isExcludedDate(thisDay);
|
|
415
|
+
calendarDay.update(thisDay, hidden, disabled);
|
|
416
|
+
thisDay.setDate(thisDay.getDate() + 1);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* @param {boolean} [focus] - Focus the day button
|
|
422
|
+
*/
|
|
423
|
+
setCurrentDate(focus = true) {
|
|
424
|
+
const {
|
|
425
|
+
currentDate
|
|
426
|
+
} = this;
|
|
427
|
+
this.calendarDays.forEach(calendarDay => {
|
|
428
|
+
calendarDay.$button.classList.add('moj-datepicker__button');
|
|
429
|
+
calendarDay.$button.classList.add('moj-datepicker__calendar-day');
|
|
430
|
+
calendarDay.$button.setAttribute('tabindex', '-1');
|
|
431
|
+
calendarDay.$button.classList.remove(this.selectedDayButtonClass);
|
|
432
|
+
const calendarDayDate = calendarDay.date;
|
|
433
|
+
calendarDayDate.setHours(0, 0, 0, 0);
|
|
434
|
+
const today = new Date();
|
|
435
|
+
today.setHours(0, 0, 0, 0);
|
|
436
|
+
if (calendarDayDate.getTime() === currentDate.getTime() /* && !calendarDay.button.disabled */) {
|
|
437
|
+
if (focus) {
|
|
438
|
+
calendarDay.$button.setAttribute('tabindex', '0');
|
|
439
|
+
calendarDay.$button.focus();
|
|
440
|
+
calendarDay.$button.classList.add(this.selectedDayButtonClass);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (this.inputDate && calendarDayDate.getTime() === this.inputDate.getTime()) {
|
|
444
|
+
calendarDay.$button.classList.add(this.currentDayButtonClass);
|
|
445
|
+
calendarDay.$button.setAttribute('aria-current', 'date');
|
|
446
|
+
} else {
|
|
447
|
+
calendarDay.$button.classList.remove(this.currentDayButtonClass);
|
|
448
|
+
calendarDay.$button.removeAttribute('aria-current');
|
|
449
|
+
}
|
|
450
|
+
if (calendarDayDate.getTime() === today.getTime()) {
|
|
451
|
+
calendarDay.$button.classList.add(this.todayButtonClass);
|
|
452
|
+
} else {
|
|
453
|
+
calendarDay.$button.classList.remove(this.todayButtonClass);
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
// if no date is tab-able, make the first non-disabled date tab-able
|
|
458
|
+
if (!focus) {
|
|
459
|
+
const enabledDays = this.calendarDays.filter(calendarDay => {
|
|
460
|
+
return window.getComputedStyle(calendarDay.$button).display === 'block' && !calendarDay.$button.disabled;
|
|
461
|
+
});
|
|
462
|
+
enabledDays[0].$button.setAttribute('tabindex', '0');
|
|
463
|
+
this.currentDate = enabledDays[0].date;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* @param {Date} date - Date to select
|
|
469
|
+
*/
|
|
470
|
+
selectDate(date) {
|
|
471
|
+
if (this.isExcludedDate(date)) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
this.$calendarButton.querySelector('span').innerText = `Choose date. Selected date is ${this.formattedDateHuman(date)}`;
|
|
475
|
+
this.$input.value = this.formattedDateFromDate(date);
|
|
476
|
+
const changeEvent = new Event('change', {
|
|
477
|
+
bubbles: true,
|
|
478
|
+
cancelable: true
|
|
479
|
+
});
|
|
480
|
+
this.$input.dispatchEvent(changeEvent);
|
|
481
|
+
this.closeDialog();
|
|
482
|
+
}
|
|
483
|
+
isOpen() {
|
|
484
|
+
return this.$dialog.classList.contains('moj-datepicker__dialog--open');
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* @param {MouseEvent} event - Click event
|
|
489
|
+
*/
|
|
490
|
+
toggleDialog(event) {
|
|
491
|
+
event.preventDefault();
|
|
492
|
+
if (this.isOpen()) {
|
|
493
|
+
this.closeDialog();
|
|
494
|
+
} else {
|
|
495
|
+
this.setMinAndMaxDatesOnCalendar();
|
|
496
|
+
this.openDialog();
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
openDialog() {
|
|
500
|
+
this.$dialog.hidden = false;
|
|
501
|
+
this.$dialog.classList.add('moj-datepicker__dialog--open');
|
|
502
|
+
this.$calendarButton.setAttribute('aria-expanded', 'true');
|
|
503
|
+
|
|
504
|
+
// position the dialog
|
|
505
|
+
// if input is wider than dialog pin it to the right
|
|
506
|
+
if (this.$input.offsetWidth > this.$dialog.offsetWidth) {
|
|
507
|
+
this.$dialog.style.right = `0px`;
|
|
508
|
+
}
|
|
509
|
+
this.$dialog.style.top = `${this.$input.offsetHeight + 3}px`;
|
|
510
|
+
|
|
511
|
+
// get the date from the input element
|
|
512
|
+
this.inputDate = this.formattedDateFromString(this.$input.value);
|
|
513
|
+
this.currentDate = this.inputDate;
|
|
514
|
+
this.currentDate.setHours(0, 0, 0, 0);
|
|
515
|
+
this.updateCalendar();
|
|
516
|
+
this.setCurrentDate();
|
|
517
|
+
}
|
|
518
|
+
closeDialog() {
|
|
519
|
+
this.$dialog.hidden = true;
|
|
520
|
+
this.$dialog.classList.remove('moj-datepicker__dialog--open');
|
|
521
|
+
this.$calendarButton.setAttribute('aria-expanded', 'false');
|
|
522
|
+
this.$calendarButton.focus();
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* @param {Date} date - Date to go to
|
|
527
|
+
* @param {boolean} [focus] - Focus the day button
|
|
528
|
+
*/
|
|
529
|
+
goToDate(date, focus) {
|
|
530
|
+
const current = this.currentDate;
|
|
531
|
+
this.currentDate = date;
|
|
532
|
+
if (current.getMonth() !== this.currentDate.getMonth() || current.getFullYear() !== this.currentDate.getFullYear()) {
|
|
533
|
+
this.updateCalendar();
|
|
534
|
+
}
|
|
535
|
+
this.setCurrentDate(focus);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// day navigation
|
|
539
|
+
focusNextDay() {
|
|
540
|
+
const date = new Date(this.currentDate);
|
|
541
|
+
date.setDate(date.getDate() + 1);
|
|
542
|
+
this.goToDate(date);
|
|
543
|
+
}
|
|
544
|
+
focusPreviousDay() {
|
|
545
|
+
const date = new Date(this.currentDate);
|
|
546
|
+
date.setDate(date.getDate() - 1);
|
|
547
|
+
this.goToDate(date);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// week navigation
|
|
551
|
+
focusNextWeek() {
|
|
552
|
+
const date = new Date(this.currentDate);
|
|
553
|
+
date.setDate(date.getDate() + 7);
|
|
554
|
+
this.goToDate(date);
|
|
555
|
+
}
|
|
556
|
+
focusPreviousWeek() {
|
|
557
|
+
const date = new Date(this.currentDate);
|
|
558
|
+
date.setDate(date.getDate() - 7);
|
|
559
|
+
this.goToDate(date);
|
|
560
|
+
}
|
|
561
|
+
focusFirstDayOfWeek() {
|
|
562
|
+
const date = new Date(this.currentDate);
|
|
563
|
+
const firstDayOfWeekIndex = this.config.weekStartDay === 'sunday' ? 0 : 1;
|
|
564
|
+
const dayOfWeek = date.getDay();
|
|
565
|
+
const diff = dayOfWeek >= firstDayOfWeekIndex ? dayOfWeek - firstDayOfWeekIndex : 6 - dayOfWeek;
|
|
566
|
+
date.setDate(date.getDate() - diff);
|
|
567
|
+
date.setHours(0, 0, 0, 0);
|
|
568
|
+
this.goToDate(date);
|
|
569
|
+
}
|
|
570
|
+
focusLastDayOfWeek() {
|
|
571
|
+
const date = new Date(this.currentDate);
|
|
572
|
+
const lastDayOfWeekIndex = this.config.weekStartDay === 'sunday' ? 6 : 0;
|
|
573
|
+
const dayOfWeek = date.getDay();
|
|
574
|
+
const diff = dayOfWeek <= lastDayOfWeekIndex ? lastDayOfWeekIndex - dayOfWeek : 7 - dayOfWeek;
|
|
575
|
+
date.setDate(date.getDate() + diff);
|
|
576
|
+
date.setHours(0, 0, 0, 0);
|
|
577
|
+
this.goToDate(date);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Month navigation
|
|
582
|
+
*
|
|
583
|
+
* @param {KeyboardEvent | MouseEvent} event - Key press or click event
|
|
584
|
+
* @param {boolean} [focus] - Focus the day button
|
|
585
|
+
*/
|
|
586
|
+
focusNextMonth(event, focus = true) {
|
|
587
|
+
event.preventDefault();
|
|
588
|
+
const date = new Date(this.currentDate);
|
|
589
|
+
date.setMonth(date.getMonth() + 1, 1);
|
|
590
|
+
this.goToDate(date, focus);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* @param {KeyboardEvent | MouseEvent} event - Key press or click event
|
|
595
|
+
* @param {boolean} [focus] - Focus the day button
|
|
596
|
+
*/
|
|
597
|
+
focusPreviousMonth(event, focus = true) {
|
|
598
|
+
event.preventDefault();
|
|
599
|
+
const date = new Date(this.currentDate);
|
|
600
|
+
date.setMonth(date.getMonth() - 1, 1);
|
|
601
|
+
this.goToDate(date, focus);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Year navigation
|
|
606
|
+
*
|
|
607
|
+
* @param {KeyboardEvent | MouseEvent} event - Key press or click event
|
|
608
|
+
* @param {boolean} [focus] - Focus the day button
|
|
609
|
+
*/
|
|
610
|
+
focusNextYear(event, focus = true) {
|
|
611
|
+
event.preventDefault();
|
|
612
|
+
const date = new Date(this.currentDate);
|
|
613
|
+
date.setFullYear(date.getFullYear() + 1, date.getMonth(), 1);
|
|
614
|
+
this.goToDate(date, focus);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* @param {KeyboardEvent | MouseEvent} event - Key press or click event
|
|
619
|
+
* @param {boolean} [focus] - Focus the day button
|
|
620
|
+
*/
|
|
621
|
+
focusPreviousYear(event, focus = true) {
|
|
622
|
+
event.preventDefault();
|
|
623
|
+
const date = new Date(this.currentDate);
|
|
624
|
+
date.setFullYear(date.getFullYear() - 1, date.getMonth(), 1);
|
|
625
|
+
this.goToDate(date, focus);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Name for the component used when initialising using data-module attributes.
|
|
630
|
+
*/
|
|
631
|
+
}
|
|
632
|
+
DatePicker.moduleName = 'moj-date-picker';
|
|
633
|
+
/**
|
|
634
|
+
* Date picker default config
|
|
635
|
+
*
|
|
636
|
+
* @type {DatePickerConfig}
|
|
637
|
+
*/
|
|
638
|
+
DatePicker.defaults = Object.freeze({
|
|
639
|
+
leadingZeros: false,
|
|
640
|
+
weekStartDay: 'monday',
|
|
641
|
+
input: {
|
|
642
|
+
selector: '.moj-js-datepicker-input'
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
/**
|
|
646
|
+
* Date picker config schema
|
|
647
|
+
*
|
|
648
|
+
* @satisfies {Schema<DatePickerConfig>}
|
|
649
|
+
*/
|
|
650
|
+
DatePicker.schema = Object.freeze(/** @type {const} */{
|
|
651
|
+
properties: {
|
|
652
|
+
excludedDates: {
|
|
653
|
+
type: 'string'
|
|
654
|
+
},
|
|
655
|
+
excludedDays: {
|
|
656
|
+
type: 'string'
|
|
657
|
+
},
|
|
658
|
+
leadingZeros: {
|
|
659
|
+
type: 'boolean'
|
|
660
|
+
},
|
|
661
|
+
maxDate: {
|
|
662
|
+
type: 'string'
|
|
663
|
+
},
|
|
664
|
+
minDate: {
|
|
665
|
+
type: 'string'
|
|
666
|
+
},
|
|
667
|
+
weekStartDay: {
|
|
668
|
+
type: 'string'
|
|
669
|
+
},
|
|
670
|
+
input: {
|
|
671
|
+
type: 'object'
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
class DSCalendarDay {
|
|
676
|
+
/**
|
|
677
|
+
*
|
|
678
|
+
* @param {HTMLButtonElement} $button
|
|
679
|
+
* @param {number} index
|
|
680
|
+
* @param {number} row
|
|
681
|
+
* @param {number} column
|
|
682
|
+
* @param {DatePicker} picker
|
|
683
|
+
*/
|
|
684
|
+
constructor($button, index, row, column, picker) {
|
|
685
|
+
this.index = index;
|
|
686
|
+
this.row = row;
|
|
687
|
+
this.column = column;
|
|
688
|
+
this.$button = $button;
|
|
689
|
+
this.picker = picker;
|
|
690
|
+
this.date = new Date();
|
|
691
|
+
this.$button.addEventListener('keydown', this.keyPress.bind(this));
|
|
692
|
+
this.$button.addEventListener('click', this.click.bind(this));
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* @param {Date} day - the Date for the calendar day
|
|
697
|
+
* @param {boolean} hidden - visibility of the day
|
|
698
|
+
* @param {boolean} disabled - is the day selectable or excluded
|
|
699
|
+
*/
|
|
700
|
+
update(day, hidden, disabled) {
|
|
701
|
+
const label = day.getDate();
|
|
702
|
+
let accessibleLabel = this.picker.formattedDateHuman(day);
|
|
703
|
+
if (disabled) {
|
|
704
|
+
this.$button.setAttribute('aria-disabled', 'true');
|
|
705
|
+
accessibleLabel = `Excluded date, ${accessibleLabel}`;
|
|
706
|
+
} else {
|
|
707
|
+
this.$button.removeAttribute('aria-disabled');
|
|
708
|
+
}
|
|
709
|
+
if (hidden) {
|
|
710
|
+
this.$button.style.display = 'none';
|
|
711
|
+
} else {
|
|
712
|
+
this.$button.style.display = 'block';
|
|
713
|
+
}
|
|
714
|
+
this.$button.setAttribute('data-testid', this.picker.formattedDateFromDate(day));
|
|
715
|
+
this.$button.innerHTML = `<span class="govuk-visually-hidden">${accessibleLabel}</span><span aria-hidden="true">${label}</span>`;
|
|
716
|
+
this.date = new Date(day);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* @param {MouseEvent} event - Click event
|
|
721
|
+
*/
|
|
722
|
+
click(event) {
|
|
723
|
+
this.picker.goToDate(this.date);
|
|
724
|
+
this.picker.selectDate(this.date);
|
|
725
|
+
event.stopPropagation();
|
|
726
|
+
event.preventDefault();
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* @param {KeyboardEvent} event - Keydown event
|
|
731
|
+
*/
|
|
732
|
+
keyPress(event) {
|
|
733
|
+
let calendarNavKey = true;
|
|
734
|
+
switch (event.key) {
|
|
735
|
+
case 'ArrowLeft':
|
|
736
|
+
this.picker.focusPreviousDay();
|
|
737
|
+
break;
|
|
738
|
+
case 'ArrowRight':
|
|
739
|
+
this.picker.focusNextDay();
|
|
740
|
+
break;
|
|
741
|
+
case 'ArrowUp':
|
|
742
|
+
this.picker.focusPreviousWeek();
|
|
743
|
+
break;
|
|
744
|
+
case 'ArrowDown':
|
|
745
|
+
this.picker.focusNextWeek();
|
|
746
|
+
break;
|
|
747
|
+
case 'Home':
|
|
748
|
+
this.picker.focusFirstDayOfWeek();
|
|
749
|
+
break;
|
|
750
|
+
case 'End':
|
|
751
|
+
this.picker.focusLastDayOfWeek();
|
|
752
|
+
break;
|
|
753
|
+
case 'PageUp':
|
|
754
|
+
{
|
|
755
|
+
if (event.shiftKey) {
|
|
756
|
+
this.picker.focusPreviousYear(event);
|
|
757
|
+
} else {
|
|
758
|
+
this.picker.focusPreviousMonth(event);
|
|
759
|
+
}
|
|
760
|
+
break;
|
|
761
|
+
}
|
|
762
|
+
case 'PageDown':
|
|
763
|
+
{
|
|
764
|
+
if (event.shiftKey) {
|
|
765
|
+
this.picker.focusNextYear(event);
|
|
766
|
+
} else {
|
|
767
|
+
this.picker.focusNextMonth(event);
|
|
768
|
+
}
|
|
769
|
+
break;
|
|
770
|
+
}
|
|
771
|
+
default:
|
|
772
|
+
calendarNavKey = false;
|
|
773
|
+
break;
|
|
774
|
+
}
|
|
775
|
+
if (calendarNavKey) {
|
|
776
|
+
event.preventDefault();
|
|
777
|
+
event.stopPropagation();
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* Date picker config
|
|
784
|
+
*
|
|
785
|
+
* @typedef {object} DatePickerConfig
|
|
786
|
+
* @property {string} [excludedDates] - Dates that cannot be selected
|
|
787
|
+
* @property {string} [excludedDays] - Days that cannot be selected
|
|
788
|
+
* @property {boolean} [leadingZeros] - Whether to add leading zeroes when populating the field
|
|
789
|
+
* @property {string} [minDate] - The earliest available date
|
|
790
|
+
* @property {string} [maxDate] - The latest available date
|
|
791
|
+
* @property {string} [weekStartDay] - First day of the week in calendar view
|
|
792
|
+
* @property {object} [input] - Input config
|
|
793
|
+
* @property {string} [input.selector] - Selector for the input element
|
|
794
|
+
* @property {Element | null} [input.element] - HTML element for the input
|
|
795
|
+
*/
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* @import { Schema } from 'govuk-frontend/dist/govuk/common/configuration.mjs'
|
|
799
|
+
*/
|
|
800
|
+
|
|
801
|
+
exports.DatePicker = DatePicker;
|
|
802
|
+
|
|
803
|
+
}));
|
|
804
|
+
//# sourceMappingURL=date-picker.bundle.js.map
|