-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
66 lines (60 loc) · 2.09 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
const all_list = [...document.querySelectorAll('.ToDoList')]
let instantion = []
class Lista {
constructor(elemets, list, btns_delete, btn_new_task) {
this.elemets = elemets
this.list = list
this.btns_delete = btns_delete
this.id = 0
this.btns_new_task = btn_new_task
}
delete(i) {
const n = this.elemets.findIndex(e => e.dataset.key == i)
this.btns_delete[n].remove()
this.elemets[n].remove()
//this.newsize()
}
newTask() {
const text = this.list.querySelector('input').value
if (text !== '') {
let e = document.createElement('li')
e.textContent = text
e.dataset.key = ++this.id
let button = document.createElement('button')
button.textContent = 'USUŃ'
button.addEventListener('click', () => {
e.remove()
//this.delete(e.dataset.key) //also work
})
e.appendChild(button)
this.list.appendChild(e)
this.newsize()
}
}
newsize() {
this.elemets = [...this.list.querySelectorAll('li')]
this.btns_delete = [...this.list.querySelectorAll('button')]
}
listen() {
this.btns_delete.forEach((element) => {
if (element.parentNode.dataset.key == null) {
element.parentNode.dataset.key = ++this.id
element.addEventListener('click', () => {
this.delete(element.parentNode.dataset.key)
})
}
});
}
}
all_list.forEach((e, i) => {
const elements = [...e.querySelectorAll('li')]
const list = all_list[i]
const btns_delete = e.querySelectorAll('button')
const btn_new_task = e.querySelector('input[type="submit"]')
const list_name = e.dataset.list_name
instantion.push(new Lista(elements, list, btns_delete, btn_new_task))
instantion[i].btns_new_task.addEventListener('click', () => {
instantion[i].newTask()
})
instantion[i].listen()
})