-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (117 loc) · 3.09 KB
/
app.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
// variable for storing data and colors
var data = {
notes: [],
currentColor: "#ffffff",
};
// button colors
var colors = [
"#ffffff",
"#f28b82",
"#fbbc04",
"#fff475",
"#ccff90",
"#a7ffeb",
"#cbf0f8",
"#aecbfa",
"#d7aefb",
"#fdcfe8",
"#e6c9a8",
"#e8eaed",
];
// listener for enter key
document.querySelector(".addtext").addEventListener("keyup", (e) => {
if (event.keyCode === 13) {
e.preventDefault();
addNote();
}
});
// listener for submit button
document.querySelector("#submit").addEventListener("click", () => {
addNote();
});
// listener to change color
document.querySelectorAll(".colors button").forEach((button) => {
button.addEventListener("click", () => {
changeColor();
});
});
// function for adding new note to the list
var addNote = () => {
if (document.querySelector(".addtext").value !== "") {
let value = document.querySelector(".addtext").value;
document.querySelector(".addtext").value = "";
data.notes.push(value);
renderElement();
updateStorage();
}
};
// function for changing color
var changeColor = () => {
document.querySelectorAll(".note, .addNote, .colorDiv").forEach((note) => {
if (event.srcElement.style.background !== undefined) {
note.style.background = event.srcElement.style.background;
data.currentColor = event.srcElement.style.background;
updateStorage();
} else {
note.style.background = data.currentColor;
}
});
};
// function for rendering elements to the page
var renderElement = () => {
let note = document.querySelectorAll(".note");
note.forEach((i) => {
i.outerHTML = "";
});
for (let i in data.notes) {
let note = document.createElement("div");
note.className = "note";
let text = document.createElement("input");
text.contentEditable = "true";
text.addEventListener("change", () => {
data.notes[i] = text.value;
updateStorage();
});
text.value = data.notes[i];
note.appendChild(text);
button = document.createElement("button");
button.className = "delete";
button.innerHTML = "Trash";
button.addEventListener("click", () => {
data.notes.splice(i, 1);
renderElement();
updateStorage();
});
note.appendChild(button);
document
.querySelector(".container")
.insertBefore(note, document.querySelector(".colorDiv"));
}
document.querySelectorAll(".note, .addNote, .colorDiv").forEach((note) => {
note.style.background = data.currentColor;
});
};
// function for updating local storage
var updateStorage = () => {
localStorage.data = JSON.stringify(data);
};
// Event listener for updating and adding stuff after page load
window.addEventListener("load", () => {
if (localStorage.data) {
data = JSON.parse(localStorage.data);
} else {
data.notes = [
"You can add notes",
"Delete notes",
"Edit notes",
"Change color",
"All changes are stored in local storage",
];
}
var i = 0;
document.querySelectorAll(".colors button").forEach((button) => {
button.style.background = colors[i];
i += 1;
});
renderElement();
});