-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (69 loc) · 2.54 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
const storeData = data => window.localStorage.setItem("data", JSON.stringify(data))
const getData = key => window.localStorage.getItem(key)
const addBtn = document.getElementById("add-btn");
const input = document.getElementById("text-input");
const list = document.querySelector("ul");
const data = JSON.parse(getData("data") || "[]");
// Click Listener for button
addBtn.addEventListener("click", () => {
if(input.value) { // TO make sure the text is not empty
data.push({
id: Math.floor(Math.random() * 10000), // Random Id
text: input.value,
isFinished: false,
color: getColor(),
});
storeData(data); // to store the new data
showData(data);
}
input.value = "";
});
const getColor = () => { // Random color
return {
red: Math.random() * 256,
green: Math.random() * 256,
blue: Math.random() * 256,
}
}
const showData = (data) => {
list.innerHTML = ""; // To make sure the list is empty
data.map((item) => { // Loop
list.innerHTML += `
<li style="background-color: rgb(${item.color.red}, ${item.color.green}, ${item.color.blue})">
<p style="text-decoration: ${item.isFinished ? "line-through": "none"};
color:${isLight(item.color.red, item.color.green, item.color.blue) ? "white": "black"}">
${item.text}
</p>
<div class="delete-edit">
<button class="delete" onClick="remove(${item.id})">
<img src="${isLight(item.color.red, item.color.green, item.color.blue) ?
"./assets/icons/delete-white.png": "./assets/icons/delete.png"}" alt="delete"/>
</button>
<button id="check-btn" class="edit" onClick="check(${item.id})">
<img src="${isLight(item.color.red, item.color.green, item.color.blue) ?
"./assets/icons/check-white.png": "./assets/icons/check.png"}" alt="edit"/>
</button>
</div>
</li>
`
});
}
const check = (id) => {
const index = data.findIndex((item) => item.id === id);
data[index].isFinished = !data[index].isFinished;
storeData(data); // to store the new data
showData(data);
}
const remove = (id) => {
const index = data.findIndex((item) => item.id === id);
if(index != -1) {
data.splice(index, 1);
storeData(data); // to store the new data
showData(data);
}
}
const isLight = (r, g, b) => {
const yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
return yiq < 128;
}
showData(data);