-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (117 loc) · 3.81 KB
/
script.js
File metadata and controls
147 lines (117 loc) · 3.81 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
const todos = [];
const RENDER_EVENT = 'render-todo';
function generateId() {
return +new Date();
}
function generateTodoObject(id, task, timestamp, isCompleted) {
return {
id,
task,
timestamp,
isCompleted
}
}
function findTodo(todoId) {
for (todoItem of todos) {
if (todoItem.id === todoId) {
return todoItem;
}
}
return null;
}
function findTodoIndex(todoId) {
for (index in todos) {
if (todos[index].id === todoId) {
return index;
}
}
return -1;
}
function makeTodo(todoObject) {
const {
id,
task,
timestamp,
isCompleted
} = todoObject;
const textTitle = document.createElement('h2');
textTitle.innerText = task;
const textTimestamp = document.createElement('p');
textTimestamp.innerText = timestamp;
const textContainer = document.createElement('div');
textContainer.classList.add('inner');
textContainer.append(textTitle, textTimestamp);
const container = document.createElement('div');
container.classList.add('item', 'shadow');
container.append(textContainer);
container.setAttribute('id', `todo-${id}`);
if (isCompleted) {
const undoButton = document.createElement('button');
undoButton.classList.add('undo-button');
undoButton.addEventListener('click', function () {
undoTaskFromCompleted(id);
});
const trashButton = document.createElement('button');
trashButton.classList.add('trash-button');
trashButton.addEventListener('click', function () {
removeTaskFromCompleted(id);
});
container.append(undoButton, trashButton);
} else {
const checkButton = document.createElement('button');
checkButton.classList.add('check-button');
checkButton.addEventListener('click', function () {
addTaskToCompleted(id);
});
container.append(checkButton);
}
return container;
}
function addTodo() {
const textTodo = document.getElementById('title').value;
const timestamp = document.getElementById('date').value;
const generatedID = generateId();
const todoObject = generateTodoObject(generatedID, textTodo, timestamp, false);
todos.push(todoObject);
document.dispatchEvent(new Event(RENDER_EVENT));
}
function addTaskToCompleted(todoId /* HTMLELement */ ) {
const todoTarget = findTodo(todoId);
if (todoTarget == null) return;
todoTarget.isCompleted = true;
document.dispatchEvent(new Event(RENDER_EVENT));
}
function removeTaskFromCompleted(todoId /* HTMLELement */ ) {
const todoTarget = findTodoIndex(todoId);
if (todoTarget === -1) return;
todos.splice(todoTarget, 1);
document.dispatchEvent(new Event(RENDER_EVENT));
}
function undoTaskFromCompleted(todoId /* HTMLELement */ ) {
const todoTarget = findTodo(todoId);
if (todoTarget == null) return;
todoTarget.isCompleted = false;
document.dispatchEvent(new Event(RENDER_EVENT));
}
document.addEventListener('DOMContentLoaded', function () {
const submitForm /* HTMLFormElement */ = document.getElementById('form');
submitForm.addEventListener('submit', function (event) {
event.preventDefault();
addTodo();
});
});
document.addEventListener(RENDER_EVENT, function () {
const uncompletedTODOList = document.getElementById('todos');
const listCompleted = document.getElementById('completed-todos');
// clearing list item
uncompletedTODOList.innerHTML = '';
listCompleted.innerHTML = '';
for (todoItem of todos) {
const todoElement = makeTodo(todoItem);
if (todoItem.isCompleted) {
listCompleted.append(todoElement);
} else {
uncompletedTODOList.append(todoElement);
}
}
});