-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
128 lines (112 loc) · 4.07 KB
/
popup.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
document.addEventListener('DOMContentLoaded', function() {
const taskInput = document.getElementById('taskInput');
const categorySelect = document.getElementById('categorySelect');
const addTaskBtn = document.getElementById('addTaskBtn');
const taskList = document.getElementById('taskList');
let editingTaskId = null;
let tasks = [];
// Load tasks from storage when popup opens
chrome.storage.sync.get('tasks', function(data) {
tasks = data.tasks || [];
renderTasks(tasks);
});
// Add task event listener
addTaskBtn.addEventListener('click', function() {
const taskText = taskInput.value.trim();
const category = categorySelect.value;
if (taskText === '') return;
if (editingTaskId !== null) {
// Editing existing task
editTask(editingTaskId, taskText, category);
editingTaskId = null;
} else {
// Create new task object
const newTask = {
text: taskText,
category: category,
completed: false,
id: Date.now()
};
// Save task to storage and update tasks array
tasks.push(newTask);
chrome.storage.sync.set({ 'tasks': tasks }, function() {
renderTasks(tasks);
});
}
// Clear input fields
taskInput.value = '';
taskInput.focus();
});
// Render tasks
function renderTasks(tasks) {
taskList.innerHTML = '';
tasks.forEach(task => {
const li = createTaskElement(task);
taskList.appendChild(li);
});
}
// Create task element
function createTaskElement(task) {
const li = document.createElement('li');
li.textContent = task.text;
li.classList.add('task-item', task.category);
li.classList.toggle('completed', task.completed);
// Create category icon
const categoryIcon = document.createElement('i');
categoryIcon.classList.add('category-icon', 'fas');
if (task.category === 'dsa') {
categoryIcon.classList.add('fa-code');
} else if (task.category === 'dev') {
categoryIcon.classList.add('fa-laptop-code');
} else if (task.category === 'personal') {
categoryIcon.classList.add('fa-user');
}
li.appendChild(categoryIcon);
// Create edit icon
const editIcon = document.createElement('i');
editIcon.classList.add('fas', 'fa-edit', 'edit-icon');
editIcon.addEventListener('click', function() {
editTaskStart(task);
});
li.appendChild(editIcon);
// Create delete icon
const deleteIcon = document.createElement('i');
deleteIcon.classList.add('fas', 'fa-trash', 'delete-icon');
deleteIcon.addEventListener('click', function() {
deleteTask(task.id);
});
li.appendChild(deleteIcon);
// Task click event to toggle completion
li.addEventListener('click', function() {
task.completed = !task.completed;
chrome.storage.sync.set({ 'tasks': tasks }, function() {
renderTasks(tasks);
});
});
return li;
}
// Edit task start
function editTaskStart(task) {
taskInput.value = task.text;
categorySelect.value = task.category;
editingTaskId = task.id;
}
// Edit task
function editTask(taskId, newText, newCategory) {
const taskIndex = tasks.findIndex(task => task.id === taskId);
if (taskIndex !== -1) {
tasks[taskIndex].text = newText;
tasks[taskIndex].category = newCategory;
chrome.storage.sync.set({ 'tasks': tasks }, function() {
renderTasks(tasks);
});
}
}
// Delete task function
function deleteTask(taskId) {
tasks = tasks.filter(task => task.id !== taskId);
chrome.storage.sync.set({ 'tasks': tasks }, function() {
renderTasks(tasks);
});
}
});