-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
90 lines (81 loc) · 2.22 KB
/
main.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
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
const items = JSON.parse(localStorage.getItem('items')) || [];
let taskList, addItems, actions;
document.addEventListener("DOMContentLoaded", evt => {
readyFuntion();
});
function readyFuntion(){
addItems = document.querySelector('.add-Items');
taskList = document.querySelector('.list');
actions = document.querySelector('.actions');
populateList(items, taskList);
addItems.addEventListener('submit', addItem);
taskList.addEventListener('click', toggleDone);
actions.addEventListener('click', deteminateAction);
}
function addItem(e) {
e.preventDefault();
const Text = (this.querySelector('[name=item]')).value;
const item = {
Text,
done: false
};
items.push(item);
populateList(items, taskList);
localStorage.setItem('items', JSON.stringify(items));
this.reset();
}
function populateList(tasks = [], taskList){
taskList.innerHTML = tasks.map((task, i)=> {
return `
<li>
<input type="checkbox" data-index=${i} id="item${i}" ${task.done ? 'checked':''}/>
<label for="item${i}">${task.Text}</label>
<input type="radio" data-index=${i} value="this" id="delete${i}"/>
<label for="delete${i}"></label>
</li>
`;
}).join('');
}
function toggleDone(e){
if (e.target.matches('input[type="checkbox"]')){
const element = e.target;
const index = element.dataset.index;
items[index].done = !items[index].done;
localStorage.setItem('items', JSON.stringify(items));
populateList(items, taskList);
}else if (e.target.matches('input[type="radio"]')){
const element = e.target;
const index = element.dataset.index;
items.splice(index, 1);
localStorage.setItem('items', JSON.stringify(items));
populateList(items, taskList);
}else{
return;
}
}
function deteminateAction(e){
e.preventDefault();
const element = e.target;
const action = element.dataset.action;
switch (action) {
case 'remove':
clearAll();
break;
case 'change':
var status = ~~element.dataset.done;
changeStatus(status);
break;
}
}
function clearAll(){
localStorage.removeItem('items');
while (items.length) items.pop()
populateList(items, taskList);
}
function changeStatus(status){
items.map((task) => {
task.done = status;
});
localStorage.setItem('items', JSON.stringify(items));
populateList(items, taskList);
}