-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
263 lines (207 loc) · 7.47 KB
/
script.js
File metadata and controls
263 lines (207 loc) · 7.47 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// * The Procedural Way
const date = new Date();
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
// date
const indexDate = document.getElementById('categoriesDate');
if (indexDate) {
indexDate.textContent = `Today, ${date.getDate()} ${
months[date.getMonth()]
} ${date.getFullYear()}`;
}
const categoryDate = document.getElementById('categoryDate');
if (categoryDate) {
categoryDate.textContent = `Today, ${date.getDate()} ${
months[date.getMonth()]
} ${date.getFullYear()}`;
}
// containers
const categories = document.getElementById('categories');
const toDos = document.getElementById('toDos');
// add category or to-do
const addToDo = document.getElementById('addToDo');
const addCategory = document.getElementById('addCategory');
const backToCategory = document.getElementById('backToCategory');
const addCategoryForm = document.getElementById('addCategoryForm');
const addCategoryOverlay = document.getElementById('addCategoryOverlay');
const addTodoForm = document.getElementById("addTodoForm");
const addTodoOverlay = document.getElementById("addTodoOverlay");
if (backToCategory) {
backToCategory.addEventListener('click', () => {
window.location.href = 'http://127.0.0.1:5500/index.html'
})
}
// Start Handling Categories
function renderCategories(id, name) {
const noCategories = document.getElementById('noCategories');
const categoryEl = document.createElement('div');
categoryEl.classList.add('category');
const categoryLink = document.createElement("a");
categoryLink.classList.add("category-link");
categoryLink.href = `./category.html?categoryid=${id}`;
categoryLink.textContent = name;
categoryEl.appendChild(categoryLink);
const icons = document.createElement('div');
icons.classList.add('icons');
const editIcon = document.createElement('i');
editIcon.classList.add('material-icons');
editIcon.textContent = 'create';
const trashIcon = document.createElement('i');
trashIcon.classList.add('material-icons');
trashIcon.textContent = 'delete';
trashIcon.addEventListener('click', () => {
document.querySelector('.items-container').removeChild(categoryEl);
localStorage.removeItem(`${id}`);
if (document.querySelector('.items-container').childElementCount <= 4) {
noCategories.classList.remove('invisible');
}
})
editIcon.addEventListener('click', () => {
console.log(name);
})
icons.appendChild(editIcon);
icons.appendChild(trashIcon);
categoryEl.appendChild(icons);
noCategories.classList.add('invisible');
document.querySelector('.items-container').appendChild(categoryEl);
addCategoryForm.classList.add('invisible');
addCategoryOverlay.classList.add('invisible');
}
if (addCategory) {
if (localStorage.length > 0) {
for (id in localStorage) {
if (localStorage.hasOwnProperty(id)) {
renderCategories(id, JSON.parse(localStorage[id]).name)
}
}
}
const categoryFormInput = addCategoryForm.querySelector('input');
addCategory.addEventListener('click', () => {
addCategoryForm.classList.remove('invisible');
addCategoryOverlay.classList.remove('invisible');
})
addCategoryOverlay.addEventListener('click', () => {
addCategoryForm.classList.add('invisible');
addCategoryOverlay.classList.add('invisible');
categoryFormInput.value = 'Untitled category';
})
addCategoryForm.addEventListener('submit', (e) => {
e.preventDefault();
if (categoryFormInput.value.trim()) {
const category = {
id: Math.random(),
name: categoryFormInput.value.trim(),
todos: []
}
localStorage.setItem(`${category.id}`, JSON.stringify(category));
renderCategories(category.id, category.name);
addCategoryForm.classList.add('invisible');
addCategoryOverlay.classList.add('invisible');
categoryFormInput.value = 'Untitled category';
}
})
}
// start handling todos
if (addToDo) {
const currentUrl = new URL(window.location.href);
const currentUrlParam = currentUrl.searchParams.get('categoryid');
const todoInput = addTodoForm.querySelector('input');
const noTodos = document.getElementById('noTodos');
const categoryHeading = document.getElementById('categoryHeading');
const categoryObject = JSON.parse(localStorage.getItem(currentUrlParam));
addToDo.addEventListener('click', () => {
addTodoForm.classList.remove('invisible');
addTodoOverlay.classList.remove('invisible');
})
addTodoOverlay.addEventListener("click", () => {
addTodoForm.classList.add("invisible");
addTodoOverlay.classList.add("invisible");
todoInput.value = '';
})
// display correct category name on each category todo page
categoryHeading.textContent = categoryObject.name;
function renderTodos(name) {
const todoEl = document.createElement("div");
todoEl.classList.add("category");
const circleLink = document.createElement("div");
circleLink.classList.add("circle-link");
const circle = document.createElement("div");
circle.classList.add("circle");
const checkMark = document.createElement('i');
checkMark.textContent = 'done';
checkMark.classList.add('material-icons');
checkMark.classList.add('invisible');
checkMark.classList.add('check-mark');
circle.appendChild(checkMark);
const todoName = document.createElement("p");
todoName.classList.add("category-link");
todoName.textContent = name;
circleLink.appendChild(circle);
circleLink.appendChild(todoName);
todoEl.appendChild(circleLink);
// toggle done or undone
circleLink.addEventListener('click', () => {
todoName.classList.toggle('done');
checkMark.classList.toggle('invisible');
for (todo of categoryObject.todos) {
if (todo.name == name) {
todo.done = !todo.done;
}
}
localStorage.setItem(currentUrlParam, JSON.stringify(categoryObject));
})
const icons = document.createElement('div');
icons.classList.add('icons');
const editIcon = document.createElement('i');
editIcon.classList.add('material-icons');
editIcon.textContent = 'create';
const trashIcon = document.createElement('i');
trashIcon.classList.add('material-icons');
trashIcon.textContent = 'delete';
// delete todo
trashIcon.addEventListener('click', () => {
document.querySelector('.items-container').removeChild(todoEl);
categoryObject.todos = categoryObject.todos.filter(todo => (todo.name != name));
localStorage.setItem(currentUrlParam, JSON.stringify(categoryObject));
if (document.querySelector('.items-container').childElementCount <= 4) {
noTodos.classList.remove('invisible');
}
})
icons.appendChild(editIcon);
icons.appendChild(trashIcon);
todoEl.appendChild(icons);
noTodos.classList.add('invisible');
document.querySelector('.items-container').appendChild(todoEl);
}
for (todo of categoryObject.todos) {
renderTodos(todo.name);
}
addTodoForm.addEventListener('submit', (e) => {
e.preventDefault();
if (todoInput.value.trim()) {
const todo = {
name: todoInput.value.trim(),
done: false
}
// add todos to localstorage
categoryObject.todos.push(todo);
localStorage.setItem(currentUrlParam, JSON.stringify(categoryObject));
renderTodos(todo.name);
addTodoForm.classList.add("invisible");
addTodoOverlay.classList.add("invisible");
todoInput.value = '';
}
})
}