-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepicker-step2.js
206 lines (182 loc) · 6.49 KB
/
datepicker-step2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Setup
//const todaysDate = document.querySelector('#todayIs');
const inputDate = document.querySelector('#date');
const chosenDate = document.querySelector('#chosenDate');
const inputError = document.querySelector('#error-date');
const dialog = document.querySelector("dialog");
const btnOpenDP = document.getElementById("dp1-opener");
const btnCloseDP = dialog.querySelector("#dp1-closer");
const btnPrevious = dialog.querySelector('#dp1-previous');
const btnNext = dialog.querySelector('#dp1-next');
const btnSubmit = document.querySelector('#go');
const monthIndicator = dialog.querySelector('#dp1-currentmonth');
const monthsOfYear = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const earliestDate = moment('2023-10-17');
const latestDate = moment('2023-12-17');
let currentDate = moment('2023-11-18');
// On Load
//todayIs();
btnOpenDP.removeAttribute('hidden');
// Events
// "Show the dialog" button opens the dialog modally
btnOpenDP.addEventListener("click", () => {
dialog.showModal();
createCalendarTable(currentDate);
//setMonthIndicator();
});
// "Close" button closes the dialog
btnCloseDP.addEventListener("click", () => {
dialog.close();
});
// Cycle through available months
btnPrevious.addEventListener('click', function () {
goToPreviousMonth();
setMonthIndicator();
})
btnNext.addEventListener('click', function () {
goToNextMonth();
setMonthIndicator();
})
// Submit the form
btnSubmit.addEventListener('click', function () {
const currentText = inputDate.value;
const valueReformatted = moment(currentText, 'DD/MM/YY');
console.log(valueReformatted);
if (valueReformatted._isValid) {
chosenDate.innerHTML = valueReformatted.format('dddd, MMMM Do YYYY');
chosenDate.parentElement.removeAttribute('hidden');
inputDate.classList.remove('error');
//inputDate.setAttribute('aria-labelledby', 'error-date');
inputError.setAttribute('hidden', 'true');
} else {
inputDate.classList.add('error');
//inputDate.removeAttribute('aria-labelledby')
inputError.removeAttribute('hidden');
}
})
// Functions
function todayIs () {
const now = moment().format('dddd, MMMM Do YYYY');
todaysDate.innerHTML = now;
}
function setMonthIndicator () {
console.log("set month indicator");
let whichMonth = currentDate.month();
whichMonth = monthsOfYear[whichMonth];
monthIndicator.innerHTML = whichMonth;
}
function createCalendarTable (whichDate) {
console.log("create calendar table: " + whichDate);
// setup
const tableLocation = dialog.querySelector('.dp-body');
const tableCalendar = tableLocation.querySelector('table');
setMonthIndicator();
// get current details
const daysInCurrentMonth = whichDate.daysInMonth()
const weeksInCurrentMonth = 6 // temp hardcode, was Math.ceil(daysInCurrentMonth / 7) but that broke September
// create table cells
for (var j = 0; j < weeksInCurrentMonth; j++) {
var contentRow = document.createElement('tr');
for (var k = 0; k < 7; k++) {
var contentCell = document.createElement('td');
contentRow.appendChild(contentCell);
}
tableCalendar.appendChild(contentRow);
}
// add buttons to cells
populateTable(daysInCurrentMonth);
// add listener for any selections. gotta do it after the buttons are created.
tableCalendar.addEventListener('click', function (e) {
sendSelectedDate(e);
})
}
function populateTable (howManyDays) {
console.log("populate table: " + howManyDays);
// put all cells in a collection
const tableCells = dialog.querySelectorAll('td');
// clear out any previous content
tableCells.forEach(function (cell) {
cell.innerHTML = '';
})
// put updated content in them
const startDayOfThisMonth = currentDate.startOf('month').day();
for (var m = 0; m < howManyDays; m++) {
let currentDay = startDayOfThisMonth + m;
let currentCell = tableCells[currentDay];
currentCell.innerHTML = '<button type="button">' + (m + 1) + '</button>';
}
let whichMonth = currentDate.month();
disableInvalidDates(whichMonth);
}
function disableInvalidDates (currentMonth) {
console.log("disable invalid dates: " + currentMonth);
const earliestMonth = earliestDate.month();
const latestMonth = latestDate.month();
let btnsToDisable = [];
if ((currentMonth > earliestMonth) && (currentMonth < latestMonth)) {
// do nothing
} else {
const existingTable = dialog.querySelector('table');
const tableButtons = existingTable.querySelectorAll('button');
if (currentMonth <= earliestMonth) {
tableButtons.forEach(function (button, i) {
if (i < (earliestDate.date() - 1)) { // buttons have moved now due to day of week fix
btnsToDisable = btnsToDisable.concat(button);
}
})
updatePrevNextStatus(btnPrevious);
} else if (currentMonth >= latestMonth) {
tableButtons.forEach(function (button, i) {
if (i > (latestDate.date() - 1)) { // buttons have moved now due to day of week fix
btnsToDisable = btnsToDisable.concat(button);
}
})
updatePrevNextStatus(btnNext);
}
btnsToDisable.forEach(function (button) {
button.setAttribute('disabled', 'disabled');
})
}
}
function updatePrevNextStatus (whichButton) {
console.log("updating Prev/Next status: " + whichButton);
if (whichButton === btnPrevious) {
btnPrevious.setAttribute('disabled', 'disabled');
} else if (whichButton === btnNext) {
btnNext.setAttribute('disabled', 'disabled');
} else {
btnPrevious.removeAttribute('disabled');
btnNext.removeAttribute('disabled');
}
}
function goToPreviousMonth () {
console.log("go to prev");
currentDate = currentDate.subtract(1, 'month');
const newDays = currentDate.daysInMonth();
updatePrevNextStatus();
populateTable(newDays);
monthIndicator.focus();
}
// adapt for Next Month function
function goToNextMonth () {
console.log("go to next");
currentDate = currentDate.add(1, 'month')
const newDays = currentDate.daysInMonth()
updatePrevNextStatus()
populateTable(newDays)
monthIndicator.focus();
}
function sendSelectedDate (whichOne) {
console.log('send selected date');
const whichBtn = whichOne.target;
const btnDates = dialog.querySelectorAll('.dp-body button');
btnDates.forEach(function (button) {
button.classList.remove('is-selected');
})
whichBtn.classList.add('is-selected');
const whichDay = whichBtn.innerHTML;
const whichDate = currentDate.date(whichDay).format('DD/MM/YYYY');
inputDate.value = '';
inputDate.value = whichDate;
dialog.close();
}