-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
87 lines (72 loc) · 3.07 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
const months = [
"1月", "2月", "3月", "4月",
"5月", "6月", "7月", "8月",
"9月", "10月", "11月", "12月",
];
const monthAndYearDisplay = document.getElementById("monthAndYear");
const daysContainer = document.querySelector(".calendar-days");
const prevBtn = document.getElementById("prevMonth");
const nextBtn = document.getElementById("nextMonth");
const fullWidthChars = "0123456789";
function toFullWidth(num) {
return num.toString().replace(/[0-9]/g, (digit) => fullWidthChars[digit]);
}
let currentDate = new Date();
function renderCalendar() {
const currentMonth = currentDate.getMonth();
const currentYear = currentDate.getFullYear();
const today = new Date();
const todayDate = today.getDate();
const todayMonth = today.getMonth();
const todayYear = today.getFullYear();
const firstDayOfMonth = new Date(currentYear, currentMonth, 1).getDay();
const totalDaysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate();
monthAndYearDisplay.textContent = `${months[currentMonth]} ${toFullWidth(currentYear)}`;
// Optimization: Use DocumentFragment
const fragment = document.createDocumentFragment();
for (let i = 0; i < firstDayOfMonth; i++) {
const day = document.createElement("div");
day.classList.add("empty", `empty-${i}`);
fragment.appendChild(day);
}
for (let i = 1; i <= totalDaysInMonth; i++) {
const day = document.createElement("div");
day.textContent = toFullWidth(i);
// Pre-calculate styling classes
const dayClasses = ["day"];
if (currentYear < todayYear || (currentYear === todayYear && currentMonth < todayMonth) || (currentYear === todayYear && currentMonth === todayMonth && i < todayDate)) {
dayClasses.push("past");
} else if (i === todayDate && currentMonth === todayMonth && currentYear === todayYear) {
dayClasses.push("today");
}
day.className = dayClasses.join(" ");
day.setAttribute('data-year', currentYear);
day.setAttribute('data-month', currentMonth);
day.setAttribute('data-day', i);
fragment.appendChild(day);
}
// Optimization: Append fragment at once
daysContainer.innerHTML = "";
daysContainer.appendChild(fragment);
}
// Event delegation for click handling
daysContainer.addEventListener('click', function(event) {
if (event.target.classList.contains('day')) {
const year = event.target.getAttribute('data-year');
const month = event.target.getAttribute('data-month');
const day = event.target.getAttribute('data-day');
// Handle day click here
console.log(`Clicked on ${year}-${month}-${day}`);
}
});
prevBtn.addEventListener("click", function() {
currentDate.setMonth(currentDate.getMonth() - 1);
renderCalendar();
window.refreshColors();
});
nextBtn.addEventListener("click", function() {
currentDate.setMonth(currentDate.getMonth() + 1);
renderCalendar();
window.refreshColors();
});
renderCalendar();