botserver/ui/desktop/tasks/tasks.js

78 lines
1.6 KiB
JavaScript
Raw Normal View History

2025-11-20 18:29:55 -03:00
window.tasksApp = function tasksApp() {
return {
2025-11-20 18:29:55 -03:00
newTask: "",
filter: "all",
tasks: [],
2025-11-20 18:29:55 -03:00
init() {
2025-11-20 18:29:55 -03:00
const saved = localStorage.getItem("tasks");
if (saved) {
try {
this.tasks = JSON.parse(saved);
} catch (e) {
2025-11-20 18:29:55 -03:00
console.error("Failed to load tasks:", e);
this.tasks = [];
}
}
},
2025-11-20 18:29:55 -03:00
addTask() {
2025-11-20 18:29:55 -03:00
if (this.newTask.trim() === "") return;
this.tasks.push({
id: Date.now(),
text: this.newTask.trim(),
completed: false,
2025-11-20 18:29:55 -03:00
createdAt: new Date().toISOString(),
});
2025-11-20 18:29:55 -03:00
this.newTask = "";
this.save();
},
2025-11-20 18:29:55 -03:00
toggleTask(id) {
2025-11-20 18:29:55 -03:00
const task = this.tasks.find((t) => t.id === id);
if (task) {
task.completed = !task.completed;
this.save();
}
},
2025-11-20 18:29:55 -03:00
deleteTask(id) {
2025-11-20 18:29:55 -03:00
this.tasks = this.tasks.filter((t) => t.id !== id);
this.save();
},
2025-11-20 18:29:55 -03:00
clearCompleted() {
2025-11-20 18:29:55 -03:00
this.tasks = this.tasks.filter((t) => !t.completed);
this.save();
},
2025-11-20 18:29:55 -03:00
save() {
try {
2025-11-20 18:29:55 -03:00
localStorage.setItem("tasks", JSON.stringify(this.tasks));
} catch (e) {
2025-11-20 18:29:55 -03:00
console.error("Failed to save tasks:", e);
}
},
2025-11-20 18:29:55 -03:00
get filteredTasks() {
2025-11-20 18:29:55 -03:00
if (this.filter === "active") {
return this.tasks.filter((t) => !t.completed);
}
2025-11-20 18:29:55 -03:00
if (this.filter === "completed") {
return this.tasks.filter((t) => t.completed);
}
return this.tasks;
},
2025-11-20 18:29:55 -03:00
get activeTasks() {
2025-11-20 18:29:55 -03:00
return this.tasks.filter((t) => !t.completed).length;
},
2025-11-20 18:29:55 -03:00
get completedTasks() {
2025-11-20 18:29:55 -03:00
return this.tasks.filter((t) => t.completed).length;
},
};
2025-11-20 18:29:55 -03:00
};