-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 1.46 KB
/
index.js
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
import { $, $$ } from './js/utils.js';
import { load, save } from './js/storage.js';
// Module-level constants for element references that are reused.
const MAIN = $('#main');
const COMPLETE_ALL = $('#complete-all');
const NEW_TASK = $('#new-task');
const TASK_LIST = $('#task-list');
// Create a new `task-item` element.
export function create_task(task, complete) {
const el = document.createElement('task-item');
el.task = task;
el.complete = complete;
el.addEventListener('complete', save);
el.addEventListener('change', save);
el.addEventListener('delete', save);
return el;
}
// Add a task to the list.
function add_task() {
const task = NEW_TASK.value.trim();
if (task !== '') {
COMPLETE_ALL.checked = false;
TASK_LIST.appendChild(create_task(task, false));
}
NEW_TASK.value = '';
save();
}
// Event handling
COMPLETE_ALL.addEventListener('change', () => {
const value = COMPLETE_ALL.checked;
for (const el of $$('task-item')) {
el.complete = value;
}
save();
});
NEW_TASK.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
add_task();
}
});
$('#add-task').addEventListener('click', add_task);
$('#filter').addEventListener('change', () => {
MAIN.dataset.filter = $('input[name="filter"]:checked').value;
save();
});
$('#clear-completed').addEventListener('click', () => {
for (const el of $$('task-item.complete')) {
el.remove();
}
save();
});
// Load saved state from localStorage.
load();