-
Notifications
You must be signed in to change notification settings - Fork 0
/
to.js
160 lines (147 loc) · 5.47 KB
/
to.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
const todoValue = document.getElementById("todoText");
const todoAlert = document.getElementById("Alert");
const listItems = document.getElementById("list-items");
const addUpdate = document.getElementById("AddUpdateClick");
let todo = JSON.parse(localStorage.getItem("todo-list"));
if (!todo) {
todo = [];
}
function CreateToDoItems() {
if (todoValue.value === "") {
todoAlert.innerText = "Please enter your todo text!";
todoValue.focus();
} else {
let IsPresent = false;
todo.forEach((element) => {
if (element.item == todoValue.value) {
IsPresent = true;
}
});
if (IsPresent) {
setAlertMessage("This item already present in the list!");
return;
}
let li = document.createElement("li");
const todoItems = `<div title="Hit Double Click and Complete" ondblclick="CompletedToDoItems(this)">${todoValue.value}</div><div>
<img class="edit todo-controls" onclick="UpdateToDoItems(this)" src="https://cdn0.iconfinder.com/data/icons/graphic-design-tools/512/YPS__marker_pen_pencil_tablet_draw_write_sign_signature_graphic_tool-512.png" />
<img class="delete todo-controls" onclick="DeleteToDoItems(this)" src="http://pluspng.com/img-png/delete-button-png-delete-icon-1600.png" /></div></div>`;
li.innerHTML = todoItems;
listItems.appendChild(li);
if (!todo) {
todo = [];
}
let itemList = { item: todoValue.value, status: false };
todo.push(itemList);
setLocalStorage();
}
todoValue.value = "";
setAlertMessage("Todo item Created Successfully!");
}
function ReadToDoItems() {
todo.forEach((element) => {
let li = document.createElement("li");
let style = "";
if (element.status) {
style = "style='text-decoration: line-through'";
}
const todoItems = `<div ${style} title="Hit Double Click and Complete" ondblclick="CompletedToDoItems(this)">${
element.item
}
${
style === ""
? ""
: '<img class="todo-controls" src="/images/check-mark.png" />'
}</div><div>
${
style === ""
? '<img class="edit todo-controls" onclick="UpdateToDoItems(this)" src="https://cdn0.iconfinder.com/data/icons/graphic-design-tools/512/YPS__marker_pen_pencil_tablet_draw_write_sign_signature_graphic_tool-512.png" />'
: ""
}
<img class="delete todo-controls" onclick="DeleteToDoItems(this)" src="http://pluspng.com/img-png/delete-button-png-delete-icon-1600.png" /></div></div>`;
li.innerHTML = todoItems;
listItems.appendChild(li);
});
}
ReadToDoItems();
function UpdateToDoItems(e) {
if (
e.parentElement.parentElement.querySelector("div").style.textDecoration ===
""
) {
todoValue.value =
e.parentElement.parentElement.querySelector("div").innerText;
updateText = e.parentElement.parentElement.querySelector("div");
addUpdate.setAttribute("onclick", "UpdateOnSelectionItems()");
addUpdate.setAttribute("src", "https://cdn.pixabay.com/photo/2014/04/02/10/55/plus-304947_1280.png");
todoValue.focus();
}
}
function UpdateOnSelectionItems() {
let IsPresent = false;
todo.forEach((element) => {
if (element.item == todoValue.value) {
IsPresent = true;
}
});
if (IsPresent) {
setAlertMessage("This item already present in the list!");
return;
}
todo.forEach((element) => {
if (element.item == updateText.innerText.trim()) {
element.item = todoValue.value;
}
});
setLocalStorage();
updateText.innerText = todoValue.value;
addUpdate.setAttribute("onclick", "CreateToDoItems()");
addUpdate.setAttribute("src", "https://cdn.pixabay.com/photo/2014/04/02/10/55/plus-304947_1280.png");
todoValue.value = "";
setAlertMessage("Todo item Updated Successfully!");
}
function DeleteToDoItems(e) {
let deleteValue =
e.parentElement.parentElement.querySelector("div").innerText;
if (confirm(`Are you sure. Due you want to delete this ${deleteValue}!`)) {
e.parentElement.parentElement.setAttribute("class", "deleted-item");
todoValue.focus();
todo.forEach((element) => {
if (element.item == deleteValue.trim()) {
todo.splice(element, 1);
}
});
setTimeout(() => {
e.parentElement.parentElement.remove();
}, 1000);
setLocalStorage();
}
}
function CompletedToDoItems(e) {
if (e.parentElement.querySelector("div").style.textDecoration === "") {
const img = document.createElement("img");
img.src = "/images/check-mark.png";
img.className = "todo-controls";
e.parentElement.querySelector("div").style.textDecoration = "line-through";
e.parentElement.querySelector("div").appendChild(img);
e.parentElement.querySelector("img.edit").remove();
todo.forEach((element) => {
if (
e.parentElement.querySelector("div").innerText.trim() == element.item
) {
element.status = true;
}
});
setLocalStorage();
setAlertMessage("Todo item Completed Successfully!");
}
}
function setLocalStorage() {
localStorage.setItem("todo-list", JSON.stringify(todo));
}
function setAlertMessage(message) {
todoAlert.removeAttribute("class");
todoAlert.innerText = message;
setTimeout(() => {
todoAlert.classList.add("toggleMe");
}, 1000);
}