This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
170 lines (143 loc) · 4.13 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
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
let todos = JSON.parse(localStorage.getItem("todos")) || [];
function createElement(tag, probs, ...children) {
const element = document.createElement(tag);
Object.keys(probs).forEach(key => {
if (key === "style") {
Object.keys(probs[key]).forEach(i => {
element.style[i] = probs.style[i];
});
} else {
element[key] = probs[key];
}
});
children.forEach(item => {
if (typeof item === "string") {
element.innerText = item;
} else {
element.appendChild(item);
}
});
return element;
}
function createTodoItem(data) {
const label = createElement(
"label",
{ className: "my-checkbox" },
createElement("input", {
type: "checkbox",
checked: data.confirm ? true : false
}),
createElement("span", {})
);
const editInput = createElement("input", {
className: "text-edit",
type: "text"
});
const span = createElement("span", { className: "task" }, data.title);
const groupBtn = createElement(
"div",
{ className: "btn-group" },
createElement("i", { className: "fas fa-arrow-left menu" }),
createElement("button", { className: "edit blue-button" }, "Edit"),
createElement("button", { className: "remove red-button" }, "Remove")
);
const li = createElement(
"li",
{ className: `todos-item ${data.confirm ? "complate" : ""}` },
label,
span,
editInput,
groupBtn
);
li.dataset.id = data.id;
bindEvents(li);
return li;
}
document.getElementById("form-add").addEventListener("submit", addTodo);
function menuFunc() {
const menu = this.parentNode;
this.classList.toggle("menu-opened");
menu.classList.toggle("opened");
}
function editTodoItem() {
const item = this.parentNode.parentNode;
const task = item.querySelector(".task");
const editInput = item.querySelector(".text-edit");
const isEditing = item.classList.contains("editing");
const id = this.parentNode.parentNode.dataset.id;
const index = todos.find(item => item.id === +id);
if (isEditing) {
task.innerText = editInput.value;
index.title = editInput.value;
this.innerText = "Edit";
save();
} else {
editInput.value = task.innerText;
this.innerText = "Save";
}
item.classList.toggle("editing");
}
function removeTodoItem() {
const item = this.parentNode.parentNode;
const id = this.parentNode.parentNode.dataset.id;
const index = todos.findIndex(item => item.id === +id);
todos.splice(index, 1);
save();
todosList.removeChild(item);
counItems.innerText = getCountTodos();
}
function toggleTodoItem() {
const item = this.parentNode.parentNode;
item.classList.toggle("complate");
}
function bindEvents(todoItem) {
const menu = todoItem.querySelector(".menu");
const edit = todoItem.querySelector(".edit");
const remove = todoItem.querySelector(".remove");
const checkbox = todoItem.querySelector(".my-checkbox input");
menu.addEventListener("click", menuFunc);
edit.addEventListener("click", editTodoItem);
remove.addEventListener("click", removeTodoItem);
checkbox.addEventListener("click", toggleTodoItem);
}
function addTodo(e) {
e.preventDefault();
const input = document.getElementById("input");
const data = {
title: input.value,
confirm: false,
id: new Date().getTime()
};
todosList.appendChild(createTodoItem(data));
counItems.innerText = getCountTodos();
todos.push(data);
save();
input.value = "";
}
function findTodoItem(id) {
return todos.find(item => item.id == id);
}
function save() {
localStorage.setItem("todos", JSON.stringify(todos));
}
function getCountTodos() {
return document.querySelectorAll(".todos-item").length;
}
function load() {
let todos = JSON.parse(localStorage.getItem("todos"));
console.log(todos);
for (let i in todos) {
todosList.appendChild(createTodoItem(todos[i]));
}
counItems.innerText = getCountTodos();
return todosList;
}
const todosList = document.getElementById("todos-list");
const counItems = document.getElementById("count-items");
const todoItems = document.querySelectorAll(".todos-item");
function main() {
todoItems.forEach(item => bindEvents(item));
counItems.innerText = getCountTodos();
load();
}
main();