forked from iamjonjackson/html5-js-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
203 lines (167 loc) · 7.44 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Tutorial - Todo App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Javascript Todo App</h1>
<div class="row mt-4">
<div class="col-md-8">
<ul id="todo-list">
</ul>
</div>
<div class="col-md-4">
<form>
<input type="text" class="form-control" id="todoTitle" placeholder="Todo title...">
<textarea rows="3" class="form-control mt-3" id="todoDesc" placeholder="Todo description..."></textarea>
<input type="date" class="form-control" id="todoDeadline" placeholder="Todo deadline...">
<button onclick="newTodo()" class="addBtn btn btn-sm btn-primary form-control mt-3">Add</span>
</form>
<button onclick="deleteAllTodos()" class="addBtn btn btn-sm btn-danger form-control mt-5">Delete All</span>
</div>
</div>
</div>
<div class="modal" tabindex="-1" role="dialog" id="todoModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Todo Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form class="modal-body">
<p>Title: <input type="text" class="form-control" id="todo-title" placeholder="Todo title..."></p>
<p>Description: <textarea rows="3" class="form-control mt-3" id="todo-description"
placeholder="Todo description..."></textarea></p>
<p>Deadline: <input type="date" class="form-control" id="todo-deadline" placeholder="Todo deadline..."></p>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick=newTodo()>Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
<script>
var json_data = JSON.parse( localStorage.getItem('json_data') );
if (json_data) {
json_data.forEach(element => {
if (element) {
newTodo(element.title, element.description, element.deadline, element.id);
}
});
}
registerEventListeners();
function registerEventListeners() {
var deleteButtons = document.querySelectorAll(".delete");
deleteButtons.forEach(function(button) {
button.addEventListener("click", function() {
var todoID = this.parentNode.getAttribute('data-id');
deleteTodo(todoID);
});
});
var todoLinks = document.querySelectorAll("a[data-target='#todoModal']");
todoLinks.forEach(function(link) {
link.addEventListener("click", function() {
var todoID = this.parentNode.getAttribute('data-id');
var currentTodo = getTodo(todoID);
// console.log(currentTodo);
document.getElementById("todo-title").value = currentTodo.title;
document.getElementById("todo-description").innerHTML = currentTodo.description;
document.getElementById("todo-deadline").value = currentTodo.deadline;
var saveButton = document.querySelector("#todoModal .modal-footer .btn-primary");
saveButton.onclick = function() { saveTodo(todoID); };
});
});
}
function getTodo(id) {
var json_temp = JSON.parse( localStorage.getItem('json_data') );
return json_temp[id];
}
function deleteTodo(todoID) {
var todoElement = document.querySelector("li[data-id='" + todoID + "']");
if (todoElement) {
todoElement.style.display = "none";
}
var json_temp = JSON.parse( localStorage.getItem('json_data') );
delete json_temp[todoID];
localStorage.setItem('json_data',
JSON.stringify(json_temp)
);
}
function saveTodo(todoID) {
var updatedTitle = document.getElementById("todo-title").value;
var updatedDesc = document.getElementById("todo-description").value;
var updatedDeadline = document.getElementById("todo-deadline").value;
var json_temp = JSON.parse(localStorage.getItem('json_data'));
json_temp[todoID].title = updatedTitle;
json_temp[todoID].description = updatedDesc;
json_temp[todoID].deadline = updatedDeadline;
localStorage.setItem('json_data', JSON.stringify(json_temp));
var todoElement = document.querySelector("li[data-id='" + todoID + "']");
todoElement.innerHTML = `<a href="#" data-toggle="modal" data-target="#todoModal">${updatedTitle}</a> <a href="#" class="btn btn-sm btn-danger m-1 delete">Delete</a> Deadline: ${updatedDeadline}`;
registerEventListeners();
$('#todoModal').modal('hide');
}
function newTodo(todoTitle, todoDesc, todoDeadline, todoID) {
if (!todoTitle && !todoID ) {
todoTitle = document.getElementById("todoTitle").value;
todoDesc = document.getElementById("todoDesc").value;
todoDeadline = document.getElementById("todoDeadline").value;
if (todoTitle) {
var todoID = storeTodoLocal(todoTitle, todoDesc, todoDeadline);
}
}
if (todoTitle) {
// console.log(todoTitle);
var todoHTML = '<a href="#" data-toggle="modal" data-target="#todoModal">' + todoTitle + '</a> <a href="#" class="btn btn-sm btn-danger m-1 delete">Delete</a>' + "Deadline: " + todoDeadline;
var todoList = document.getElementById("todo-list");
var newTodoElement = document.createElement("li");
newTodoElement.setAttribute("data-id", todoID);
newTodoElement.innerHTML = todoHTML;
todoList.appendChild(newTodoElement);
registerEventListeners();
}
}
function storeTodoLocal(todoTitle, todoDesc, todoDeadline) {
// retrieve and parse existing JSON from localstorage
var json_temp = JSON.parse( localStorage.getItem('json_data') );
if (!json_temp) {
json_temp = [];
}
// creating a new todo ID based on length of existing localstorage array
var todoID = json_temp.length;
// add new todo object to JSON
json_temp.push({
"id": todoID,
"title": todoTitle,
"description": todoDesc,
"deadline": todoDeadline,
"completed": false
});
// log updated JSON to console
console.log(json_temp);
// stringify updated JSON and store back in localStorage
localStorage.setItem('json_data',
JSON.stringify(json_temp)
);
// return ID of new todo
return todoID;
}
function deleteAllTodos() {
if (confirm("Are you sure you want to delete all of your Todos?")) {
localStorage.removeItem('json_data');
document.getElementById("todo-list").innerHTML = "";
}
}
</script>
</html>