-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
82 lines (79 loc) · 2.24 KB
/
index.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
// new 연산자로 URLSearchParams 인스턴스 생성
// const queryString = new URLSearchParams(window.location.search);
// const inputText = queryString.get("input-text");
// const inputHiddens = queryString.getAll("input-hidden");
// const inputHidden = inputHiddens[0];
// const inputTextObject = document.getElementsByName("input-text")[0];
// inputTextObject.value = inputText;
// inputTextObject.focus();
class App {
constructor(querySelector) {
this.elem = document.querySelector(querySelector);
}
init() {
if (!this.elem) {
return;
}
this.getItems();
this.assignElement();
this.attatchEventListeners();
this.render();
}
getItems() {
const items = sessionStorage.getItem("items");
this.items = JSON.parse(items || "[]");
}
setItems() {
const items = JSON.stringify(this.items);
sessionStorage.setItem("items", items);
}
assignElement() {
this.elemList = this.elem.querySelector("#list");
this.elemListItem = this.elem.querySelector("#listItem");
this.elemInputText = this.elem.querySelector("[name='input-text']");
}
attatchEventListeners() {
this.elem.addEventListener("click", (e) => this.onClick(e));
this.elem.addEventListener("submit", (e) => this.onSubmit(e));
}
onClick(e) {
const target = e.target;
const action = target.dataset.action;
if (action) {
const index = target.parentElement.dataset.index;
this[action](index);
}
}
onSubmit(e) {
e.preventDefault();
this.create();
}
create() {
this.items.push(this.elemInputText.value);
this.elemInputText.value = "";
this.setItems();
this.render();
}
update(index) {
const name = this.elem.querySelectorAll("[name='names']")[index].value;
this.items[index] = name;
this.setItems();
this.render();
}
delete(index) {
this.items.splice(index, 1);
this.setItems();
this.render();
}
render() {
this.elemList.innerHTML = "";
for (let index in this.items) {
const newDivChild = this.elemListItem.cloneNode(true);
newDivChild.dataset.index = index;
newDivChild.querySelector("input").value = this.items[index];
this.elemList.appendChild(newDivChild);
}
}
}
const app = new App("#app");
app.init();