-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (72 loc) · 3.3 KB
/
script.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
const wrapper = document.getElementById('wrapper')
const main = document.getElementById('main')
const createTask = document.getElementById('createTask')
const inputAdd = document.getElementById('inputAdd')
const btnAdd = document.getElementById('btnAdd')
let arr = JSON.parse(localStorage.getItem('tasks')) || [];
localStorage.setItem(`tasks`, JSON.stringify(arr))
const renderFunc = () => {
let index = 1
for (let item of arr) {
const taskPanel = document.createElement(`div`)
taskPanel.className = `task-panel`
main.append(taskPanel)
const count = document.createElement(`span`)
count.className = `task-panel__number`
count.textContent = `${index}`.padStart(2, `0`) + `.`
const inputContainer = document.createElement(`div`)
inputContainer.className = `task-panel__input-container`
inputContainer.style.background = item.background
taskPanel.append(count, inputContainer)
const taskInput = document.createElement(`input`)
taskInput.className = `task-panel__input`
taskInput.value = item.value
taskInput.disabled = item.disabled
taskInput.type = `text`
const taskCheckbox = document.createElement(`input`)
taskCheckbox.className = `task-panel__checkbox`
taskCheckbox.type = `checkbox`
taskCheckbox.checked = item.boolStatus
inputContainer.append(taskInput, taskCheckbox)
const delleteBtn = document.createElement(`button`)
delleteBtn.className = `dellete`
delleteBtn.id = index
delleteBtn.textContent = `dell.`
taskPanel.append(delleteBtn)
taskInput.addEventListener('input', () => {
arr[+delleteBtn.id - 1].value = taskInput.value
localStorage.setItem(`tasks`, JSON.stringify(arr))
})
taskCheckbox.addEventListener('change', () => {
arr[+delleteBtn.id - 1].boolStatus = taskCheckbox.checked
localStorage.setItem(`tasks`, JSON.stringify(arr))
if (taskCheckbox.checked === true) {
arr[+delleteBtn.id - 1].background = `grey`
arr[+delleteBtn.id - 1].disabled = true
inputContainer.style.background = item.background
taskInput.disabled = item.disabled
localStorage.setItem(`tasks`, JSON.stringify(arr))
} else {
arr[+delleteBtn.id - 1].background = `teal`
arr[+delleteBtn.id - 1].disabled = false
inputContainer.style.background = item.background
taskInput.disabled = item.disabled
localStorage.setItem(`tasks`, JSON.stringify(arr))
}
})
delleteBtn.addEventListener('click', () => {
arr.shift(arr[+delleteBtn.id - 1])
localStorage.setItem(`tasks`, JSON.stringify(arr))
location.reload()
})
index += 1
}
}
renderFunc()
btnAdd.addEventListener(`click`, () => {
arr.push({ value: inputAdd.value, boolStatus: false, background: `teal`, disabled: false })
inputAdd.value = ``
main.innerHTML = ``
renderFunc()
localStorage.setItem(`tasks`, JSON.stringify(arr))
})