-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.js
More file actions
139 lines (125 loc) · 3.18 KB
/
todo.js
File metadata and controls
139 lines (125 loc) · 3.18 KB
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
const pending = document.querySelector(".pending_items");
const finished = document.querySelector(".finished_items");
const todoForm = document.querySelector(".todo_form");
const inputToDo = document.querySelector(".input_todo");
let pendingList, finishedList;
function getTextObj(text) {
return {
id: String(Date.now()),
text,
};
}
function savePending(text) {
pendingList.push(text);
}
function removePending(textId) {
pendingList = pendingList.filter((text) => {
return text.id !== textId;
});
}
function removeFinished(textId) {
finishedList = finishedList.filter((text) => {
return text.id !== textId;
});
}
function findInPending(textId) {
return pendingList.find((text) => {
return text.id === textId;
});
}
function findInFinished(textId) {
return finishedList.find((text) => {
return text.id === textId;
});
}
function addFinished(text) {
finishedList.push(text);
}
function addPending(text) {
pendingList.push(text);
}
function delText(e) {
const li = e.target.parentNode;
li.parentNode.removeChild(li);
removeFinished(li.id);
removePending(li.id);
save();
}
function handleFinishClick(e) {
const li = e.target.parentNode;
li.parentNode.removeChild(li);
const text = findInPending(li.id);
removePending(li.id);
addFinished(text);
paintFinished(text);
save();
}
function handleBackClick(e) {
const li = e.target.parentNode;
li.parentNode.removeChild(li);
const text = findInFinished(li.id);
removeFinished(li.id);
addPending(text);
paintPending(text);
save();
}
function buildLi(text) {
const li = document.createElement("li");
const span = document.createElement("span");
const delBtn = document.createElement("button");
span.innerText = text.text;
delBtn.innerText = "❌";
delBtn.addEventListener("click", delText);
li.append(span, delBtn);
li.id = text.id;
return li;
}
function paintFinished(text) {
const paintLi = buildLi(text);
const backBtn = document.createElement("button");
backBtn.innerText = "🔙";
backBtn.addEventListener("click", handleBackClick);
paintLi.append(backBtn);
finished.append(paintLi);
}
function paintPending(text) {
const paintLi = buildLi(text);
const nextBtn = document.createElement("button");
nextBtn.innerText = "✅";
nextBtn.addEventListener("click", handleFinishClick);
paintLi.append(nextBtn);
pending.append(paintLi);
}
function save() {
localStorage.setItem("pending", JSON.stringify(pendingList));
localStorage.setItem("finished", JSON.stringify(finishedList));
}
function load() {
pendingList = JSON.parse(localStorage.getItem("pending")) || [];
finishedList = JSON.parse(localStorage.getItem("finished")) || [];
}
function restore() {
pendingList.forEach((text) => {
paintPending(text);
});
finishedList.forEach((text) => {
paintFinished(text);
});
}
function handleSubmit(e) {
e.preventDefault();
if (inputToDo.value === "") {
return alert("할일을 입력해주세요 제발!!!!!!!!!!!");
}
textObj = getTextObj(inputToDo.value);
inputToDo.value = "";
paintPending(textObj);
savePending(textObj);
save();
}
function init() {
todoForm.addEventListener("submit", handleSubmit);
load();
restore();
}
init();