-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
230 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,6 @@ | |
"eslint": "^9.12.0", | ||
"globals": "^15.11.0", | ||
"prettier": "^3.3.3" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,184 @@ | ||
/* global gsap */ | ||
|
||
let todoList = JSON.parse(localStorage.getItem('todoList')) || []; | ||
let todoListhtml = ''; | ||
console.log(todoList); | ||
|
||
function addTodo() { | ||
const inputNameElement = document.querySelector('.js-name-input'); | ||
let name = inputNameElement.value; | ||
const inputDateElement = document.querySelector('.js-date-input'); | ||
let date = inputDateElement.value; | ||
const inputTimeElement = document.querySelector('.js-time-input'); | ||
let time = inputTimeElement.value; | ||
|
||
// Validation checks | ||
const name = inputNameElement.value.trim(); | ||
const date = inputDateElement.value; | ||
const time = inputTimeElement.value; | ||
|
||
if (!name || !date || !time) { | ||
alert('Please fill in all fields: task, date, and time.'); | ||
return; | ||
return; // Exit the function if any field is empty | ||
} | ||
|
||
todoList.push({ name, date, time }); | ||
localStorage.setItem('todoList', JSON.stringify(todoList)); | ||
const newTodo = { name, date, time }; | ||
todoList.push(newTodo); | ||
saveTodoList(); | ||
|
||
inputNameElement.value = ''; | ||
inputDateElement.value = ''; | ||
inputTimeElement.value = ''; | ||
setDefaultDateTime(); | ||
|
||
// Update the displayed list with animation | ||
updateTodoList(true); // Pass true to animate on addition | ||
updateTodoList(); | ||
|
||
// Animate the newly added item | ||
const todoListElement = document.querySelector('.js-todo-list'); | ||
const newItem = todoListElement.lastElementChild; | ||
if (newItem) { | ||
gsap.from(newItem, { | ||
opacity: 0, | ||
y: 20, | ||
duration: 0.5, | ||
ease: 'back.out(1.7)', | ||
}); | ||
} | ||
} | ||
|
||
function deleteTodo(index) { | ||
// Create a reference to the element to animate | ||
const itemToDelete = document.querySelector(`.todo-item[data-index="${index}"]`); | ||
const itemToDelete = document.querySelector( | ||
`.todo-item[data-index="${index}"]` | ||
); | ||
|
||
// Animate the item before removing it | ||
gsap.to(itemToDelete, { | ||
opacity: 0, | ||
scale: 0.5, | ||
duration: 0.3, | ||
x: 100, | ||
duration: 0.5, | ||
ease: 'power2.in', | ||
onComplete: () => { | ||
// Remove the specific todo from the list | ||
todoList.splice(index, 1); | ||
localStorage.setItem('todoList', JSON.stringify(todoList)); | ||
saveTodoList(); | ||
updateTodoList(); | ||
}, | ||
}); | ||
} | ||
|
||
function updateTodoList(animate = false) { | ||
// Sort todoList by date and time before rendering | ||
todoList.sort((a, b) => { | ||
const dateA = new Date(a.date + ' ' + a.time); | ||
const dateB = new Date(b.date + ' ' + b.time); | ||
return dateA - dateB; // Sort by ascending date and time | ||
}); | ||
function editTodo(index) { | ||
const todo = todoList[index]; | ||
const inputNameElement = document.querySelector('.js-name-input'); | ||
const inputDateElement = document.querySelector('.js-date-input'); | ||
const inputTimeElement = document.querySelector('.js-time-input'); | ||
const addButton = document.querySelector('.js-add-button'); | ||
|
||
const addElement = document.querySelector('.js-add-html'); | ||
todoListhtml = ''; | ||
|
||
for (let i = 0; i < todoList.length; i++) { | ||
todoListhtml += ` | ||
<div class="todo-item small-container" data-index="${i}">${todoList[i].name}</div> | ||
<div class="small-container">${todoList[i].date} ${todoList[i].time}</div> | ||
<button class="js-delete-button" onclick="deleteTodo(${i});"> | ||
<img src="assets/delete-icon.png" alt="Delete" width="16" height="16">delete | ||
</button> | ||
<button class="js-edit-button" onclick="editTodo(${i});"> | ||
<img src="assets/edit-icon.png" alt="Edit" width="16" height="16">edit | ||
</button>`; | ||
} | ||
|
||
addElement.innerHTML = todoListhtml; | ||
|
||
if (animate) { | ||
// Animate the newly added items | ||
const newItems = document.querySelectorAll('.todo-item'); | ||
newItems.forEach(item => { | ||
gsap.from(item, { | ||
opacity: 0, | ||
scale: 0.5, | ||
duration: 0.3, | ||
}); | ||
}); | ||
} | ||
inputNameElement.value = todo.name; | ||
inputDateElement.value = todo.date; | ||
inputTimeElement.value = todo.time; | ||
|
||
addButton.innerHTML = | ||
'<img src="assets/edit-icon.png" alt="Update" width="16" height="16"> Update'; | ||
addButton.onclick = () => updateTodo(index); | ||
|
||
// Highlight the item being edited | ||
const itemToEdit = document.querySelector( | ||
`.todo-item[data-index="${index}"]` | ||
); | ||
gsap.to(itemToEdit, { | ||
backgroundColor: '#fffacd', | ||
duration: 0.3, | ||
yoyo: true, | ||
repeat: 1, | ||
}); | ||
} | ||
|
||
function updateTodo(index) { | ||
const inputNameElement = document.querySelector('.js-name-input'); | ||
const inputDateElement = document.querySelector('.js-date-input'); | ||
const inputTimeElement = document.querySelector('.js-time-input'); | ||
const addButton = document.querySelector('.js-add-button'); | ||
|
||
// Validation checks | ||
if ( | ||
!inputNameElement.value || | ||
!inputDateElement.value || | ||
!inputTimeElement.value | ||
) { | ||
alert('Please fill in all fields: task, date, and time.'); | ||
return; | ||
} | ||
|
||
// Update the todo in the list | ||
todoList[index].name = inputNameElement.value; | ||
todoList[index].date = inputDateElement.value; | ||
todoList[index].time = inputTimeElement.value; | ||
const name = inputNameElement.value.trim(); | ||
const date = inputDateElement.value; | ||
const time = inputTimeElement.value; | ||
|
||
// Update local storage | ||
localStorage.setItem('todoList', JSON.stringify(todoList)); | ||
todoList[index] = { name, date, time }; | ||
saveTodoList(); | ||
|
||
// Clear the input fields | ||
inputNameElement.value = ''; | ||
inputDateElement.value = ''; | ||
inputTimeElement.value = ''; | ||
setDefaultDateTime(); | ||
|
||
// Change the update button back to an add button | ||
const addButton = document.querySelector('.js-add-button'); | ||
addButton.innerHTML = 'Add'; | ||
addButton.innerHTML = | ||
'<img src="assets/add-icon.png" alt="Add" width="16" height="16"> Add'; | ||
addButton.onclick = addTodo; | ||
|
||
// Update the displayed list | ||
updateTodoList(); | ||
|
||
// Animate the updated item | ||
const updatedItem = document.querySelector( | ||
`.todo-item[data-index="${index}"]` | ||
); | ||
if (updatedItem) { | ||
gsap.from(updatedItem, { | ||
scale: 0.8, | ||
duration: 0.5, | ||
ease: 'elastic.out(1, 0.5)', | ||
}); | ||
} | ||
} | ||
|
||
function updateTodoList() { | ||
todoList.sort( | ||
(a, b) => new Date(a.date + ' ' + a.time) - new Date(b.date + ' ' + b.time) | ||
); | ||
|
||
const todoListElement = document.querySelector('.js-todo-list'); | ||
todoListElement.innerHTML = ''; | ||
|
||
todoList.forEach((todo, index) => { | ||
const todoItem = document.createElement('div'); | ||
todoItem.className = 'todo-item'; | ||
todoItem.setAttribute('data-index', index); | ||
todoItem.innerHTML = ` | ||
<div class="todo-content"> | ||
<div class="todo-name">${todo.name}</div> | ||
<div class="todo-datetime">${todo.date} ${todo.time}</div> | ||
</div> | ||
<div class="todo-actions"> | ||
<button class="js-edit-button"> | ||
<img src="assets/edit-icon.png" alt="Edit" width="16" height="16"> Edit | ||
</button> | ||
<button class="js-delete-button"> | ||
<img src="assets/delete-icon.png" alt="Delete" width="16" height="16"> Delete | ||
</button> | ||
</div> | ||
`; | ||
|
||
const editButton = todoItem.querySelector('.js-edit-button'); | ||
const deleteButton = todoItem.querySelector('.js-delete-button'); | ||
|
||
editButton.addEventListener('click', () => editTodo(index)); | ||
deleteButton.addEventListener('click', () => deleteTodo(index)); | ||
|
||
todoListElement.appendChild(todoItem); | ||
}); | ||
} | ||
|
||
function setDefaultDateTime() { | ||
const inputDateElement = document.querySelector('.js-date-input'); | ||
const inputTimeElement = document.querySelector('.js-time-input'); | ||
|
||
const now = new Date(); | ||
const date = now.toISOString().split('T')[0]; // YYYY-MM-DD format | ||
const time = now.toTimeString().split(' ')[0].slice(0, 5); // HH:MM format | ||
const date = now.toISOString().split('T')[0]; | ||
const time = now.toTimeString().split(' ')[0].slice(0, 5); | ||
|
||
inputDateElement.value = date; | ||
inputTimeElement.value = time; | ||
} | ||
|
||
// Initialize the todo list and set default date and time on page load | ||
function saveTodoList() { | ||
localStorage.setItem('todoList', JSON.stringify(todoList)); | ||
} | ||
|
||
function setupEventListeners() { | ||
const addButton = document.querySelector('.js-add-button'); | ||
addButton.addEventListener('click', addTodo); | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
updateTodoList(); | ||
setDefaultDateTime(); | ||
|
||
// Set focus on the name input field | ||
const inputNameElement = document.querySelector('.js-name-input'); | ||
inputNameElement.focus(); | ||
setupEventListeners(); | ||
document.querySelector('.js-name-input').focus(); | ||
}); |
Oops, something went wrong.