-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.html
More file actions
140 lines (114 loc) · 3.47 KB
/
index.html
File metadata and controls
140 lines (114 loc) · 3.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
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet"
crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css">
<title>SKKU TODO</title>
<style>
.container {
width: 640px;
}
</style>
</head>
<body>
<nav class="navbar navbar-light bg-light">
<div class="container">
<span class="navbar-brand mb-0 h1"><i class="bi bi-card-checklist"></i> SKKU-Todo</span>
</div>
</nav>
<div class="container">
<div class="d-flex align-items-center mb-2 mt-2">
<input type="text" class="form-control" id="task-input" placeholder="Enter a task here">
<button type="button" id="add" class="btn btn-primary ms-1 text-nowrap"><i class="bi bi-plus"></i>
Add</button>
</div>
<div class="d-flex">
<div class="flex-grow-1 bg-light rounded-2 p-2 me-1 w-50">
<h3>Todos</h3>
<div id="todo-list">
<div class="task bg-light p-1 rounded-2 ps-2 d-flex align-items-center">
</div>
</div>
</div>
<div class="flex-grow-1 bg-light rounded-2 p-2 w-50">
<h3>Done</h3>
<div id="done-list">
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
crossorigin="anonymous"></script>
<script>
const Type = {
Todo: 1,
Done: 2,
};
let tasks = [];
function loadTasks() {
let lastTasks = localStorage.getItem("tasks");
if (!lastTasks) return;
tasks = JSON.parse(lastTasks);
tasks.forEach(addToList);
}
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function addToList(task) {
let div = document.createElement("div");
div.className = "task bg-light p-1 rounded-2 ps-2 d-flex align-items-center";
let span = document.createElement("span");
span.className = "me-auto";
span.textContent = task.text;
div.appendChild(span);
if (task.type === Type.Todo) {
let buttonDone = document.createElement("button");
buttonDone.className = "btn btn-sm btn-success me-1";
buttonDone.innerHTML = '<i class="bi bi-check"></i>';
div.appendChild(buttonDone);
buttonDone.addEventListener("click", () => {
/* TODO */
});
}
let buttonRemove = document.createElement("button");
buttonRemove.className = "btn btn-sm btn-danger";
buttonRemove.innerHTML = '<i class="bi bi-x"></i>';
div.appendChild(buttonRemove);
buttonRemove.addEventListener("click", () => {
div.remove();
tasks = tasks.filter(t => t !== task);
saveTasks();
});
let list = document.querySelector(task.type === Type.Todo ? "#todo-list" : "#done-list");
list.appendChild(div);
}
window.addEventListener("load", () => {
loadTasks();
});
let button = document.querySelector("#add");
button.addEventListener("click", () => {
// 1. Read the text in #task-input.
let input = document.querySelector("#task-input");
let text = input.value;
if (!text.length) return;
// 2. Create a new Task object.
let task = {
text: text,
type: Type.Todo
};
// 3. Append the new Task object to tasks
tasks.push(task);
saveTasks();
// 4. Create a new task item and attach it to #todo-list.
addToList(task);
// 5. Clear #task-input.
input.value = "";
});
</script>
</body>
</html>