-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepicker-aria.js
221 lines (196 loc) · 6.94 KB
/
datepicker-aria.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// globals
const todaysDate = document.querySelector('#todayIs')
const inputDate = document.querySelector('#date')
const inputError = document.querySelector('#error-date')
const btnSubmit = document.querySelector('#go')
const chosenDate = document.querySelector('#chosenDate')
const btnToggle = document.querySelector('#dp1-toggler')
const datePicker = document.querySelector('#dp1')
const btnClose = document.querySelector('#dp1-close')
// const headingPicker = document.querySelector('#dp1-heading')
const btnPrevious = document.querySelector('#dp1-previous')
const btnNext = document.querySelector('#dp1-next')
const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
const monthsOfYear = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const earliestDate = moment('2018-06-17')
const latestDate = moment('2018-10-17')
let monthIndicator = document.querySelector('#dp1-currentmonth')
let currentDate = moment('2018-07-17')
// on load
todayIs()
inputDate.value = ''
btnToggle.classList.remove('hidden')
createCalendarTable(currentDate)
// events
btnSubmit.addEventListener('click', function () {
const currentText = inputDate.value
const valueReformatted = moment(currentText, 'DD/MM/YY')
if (valueReformatted.isValid) {
const textReformatted = valueReformatted.format('dddd, MMMM Do YYYY')
chosenDate.innerHTML = 'You have chosen ' + textReformatted
inputDate.classList.remove('error')
inputDate.setAttribute('aria-labelledby', 'error-date')
inputError.classList.add('hidden')
} else {
inputDate.classList.add('error')
inputDate.removeAttribute('aria-labelledby')
inputError.classList.remove('hidden')
}
})
btnPrevious.addEventListener('click', function () {
goToPreviousMonth()
setMonthIndicator()
})
btnNext.addEventListener('click', function () {
goToNextMonth()
setMonthIndicator()
})
btnToggle.addEventListener('click', function (e) {
const body = document.querySelector('body')
if (body.classList.contains('has-dialog')) {
closeDialog()
} else {
openDialog('dp1', e.target, 'dp1-heading')
}
})
btnClose.addEventListener('click', function (e) {
closeDialog()
})
document.addEventListener('keyup', function (e) {
e.preventDefault()
findWhichKey(e)
})
// functions
function todayIs () {
const now = moment().format('dddd, MMMM Do YYYY')
todaysDate.innerHTML = now
}
function goToPreviousMonth () {
currentDate = currentDate.subtract(1, 'month')
const newDays = currentDate.daysInMonth()
updatePrevNextStatus()
populateTable(newDays)
// headingPicker.focus() aria-live handles this
}
function goToNextMonth () {
currentDate = currentDate.add(1, 'month')
const newDays = currentDate.daysInMonth()
updatePrevNextStatus()
populateTable(newDays)
// headingPicker.focus() aria-live handles this
}
function findWhichKey (pressedKey) {
if (pressedKey.key === 'Escape') {
closeDialog()
}
}
function createCalendarTable (whichDate) {
// setup
const tableLocation = datePicker.querySelector('.dp-body')
const tableCalendar = document.createElement('table')
setMonthIndicator()
// create table headers
var headerRow = document.createElement('tr')
for (var i = 0; i < 7; i++) {
var headerCell = document.createElement('th')
headerCell.setAttribute('scope', 'col')
var headerContent = document.createTextNode(daysOfWeek[i])
headerCell.appendChild(headerContent)
headerRow.appendChild(headerCell)
}
tableCalendar.appendChild(headerRow)
// get current details
const daysInCurrentMonth = whichDate.daysInMonth()
const weeksInCurrentMonth = 6 // temp hardcode, was Math.ceil(daysInCurrentMonth / 7) but that broke September 2018
// 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)
}
tableLocation.appendChild(tableCalendar)
// add buttons to cells
populateTable(daysInCurrentMonth)
// add listener for any selections. if I do it before the buttons are there it doesn't work
const tablePicker = document.querySelector('table')
tablePicker.addEventListener('click', function (e) {
sendSelectedDate(e)
})
}
function setMonthIndicator () {
let whichMonth = currentDate.month()
whichMonth = monthsOfYear[whichMonth]
monthIndicator.innerHTML = ' in ' + whichMonth
}
function populateTable (howManyDays) {
// put all cells in a collection
const tableCells = document.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) {
const earliestMonth = earliestDate.month()
const latestMonth = latestDate.month()
let btnsToDisable = []
if ((currentMonth > earliestMonth) && (currentMonth < latestMonth)) {
// do nothing
} else {
const existingTable = document.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) {
if (whichButton === btnPrevious) {
btnPrevious.setAttribute('disabled', 'disabled')
} else if (whichButton === btnNext) {
btnNext.setAttribute('disabled', 'disabled')
} else {
btnPrevious.removeAttribute('disabled')
btnNext.removeAttribute('disabled')
}
}
function sendSelectedDate (whichOne) {
const whichBtn = whichOne.target
const btnDates = document.querySelectorAll('.dp-body button')
btnDates.forEach(function (button) {
button.removeAttribute('aria-current')
})
whichBtn.setAttribute('aria-current', 'selected')
const whichDay = whichBtn.innerHTML
const whichDate = currentDate.date(whichDay).format('DD/MM/YYYY')
inputDate.value = ''
inputDate.value = whichDate
closeDialog();
}