-
Notifications
You must be signed in to change notification settings - Fork 0
/
calenderPage.js
136 lines (106 loc) · 3.66 KB
/
calenderPage.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
let nav = 0;
let clicked = null;
let events = localStorage.getItem('events') ? JSON.parse(localStorage.getItem('events')) : [];
const calendar = document.getElementById('calendar');
const newEventModal = document.getElementById('newEventModal');
const deleteEventModal = document.getElementById('deleteEventModal');
const backDrop = document.getElementById('modalBackDrop');
const eventTitleInput = document.getElementById('eventTitleInput');
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
function openModal(date) {
clicked = date;
const eventForDay = events.find(e => e.date === clicked);
if (eventForDay) {
document.getElementById('eventText').innerText = eventForDay.title;
deleteEventModal.style.display = 'block';
} else {
newEventModal.style.display = 'block';
}
backDrop.style.display = 'block';
}
function load() {
const dt = new Date();
if (nav !== 0) {
dt.setMonth(new Date().getMonth() + nav);
}
const day = dt.getDate();
const month = dt.getMonth();
const year = dt.getFullYear();
const firstDayOfMonth = new Date(year, month, 1);
const daysInMonth = new Date(year, month + 1, 0).getDate();
const dateString = firstDayOfMonth.toLocaleDateString('en-us', {
weekday: 'long',
year: 'numeric',
month: 'numeric',
day: 'numeric',
});
const paddingDays = weekdays.indexOf(dateString.split(', ')[0]);
document.getElementById('monthDisplay').innerText =
`${dt.toLocaleDateString('en-us', { month: 'long' })} ${year}`;
calendar.innerHTML = '';
for(let i = 1; i <= paddingDays + daysInMonth; i++) {
const daySquare = document.createElement('div');
daySquare.classList.add('day');
const dayString = `${month + 1}/${i - paddingDays}/${year}`;
if (i > paddingDays) {
daySquare.innerText = i - paddingDays;
const eventForDay = events.find(e => e.date === dayString);
if (i - paddingDays === day && nav === 0) {
daySquare.id = 'currentDay';
}
if (eventForDay) {
const eventDiv = document.createElement('div');
eventDiv.classList.add('event');
eventDiv.innerText = eventForDay.title;
daySquare.appendChild(eventDiv);
}
daySquare.addEventListener('click', () => openModal(dayString));
} else {
daySquare.classList.add('padding');
}
calendar.appendChild(daySquare);
}
}
function closeModal() {
eventTitleInput.classList.remove('error');
newEventModal.style.display = 'none';
deleteEventModal.style.display = 'none';
backDrop.style.display = 'none';
eventTitleInput.value = '';
clicked = null;
load();
}
function saveEvent() {
if (eventTitleInput.value) {
eventTitleInput.classList.remove('error');
events.push({
date: clicked,
title: eventTitleInput.value,
});
localStorage.setItem('events', JSON.stringify(events));
closeModal();
} else {
eventTitleInput.classList.add('error');
}
}
function deleteEvent() {
events = events.filter(e => e.date !== clicked);
localStorage.setItem('events', JSON.stringify(events));
closeModal();
}
function initButtons() {
document.getElementById('nextButton').addEventListener('click', () => {
nav++;
load();
});
document.getElementById('backButton').addEventListener('click', () => {
nav--;
load();
});
document.getElementById('saveButton').addEventListener('click', saveEvent);
document.getElementById('cancelButton').addEventListener('click', closeModal);
document.getElementById('deleteButton').addEventListener('click', deleteEvent);
document.getElementById('closeButton').addEventListener('click', closeModal);
}
initButtons();
load();