-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
89 lines (71 loc) · 2.24 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
'use script';
const addKastBtn = document.getElementById('add-kast-btn');
const deskKastInput = document.getElementById('description-kast');
const mytoWrapper = document.querySelector('.myto-wrapper');
let kasts;
!localStorage.kasts ? kasts = [] : kasts = JSON.parse(localStorage.getItem('kasts'));
let mytoItemElems = [];
function Kast(description) {
this.description = description;
this.completed = false;
}
const createTemplate = (kast, index) => {
return `
<div class="myto-item">
<div class="description ${kast.completed ? 'checked' : ''}">${kast.description}
<div class="buttons">
<input onclick="completeKast(${index})" class="btn-complete" type="checkbox" ${kast.completed ? 'checked' : ''}>
<button onclick="deleteKast(${index})" class="btn-delete">Очистить</button>
</div>
</div>
</div>
`
}
const filterKasts = () => {
const activeKasts = kasts.length && kasts.filter(item => item.completed == false);
const completedKasts = kasts.length && kasts.filter(item => item.completed == true);
kasts = [...activeKasts,...completedKasts];
}
const fillHtmlList = () => {
mytoWrapper.innerHTML = "";
if(kasts.length > 0) {
filterKasts();
kasts.forEach((item, index) => {
mytoWrapper.innerHTML += createTemplate(item, index);
});
mytoItemElems = document.querySelectorAll('.myto-item');
}
}
fillHtmlList();
const updateLocal = () => {
localStorage.setItem('kasts', JSON.stringify(kasts));
}
const completeKast = index => {
console.log(index);
kasts[index].completed = !kasts[index].completed;
if(kasts[index].completed) {
mytoItemElems[index].classList.add('checked');
}else {
mytoItemElems[index].classList.remove('checked');
}
updateLocal();
fillHtmlList();
}
///event listener
addKastBtn.addEventListener('click', () => {
kasts.push(new Kast(deskKastInput.value));
updateLocal();
fillHtmlList();
deskKastInput.value = '';
})
const deleteKast = index => {
setTimeout(() => {
kasts.splice(index, 1);
updateLocal();
fillHtmlList();
},800)
}
//const kast = {
// description : 'прогуляться на воздухе',
//completed : false
//}