-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (166 loc) · 5.68 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
179
180
181
182
const formTrigger = document.querySelector("[data-form]"),
form = document.querySelector(".form"),
formClose = document.querySelector("[data-close]"),
titleTag = document.querySelector(".title"),
pTitle = document.querySelector("header p");
(closeIcon = document.querySelector("header i")),
(descTag = document.querySelector(".description"));
datePicker = document.querySelector(".datePicker");
submitBtn = document.querySelector(".btn_submit");
showArchivedCheckbox = document.querySelector("#showArchived");
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let isUpdate = false, showArchived = false,
updateId;
const notes = JSON.parse(localStorage.getItem("notes") || insertDefault());
showArchivedCheckbox.addEventListener("change", () => {
showArchived = showArchivedCheckbox.checked;
showNotes();
});
formTrigger.addEventListener("click", () => {
submitBtn.innerText = "Add";
datePicker.hidden = true;
categoryTag.hidden=false;
titleTag.value = "";
descTag.value = "";
form.classList.add("show");
form.classList.remove("hide");
});
formClose.addEventListener("click", () => {
form.classList.add("hide");
form.classList.remove("show");
});
let categoryTag = document.getElementById("categories");
function onChange () {
let value = categoryTag.value;
let text = (categoryValue =
categoryTag.options[categoryTag.selectedIndex].text);
}
onChange();
function showNotes () {
if (!notes) return;
document.querySelectorAll(".note").forEach((li) => li.remove());
notes
.filter((note) => note.isArchived === showArchived)
.forEach((note, id) => {
let filterDesc = note.description
? note.description.replaceAll("\n", "<br/>")
: "";
let date = note.dates[note.dates.length - 1];
let dates = note.dates.length > 1
? note.dates[note.dates.length - 1] + ' : ' + note.dates[note.dates.length - 2]
: '';
let liTag = `<table class="note">
<tr class="details">
<td class = "categoryId"><i class ='${note.categoryId}'></i></td>
<td class ="title">${note.title}</td>
<td class ="date"> ${date} </td>
<td class ="categories">${note.category}</td>
<td class="content">${filterDesc}</td>
<td class="dates"> ${dates} </td>
<td class="edit" onClick='updateNote(${note.id} , "${note.title} "," ${note.description}","${date}","${note.categoryId}" )'><i class="uil uil-pen"></i></td>
<td class="arcived" onClick= "archiveNote(${note.id})"><i class="uil uil-folder-plus"></i></td>
<td class="delete" onClick = "deleteNote(${note.id})"><i class="uil uil-trash"></i></td>
</tr>
</table>`;
formTrigger.insertAdjacentHTML("beforebegin", liTag);
});
}
showNotes();
function deleteNote (noteId) {
let valueToDelete = [...notes].find((note) => note.id == noteId);
let index = notes.indexOf(valueToDelete);
notes.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notes));
showNotes();
showCategories();
}
function archiveNote (noteId) {
let valueToUpdate = notes.find((t) => t.id === noteId);
valueToUpdate.isArchived = !valueToUpdate.isArchived;
localStorage.setItem("notes", JSON.stringify(notes));
showNotes();
showCategories();
}
function updateNote(noteId, title, description, date, categoryId) {
let parsetDesc = description ? description.replaceAll("<br/>", "\r\n") : "";
updateId = noteId;
isUpdate = true;
formTrigger.click();
titleTag.value = title;
descTag.value = parsetDesc;
categoryTag.value = categoryId;
submitBtn.innerText = "Update";
datePicker.hidden = false;
datePicker.value = date;
}
function showMenu(elem) {
elem.parentElement.classList.add("show");
document.addEventListener("click", (e) => {
if (e.target.tagName != "I" || e.target != elem) {
elem.parentElement.classList.remove("show");
}
});
}
submitBtn.addEventListener("click", (e) => {
e.preventDefault();
let title = titleTag.value.trim(),
description = descTag.value.trim();
let categoryId = categoryTag.value;
let category = categoryTag.options[categoryTag.selectedIndex].text;
if (title || description) {
if (isUpdate) {
update(title, description, category, categoryId);
} else {
create(title, description, category, categoryId);
}
localStorage.setItem("notes", JSON.stringify(notes));
showNotes();
showCategories();
formClose.click();
}
});
function update(title, description, category, categoryId) {
let valueToUpdate = notes.find((t) => t.id === updateId);
valueToUpdate.title = title;
valueToUpdate.description = description;
valueToUpdate.category = category;
valueToUpdate.categoryId = categoryId;
if (datePicker.value && datePicker.value !== valueToUpdate.date) {
let newDate = new Date(datePicker.value),
month = ("0" + (newDate.getMonth() + 1)).slice(-2);
(day = newDate.getDate()), (year = newDate.getFullYear());
valueToUpdate.dates.push(`${year}-${month}-${day}`);
}
}
function create(title, description, category, categoryId) {
let id = Date.now();
let createdDate = new Date(),
month = ("0" + (createdDate.getMonth() + 1)).slice(-2);
(day = createdDate.getDate()), (year = createdDate.getFullYear());
let isArchived = false;
let dates = [];
dates.push(`${year}-${month}-${day}`);
let noteInfo = {
id,
categoryId,
title,
description,
category,
isArchived,
dates,
};
notes.push(noteInfo);
}