-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
380 lines (342 loc) · 12.5 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// naming conventions
/*
FUNCTIONS
create refers to complete creation of a working component
make refers to node generation using markup only
*/
/*
CODE FORMAT
The code in this file is organized in the following way:
1. create data structures
2. read from local storage
3. make GUI visible and add asynchronous event listeners(which will be the drivers of this program)
a. create HTML for list adder component
b. add the following event listeners for list adder component:
=> toggle form
=> create list (override form submit event listener)
c. make a createList function
=> create HTML for list
=> create an object for the list and add it to the relevant data structure
=> add the following event listeners for the list
-> delete list
-> toggle task adder form
-> create todo item (overrride form submit event listener)
d. make a createTodoItem function
=> create HTML for todoitem
=> create an object for the todoitem and add it to the relevant data structure
=> add the following event listeners for the todo item
-> delete todo item
-> edit todo item
-> mark as completed / pending
-> show / hide details
*/
// 1. create data structures
let lists = [];
class TodoList {
constructor(title) {
this.title = title;
this.items = [];
}
}
class TodoItem {
constructor(title, desc, due, priority, isPending = true) {
this.arr = [title, desc, due, priority, isPending];
}
}
// 2. read from local storage
(function() {
function isStorageAvailable(type) {
let storage;
try {
storage = window[type];
const x = "__storage_test__";
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return (
e instanceof DOMException &&
e.name === "QuotaExceededError" &&
// acknowledge QuotaExceededError only if there's something already stored
storage &&
storage.length !== 0
);
}
}
if (isStorageAvailable("localStorage")) {
// Yippee! We can use localStorage awesomeness
const retVal = localStorage.getItem("TodoListTOP");
if (retVal !== null) {
lists = JSON.parse(localStorage.getItem("TodoListTOP"));
} else {
lists = [];
}
} else {
// Too bad, no localStorage for us
alert("Local storage is not supported by this browser, so your data will be lost once you end this browser session!");
}
console.log("reading complete!");
console.log(lists);
})();
// 3. make GUI visible and add asynchronous event listeners(which will be the drivers of this program)
const listsContainer = document.querySelector('#lists-container');
(function() {
function component(tag, content) {
const el = document.createElement(tag);
el.innerHTML = content;
return el;
}
function toggleSVGandForm(form, img, formName) {
if (form.classList.contains('show')) {
form.classList.remove('show');
img.src = './media/plus-circle-outline.svg';
img.setAttribute('aria-label', `click this button to open ${formName} form`);
} else {
form.classList.add('show');
img.src = './media/minus-circle-outline.svg';
img.setAttribute('aria-label', `click this button to close ${formName} form`);
}
}
function makeListAdderForm(form, toggleButtonId) {
return component('div', `
<button id="${toggleButtonId}">
<img
src="./media/plus-circle-outline.svg"
height="30"
aria-label="click this button to open list adder form"
/>
</button>
<form id=${form}>
<input type="text" name="listname" />
<button>Create</button>
</form>
`);
}
(function() {
// a. create HTML for list adder component
const formId = 'list-adder-form';
const toggleButtonId = 'list-adder-display-toggle';
const listAdderHTML = makeListAdderForm(formId, toggleButtonId);
const listAdderId = "list-adder-container";
listAdderHTML.id = listAdderId;
listsContainer.appendChild(listAdderHTML);//!!!
// b. add the following event listeners for list adder component:
// => toggle form
// => create list (override form submit event listener)
const DOMref = document.querySelector(`#${listAdderId}`);
const img = DOMref.querySelector('img');
DOMref.querySelector(`#${toggleButtonId}`).addEventListener('click', (e) => {
const f = document.querySelector(`#${formId}`);
toggleSVGandForm(f, img, "list adder");
});
document.querySelector(`#${formId}`).addEventListener('submit', (e) => {
createList(e.target[0].value);
e.target[0].value = "";
toggleSVGandForm(e.target, img, "list adder");
e.preventDefault();
});
})();
function makeList(listTitle) {
return component('div', `
<h2>
${listTitle}
<div class="delete">
❌
</div>
</h2>
<button class="add-new-task">
<img
src="./media/plus-circle-outline.svg"
height="30"
aria-label="click this button to open task adder form"
/>
</button>
<form class="task-adder">
<div><input type="text" name="title" required /></div>
<div><input type="text" name="desc" /></div>
<div><input type="date" name="due-date" /></div>
<div><input type="tel" name="priority" /></div>
<div><button type="submit">Add to list</button></div>
</form>
<h3>Pending</h3>
<ul class="list"></ul>
<h3>Completed</h3>
<ul class="list completed"></ul>
`);
}
// c. make a createList function
// => create HTML for list
// => create an object for the list and add it to the relevant data structure
// => add the following event listeners for the list
// -> delete list
// -> toggle task adder form
// -> create todo item (overrride form submit event listener)
function createList(listTitle, listObj = null) {
const listHTML = makeList(listTitle);
let listId = `${listTitle.replace(/\s/g, '-')}-wrapper`;
listId = listId.replace(/[^A-Za-z\-]/g, "");
listHTML.id = listId;
listHTML.classList.add(`index-${lists.length}`);
listsContainer.appendChild(listHTML);
const DOMref = document.querySelector(`#${listId}`);
if (null === listObj) {
listObj = new TodoList(listTitle);
lists.push(listObj);
}
DOMref.querySelector('h2 > div.delete').addEventListener('click', () => {
let index = -1;
Array.from(DOMref.classList).forEach((c) => {
const match = c.match(/^index-\d+$/);
if (match) {
index = Number(c.replace(/index-/, ""));
}
});
lists.splice(index, 1);
listsContainer.removeChild(DOMref);
for (let i = index; i < listsContainer.children.length; i++) {
const regex = /^index-\d+$/;
Array.from(listsContainer.children[i].classList).forEach((className) => {
if (regex.test(className)) {
listsContainer.children[i].classList.remove(className);
}
});
listsContainer.children[i].classList.add(`index-${i}`);
}
});
DOMref.querySelector('.add-new-task').addEventListener('click', (e) => {
toggleSVGandForm(taskAdder, img, "task adder");
});
const taskAdder = DOMref.querySelector('.task-adder');
const img = DOMref.querySelector('img');
taskAdder.addEventListener('submit', (e) => {
const edotTarget = e.target;
const title = edotTarget[0].value;
const desc = edotTarget[1].value;
const dueDate = edotTarget[2].value;
const priority = edotTarget[3].value;
const newItem = createTodoItem(title, desc, dueDate, priority, listObj);
edotTarget.reset();
toggleSVGandForm(edotTarget, img, "task adder");
e.preventDefault();
});
}
function makeTodoItem(title, desc, dueDate, priority) {
const li = document.createElement('li');
const form = document.createElement('form');
form.innerHTML += ('<input type="radio" class="mark-complete" />');
const content = document.createElement('div');
const titleEl = component('h3', title);
titleEl.classList.add('item-data');
content.appendChild(titleEl);
const descEl = component('p', desc);
descEl.classList.add('item-data');
content.appendChild(descEl);
const dueEl = component('p', `Due: ${dueDate}`);
dueEl.classList.add('item-data');
content.appendChild(dueEl);
const priorityEl = component('p', `Priority: ${priority}`);
priorityEl.classList.add('item-data');
content.appendChild(priorityEl);
const deleteIt = document.createElement('div');
deleteIt.classList.add('delete');
deleteIt.innerHTML = "❌";
li.appendChild(form);
li.appendChild(content);
li.appendChild(deleteIt);
return li;
}
function createTodoItem(title, desc, dueDate, priority, listObj, isPending = true, itemObj = null) {
if (itemObj === null) {
itemObj = new TodoItem(title, desc, dueDate, priority, true);
listObj.items.push(itemObj);
}
const objIndex = listObj.items.length - 1;
const li = makeTodoItem(title, desc, dueDate, priority);
let listId = `${listObj.title.replace(/\s/g, '-')}-wrapper`;
listId = listId.replace(/[^A-Za-z\-]/g, "");
document.querySelector(`#${listId}`).querySelector('.list').appendChild(li);
const temp = document.querySelector(`#${listId}`).querySelector('.list');
const DOMref = temp.children[temp.children.length-1];
const DFA = DOMref.querySelector('div:has(> .item-data)')
DFA.classList.add('state-0');
DFA.addEventListener('click', (event) => {
const et = event.target;
const etp = et.parentElement;
if (etp.classList.contains('state-0')) {
etp.classList.remove('state-0');
etp.classList.add('state-1');
} else if (etp.classList.contains('state-1')) {
etp.classList.remove('state-1');
etp.classList.add('state-2');
for (let i = 0; i < etp.children.length; i++) {
if (!etp.children[i].classList.contains('item-data')) {
continue;
}
const val = etp.children[i].innerHTML;
let input = "";
if (i == 2) {
input = `<input class="item-data" type="date" value="${val.slice(5)}">`;
} else {
input = `<input class="item-data" type="text" value="${val}">`;
}
etp.children[i].innerHTML = input;
}
} else if (etp.classList.contains('state-2')) {
etp.classList.remove('state-2');
etp.classList.add('state-0');
for (let i = 0; i < etp.children.length; i++) {
if (!etp.children[i].classList.contains("item-data")) continue;
let input = etp.children[i].children[0].value;
itemObj.arr[i] = input;
if (i == 2) {
input = "Due: " + input;
}
etp.children[i].innerHTML = input;
}
}
});
function markCompleteOrPending() {
const list = DOMref.parentElement
DOMref.querySelector('input.mark-complete').checked = false;
list.removeChild(DOMref)
const pendingList = list.parentElement.querySelector('.list');
const completedList = list.parentElement.querySelector('.list.completed')
if (pendingList === list) {
completedList.appendChild(DOMref);
itemObj.arr[itemObj.arr.length - 1] = false;
} else {
list.parentElement.querySelector('.list').appendChild(DOMref);
itemObj.arr[itemObj.arr.length - 1] = true;
}
}
DOMref.querySelector('input.mark-complete').onclick = markCompleteOrPending;
if (isPending === false) {
markCompleteOrPending();
}
DOMref.querySelector('div.delete').addEventListener('click', () => {
DOMref.parentElement.removeChild(DOMref);
listObj.items = listObj.items.filter(arrVal => arrVal !== itemObj);
})
}
if (lists.length === 0) createList("Default List");
else {
for (let i = 0; i < lists.length; i++) {
createList(lists[i].title, lists[i]);
for (let j = 0; j < lists[i].items.length; j++) {
const ij = lists[i].items[j];
createTodoItem(
ij.arr[0],
ij.arr[1],
ij.arr[2],
ij.arr[3],
lists[i],
ij.arr[4],
ij
);
}
}
}
})();
window.addEventListener("beforeunload", function() {
localStorage.setItem("TodoListTOP", JSON.stringify(lists));
});