-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
67 lines (67 loc) · 2.3 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
"use strict";
// Enums
var FilterState;
(function (FilterState) {
FilterState["All"] = "all";
FilterState["Active"] = "active";
FilterState["Completed"] = "completed";
})(FilterState || (FilterState = {}));
// DOM Elements
const taskForm = document.getElementById('task-form');
const taskInput = document.getElementById('task-input');
const taskList = document.getElementById('task-list');
const tasksCount = document.getElementById('tasks-count');
const filterAll = document.getElementById('filter-all');
const filterActive = document.getElementById('filter-active');
const filterCompleted = document.getElementById('filter-completed');
// Task array
let tasks = [];
// Add task
const addTask = (e) => {
e.preventDefault();
const taskText = taskInput.value.trim();
if (taskText) {
const task = {
id: Date.now(),
text: taskText,
completed: false
};
tasks.push(task);
renderTasks();
taskInput.value = '';
}
};
// Toggle task completion
const toggleTask = (id) => {
tasks = tasks.map(task => task.id === id ? Object.assign(Object.assign({}, task), { completed: !task.completed }) : task);
renderTasks();
};
// Delete task
const deleteTask = (id) => {
tasks = tasks.filter(task => task.id !== id);
renderTasks();
};
// Render tasks
const renderTasks = (filter = FilterState.All) => {
let filteredTasks = tasks;
if (filter === FilterState.Active) {
filteredTasks = tasks.filter(task => !task.completed);
}
else if (filter === FilterState.Completed) {
filteredTasks = tasks.filter(task => task.completed);
}
taskList.innerHTML = filteredTasks.map(task => `
<li class="${task.completed ? 'completed' : ''}">
<span onclick="toggleTask(${task.id})">${task.text}</span>
<button onclick="deleteTask(${task.id})">Delete</button>
</li>
`).join('');
tasksCount.textContent = `${tasks.filter(task => !task.completed).length} tasks left`;
};
// Event listeners
taskForm.addEventListener('submit', addTask);
filterAll.addEventListener('click', () => renderTasks(FilterState.All));
filterActive.addEventListener('click', () => renderTasks(FilterState.Active));
filterCompleted.addEventListener('click', () => renderTasks(FilterState.Completed));
// Initial render
renderTasks();