-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
205 lines (192 loc) · 6.5 KB
/
index.html
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
<html>
<head>
<title>My todos</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="./js/init.js" type="text/javascript"></script>
<script src="./js/todo.js" type="text/javascript"></script>
<script src="./js/todoItemTemplate.js" type="text/javascript"></script>
<link href="./css/styles.css" rel="stylesheet" type="text/css" />
<link
rel="icon"
type="image/png"
sizes="32x32"
href="./favicon-32x32.png"
/>
<link
rel="icon"
type="image/png"
sizes="96x96"
href="./favicon-96x96.png"
/>
<link
rel="icon"
type="image/png"
sizes="16x16"
href="./favicon-16x16.png"
/>
<script>
var categories = initApp();
var isOverlayVisible = false;
var errors = {};
const todos = new TodoList();
</script>
</head>
<body onload="loadList();">
<div class="container">
<header>
<div class="title-container">
<img src="./images/checklist.png" width="26.1" height="27" />
<div class="title">My todo's</div>
</div>
<div id="number-of-todo-items" class="caption"></div>
</header>
<div id="todo-items-list-container"></div>
<div class="button-container">
<div class="spacer"></div>
<button onclick="toggleFormVisibility();">+ add item</button>
</div>
</div>
<div class="form-overlay form-overlay-hidden" id="form-overlay">
<div class="form" id="form">
<div class="todo-form-title">Add to my todo's list</div>
<div class="form-item-container">
<div>Category</div>
<select id="category" class="select"></select>
<script>
var categoryList = document.querySelector("#category");
var list = "";
categories.forEach(
(cat) =>
(list += `<option value="${cat.id}">${cat.name}</option>`)
);
categoryList.innerHTML = list;
</script>
</div>
<div class="form-item-container">
<div>Due date</div>
<input type="date" class="text-input" id="due-date" />
</div>
<div class="form-item-container">
<div>Name</div>
<input type="text" class="text-input" id="name" />
</div>
<div class="button-container">
<button onclick="onSaveItem();">Save</button>
<div class="cancel-text" onclick="toggleFormVisibility()">cancel</div>
</div>
</div>
</div>
</body>
<script>
// refresh todo list and rerenders
const loadList = async (fadeInId) => {
const items = document.getElementsByClassName("todo-item-main-container");
for (var i = 0; items.length !== 0; i++) {
items[0].remove();
}
const app = window.document.getElementById("todo-items-list-container");
var todoList = todos.getList();
todoList.forEach((item) => {
const html = todoItemTemplate(
item,
categories.filter((cat) => cat.id == item.categoryId)[0]
);
if (item.id === fadeInId) html.classList.add("fade-in");
app.appendChild(html);
});
const numberOfTodosContainer = document.getElementById(
"number-of-todo-items"
);
numberOfTodosContainer.innerHTML = `${todoList.length} ${
todoList.length > 1 ? "items" : "item"
}`;
};
// Save
function onSaveItem() {
let item = {
name: document.getElementById("name").value,
categoryId: document.getElementById("category").value,
dueDate: document.getElementById("due-date").value,
};
const todoItem = new Todo(item);
todos
.add(todoItem)
.then(() => {
loadList(todoItem.id);
//reset form
document.getElementById("name").value = "";
document.getElementById("category").value = 0;
document.getElementById("due-date").value = "";
toggleFormVisibility();
})
.catch((err) => {
console.log(err);
let errorText = "";
for (const key in err) {
errorText += err[key] + "\n";
}
alert(errorText);
});
}
//toggle done status
async function onToggleDone(id) {
var todoList = todos.getList();
const index = todoList.findIndex((todo) => todo.id === id);
todos
.updateItem({ ...todoList[index], done: !todoList[index].done })
.then(() => loadList());
}
// Delete todo item
function onRemoveButtonClick(id) {
const elementToHide = document.getElementById(
`todo-item-main-container-${id}`
);
elementToHide.addEventListener("animationend", (e) => {
var todoList = todos.getList();
elementToHide.removeEventListener("animationend", null);
elementToHide.remove();
const index = todoList.findIndex((todo) => todo.id === id);
const todoItem = todoList[index];
todos.delete(todoItem).then(() => loadList());
});
elementToHide.classList.remove("fade-in");
elementToHide.classList.add("fade-out");
}
// Shows delete button
function setRemoveButtonVisible(id) {
const button = document.getElementById(`remove-item-button-${id}`);
button.classList.add("remove-item-button-visible");
}
// Hides delete button
function setRemoveButtonHidden(id) {
const buttonToHide = document.getElementById(`remove-item-button-${id}`);
buttonToHide.classList.remove("remove-item-button-visible");
}
// Listen for clicks outside form to close it
window.addEventListener("click", function (e) {
if (
document.getElementById("form-overlay").contains(e.target) &&
!document.getElementById("form").contains(e.target)
) {
toggleFormVisibility();
}
});
// Toggle form visibility
function toggleFormVisibility() {
var formOverlay = document.getElementById("form-overlay");
if (isOverlayVisible) {
formOverlay.classList.add("form-overlay-hidden");
} else {
formOverlay.classList.remove("form-overlay-hidden");
}
isOverlayVisible = !isOverlayVisible;
}
//Set datepicker minimum value to today
var datePicker = document.getElementById("due-date");
datePicker.min = new Date().toISOString().split("T")[0];
</script>
</html>