-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemo.js
202 lines (186 loc) · 5.97 KB
/
memo.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const $ = function (target) {
return document.querySelector(target);
};
let todos = [];
const form = $('.input-form');
const input = form.querySelector('input');
const ul = $('.todo-lists');
const modal = $('dialog');
let listCount = 0;
function loadStorage() {
if (
JSON.parse(localStorage.getItem('todos')).length &&
JSON.parse(localStorage.getItem('todos')).length !== 0
) {
JSON.parse(localStorage.getItem('todos')).forEach((el) => {
todos.push(el);
});
}
const frag = document.createDocumentFragment();
if (todos.length !== 0) {
todos.forEach((el) => {
let li = document.createElement('li');
li.classList.add('todo-content');
li.setAttribute('id', listCount);
listCount++;
let span = `
<span>${el}</span>
<div class="todo-btns">
<button class="edit-Btn" type="button">
<img src="./images/editBtn.png" alt="할일수정버튼" />
</button>
<button class="delete-Btn" type="button">
<img src="./images/deleteBtn.png" alt="할일삭제버튼" />
</button>
</div>
`;
li.innerHTML = span;
if (listCount >= 5) {
ul.style.cssText = 'height: unset; min-height: 1000px';
}
frag.append(li);
ul.append(frag);
});
}
}
window.addEventListener('load', loadStorage);
const addTodo = function (e) {
e.preventDefault();
const frag = document.createDocumentFragment();
let checkExistAdd = todos.indexOf(input.value);
if (input.value.length !== 0 && checkExistAdd === -1) {
let li = document.createElement('li');
li.classList.add('todo-content');
li.setAttribute('id', listCount);
listCount++;
let span = `
<span>${input.value}</span>
<div class="todo-btns">
<button class="edit-Btn" type="button">
<img src="./images/editBtn.png" alt="할일수정버튼" />
</button>
<button class="delete-Btn" type="button">
<img src="./images/deleteBtn.png" alt="할일삭제버튼" />
</button>
</div>
`;
li.innerHTML = span;
frag.append(li);
ul.append(frag);
todos.push(input.value);
localStorage.clear();
localStorage.setItem('todos', JSON.stringify(todos));
input.value = '';
if (listCount == 5) {
ul.style.cssText = 'height: unset; min-height: 1000px';
}
} else {
input.value = '';
}
};
const addTodoBtn = $('.addTodoBtn');
addTodoBtn.addEventListener('click', addTodo);
ul.addEventListener('click', (event) => {
if (event.target.className === 'delete-Btn') {
let askDelete = `
<div class='dialog-container'>
<p>정말 삭제하시겠습니까?</p>
<form method="dialog">
<div>
<button value='yes'>예</button>
<button value='no'>아니오</button>
</div>
</form>
</div>`;
modal.innerHTML = '';
modal.innerHTML = askDelete;
modal.showModal();
modal.addEventListener(
'close',
() => {
if (modal.returnValue === 'yes') {
todos = [
...todos.filter((el) => {
return (
el !==
event.target.parentNode.previousElementSibling.textContent
);
}),
];
localStorage.clear();
localStorage.setItem('todos', JSON.stringify(todos));
event.target.parentNode.parentNode.remove();
listCount--;
if (listCount == 4) {
ul.style.cssText = 'height: 561px; min-height: unsetpx';
}
}
},
{ once: true }
);
}
if (event.target.className === 'edit-Btn') {
let askEdit = `
<div class='dialog-container'>
<p>새로운 내용을 입력해주세요</p>
<form method="dialog">
<input maxlength="22" type="text"></input>
<div>
<button value='yes'>확인</button>
<button value='no'>취소</button>
</div>
</form>
</div>`;
modal.innerHTML = '';
modal.innerHTML = askEdit;
modal.showModal();
modal.addEventListener(
'close',
() => {
if (
modal.returnValue === 'yes' &&
modal.querySelector('input').value.trim() !== ''
) {
let editedContentIndex = todos.indexOf(
event.target.parentNode.previousElementSibling.textContent
);
todos.splice(
editedContentIndex,
0,
modal.querySelector('input').value
);
todos.splice(editedContentIndex + 1, 1);
localStorage.clear();
localStorage.setItem('todos', JSON.stringify(todos));
event.target.parentNode.previousElementSibling.textContent =
modal.querySelector('input').value;
}
},
{ once: true }
);
}
});
//[할 일]
//edit, delete 버튼
// edit, delete 모달로 만들기?
// edit 방식 고민해보기
//로컬스토리지 저장 기능
//listCount 따라서 height랑 min-height 속성 바꿔주기
//로컬스토리지에
//li span textContent값 저장하기
//listCount 저장하기
//todo 로컬 스토리지 전
//* 1. 입력할 수 있는 기능
//* 1-1 콘텐츠의 글자수 제한
//* 1-2 둘중 하나라도 입력을 안했을시 alert 을 띄움
//* 2. 저장을 누를수 있는 기능
//* 2-1 '현재 입력된 todo가 없습니다' 텍스트 사라짐
//* 2-2 저장한 값을 화면에 불러옴
//* 2-3 저장과 동시에 인풋창이 초기화
//* 2-4 저장을 누른 시점의 날짜 기록
//* 2-5 삭제버튼을 누르면 다시 '현재 입력된 todo가 없습니다' 텍스트가 돌아와야함
//* 3. 삭제 기능
//* 3-1 삭제하면 화면에서 사라짐
//todo 로컬 스토리지 이용
//
//todo 모달창 기능