-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
178 lines (150 loc) · 4.96 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"Novermber",
"December",
];
const lang = navigator.language;
const d = new Date();
const dayname = d.toLocaleString(lang, { weekday: "long" });
const daynumber = d.getDate();
const month = months[d.getMonth()];
document.querySelector(".date").innerHTML = `${dayname} , ${daynumber}th`;
document.querySelector(".month").innerHTML = month;
const taskinput = document.querySelector(".task-input");
const taskaddbtn = document.querySelector(".add-task-btn");
let filter = document.querySelectorAll(".task-menu-container span");
const checkbox = document.querySelector(".checkbox");
const todos = JSON.parse(localStorage.getItem("todo-list") || "[]");
console.log(todos);
let UpdateId;
let isUpdate = false;
filter.forEach((btn) => {
btn.addEventListener("click", () => {
document.querySelector("span.active").classList.remove("active");
btn.classList.add("active");
showTodos(btn.id);
});
});
function showTodos(filter) {
document.querySelectorAll(".task-list").forEach((todo) => {
todo.remove();
});
let liTag = "";
todos.forEach((todo, id) => {
let isCompleted = todo.status == "completed" ? "checked" : "";
if (filter == todo.status || filter == "all") {
liTag += `<div class="task-list">
<label for="${id}">
<input onclick="taskcomplete(this)"
type="checkbox" id="${id}" class="checkbox"
onclick="taskcomplete(this)" ${isCompleted}
>
<h3 class="task ${isCompleted}">${todo.name}</h3>
</label>
<i onclick="showMenu(this)" class="fa-solid fa-ellipsis" ></i>
<div class="menu">
<span onclick="editTask(${id},'${todo.name}')" class="edit-btn">
<i class="fa-solid fa-pen"></i>
Edit
</span>
<span onclick="deleteTask(${id})" class="delete-btn">
<i class="fa-solid fa-trash"></i>
Delete
</span>
</div>
</div>`;
}
});
document.querySelector(".task-list-container").innerHTML =
liTag ||
`
<span><i class="fa-solid fa-clipboard-list"></i></span>
<span class="current-time ">Today time is</span>
<span class="no-task-message">No task here yet</span>`;
}
showTodos("all");
taskaddbtn.addEventListener("click", (e) => {
if (taskinput.value != "") {
var divlist = document.querySelectorAll(".task-list").length;
document.querySelector(".number-of-tasks").innerHTML = divlist + " Tasks";
let userTask = taskinput.value;
if (!isUpdate) {
let taskinfo = { name: userTask, status: "Pending" };
todos.push(taskinfo);
} else {
isUpdate = false;
todos[UpdateId].name = userTask;
}
localStorage.setItem("todo-list", JSON.stringify(todos));
taskinput.value = "";
showTodos("all");
}
var divlist = document.querySelectorAll(".task-list").length;
document.querySelector(".number-of-tasks").innerHTML = divlist + " Tasks";
});
function taskcomplete(elem) {
if (elem.checked) {
elem.nextElementSibling.classList.add("checked");
todos[elem.id].status = "Completed";
} else {
elem.nextElementSibling.classList.remove("checked");
todos[elem.id].status = "Pending";
}
localStorage.setItem("todo-list", JSON.stringify(todos));
}
function showMenu(selectelem) {
selectelem.parentElement.lastElementChild.classList.add("show");
document.addEventListener("click", (e) => {
if (e.target.tagName != "I" || e.target != selectelem) {
selectelem.parentElement.lastElementChild.classList.remove("show");
}
});
}
function deleteTask(deleteId) {
todos.splice(deleteId, 1);
localStorage.setItem("todo-list", JSON.stringify(todos));
showTodos("all");
var divlist = document.querySelectorAll(".task-list").length;
document.querySelector(".number-of-tasks").innerHTML = divlist + " Tasks";
}
function editTask(taskId, taskName) {
isUpdate = true;
UpdateId = taskId;
taskinput.value = taskName;
taskinput.focus();
}
document.querySelector(".clear-all-btn").addEventListener("click", () => {
todos.splice(0, todos.length);
localStorage.setItem("todo-list", JSON.stringify(todos));
showTodos("all");
var divlist = document.querySelectorAll(".task-list").length;
document.querySelector(".number-of-tasks").innerHTML = divlist + " Tasks";
});
var divlist = document.querySelectorAll(".task-list").length;
document.querySelector(".number-of-tasks").innerHTML = divlist + " Tasks";
///// extra timing function
const CurrentTime = document.querySelector(".current-time");
console.log(CurrentTime);
function putzero(num){
return (num<10)?`0${num}`:num
}
function showTime() {
let cdate = new Date();
let hours = cdate.getHours()
let AmOrPm = (hours>12)?`Pm`:`Am`
let stringDate = `${putzero( hours%12)} : ${putzero(cdate.getMinutes()) } : ${ putzero(cdate.getSeconds()) } ${AmOrPm}`
CurrentTime.innerHTML = stringDate
setTimeout(function(){
showTime()
},1000)
}
showTime();