-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
165 lines (138 loc) · 6.26 KB
/
app.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import handleMangeClass from "./Funcs/handleMangeClass.js"
import welcomUser from "./welcomUser.js"
//Elements
const parentMyList = document.querySelector('#parentMyList')
const addNewList = document.querySelector('#addNewList')
const inpNewListName = document.querySelector('#inpNewListName')
const btnSetNewNameList = document.querySelector('#btnSetNewNameList')
const parentModal = document.querySelector('#parentModal')
const btnCloseModal = document.querySelector('#btnCloseModal')
const addTodoBtn = document.querySelector('#addTodoBtn')
const inputNewTodo = document.querySelector('#inputNewTodo')
const parentTodos = document.querySelector('#parentTodos')
//
let nameListActive = ''
const date = new Date()
//Funcs
welcomUser()
const selectItemList = () => {
if (Array.of(parentMyList)[0].children.length !== 1) {
let liItem = [...Array.of(parentMyList)[0].children]
liItem.slice(0, liItem.length - 1).forEach(item => {
if (item.classList.contains('active-list')) {
handleMangeClass(item, '', 'active-list')
item.children[0].remove()
}
})
}
}
const checkForExistTextList = () => {
if (localStorage.getItem('list-todos')) {
const datasList = JSON.parse(localStorage.getItem('list-todos'))
if (datasList.length > 1) {
let indexID = 0
datasList.forEach(item => {
parentMyList.insertAdjacentHTML('beforeend', `
<li class="active-list" data-id="${indexID++}"> ${item.value} <span class="btnCloseList"><i class="bi bi-x"></i></span></li>
`)
})
} else {
parentMyList.insertAdjacentHTML('beforeend', `
<li class="active-list" data-id="0"> ${JSON.parse(localStorage.getItem('list-todos'))[0] ? JSON.parse(localStorage.getItem('list-todos'))[0].value : JSON.parse(localStorage.getItem('list-todos')).value} <span class="btnCloseList"><i class="bi bi-x"></i></span></li>
`)
}
selectItemList()
let btnCloseListElements = document.querySelectorAll('.btnCloseList')
btnCloseListElements.forEach(element => {
let id = element.parentElement.getAttribute('data-id')
element.addEventListener('click', () => btnCloseListHandler(id))
})
}
}
const addNewListHandler = () => {
if (parentModal.classList.contains('modal-hide')) handleMangeClass(parentModal, 'modal-visible', 'modal-hide')
else handleMangeClass(parentModal, 'modal-visible', 'modal-hide')
}
const inputKeyCodeHandler = e => e.key === 'Enter' && btnSetNewNameListHandler()
const closeKeyCodeHandler = e => e.key === 'Escape' && parentModal.classList.contains('modal-visible') && btnCloseModalHandler()
const btnCloseListHandler = ID => {
if (ID == 0) {
localStorage.removeItem('list-todos')
parentMyList.innerHTML = ''
} else {
const storage = JSON.parse(localStorage.getItem('list-todos'))
localStorage.removeItem('list-todos')
const filterTodos = storage.filter(todo => todo.id != ID)
filterTodos[filterTodos.length - 1].active = true
localStorage.setItem('list-todos', [JSON.stringify(filterTodos)])
parentMyList.innerHTML = ''
checkForExistTextList()
}
}
const btnSetNewNameListHandler = () => {
let inpName = inpNewListName.value
if (inpName.length) {
handleMangeClass(parentModal, 'modal-hide', 'modal-visible')
let newList = {}
if (localStorage.getItem('list-todos')) {
let datasPast = JSON.parse(localStorage.getItem('list-todos'))
newList = {
id: datasPast.length ? datasPast.length : 1,
value: inpName,
active: true
}
localStorage.removeItem('list-todos')
if (datasPast.constructor === Array) {
datasPast.map(item => item.active && (item.active = false))
localStorage.setItem('list-todos', JSON.stringify([...datasPast, newList]))
}
else localStorage.setItem('list-todos', JSON.stringify([datasPast, newList]))
} else localStorage.setItem('list-todos', JSON.stringify({ id: 0, value: inpName, active: true }))
parentMyList.insertAdjacentHTML('beforeend', `
<li class="active-list" data-id="${newList.id === undefined ? 0 : newList.id}"> ${inpName} <span class="btnCloseList"><i class="bi bi-x"></i></span></li>
`)
selectItemList()
let btnCloseListElements = document.querySelectorAll('.btnCloseList')
btnCloseListElements.forEach(element => {
let id = element.parentElement.getAttribute('data-id')
element.addEventListener('click', () => btnCloseListHandler(id))
})
}
nameListActive = inpNewListName.value
inpNewListName.value = ''
}
const btnCloseModalHandler = () => {
inpNewListName.value = ''
handleMangeClass(parentModal, 'modal-hide', 'modal-visible')
}
const addTodoBtnHandler = e => {
e.preventDefault()
let inputValTodo = inputNewTodo.value
if (inputValTodo.length > 1) {
let dateSaveTodo = date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear()
parentTodos.insertAdjacentHTML('beforeend', `
<div class="user-todo">
<div class="user-todo__left">
<input type="checkbox">
<div class="user-todo__info">
<p class="user-todo__date">${dateSaveTodo}</p>
<p class="user-todo__text">${inputValTodo}</p>
</div>
</div>
<div class="user-todo__right">
<p class="user-todo__list"># Li - ${nameListActive}</p>
<i class="bi bi-x"></i>
</div>
</div>
`)
inputNewTodo.value = ''
}
}
//Events
addNewList.addEventListener('click', addNewListHandler)
btnSetNewNameList.addEventListener('click', btnSetNewNameListHandler)
btnCloseModal.addEventListener('click', btnCloseModalHandler)
inpNewListName.addEventListener('keydown', inputKeyCodeHandler)
addTodoBtn.addEventListener('click', addTodoBtnHandler)
window.addEventListener('keydown', closeKeyCodeHandler)
window.addEventListener('load', checkForExistTextList)