-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
157 lines (123 loc) · 5.23 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
import { taskBox, formWrapper, updateForm } from "./DOM_elements.js";
var x = window.localStorage.length + 1;
if (x > 1) {
// Array Of Local Storage Keys
let mySavedNotes = Object.keys(window.localStorage);
mySavedNotes = mySavedNotes.filter((ele) => ele != "darkMode");
// Insert Elements Into The Page
for (let i = 0; i < mySavedNotes.length; i++) {
let previousNotes = JSON.parse(window.localStorage.getItem(mySavedNotes[i])) ?? {};
let oldNoteBox = taskBox.cloneNode(true);
oldNoteBox.querySelector("h5").textContent = previousNotes.title;
let taskDetailsStyled = previousNotes.details.split("//");
for (let i = 0; i < taskDetailsStyled.length; i++) {
oldNoteBox.querySelector("p").appendChild(document.createTextNode(taskDetailsStyled[i]));
oldNoteBox.querySelector("p").appendChild(document.createElement("br"));
}
oldNoteBox.style.backgroundColor = previousNotes.color;
oldNoteBox.setAttribute("data-order", i + 1);
document.querySelector("section").appendChild(oldNoteBox);
}
}
// Get Task Infos
let taskName = document.getElementById("title");
let taskDetails = document.getElementById("description");
document.getElementById("add").onclick = function () {
// Generate Note Backgound Color
let colorScheme = document.querySelectorAll("input[type='radio']");
var color = "#000";
colorScheme.forEach(function (ele) {
if (ele.checked) {
color = ele.value;
}
});
if (taskName.value.trim() != "" && taskDetails.value.trim() != "") {
// Create New Box
let taskPrototype = taskBox.cloneNode(true);
// Set Note Title
taskPrototype.querySelector("h5").textContent = taskName.value.trim();
// Set Note Details
//? Making The Ability To Insert Line Breaks Without Using innerHTML Method
let taskDetailsStyled = taskDetails.value.split("//");
// Add Note Title, Details & Color To Local Storage
let newNote = {
title: taskName.value.trim(),
details: taskDetails.value,
color: color,
};
localStorage.setItem(`${newNote.title.replaceAll(" ", "_")}`, JSON.stringify(newNote));
for (let i = 0; i < taskDetailsStyled.length; i++) {
taskPrototype.querySelector("p").appendChild(document.createTextNode(taskDetailsStyled[i]));
taskPrototype.querySelector("p").appendChild(document.createElement("br"));
}
// Clear Input Fields When Creating The Note
taskName.value = "";
taskDetails.value = "";
//! taskPrototype.className = `task-${x}` will override all classes values
taskPrototype.classList.add(`task-${x}`);
taskPrototype.setAttribute("data-order", x);
taskPrototype.style.setProperty("background-color", color);
document.querySelector("section").appendChild(taskPrototype);
x++;
}
};
addEventListener("click", function (evt) {
if (evt.target.classList.contains("discard-task")) {
let targetBox = evt.target.parentElement.parentElement.parentElement;
// Discard The Target Element From The Local Storage
window.localStorage.removeItem(`${targetBox.querySelector("h5").textContent.replaceAll(" ", "_")}`);
// Remove Element From The HTML Page
targetBox.remove();
// Sort Notes Order After Deleting An Element
document.querySelectorAll(".task-box").forEach(function (note, order) {
note.setAttribute("data-order", order + 1);
});
}
});
// Update Note
addEventListener("click", function (evt) {
if (evt.target.textContent == "Update") {
let noteBox = evt.target.parentElement.parentElement.parentElement;
let previousNoteTitle = noteBox.querySelector("h5").textContent;
let previousNoteDesc = noteBox.querySelector("p").textContent;
let previousNoteBg = noteBox.style.backgroundColor;
updateForm.querySelector("#noteTitleUpdate").value = previousNoteTitle;
updateForm.querySelector("#noteDescriptionUpdate").value = previousNoteDesc;
document.body.appendChild(formWrapper);
// Update
formWrapper.querySelector("#update").onclick = function () {
let newData = {
title: formWrapper.querySelector("#noteTitleUpdate").value.trim(),
details: formWrapper.querySelector("#noteDescriptionUpdate").value.trim(),
color: previousNoteBg,
};
if (previousNoteTitle === newData.title) {
window.localStorage.setItem(previousNoteTitle.replaceAll(" ", "_"), JSON.stringify(newData));
} else {
window.localStorage.setItem(newData.title.replaceAll(" ", "_"), JSON.stringify(newData));
window.localStorage.removeItem(previousNoteTitle.replaceAll(" ", "_"));
}
noteBox.remove();
formWrapper.remove();
window.location.reload();
};
// Cancel
formWrapper.querySelector("#cancel").onclick = function () {
formWrapper.remove();
};
}
});
// Switch To Dark Mode
document.querySelector(".dark-mode-switcher").onclick = function () {
this.classList.toggle("active");
document.body.classList.toggle("night-mode");
if (this.classList.contains("active")) {
window.localStorage.setItem("darkMode", true);
} else {
window.localStorage.setItem("darkMode", false);
}
};
if (window.localStorage.getItem("darkMode") == "true") {
document.body.classList.add("night-mode");
document.querySelector(".dark-mode-switcher").classList.add("active");
}