-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsideCalendar.ts
154 lines (129 loc) · 3.99 KB
/
sideCalendar.ts
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
import { isToday, setDate } from "./dateHelpers.js";
import { NavDirection } from "./commonTypes.js";
import { SideCalendarState } from "./sideCalendarState.js";
import { assertHTMLElement, unreachable } from "./utils.js";
const MAX_NUMBER_OF_CELLS = 42;
export class SideCalendar {
private currentDateDisplay: HTMLElement;
private cells: NodeListOf<HTMLElement>;
private state: SideCalendarState;
constructor(root: HTMLElement) {
this.currentDateDisplay = assertHTMLElement<HTMLElement>(
".current-date",
root
);
this.cells = root.querySelectorAll(".calendar-dates__cell");
this.state = new SideCalendarState(new Date());
root
.querySelector("#side-calendar-left-arrow")
?.addEventListener("click", () => {
this.navigate(NavDirection.Prev);
});
root
.querySelector("#side-calendar-right-arrow")
?.addEventListener("click", () => {
this.navigate(NavDirection.Next);
});
window.addEventListener("load", () => {
this.rerender();
});
}
private navigate(direction: NavDirection) {
this.updateDisplayDate(direction);
this.rerender();
}
private rerender() {
this.displayCurrentDate();
this.renderSideCalendarCells();
}
private getOffset(direction: NavDirection) {
switch (direction) {
case NavDirection.Next:
return 1;
case NavDirection.Prev:
return -1;
default:
unreachable(direction);
}
}
private updateDisplayDate(direction: NavDirection) {
this.state = new SideCalendarState(
new Date(
this.state.displayDate.getFullYear(),
this.state.displayDate.getMonth() + this.getOffset(direction)
)
);
}
private displayCurrentDate() {
this.currentDateDisplay.innerHTML = `${this.state.displayDate.toLocaleString(
"default",
{ month: "long" }
)} ${this.state?.displayDate.getFullYear()}`;
}
private renderCurrentMonthCells() {
for (let i = 1; i <= this.state.displayMonthLength; i++) {
const currentCell = this.cells[i + this.state.monthStartWeekDay - 1];
currentCell.innerHTML = i.toString();
currentCell.classList.remove("calendar-dates__cell--gray");
if (isToday(setDate(this.state.displayDate, i))) {
this.addHighlight(currentCell);
} else {
this.removeHighlight(currentCell);
}
}
}
private addHighlight(element: HTMLElement) {
element.classList.add("current-day-styling");
}
private removeHighlight(element: HTMLElement) {
element.classList.remove("current-day-styling");
}
private renderPrevMonthCells() {
for (let i = 0; i < this.state.monthStartWeekDay; i++) {
const currentCell = this.cells[i];
currentCell.innerHTML = (
this.state.prevMonthLength -
this.state.monthStartWeekDay +
1 +
i
).toString();
currentCell.classList.add("calendar-dates__cell--gray");
const prevMonth = new Date(this.state.displayDate);
prevMonth.setDate(0);
if (isToday(setDate(prevMonth, i))) {
this.addHighlight(currentCell);
} else {
this.removeHighlight(currentCell);
}
}
}
private renderNextMonthCells() {
for (
let i = 1;
i <=
MAX_NUMBER_OF_CELLS -
this.state.displayMonthLength -
this.state.monthStartWeekDay;
i++
) {
const currentCell =
this.cells[
i + this.state.displayMonthLength + this.state.monthStartWeekDay - 1
];
currentCell.innerHTML = i.toString();
currentCell.classList.add("calendar-dates__cell--gray");
const nextMonth = new Date(this.state.displayDate);
nextMonth.setDate(this.state.displayMonthLength + 1);
if (isToday(setDate(nextMonth, i))) {
this.addHighlight(currentCell);
} else {
this.removeHighlight(currentCell);
}
}
}
private renderSideCalendarCells() {
this.renderCurrentMonthCells();
this.renderPrevMonthCells();
this.renderNextMonthCells();
}
}