-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (138 loc) · 5.52 KB
/
script.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
156
let days;
let firstDay;
const tasksFromLocalStorage = localStorage.getItem("tasks");
let tasks = [];
if (!tasksFromLocalStorage) {
localStorage.setItem("tasks", []);
} else {
tasks = JSON.parse(tasksFromLocalStorage);
}
document.addEventListener("DOMContentLoaded", function () {
let monthElement = document.getElementById("month");
days = document.getElementById("days");
function createCalendar(year, month) {
let date = new Date();
let y = date.getYear() ;
let m = date.getMonth() ;
let daysInMonth = getDaysInMonth( y , m )
firstDay = new Date(y, m, 1).getDay();
monthElement.textContent = `${date.toLocaleString('default', { month: 'long' })} ${year}`;
days.innerHTML = "";
for (let i = 1-firstDay-2; i <= daysInMonth; i++) {
if (i >= 1) {
let day = document.createElement("div");
day.textContent = i;
days.appendChild(day);
} else {
days.appendChild(document.createElement("div"))
}
}
}
function getDaysInMonth(year, month) {
return new Date(year, month, 0).getDate();
}
let currentdate = new Date();
let currentYear = currentdate.getFullYear();
let currentMonth = currentdate.getMonth();
createCalendar(currentYear, currentMonth);
tasks.forEach((taskItem) => {
const taskText = taskItem.text;
let taskDeadlineValue = taskItem.deadline;
let taskDeadline;
if (!taskDeadlineValue) {
taskDeadline = new Date();
} else {
taskDeadline = new Date(taskDeadlineValue);
}
if (taskText.trim() !== "") {
var completeButton = document.createElement("button");
completeButton.classList.add("circular-btn");
var list = document.getElementById("task");
var task = document.createElement("li");
if (taskItem.completed) {
task.classList.toggle("completed");
}
list.appendChild(task);
task.appendChild(completeButton);
task.setAttribute("data-taskId", taskItem.id)
task.appendChild(document.createTextNode(taskText + " Deadline: " + taskDeadline.getDate() + " " + taskDeadline.getDay()));
completeButton.onclick = markAsComplete;
let dayOfMonth = taskDeadline.getDate();
let dayElement = days.querySelector(`.days div:nth-child(${dayOfMonth+firstDay+2})`);
if (dayElement) {
let taskOnCalendar = document.createElement("div");
taskOnCalendar.classList.add("task-cal");
if (taskItem.completed) {
taskOnCalendar.classList.toggle("completed");
}
taskOnCalendar.setAttribute("data-taskId-on-cal", taskItem.id);
taskOnCalendar.textContent = taskText;
dayElement.appendChild(taskOnCalendar);
} else {
alert("Please enter a valid task and deadline.");
}
}
})
});
function saveTasksToLocalStorage () {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function addTask() {
const taskText = document.getElementById("TaskInput").value;
let taskDeadlineValue = document.getElementById("DeadlineInput").value;
let taskDeadline;
if (!taskDeadlineValue) {
taskDeadline = new Date();
} else {
taskDeadline = new Date(taskDeadlineValue);
}
const id = generateId(8);
tasks.push({id, text: taskText, deadline: taskDeadline, completed: false});
saveTasksToLocalStorage();
// console.log({tasks})
if (taskText.trim() !== "") {
var completeButton = document.createElement("button");
completeButton.classList.add("circular-btn");
var list = document.getElementById("task");
var task = document.createElement("li");
task.setAttribute("data-taskId", id);
list.appendChild(task);
task.appendChild(completeButton);
task.appendChild(document.createTextNode( taskText + " Deadline: " + taskDeadline.getDate()+" , "+ taskDeadline.toLocaleString('default', { month: 'long' })));
completeButton.onclick = markAsComplete;
let dayOfMonth = taskDeadline.getDate();
let dayElement = days.querySelector(`.days div:nth-child(${dayOfMonth+firstDay+2})`);
if (dayElement) {
let taskOnCalendar = document.createElement("div");
taskOnCalendar.setAttribute("data-taskId-on-cal", id);
taskOnCalendar.classList.add("task-cal");
taskOnCalendar.textContent = taskText;
dayElement.appendChild(taskOnCalendar);
} else {
alert("Please enter a valid task and deadline.");
}
}
}
function markAsComplete() {
var listItem = this.parentNode;
console.log({this: this.parentNode})
const taskId = this.parentNode.getAttribute("data-taskId");
console.log({taskId})
tasks.forEach((task) => {
if (task.id == taskId) {
task.completed = !task.completed;
}
})
saveTasksToLocalStorage();
document.querySelector(`[data-taskId-on-cal="${taskId}"]`).classList.toggle("completed");
listItem.classList.toggle("completed");
}
function dec2hex (dec) {
return dec.toString(16).padStart(2, "0")
}
// generateId :: Integer -> String
function generateId (len) {
var arr = new Uint8Array((len || 40) / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}