-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
181 lines (156 loc) · 5.43 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
function Todolist () {
const todos = [] // Array to hold todo items
let ultodo, input, btnAll, btnTodo, btnCompleted
// Load todos from localStorage
const loadTodosFromLocalStorage = () => {
const localTodos = localStorage.getItem('todos')
if (localTodos) {
const todoArr = JSON.parse(localTodos)
if (Array.isArray(todoArr)) {
todos.length = 0 // Clear existing todos
todos.push(...todoArr) // Push all todos to the array
}
}
}
// Save todos to localStorage
const saveTodosToLocalStorage = () => {
localStorage.setItem('todos', JSON.stringify(todos))
}
// Remove a todo item by its id
const removeTodo = (id) => {
const liToRemove = document.querySelector(`#todo-${id}`)
if (liToRemove) {
ultodo.removeChild(liToRemove) // Remove it from the DOM
}
const index = todos.findIndex(todo => todo.id === id)
if (index !== -1) {
todos.splice(index, 1) // Remove the todo from the array
saveTodosToLocalStorage() // Save the updated list
}
}
// Toggle the completion status of a todo
const toggleTodo = (id, ele) => {
const todo = todos.find(todo => todo.id === id)
if (todo) {
todo.completed = !todo.completed // Toggle completion status
saveTodosToLocalStorage() // Save changes
// Update the DOM element's class for visual feedback
ele.classList.toggle('completed')
ele.classList.toggle('uncompleted')
ele.parentNode.classList.toggle('completed')
}
}
// Create a new list item (li) element for a todo
const createLi = ({ text, completed, id }) => {
const li = document.createElement('li')
li.id = `todo-${id}`
if (completed) {
li.classList.add('completed')
}
const spancheck = document.createElement('span')
spancheck.classList.add(completed ? 'completed' : 'uncompleted')
const spancross = document.createElement('span')
spancross.classList.add('cross')
spancross.addEventListener('click', () => removeTodo(id))
spancheck.addEventListener('click', (e) => toggleTodo(id, e.target))
const textNode = document.createTextNode(text)
li.appendChild(spancheck)
li.appendChild(textNode)
li.appendChild(spancross)
return li
}
// Add a new todo to the list
const addNewTodo = (todo) => {
todos.unshift(todo) // Add to the beginning of the array
saveTodosToLocalStorage() // Save the new list
const li = createLi(todo)
ultodo.insertBefore(li, ultodo.firstChild) // Insert at the top of the list
}
// Handle adding a todo via the input field (on keyup event)
const addTodo = (e) => {
if (e.key === 'Enter' && e.target.value.trim().length > 2) {
const newId = todos.length > 0
? Math.max(...todos.map(t => t.id)) + 1
: 0
const todo = {
text: e.target.value.trim(),
id: newId,
completed: false,
}
addNewTodo(todo)
e.target.value = '' // Clear input field
}
}
// Render the todos based on the selected type (all, completed, uncompleted)
const renderTodos = (todoType) => {
ultodo.innerHTML = '' // Clear the current list
const filteredTodos = todos.filter(todo => {
if (todoType === 'all') return true
return todoType === 'completed' ? todo.completed : !todo.completed
})
filteredTodos.forEach(todo => {
const li = createLi(todo)
ultodo.appendChild(li)
})
}
// Toggle button classes (active state and disabled state)
const toggleBtnClasses = (target, btns = []) => {
target.classList.add('active')
target.setAttribute('disabled', true)
btns.forEach(btn => {
btn.removeAttribute('disabled')
btn.classList.remove('active')
})
}
// Add event listeners to buttons for filtering todos
const addListeners = () => {
btnAll = document.querySelector('#btnAll')
btnCompleted = document.querySelector('#btnCompleted')
btnTodo = document.querySelector('#btnTodo')
// Set event listeners for filtering todos
btnAll.addEventListener('click', (e) => {
toggleBtnClasses(e.target, [btnTodo, btnCompleted])
renderTodos('all')
})
btnCompleted.addEventListener('click', (e) => {
toggleBtnClasses(e.target, [btnAll, btnTodo])
renderTodos('completed')
})
btnTodo.addEventListener('click', (e) => {
toggleBtnClasses(e.target, [btnAll, btnCompleted])
renderTodos('uncompleted')
})
}
// Initialize the todo list, load from storage, and set up event listeners
const renderTodoList = () => {
loadTodosFromLocalStorage() // Load todos from localStorage
ultodo = document.querySelector('ul#todolist')
if (!ultodo) {
ultodo = document.createElement('ul')
ultodo.id = 'todolist'
document.body.appendChild(ultodo)
}
renderTodos('all') // Render all todos initially
input = document.querySelector('#todo')
if (!input) {
input = document.createElement('input')
input.id = 'todo'
input.name = 'todo'
input.placeholder = 'What do you need to do?'
document.body.insertBefore(input, ultodo)
}
input.addEventListener('keyup', addTodo) // Add todo when user presses 'Enter'
}
return {
getTodos: function () {
return todos
},
init: function () {
renderTodoList() // Render the list and set up everything
addListeners() // Add event listeners for buttons
},
}
}
// Create a new Todo list instance and initialize it
const myTodo = Todolist()
myTodo.init()