-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.js
155 lines (129 loc) · 5.08 KB
/
calendar.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
// calendar.js
class Calendar {
constructor(options = {}) {
// Default options
this.options = {
monthNames: [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
],
weekNames: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
buttonTexts: {
prevMonth: 'Previous Month',
nextMonth: 'Next Month',
prevYear: 'Previous Year',
nextYear: 'Next Year',
viewYear: 'View Full Year'
},
...options // Override defaults with user options
};
// Initialize calendar state
this.currentYear = new Date().getFullYear();
this.currentMonth = new Date().getMonth();
// Render initial calendar
this.render();
}
getMonthName(month) {
return this.options.monthNames[month];
}
getFirstDayOfMonth(year, month) {
return new Date(year, month, 1).getDay();
}
getDaysInMonth(year, month) {
return new Date(year, month + 1, 0).getDate();
}
generateCalendar(year, month) {
const daysInMonth = this.getDaysInMonth(year, month);
const firstDay = this.getFirstDayOfMonth(year, month);
const calendar = [];
let week = [];
for (let i = 0; i < firstDay; i++) {
week.push('');
}
for (let day = 1; day <= daysInMonth; day++) {
week.push(day);
if ((firstDay + day) % 7 === 0) {
calendar.push(week);
week = [];
}
}
if (week.length > 0) {
calendar.push(week);
}
return calendar;
}
renderCalendar(year, month) {
const calendar = this.generateCalendar(year, month);
const monthName = this.getMonthName(month);
let html = `<h2>${monthName} ${year}</h2>`;
html += '<table class="calendar"><thead><tr>';
for (const dayName of this.options.weekNames) {
html += `<th>${dayName}</th>`;
}
html += '</tr></thead><tbody>';
for (const week of calendar) {
html += '<tr>';
for (const day of week) {
html += `<td${day === '' ? ' class="empty"' : ''}>${day}</td>`;
}
html += '</tr>';
}
html += '</tbody></table>';
return html;
}
renderYearCalendar(year) {
const monthNames = this.options.monthNames;
let html = `<h2>${year}</h2>`;
for (let month = 0; month < 12; month++) {
html += `<h3>${monthNames[month]}</h3>`;
html += this.renderCalendar(year, month);
}
return html;
}
displayMonthCalendar(year, month) {
const calendarContainer = document.getElementById('calendar');
calendarContainer.innerHTML = this.renderCalendar(year, month);
}
displayYearCalendar(year) {
const calendarContainer = document.getElementById('calendar');
calendarContainer.innerHTML = this.renderYearCalendar(year);
}
updateCalendar(action) {
if (action === 'prev') {
if (this.currentMonth === 0) {
this.currentMonth = 11;
this.currentYear--;
} else {
this.currentMonth--;
}
} else if (action === 'next') {
if (this.currentMonth === 11) {
this.currentMonth = 0;
this.currentYear++;
} else {
this.currentMonth++;
}
} else if (action === 'prev-year') {
this.currentYear--;
} else if (action === 'next-year') {
this.currentYear++;
}
this.displayMonthCalendar(this.currentYear, this.currentMonth);
}
render() {
const calendarContainer = document.getElementById('calendar');
calendarContainer.innerHTML = this.renderCalendar(this.currentYear, this.currentMonth);
document.getElementById('prev-month').textContent = this.options.buttonTexts.prevMonth;
document.getElementById('next-month').textContent = this.options.buttonTexts.nextMonth;
document.getElementById('prev-year').textContent = this.options.buttonTexts.prevYear;
document.getElementById('next-year').textContent = this.options.buttonTexts.nextYear;
document.getElementById('view-year').textContent = this.options.buttonTexts.viewYear;
document.getElementById('prev-month').addEventListener('click', () => this.updateCalendar('prev'));
document.getElementById('next-month').addEventListener('click', () => this.updateCalendar('next'));
document.getElementById('prev-year').addEventListener('click', () => this.updateCalendar('prev-year'));
document.getElementById('next-year').addEventListener('click', () => this.updateCalendar('next-year'));
document.getElementById('view-year').addEventListener('click', () => this.displayYearCalendar(this.currentYear));
}
}
// Export as module
export default Calendar;