-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-todos-save-and-delete.html
201 lines (158 loc) · 4.85 KB
/
02-todos-save-and-delete.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
<!DOCTYPE html>
<html>
<head>
<title>Todos</title>
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="manifest" href="site.webmanifest">
<link rel="stylesheet" href="assets/css/styles.css">
</head>
<body>
<main>
<h1>Todos</h1>
<form id="add-todos">
<input type="text" id="new-todo" autofocus placeholder="Add a task">
<label for="new-todo" class="label">Add a task</label>
<button><span class="icon-plus"></span><span class="btn-txt">Add Task</span></button>
</form>
<div id="app"></div>
</main>
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/reef@4/dist/reef.min.js"></script>
<script>
//
// Variables
//
// The new todo input field
var newTodo = document.querySelector('#new-todo');
// Save the localStorage ID to a variable for easier configuration later
var storageID = 'todos';
//
// Methods
//
/**
* Create a todo component
*/
var app = new Reef('#app', {
data: {
todos: []
},
template: function (props) {
// If there are no todos, ask the user to add some
if (props.todos.length < 1) {
return '<p class="no-todos">You don\'t have any tasks yet. Add some using the form above.</p>';
}
// Generate markup for todo items
return '<ul class="todos">' + props.todos.map(function (todo, index) {
var todoHTML =
'<li ' + (todo.completed ? 'class="todo-completed"' : '') + '>' +
'<label for="todo-' + index + '">' +
'<input type="checkbox" id="todo-' + index + '" data-todo="' + index + '" ' + (todo.completed ? 'checked' : '') + '>' +
todo.item +
'</label>' +
'<button data-delete-todo="' + index + '" aria-label="Delete ' + todo.item + '"><span class="icon-close"></span><span class="btn-txt-close">Delete Task</span></button>' +
'</li>';
return todoHTML;
}).join('') + '</ul>';
}
});
/**
* Handle form submit events
* @param {Event} event The Event object
*/
var submitHandler = function (event) {
// Only run for #add-todos form
if (event.target.id !== 'add-todos') return;
// Stop the form from reloading the page
event.preventDefault();
// If the #new-todo input has no value, do nothing
if (newTodo.value.length < 1) return;
// Get a copy of the todos
var todos = app.getData().todos;
// Update data object
todos.push({
item: newTodo.value,
completed: false
});
// Render fresh UI
app.setData({todos: todos});
// Clear the field and return focus
newTodo.value = '';
newTodo.focus();
};
/**
* Mark todo item as complete
* @param {Event} event The event object
*/
var completeTodo = function (event) {
// Only run on todo items
var todo = event.target.getAttribute('data-todo');
if (!todo) return;
// Get a copy of the todos
var todos = app.getData().todos;
if (!todos[todo]) return;
// Update the todo state
todos[todo].completed = event.target.checked;
// Render a fresh UI
app.setData({todos: todos});
};
/**
* Delete a todo item from the list
* @param {Event} event The event object
*/
var deleteTodo = function (event) {
// Only run on delete button clicks
var todo = event.target.getAttribute('data-delete-todo');
if (!todo) return;
// Get a copy of the todos
var todos = app.getData().todos;
if (!todos[todo]) return;
// Confirm with the user before deleting
if (!window.confirm('Are you sure you want to delete this todo item? This cannot be undone.')) return;
// Remove the item from the todo state
todos.splice(todo, 1);
// Render a fresh UI
app.setData({todos: todos});
};
/**
* Handle click events
* @param {Event} event The Event object
*/
var clickHandler = function (event) {
// Mark todo item as complete
completeTodo(event);
// Delete todo item
deleteTodo(event);
};
/**
* Save todo items to localStorage
*/
var saveTodos = function () {
localStorage.setItem(storageID, JSON.stringify(app.getData()));
};
/**
* Load todos into state on page load
*/
var loadTodos = function () {
// Check for saved data in localStorage
var saved = localStorage.getItem(storageID);
var data = saved ? JSON.parse(saved) : {
todos: []
};
// Update the state and run an initial render
app.setData(data);
};
//
// Inits & Event Listeners
//
// Render the initial UI
loadTodos();
//Listen for form submit events
document.addEventListener('submit', submitHandler, false);
// Listen for click events
document.addEventListener('click', clickHandler, false);
// On render events, save todo items
document.addEventListener('render', saveTodos, false);
</script>
</body>
</html>