-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (92 loc) · 2.93 KB
/
main.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
window.addEventListener('load', () => {
todos = JSON.parse(localStorage.getItem('todos')) || [];
const nameInput = document.querySelector('#name');
const newTodoForm = document.querySelector('#new-todo-form');
const username = localStorage.getItem('username') || '';
nameInput.value = username;
nameInput.addEventListener('change', (e) => {
localStorage.setItem('username', e.target.value);
})
newTodoForm.addEventListener('submit', e => {
e.preventDefault();
const todo = {
content: e.target.elements.content.value,
category: e.target.elements.category.value,
done: false,
createdAt: new Date().getTime()
}
todos.push(todo);
localStorage.setItem('todos', JSON.stringify(todos));
// Reset the form
e.target.reset();
DisplayTodos()
})
DisplayTodos()
})
function DisplayTodos () {
const todoList = document.querySelector('#todo-list');
todoList.innerHTML = "";
todos.forEach(todo => {
const todoItem = document.createElement('div');
todoItem.classList.add('todo-item');
const label = document.createElement('label');
const input = document.createElement('input');
const span = document.createElement('span');
const content = document.createElement('div');
const actions = document.createElement('div');
const edit = document.createElement('button');
const deleteButton = document.createElement('button');
input.type = 'checkbox';
input.checked = todo.done;
span.classList.add('bubble');
if (todo.category == 'personal') {
span.classList.add('personal');
} else {
span.classList.add('business');
}
content.classList.add('todo-content');
actions.classList.add('actions');
edit.classList.add('edit');
deleteButton.classList.add('delete');
content.innerHTML = `<input type="text" value="${todo.content}" readonly>`;
edit.innerHTML = 'Edit';
deleteButton.innerHTML = 'Delete';
label.appendChild(input);
label.appendChild(span);
actions.appendChild(edit);
actions.appendChild(deleteButton);
todoItem.appendChild(label);
todoItem.appendChild(content);
todoItem.appendChild(actions);
todoList.appendChild(todoItem);
if (todo.done) {
todoItem.classList.add('done');
}
input.addEventListener('change', (e) => {
todo.done = e.target.checked;
localStorage.setItem('todos', JSON.stringify(todos));
if (todo.done) {
todoItem.classList.add('done');
} else {
todoItem.classList.remove('done');
}
DisplayTodos()
})
edit.addEventListener('click', (e) => {
const input = content.querySelector('input');
input.removeAttribute('readonly');
input.focus();
input.addEventListener('blur', (e) => {
input.setAttribute('readonly', true);
todo.content = e.target.value;
localStorage.setItem('todos', JSON.stringify(todos));
DisplayTodos()
})
})
deleteButton.addEventListener('click', (e) => {
todos = todos.filter(t => t != todo);
localStorage.setItem('todos', JSON.stringify(todos));
DisplayTodos()
})
})
}