-
-
Notifications
You must be signed in to change notification settings - Fork 323
Sheffield | 26-Jan-ITP | Daniel Aderibigbe | Sprint 3 | Todo-list #1152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,52 @@ | ||
| <!DOCTYPE html> | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
| <title>ToDo List</title> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>ToDo List</title> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| <link | ||
| href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" | ||
| rel="stylesheet" | ||
| /> | ||
|
|
||
| <script type="module" src="script.mjs"></script> | ||
| </head> | ||
| <body> | ||
| <div class="todo-container"> | ||
| <h1>My ToDo List</h1> | ||
| <script type="module" src="script.mjs"></script> | ||
| </head> | ||
| <body> | ||
| <div class="todo-container"> | ||
| <h1>My ToDo List</h1> | ||
|
|
||
| <div class="todo-input"> | ||
| <input type="text" id="new-task-input" placeholder="Enter a new task..." /> | ||
| <button id="add-task-btn">Add</button> | ||
| </div> | ||
| <div class="todo-input"> | ||
| <input | ||
| type="text" | ||
| id="new-task-input" | ||
| placeholder="Enter a new task..." | ||
| /> | ||
| <button id="add-task-btn">Add</button> | ||
| </div> | ||
|
|
||
| <button id="delete-completed">Delete completed tasks</button> | ||
|
|
||
| <ul id="todo-list" class="todo-list"> | ||
| </ul> | ||
| <ul id="todo-list" class="todo-list"></ul> | ||
|
|
||
| <!-- | ||
| <!-- | ||
| This is a template for the To-do list item. | ||
| It can simplify the creation of list item node in JS script. | ||
| --> | ||
| <template id="todo-item-template"> | ||
| <li class="todo-item"> <!-- include class "completed" if the task completed state is true --> | ||
| <span class="description">Task description</span> | ||
| <div class="actions"> | ||
| <button class="complete-btn"><span class="fa-solid fa-check" aria-hidden="true"></span></button> | ||
| <button class="delete-btn"><span class="fa-solid fa-trash" aria-hidden="true"></span></button> | ||
| </div> | ||
| </li> | ||
| </template> | ||
|
|
||
| </div> | ||
| </body> | ||
| <template id="todo-item-template"> | ||
| <li class="todo-item"> | ||
| <!-- include class "completed" if the task completed state is true --> | ||
| <span class="description">Task description</span> | ||
| <div class="actions"> | ||
| <button class="complete-btn"> | ||
| <span class="fa-solid fa-check" aria-hidden="true"></span> | ||
| </button> | ||
| <button class="delete-btn"> | ||
| <span class="fa-solid fa-trash" aria-hidden="true"></span> | ||
| </button> | ||
| </div> | ||
| </li> | ||
| </template> | ||
| </div> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,27 @@ | ||
| // Store everything imported from './todos.mjs' module as properties of an object named Todos | ||
| // Store everything imported from './todos.mjs' module as properties of an object named Todos | ||
| import * as Todos from "./todos.mjs"; | ||
|
|
||
| // To store the todo tasks | ||
| const todos = []; | ||
| let todos = []; | ||
|
|
||
| // Set up tasks to be performed once on page load | ||
| window.addEventListener("load", () => { | ||
| document.getElementById("add-task-btn").addEventListener("click", addNewTodo); | ||
|
|
||
| document.getElementById("delete-completed").addEventListener("click", () => { | ||
| todos = Todos.deleteCompleted(todos); | ||
| render(); | ||
| }); | ||
|
|
||
| // Populate sample data | ||
| Todos.addTask(todos, "Wash the dishes", false); | ||
| Todos.addTask(todos, "Wash the dishes", false); | ||
| Todos.addTask(todos, "Do the shopping", true); | ||
|
|
||
| render(); | ||
| }); | ||
|
|
||
|
|
||
| // A callback that reads the task description from an input field and | ||
| // append a new task to the todo list. | ||
| // A callback that reads the task description from an input field and | ||
| // append a new task to the todo list.'' | ||
| function addNewTodo() { | ||
| const taskInput = document.getElementById("new-task-input"); | ||
| const task = taskInput.value.trim(); | ||
|
|
@@ -29,10 +33,7 @@ function addNewTodo() { | |
| taskInput.value = ""; | ||
| } | ||
|
|
||
| // Note: | ||
| // - Store the reference to the <ul> element with id "todo-list" here | ||
| // to avoid querying the DOM repeatedly inside render(). | ||
| // - This variable is declared here to be close to the only function that uses it. | ||
| // Store reference once | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did this line change? |
||
| const todoListEl = document.getElementById("todo-list"); | ||
|
|
||
| // Render the whole todo list | ||
|
|
@@ -45,32 +46,29 @@ function render() { | |
| }); | ||
| } | ||
|
|
||
|
|
||
| // Note: | ||
| // - First child of #todo-item-template is a <li> element. | ||
| // We will create each ToDo list item as a clone of this node. | ||
| // - This variable is declared here to be close to the only function that uses it. | ||
| const todoListItemTemplate = | ||
| // Template reference | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did this line change? |
||
| const todoListItemTemplate = | ||
| document.getElementById("todo-item-template").content.firstElementChild; | ||
|
|
||
| // Create a <li> element for the given todo task | ||
| function createListItem(todo, index) { | ||
| const li = todoListItemTemplate.cloneNode(true); // true => Do a deep copy of the node | ||
| const li = todoListItemTemplate.cloneNode(true); | ||
|
|
||
| li.querySelector(".description").textContent = todo.task; | ||
|
|
||
| if (todo.completed) { | ||
| li.classList.add("completed"); | ||
| } | ||
|
|
||
| li.querySelector('.complete-btn').addEventListener("click", () => { | ||
| li.querySelector(".complete-btn").addEventListener("click", () => { | ||
| Todos.toggleCompletedOnTask(todos, index); | ||
| render(); | ||
| }); | ||
| li.querySelector('.delete-btn').addEventListener("click", () => { | ||
|
|
||
| li.querySelector(".delete-btn").addEventListener("click", () => { | ||
| Todos.deleteTask(todos, index); | ||
| render(); | ||
| }); | ||
|
|
||
| return li; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,4 +26,8 @@ export function toggleCompletedOnTask(todos, taskIndex) { | |
| if (todos[taskIndex]) { | ||
| todos[taskIndex].completed = !todos[taskIndex].completed; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function deleteCompleted(todos) { | ||
| return todos.filter((todo) => todo.completed === false); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well done this works nicely. (FYI: instead of checking for false you could also check for |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this line change?