1- // Store everything imported from './todos.mjs' module as properties of an object named Todos
1+ // Store everything imported from './todos.mjs' module as properties of an object named Todos
22import * as Todos from "./todos.mjs" ;
33
44// To store the todo tasks
55const todos = [ ] ;
66
7+ // Store DOM references after page load
8+ let todoListEl ;
9+ let todoListItemTemplate ;
10+
711// Set up tasks to be performed once on page load
812window . addEventListener ( "load" , ( ) => {
13+ todoListEl = document . getElementById ( "todo-list" ) ;
14+ todoListItemTemplate =
15+ document . getElementById ( "todo-item-template" ) . content . firstElementChild ;
16+
917 document . getElementById ( "add-task-btn" ) . addEventListener ( "click" , addNewTodo ) ;
18+ document . getElementById ( "delete-completed-btn" ) . addEventListener ( "click" , removeCompletedTodos ) ;
1019
1120 // Populate sample data
12- Todos . addTask ( todos , "Wash the dishes" , false ) ;
21+ Todos . addTask ( todos , "Wash the dishes" , false ) ;
1322 Todos . addTask ( todos , "Do the shopping" , true ) ;
1423
1524 render ( ) ;
1625} ) ;
1726
18-
19- // A callback that reads the task description from an input field and
27+ // A callback that reads the task description from an input field and
2028// append a new task to the todo list.
2129function addNewTodo ( ) {
2230 const taskInput = document . getElementById ( "new-task-input" ) ;
2331 const task = taskInput . value . trim ( ) ;
32+
2433 if ( task ) {
2534 Todos . addTask ( todos , task , false ) ;
2635 render ( ) ;
@@ -29,11 +38,11 @@ function addNewTodo() {
2938 taskInput . value = "" ;
3039}
3140
32- // Note:
33- // - Store the reference to the <ul> element with id "todo-list" here
34- // to avoid querying the DOM repeatedly inside render().
35- // - This variable is declared here to be close to the only function that uses it.
36- const todoListEl = document . getElementById ( "todo-list" ) ;
41+ // Remove all completed tasks
42+ function removeCompletedTodos ( ) {
43+ Todos . deleteCompleted ( todos ) ;
44+ render ( ) ;
45+ }
3746
3847// Render the whole todo list
3948function render ( ) {
@@ -45,29 +54,22 @@ function render() {
4554 } ) ;
4655}
4756
48-
49- // Note:
50- // - First child of #todo-item-template is a <li> element.
51- // We will create each ToDo list item as a clone of this node.
52- // - This variable is declared here to be close to the only function that uses it.
53- const todoListItemTemplate =
54- document . getElementById ( "todo-item-template" ) . content . firstElementChild ;
55-
5657// Create a <li> element for the given todo task
5758function createListItem ( todo , index ) {
58- const li = todoListItemTemplate . cloneNode ( true ) ; // true => Do a deep copy of the node
59+ const li = todoListItemTemplate . cloneNode ( true ) ;
5960
6061 li . querySelector ( ".description" ) . textContent = todo . task ;
62+
6163 if ( todo . completed ) {
6264 li . classList . add ( "completed" ) ;
6365 }
6466
65- li . querySelector ( ' .complete-btn' ) . addEventListener ( "click" , ( ) => {
67+ li . querySelector ( " .complete-btn" ) . addEventListener ( "click" , ( ) => {
6668 Todos . toggleCompletedOnTask ( todos , index ) ;
6769 render ( ) ;
6870 } ) ;
69-
70- li . querySelector ( ' .delete-btn' ) . addEventListener ( "click" , ( ) => {
71+
72+ li . querySelector ( " .delete-btn" ) . addEventListener ( "click" , ( ) => {
7173 Todos . deleteTask ( todos , index ) ;
7274 render ( ) ;
7375 } ) ;
0 commit comments