-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
148 lines (116 loc) · 4.04 KB
/
script.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
const datepicker = document.querySelector(".datepicker");
const dateInput = document.querySelector(".date-input");
const yearInput = datepicker.querySelector(".year-input");
const monthInput = datepicker.querySelector(".month-input");
const cancelBtn = datepicker.querySelector(".cancel");
const applyBtn = datepicker.querySelector(".apply");
const nextBtn = datepicker.querySelector(".next");
const prevBtn = datepicker.querySelector(".prev");
const dates = datepicker.querySelector(".dates");
let selectedDate = new Date();
let year = selectedDate.getFullYear();
let month = selectedDate.getMonth();
// show datepicker
dateInput.addEventListener("click", () => {
datepicker.hidden = false;
});
// hide datepicker
cancelBtn.addEventListener("click", () => {
datepicker.hidden = true;
});
// handle apply button click event
applyBtn.addEventListener("click", () => {
// set the selected date to date input
dateInput.value = selectedDate.toLocaleDateString("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
// hide datepicker
datepicker.hidden = true;
});
// handle next month nav
nextBtn.addEventListener("click", () => {
if (month === 11) year++;
month = (month + 1) % 12;
displayDates();
});
// handle prev month nav
prevBtn.addEventListener("click", () => {
if (month === 0) year--;
month = (month - 1 + 12) % 12;
displayDates();
});
// handle month input change event
monthInput.addEventListener("change", () => {
month = monthInput.selectedIndex;
displayDates();
});
// handle year input change event
yearInput.addEventListener("change", () => {
year = yearInput.value;
displayDates();
});
const updateYearMonth = () => {
monthInput.selectedIndex = month;
yearInput.value = year;
};
const handleDateClick = (e) => {
const button = e.target;
// remove the 'selected' class from other buttons
const selected = dates.querySelector(".selected");
selected && selected.classList.remove("selected");
// add the 'selected' class to current button
button.classList.add("selected");
// set the selected date
selectedDate = new Date(year, month, parseInt(button.textContent));
};
// render the dates in the calendar interface
const displayDates = () => {
// update year & month whenever the dates are updated
updateYearMonth();
// clear the dates
dates.innerHTML = "";
//* display the last week of previous month
// get the last date of previous month
const lastOfPrevMonth = new Date(year, month, 0);
for (let i = 0; i <= lastOfPrevMonth.getDay(); i++) {
const text = lastOfPrevMonth.getDate() - lastOfPrevMonth.getDay() + i;
const button = createButton(text, true, -1);
dates.appendChild(button);
}
//* display the current month
// get the last date of the month
const lastOfMOnth = new Date(year, month + 1, 0);
for (let i = 1; i <= lastOfMOnth.getDate(); i++) {
const button = createButton(i, false);
button.addEventListener("click", handleDateClick);
dates.appendChild(button);
}
//* display the first week of next month
const firstOfNextMonth = new Date(year, month + 1, 1);
for (let i = firstOfNextMonth.getDay(); i < 7; i++) {
const text = firstOfNextMonth.getDate() - firstOfNextMonth.getDay() + i;
const button = createButton(text, true, 1);
dates.appendChild(button);
}
};
const createButton = (text, isDisabled = false, type = 0) => {
const currentDate = new Date();
// determine the date to compare based on the button type
let comparisonDate = new Date(year, month + type, text);
// check if the current button is the date today
const isToday =
currentDate.getDate() === text &&
currentDate.getFullYear() === year &&
currentDate.getMonth() === month;
// check if the current button is selected
const selected = selectedDate.getTime() === comparisonDate.getTime();
const button = document.createElement("button");
button.textContent = text;
button.disabled = isDisabled;
button.classList.toggle("today", isToday && !isDisabled);
button.classList.toggle("selected", selected);
return button;
};
displayDates();