-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
431 lines (354 loc) · 14.6 KB
/
script.js
File metadata and controls
431 lines (354 loc) · 14.6 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
document.addEventListener('DOMContentLoaded', function() {
// Create stars background
createStars();
// DOM Elements
const taskInput = document.getElementById('taskInput');
const addBtn = document.getElementById('addBtn');
const taskList = document.getElementById('taskList');
const emptyState = document.getElementById('emptyState');
const filterBtns = document.querySelectorAll('.filter-btn');
const priorityOptions = document.querySelectorAll('.priority-option');
const totalTasksEl = document.getElementById('totalTasks');
const completedTasksEl = document.getElementById('completedTasks');
const pendingTasksEl = document.getElementById('pendingTasks');
// Load tasks from localStorage
let tasks = JSON.parse(localStorage.getItem('cosmicTasks')) || [];
let currentFilter = 'all';
let currentPriority = 'low';
let dragSrcEl = null;
// Initialize the app
renderTasks();
updateStats();
// Event Listeners
addBtn.addEventListener('click', addTask);
taskInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
addTask();
}
});
filterBtns.forEach(btn => {
btn.addEventListener('click', function() {
// Update active filter button
filterBtns.forEach(b => b.classList.remove('active'));
this.classList.add('active');
// Set current filter and re-render
currentFilter = this.getAttribute('data-filter');
renderTasks();
});
});
priorityOptions.forEach(option => {
option.addEventListener('click', function() {
// Update active priority option
priorityOptions.forEach(o => o.classList.remove('active'));
this.classList.add('active');
// Set current priority
currentPriority = this.getAttribute('data-priority');
});
});
// Functions
function createStars() {
const starsContainer = document.getElementById('stars');
const starCount = 150;
for (let i = 0; i < starCount; i++) {
const star = document.createElement('div');
star.classList.add('star');
// Random position and size
const size = Math.random() * 3;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
star.style.left = `${Math.random() * 100}%`;
star.style.top = `${Math.random() * 100}%`;
// Random animation delay
star.style.animationDelay = `${Math.random() * 5}s`;
starsContainer.appendChild(star);
}
}
function addTask() {
const taskText = taskInput.value.trim();
if (taskText === '') {
showNotification('Please enter a task!', 'error');
return;
}
// Create new task object
const newTask = {
id: Date.now(),
text: taskText,
completed: false,
priority: currentPriority,
createdAt: new Date().toISOString()
};
// Add to tasks array
tasks.push(newTask);
// Save to localStorage
saveTasks();
// Clear input and re-render
taskInput.value = '';
renderTasks();
updateStats();
showNotification('Task added successfully!', 'success');
}
function toggleTask(id) {
// Find task and toggle completed status
const taskIndex = tasks.findIndex(task => task.id === id);
tasks[taskIndex].completed = !tasks[taskIndex].completed;
saveTasks();
renderTasks();
updateStats();
// Show confetti if task was completed
if (tasks[taskIndex].completed) {
createConfetti();
showNotification('Task completed! Great job!', 'success');
}
}
function deleteTask(id) {
// Remove task from array
tasks = tasks.filter(task => task.id !== id);
saveTasks();
renderTasks();
updateStats();
showNotification('Task deleted!', 'info');
}
function editTask(id, newText) {
if (newText.trim() === '') {
deleteTask(id);
return;
}
// Update task text
tasks = tasks.map(task => {
if (task.id === id) {
return { ...task, text: newText.trim() };
}
return task;
});
saveTasks();
renderTasks();
}
function saveTasks() {
localStorage.setItem('cosmicTasks', JSON.stringify(tasks));
}
function updateStats() {
const total = tasks.length;
const completed = tasks.filter(task => task.completed).length;
const pending = total - completed;
totalTasksEl.textContent = total;
completedTasksEl.textContent = completed;
pendingTasksEl.textContent = pending;
}
function renderTasks() {
// Clear the task list
taskList.innerHTML = '';
// Filter tasks based on current filter
let filteredTasks = tasks;
if (currentFilter === 'active') {
filteredTasks = tasks.filter(task => !task.completed);
} else if (currentFilter === 'completed') {
filteredTasks = tasks.filter(task => task.completed);
} else if (currentFilter === 'high') {
filteredTasks = tasks.filter(task => task.priority === 'high');
}
// Show/hide empty state
if (filteredTasks.length === 0) {
emptyState.classList.remove('hidden');
} else {
emptyState.classList.add('hidden');
}
// Sort tasks by priority (high first) and completion status
filteredTasks.sort((a, b) => {
const priorityOrder = { high: 3, medium: 2, low: 1 };
if (a.completed !== b.completed) {
return a.completed ? 1 : -1;
}
return priorityOrder[b.priority] - priorityOrder[a.priority];
});
// Render each task
filteredTasks.forEach(task => {
const taskItem = document.createElement('li');
taskItem.className = `task-item ${task.completed ? 'completed' : ''}`;
taskItem.draggable = true;
taskItem.setAttribute('data-id', task.id);
const priorityClass = `priority-${task.priority}`;
taskItem.innerHTML = `
<input type="checkbox" class="task-checkbox" ${task.completed ? 'checked' : ''}>
<span class="task-text">${task.text}</span>
<span class="task-priority ${priorityClass}">${task.priority}</span>
<div class="task-actions">
<button class="edit-btn"><i class="fas fa-edit"></i></button>
<button class="delete-btn"><i class="fas fa-trash"></i></button>
</div>
`;
// Add event listeners
const checkbox = taskItem.querySelector('.task-checkbox');
const deleteBtn = taskItem.querySelector('.delete-btn');
const editBtn = taskItem.querySelector('.edit-btn');
const taskText = taskItem.querySelector('.task-text');
checkbox.addEventListener('change', () => toggleTask(task.id));
deleteBtn.addEventListener('click', () => deleteTask(task.id));
// Edit button click
editBtn.addEventListener('click', () => {
const currentText = taskText.textContent;
const input = document.createElement('input');
input.type = 'text';
input.value = currentText;
input.className = 'edit-input';
// Replace text with input
taskText.replaceWith(input);
input.focus();
// Save on Enter or blur
const saveEdit = () => {
editTask(task.id, input.value);
};
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
saveEdit();
}
});
input.addEventListener('blur', saveEdit);
});
// Double-click to edit
taskText.addEventListener('dblclick', () => {
const currentText = taskText.textContent;
const input = document.createElement('input');
input.type = 'text';
input.value = currentText;
input.className = 'edit-input';
// Replace text with input
taskText.replaceWith(input);
input.focus();
// Save on Enter or blur
const saveEdit = () => {
editTask(task.id, input.value);
};
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
saveEdit();
}
});
input.addEventListener('blur', saveEdit);
});
// Drag and drop functionality
taskItem.addEventListener('dragstart', handleDragStart);
taskItem.addEventListener('dragover', handleDragOver);
taskItem.addEventListener('dragenter', handleDragEnter);
taskItem.addEventListener('dragleave', handleDragLeave);
taskItem.addEventListener('drop', handleDrop);
taskItem.addEventListener('dragend', handleDragEnd);
taskList.appendChild(taskItem);
});
}
// Drag and Drop Functions
function handleDragStart(e) {
dragSrcEl = this;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.innerHTML);
this.classList.add('dragging');
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = 'move';
return false;
}
function handleDragEnter(e) {
this.classList.add('over');
}
function handleDragLeave(e) {
this.classList.remove('over');
}
function handleDrop(e) {
if (e.stopPropagation) {
e.stopPropagation();
}
if (dragSrcEl !== this) {
// Get task IDs
const dragId = parseInt(dragSrcEl.getAttribute('data-id'));
const dropId = parseInt(this.getAttribute('data-id'));
// Find indices in tasks array
const dragIndex = tasks.findIndex(task => task.id === dragId);
const dropIndex = tasks.findIndex(task => task.id === dropId);
// Reorder tasks array
const [draggedTask] = tasks.splice(dragIndex, 1);
tasks.splice(dropIndex, 0, draggedTask);
saveTasks();
renderTasks();
}
return false;
}
function handleDragEnd(e) {
document.querySelectorAll('.task-item').forEach(item => {
item.classList.remove('over');
item.classList.remove('dragging');
});
}
function createConfetti() {
const confettiCount = 100;
const colors = ['#6c63ff', '#ff6584', '#36d1dc', '#ffc107', '#ffffff'];
for (let i = 0; i < confettiCount; i++) {
const confetti = document.createElement('div');
confetti.classList.add('confetti');
// Random properties
const color = colors[Math.floor(Math.random() * colors.length)];
const size = Math.random() * 10 + 5;
const left = Math.random() * 100;
const animationDuration = Math.random() * 3 + 2;
confetti.style.backgroundColor = color;
confetti.style.width = `${size}px`;
confetti.style.height = `${size}px`;
confetti.style.left = `${left}%`;
confetti.style.top = '-10px';
confetti.style.opacity = '1';
confetti.style.borderRadius = Math.random() > 0.5 ? '50%' : '0';
// Animation
confetti.style.transition = `all ${animationDuration}s ease-out`;
document.body.appendChild(confetti);
// Animate
setTimeout(() => {
confetti.style.transform = `translateY(${window.innerHeight}px) rotate(${Math.random() * 720}deg)`;
confetti.style.opacity = '0';
}, 10);
// Remove after animation
setTimeout(() => {
if (confetti.parentNode) {
document.body.removeChild(confetti);
}
}, animationDuration * 1000);
}
}
function showNotification(message, type) {
// Create notification element
const notification = document.createElement('div');
notification.textContent = message;
notification.style.position = 'fixed';
notification.style.top = '20px';
notification.style.right = '20px';
notification.style.padding = '15px 20px';
notification.style.borderRadius = '5px';
notification.style.color = 'white';
notification.style.fontWeight = 'bold';
notification.style.zIndex = '1000';
notification.style.opacity = '0';
notification.style.transition = 'opacity 0.3s';
// Set background color based on type
if (type === 'success') {
notification.style.backgroundColor = '#4caf50';
} else if (type === 'error') {
notification.style.backgroundColor = '#f44336';
} else {
notification.style.backgroundColor = '#2196f3';
}
document.body.appendChild(notification);
// Fade in
setTimeout(() => {
notification.style.opacity = '1';
}, 10);
// Remove after 3 seconds
setTimeout(() => {
notification.style.opacity = '0';
setTimeout(() => {
if (notification.parentNode) {
document.body.removeChild(notification);
}
}, 300);
}, 3000);
}
});