-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
176 lines (156 loc) · 5.87 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
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
166
167
168
169
170
171
172
173
174
175
176
const button = document.getElementById("submitButton")
const input = document.getElementById("textInput")
//disable button if input is
button.disabled = true;
input.addEventListener("keyup", (e) => {
if (e.target.value === '') {
button.disabled = true
}
else {
button.disabled = false
}
if (e.key === 'Enter') {
e.preventDefault()
button.click()
button.disabled = false
}
})
//data of localStorage
let data = localStorage.getItem('todo-list-data') ? JSON.parse(localStorage.getItem('todo-list-data')) : [];
// console.log(data)
//display local storage data in html
const displayItem = () => {
let items = ''
for (let i = 0; i < data.length; i++) {
items += `<div class="item" draggable="true">
<div class="dragItems">
<i class="bi bi-three-dots-vertical"></i>
<i class="bi bi-three-dots-vertical"></i>
</div>
<div class="inputData">
<textarea class="textarea" disabled>${data[i]}</textarea>
<div class="editDlt">
<button class="editBtn"> <img src="pencil-square.svg" class="bi-pencil-square" alt="save"></button>
<button class="dltBtn"> <img src="trash3.svg" alt="save"></button>
</div>
</div>
<div class="btns">
<button class="saveBtn"> <img src="check2.svg" alt="save"></i></button>
<button class="cancelBtn"> <img src="x.svg" alt="save"></button>
</div>
</div>`
}
// console.log(items)
document.getElementById('items').innerHTML = items
activateDeleteEventlistner()
aactivateEditEventlistner()
activatecancelEventlistner()
activateSaveEventlistner()
}
if (data.length > 0) {
displayItem()
}
else {
document.getElementById('items').innerHTML = "<p class='nthnToshw'>Nothing to show</p>"
}
function activateDeleteEventlistner() {
const deleteBtn = document.querySelectorAll('.dltBtn')
deleteBtn.forEach((deleteBtn, index) => {
deleteBtn.addEventListener("click", () => {
data.splice(index, 1)
localStorage.setItem('todo-list-data', JSON.stringify(data))
displayItem()
})
});
}
function aactivateEditEventlistner() {
const editBtn = document.querySelectorAll('.editBtn')
const btns = document.querySelectorAll('.btns')
const textarea = document.querySelectorAll('.textarea')
const editDlt = document.querySelectorAll('.editDlt')
editBtn.forEach((editBtn, index) => {
editBtn.addEventListener(("click"), () => {
btns[index].style.display = 'flex'
textarea[index].disabled = false
editDlt[index].style.display = 'none'
})
})
}
function activatecancelEventlistner() {
const cancelBtn = document.querySelectorAll('.cancelBtn')
const btns = document.querySelectorAll('.btns')
const textarea = document.querySelectorAll('.textarea')
const editDlt = document.querySelectorAll('.editDlt')
cancelBtn.forEach((cancelBtn, index) => {
cancelBtn.addEventListener(("click"), () => {
btns[index].style.display = 'none'
textarea[index].disabled = true
editDlt[index].style.display = 'flex'
})
})
}
function activateSaveEventlistner() {
const saveBtn = document.querySelectorAll('.saveBtn')
const textarea = document.querySelectorAll('.textarea')
const btns = document.querySelectorAll('.btns')
const editDlt = document.querySelectorAll('.editDlt')
saveBtn.forEach((saveBtn, index) => {
saveBtn.addEventListener(("click"), () => {
data[index] = textarea[index].value
localStorage.setItem("todo-list-data", JSON.stringify(data))
btns[index].style.display = 'none'
textarea[index].disabled = true
editDlt[index].style.display = 'flex'
})
})
}
//save data to local storage
button.addEventListener("click", (e) => {
e.preventDefault();
data.unshift(input.value)
localStorage.setItem('todo-list-data', JSON.stringify(data))
displayItem()
input.value = ''
})
//drag and drop
const draggables = document.querySelectorAll('.item')
draggables.forEach(draggable => {
draggable.addEventListener('dragstart', () => {
setTimeout(() => {
draggable.classList.add('dragging')
}, 0);
})
draggable.addEventListener('dragend', () => {
draggable.classList.remove('dragging')
})
})
const itemsList = document.getElementById('items')
itemsList.addEventListener('dragover', e => {
const afterElement = getDragAfterElement(itemsList, e.clientY)
const draggable = document.querySelector('.dragging')
if (afterElement == null) {
itemsList.appendChild(draggable)
}
else {
itemsList.insertBefore(draggable, afterElement)
}
let newData = []
const textarea = document.querySelectorAll('.textarea')
for (let i = 0; i < textarea.length; i++) {
newData.push(textarea[i].value)
}
data = newData
localStorage.setItem('todo-list-data', JSON.stringify(data))
})
function getDragAfterElement(itemsList, y) {
const draggableElements = [...itemsList.querySelectorAll('.item:not(.dragging)')]
return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect()
const offset = y - box.top - box.height / 2
if (offset < 0 && offset > closest.offset) {
return { offset: offset, element: child }
} else {
return closest
}
}, { offset: Number.NEGATIVE_INFINITY }).element
}