-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
245 lines (212 loc) · 7.58 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var ul = document.getElementById('todos');
var todos = new Set(); // Collection to store added todos
var draggedItem = null;
var addButton = document.getElementById('add');
addButton.addEventListener('click', addItem);
var removeButton = document.getElementById('remove');
removeButton.addEventListener('click', removeItem);
var removeAllButton = document.getElementById('removeAll');
removeAllButton.addEventListener('click', removeAllItem);
ul.addEventListener('dblclick', function (event) {
if (event.target.tagName === 'LABEL') {
editItem(event.target);
}
});
function addItem(e) {
e.preventDefault();
var input = document.getElementById('input');
var item = input.value.trim();
if (item === '') {
alert('Please enter your TODO!');
return;
}
// Check if the todo already exists
if (todos.has(item)) {
alert('The TODO already exists!');
return;
}
todos.add(item); // Add the new todo to the collection
var xhr = new XMLHttpRequest();
xhr.open('POST', 'add_todo.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
var itemId = response.itemId;
addItemToUI(itemId, item, '0'); // Pass '0' for unchecked todo
} else {
alert(response.message);
}
} else {
alert('An error occurred while adding the todo.');
}
}
};
xhr.send('item=' + encodeURIComponent(item));
input.value = '';
}
function removeItem() {
var checkboxes = document.querySelectorAll('input[type="checkbox"]:checked');
if (checkboxes.length === 0) {
alert('No todos selected to remove.');
return;
}
checkboxes.forEach(function (checkbox) {
var itemId = checkbox.id.replace('check', '');
var xhr = new XMLHttpRequest();
xhr.open('POST', 'remove_todo.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
// Remove the item from the UI
var li = checkbox.parentNode;
li.parentNode.removeChild(li);
} else {
// Handle the error if item removal failed
alert(response.message);
}
}
};
xhr.send('id=' + encodeURIComponent(itemId));
});
}
function removeAllItem() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'remove_all_todos.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
ul.innerHTML = '';
} else {
alert(response.message);
}
} else {
alert('An error occurred while removing all todos.');
}
}
};
xhr.send();
}
function editItem(label) {
var newText = prompt('Enter the new text for this todo:', label.textContent);
if (newText !== null) {
var itemId = label.getAttribute('for').replace('check', '');
var xhr = new XMLHttpRequest();
xhr.open('POST', 'edit_todo.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
label.textContent = newText;
} else {
alert(response.message);
}
} else {
alert('An error occurred while updating the todo.');
}
}
};
xhr.send('id=' + encodeURIComponent(itemId) + '&newText=' + encodeURIComponent(newText));
}
}
// Fetch todos from the database and add them to the UI on page load
window.addEventListener('load', function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'fetch_todos.php', true);
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
var todos = response.todos;
for (var i = 0; i < todos.length; i++) {
var todo = todos[i];
addItemToUI(todo.id, todo.item, todo.checked);
}
} else {
alert(response.message);
}
} else {
alert('An error occurred while fetching the todos.');
}
}
};
xhr.send();
});
function addItemToUI(itemId, item, checked) {
var li = document.createElement('li');
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.setAttribute('id', 'check' + itemId);
checkbox.setAttribute('value', '1');
checkbox.addEventListener('change', updateDatabase);
var label = document.createElement('label');
label.htmlFor = 'check' + itemId;
label.textContent = item;
// Check if the todo should be marked as checked
if (checked === 1) {
checkbox.checked = true;
li.classList.add('myCheck');
}
li.appendChild(checkbox);
li.appendChild(label);
ul.appendChild(li);
// Apply animation to the newly added
li.classList.add('animation');
li.setAttribute('draggable', 'true');
}
function updateDatabase(event) {
var checkbox = event.target;
var isChecked = checkbox.checked ? 1 : 0;
var itemId = checkbox.id.replace('check', '');
var text = checkbox.nextElementSibling.textContent;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'update_todo.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (!response.success) {
alert('An error occurred while updating the todo.');
}
}
};
xhr.send('id=' + itemId + '&checked=' + isChecked + '&text=' + encodeURIComponent(text));
}
ul.addEventListener('dragstart', function (event) {
if (event.target.tagName === 'LI') {
draggedItem = event.target;
event.dataTransfer.setData('text/plain', ''); // Required for Firefox
}
});
ul.addEventListener('dragover', function (event) {
event.preventDefault();
});
ul.addEventListener('drop', function (event) {
event.preventDefault();
if (draggedItem && event.target.tagName === 'LI') {
// Swap the positions of the dragged item and the drop target
const dropTarget = event.target;
const dropTargetIndex = Array.from(ul.children).indexOf(dropTarget);
const draggedIndex = Array.from(ul.children).indexOf(draggedItem);
if (dropTargetIndex !== -1 && draggedIndex !== -1) {
// Remove the animation class before swapping to prevent glitches
draggedItem.classList.remove('animation');
ul.insertBefore(draggedItem, dropTargetIndex > draggedIndex ? dropTarget.nextSibling : dropTarget);
// Add animation class after swapping for smooth transition
setTimeout(() => {
draggedItem.classList.add('animation');
}, 0);
}
}
draggedItem = null;
});