-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
115 lines (105 loc) · 2.92 KB
/
app.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
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
const weekdays = ["S", "M", "T", "W", "T", "F", "S"];
const generateCalendar = _ => {
// remove any previously generated elements
document.querySelectorAll(".calendar").forEach(node => node.remove());
document.querySelectorAll("h1").forEach(node => node.remove());
// and then make the new
let year = Number.parseInt(document.querySelector("#year").value);
let month = Number.parseInt(document.querySelector("#month").value);
for(let index = 0; index < 12; index++) {
makeMonth(month, year);
if(month === 11) {
year = year + 1;
}
month = (month + 1) % 12;
}
};
const makeMonth = (month, year) => {
const first = firstDay(year, month);
const last = lastDay(year, month);
const heading = makeHeading(first);
const calendar = makeCalendar();
const running = new Date(first);
const emptyNeeded = first.getDay() === 0 ? 6 : first.getDay() - 1;
let dayCount = 1;
for(let index = 0; index < emptyNeeded; index++) {
const empty = makeEmpty(running);
calendar.appendChild(empty);
dayCount++;
}
while(running.getDate() < last.getDate()) {
const day = makeDay(running, dayCount);
calendar.appendChild(day);
running.setDate(running.getDate() + 1);
dayCount++;
}
const day = makeDay(last);
calendar.appendChild(day);
document.body.appendChild(heading);
document.body.appendChild(calendar);
};
const makeCalendar = _ => {
const el = document.createElement("div");
el.classList.add("calendar");
return el;
};
const makeEmpty = _ => {
const el = document.createElement("div");
el.classList.add("empty");
return el;
};
const makeHeading = date => {
const el = document.createElement("h1");
el.innerText = months[date.getMonth()];
return el;
};
const makeDay = (date, dayCount) => {
const number = date.getDate();
const wd = weekdays[date.getDay()];
const day = document.createElement("div");
day.classList.add("day");
if(dayCount <= 7) {
day.classList.add("top");
}
const weekday = document.createElement("div");
weekday.classList.add("weekday");
weekday.innerText = wd;
day.appendChild(weekday);
const dayNumber = document.createElement("h2");
dayNumber.innerText = number;
day.appendChild(dayNumber);
return day;
};
const firstDay = (year, month) => {
return new Date(year, month, 1);
};
const lastDay = (year, month) => {
return new Date(year, month + 1, 0);
};
const populateYears = _ => {
const date = new Date();
const select = document.querySelector("#year");
for(let index = 0; index < 10; index++) {
const option = document.createElement("option");
option.setAttribute("value", date.getFullYear());
option.innerText = date.getFullYear();
select.appendChild(option);
date.setFullYear(date.getFullYear() + 1);
}
};
document.querySelector("#generate").addEventListener("click", generateCalendar);
window.addEventListener("load", populateYears);