-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
265 lines (216 loc) · 8.3 KB
/
Copy pathscript.js
File metadata and controls
265 lines (216 loc) · 8.3 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const MONTH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
const DAYS_IN_MONTH = [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const calendar = document.querySelector('.calendar');
const calendarDays = calendar.querySelector('.calendar-days');
const monthElement = document.querySelector('#month');
const yearElement = calendar.querySelector('#year');
const taskList = document.getElementById("task-list");
const inputBox = document.getElementById("input-box");
const darkModeToggle = document.querySelector('.dark-mode-switch');
const state = {
selectedDate: null,
currentMonth: new Date().getMonth(),
currentYear: new Date().getFullYear(),
todayDate: new Date()
};
const isLeapYear = (year) => {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
};
const getFebruaryDays = (year) => isLeapYear(year) ? 29 : 28;
const getMonthDays = (month, year) => {
return month === 1 ? getFebruaryDays(year) : DAYS_IN_MONTH[month];
};
const formatDateKey = (year, month, day) => {
return `tasks_${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
};
const getSelectedDateKey = () => {
if (!state.selectedDate) return null;
const [year, month, day] = state.selectedDate.split('-');
return formatDateKey(year, month, day);
};
const createTaskElement = (text, isChecked) => {
const li = document.createElement("li");
const textNode = document.createTextNode(text);
const deleteButton = document.createElement("span");
li.appendChild(textNode);
deleteButton.textContent = "×";
li.appendChild(deleteButton);
if (isChecked) {
li.classList.add("checked");
}
return li;
};
const getTasksFromStorage = () => {
const dateKey = getSelectedDateKey();
return JSON.parse(localStorage.getItem(dateKey)) || [];
};
const saveTasksToStorage = (tasks) => {
const dateKey = getSelectedDateKey();
localStorage.setItem(dateKey, JSON.stringify(tasks));
};
const addTask = () => {
const taskText = inputBox.value.trim();
if (taskText && state.selectedDate) {
const li = createTaskElement(taskText, false);
taskList.appendChild(li);
const tasksForDate = getTasksFromStorage();
tasksForDate.push({ text: taskText, checked: false });
saveTasksToStorage(tasksForDate);
inputBox.value = "";
}
};
const updateTaskList = () => {
const updatedTasks = Array.from(taskList.querySelectorAll("li")).map(li => ({
text: li.firstChild.textContent,
checked: li.classList.contains("checked")
}));
saveTasksToStorage(updatedTasks);
};
const loadTasksForDate = (date) => {
state.selectedDate = date;
taskList.innerHTML = "";
const tasksForDate = getTasksFromStorage();
tasksForDate.forEach(task => {
const li = createTaskElement(task.text, task.checked);
taskList.appendChild(li);
});
};
const createCalendarDay = (dayNumber, isCurrentDay = false) => {
const day = document.createElement('div');
day.classList.add('calendar-day-hover');
day.innerHTML = `${dayNumber}<span></span><span></span><span></span><span></span>`;
if (isCurrentDay) {
day.classList.add('curr-date');
}
return day;
};
const createEmptyDay = () => {
const day = document.createElement('div');
day.classList.add('empty-day');
return day;
};
const generateCalendar = (month = state.currentMonth, year = state.currentYear) => {
calendarDays.innerHTML = '';
monthElement.innerHTML = MONTH_NAMES[month];
yearElement.innerHTML = year;
const firstDay = new Date(year, month, 1);
const firstDayOfWeek = firstDay.getDay();
const daysInMonth = getMonthDays(month, year);
Array.from({ length: firstDayOfWeek }).forEach(() => {
calendarDays.appendChild(createEmptyDay());
});
Array.from({ length: daysInMonth }).forEach((_, index) => {
const dayNumber = index + 1;
const isCurrentDay = !state.hasUserClicked &&
dayNumber === state.todayDate.getDate() &&
year === state.todayDate.getFullYear() &&
month === state.todayDate.getMonth();
const day = createCalendarDay(dayNumber, isCurrentDay);
calendarDays.appendChild(day);
});
attachDayClickEvents();
if (!state.hasUserClicked &&
month === state.todayDate.getMonth() &&
year === state.todayDate.getFullYear()) {
const today = calendarDays.children[firstDayOfWeek + state.todayDate.getDate() - 1];
if (today) {
today.classList.add('curr-date');
}
}
};
const attachDayClickEvents = () => {
document.querySelectorAll('.calendar-days div:not(.empty-day)').forEach(day => {
day.addEventListener('click', () => {
state.hasUserClicked = true;
document.querySelectorAll('.calendar-days div').forEach(d => {
d.classList.remove('selected');
d.classList.remove('curr-date');
});
day.classList.add('selected');
const selectedDay = parseInt(day.textContent.trim());
const isToday =
selectedDay === state.todayDate.getDate() &&
state.currentMonth === state.todayDate.getMonth() &&
state.currentYear === state.todayDate.getFullYear();
if (isToday) {
day.classList.add('selected');
}
const selectedMonth = state.currentMonth + 1;
const selectedYear = state.currentYear;
state.selectedDate = `${selectedYear}-${String(selectedMonth).padStart(2, '0')}-${String(selectedDay).padStart(2, '0')}`;
loadTasksForDate(state.selectedDate);
});
});
};
const initializeCalendar = () => {
document.querySelectorAll('.calendar-days div').forEach(day => {
const dayNumber = parseInt(day.textContent.trim());
const isToday =
dayNumber === state.todayDate.getDate() &&
state.currentMonth === state.todayDate.getMonth() &&
state.currentYear === state.todayDate.getFullYear();
if (isToday) {
day.classList.add('curr-date');
}
});
};
const handleCalendarNavigation = {
prevMonth: () => {
if (state.currentMonth === 0) {
state.currentMonth = 11;
state.currentYear--;
} else {
state.currentMonth--;
}
generateCalendar();
},
nextMonth: () => {
if (state.currentMonth === 11) {
state.currentMonth = 0;
state.currentYear++;
} else {
state.currentMonth++;
}
generateCalendar();
},
prevYear: () => {
state.currentYear--;
generateCalendar();
},
nextYear: () => {
state.currentYear++;
generateCalendar();
}
};
const handleTaskEvents = (e) => {
if (e.target.tagName === "LI") {
e.target.classList.toggle("checked");
updateTaskList();
} else if (e.target.tagName === "SPAN") {
const taskItem = e.target.parentElement;
const taskText = taskItem.firstChild.textContent;
const tasksForDate = getTasksFromStorage()
.filter(task => task.text !== taskText);
saveTasksToStorage(tasksForDate);
taskItem.remove();
}
};
document.querySelector('#prev-month').onclick = handleCalendarNavigation.prevMonth;
document.querySelector('#next-month').onclick = handleCalendarNavigation.nextMonth;
document.querySelector('#prev-year').onclick = handleCalendarNavigation.prevYear;
document.querySelector('#next-year').onclick = handleCalendarNavigation.nextYear;
taskList.addEventListener("click", handleTaskEvents);
darkModeToggle.onclick = () => {
document.querySelector('body').classList.toggle('light');
document.querySelector('body').classList.toggle('dark');
};
document.addEventListener("DOMContentLoaded", () => {
const today = new Date();
state.selectedDate = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
state.hasUserClicked = false;
generateCalendar();
loadTasksForDate(state.selectedDate);
});