-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
185 lines (146 loc) · 4.67 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
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
182
183
184
185
var remainingTask, completedTask;
var focusTime = 25;
var cycleCount = 4;
var tasks;
window.onload = async () => {
await chrome.storage.local.get(["focusTime", "longTimeCycle"], (res) => {
focusTime = res.focusTime;
cycleCount = res.longTimeCycle;
})
chrome.storage.local.set({ "state": "none" });
refreshTasks();
}
//reload the tasks
const refreshTasks = () => {
taskContainer.innerHTML = "";
chrome.storage.local.get("tasks", (data) => {
tasks=data.tasks;
remainingTask = data.tasks.filter((task) => {
return task.done === false;
})
completedTask = data.tasks.filter((task) => {
return task.done === true;
})
renderTasks();
})
}
//add tasks
const addTaskInput = document.getElementById('task-input');
const cycleInput = document.getElementById('cycles');
addTaskInput.addEventListener('keypress', (event) => {
if (event.key == 'Enter' && addTaskInput.value != "") {
if (cycleInput.value != "") {
addTask();
} else {
alert("please add number of cycles");
}
}
})
cycleInput.addEventListener('keypress', (event) => {
if (event.key == 'Enter' && cycleInput.value != "") {
if (addTaskInput.value != "") {
addTask();
} else {
alert("please add number of cycles");
}
}
})
const addTask = () => {
const task = {
text: addTaskInput.value,
pomodoroCycle: cycleInput.value,
done: false,
completedCycle: 0,
state: "pomodoro",
}
addTaskInput.value = "";
cycleInput.value = "";
chrome.storage.local.get("tasks", (data) => {
if (typeof data.tasks === 'undefined') {
data.tasks = [task];
} else {
data.tasks.push(task);
}
chrome.storage.local.set({ "tasks": data.tasks });
refreshTasks();
})
}
//update the total time, elapsed time, remaining task, completed task
const updateSecondCotainer = async () => {
console.log(focusTime);
const remTaskCount = document.getElementById('task-cnt');
remTaskCount.innerText = remainingTask.length;
const comTaskCount = document.getElementById('comp-task-cnt');
comTaskCount.innerText = completedTask.length;
var remTotalTime = 0;
var elapTotalTime = 0;
remainingTask.forEach((task) => {
remTotalTime += (Number(task.pomodoroCycle - task.completedCycle)) * focusTime;
console.log(remTotalTime);
});
completedTask.forEach((task) => {
elapTotalTime += (Number(task.pomodoroCycle)) * focusTime;
})
const estimateTime = document.getElementById('estimated-time');
timeCalculator(remTotalTime, estimateTime);
const elapsedTime = document.getElementById('elapsed-time');
timeCalculator(elapTotalTime, elapsedTime);
}
const timeCalculator = (time, element) => {
var hour = Math.floor((time) / 60);
var min = (time) % 60
if (hour) {
element.innerText = hour + " h " + min + " min";
} else {
element.innerText = min + " min";
}
}
//Render Tasks
const renderTasks = () => {
updateSecondCotainer();
tasks.forEach((task, index) => {
if(task.done===false){
renderTask(task, index);
}
})
}
const taskContainer = document.getElementById('tasks');
const renderTask = (task, index) => {
const taskRow = document.createElement('div');
taskRow.classList.add('task-row');
const text = document.createElement('span');
text.innerText = task.text;
text.classList.add('pp1')
const startBtn = document.createElement('button');
startBtn.innerText = "Start";
startBtn.classList.add('pp2');
const deletBtn = document.createElement('button');
deletBtn.innerText = "Remove";
deletBtn.classList.add('remove');
deletBtn.addEventListener('click',async()=>{
await deleteTask(index);
})
startBtn.addEventListener('click', () => {
chrome.storage.local.set({ "index": index });
window.location.href = "timer.html";
})
const cycle = document.createElement('span');
cycle.innerText = task.completedCycle + '/' + task.pomodoroCycle;
cycle.classList.add('cycles')
taskRow.appendChild(text);
taskRow.appendChild(startBtn);
taskRow.appendChild(deletBtn);
taskRow.appendChild(cycle);
taskContainer.appendChild(taskRow);
}
//delete Task
const deleteTask= async(index)=>{
tasks.splice(index,1);
await chrome.storage.local.set({"tasks":tasks});
refreshTasks();
}
//
const createPlaylist = document.getElementById('create-playlist');
createPlaylist.addEventListener('click', () => {
window.location.href = "search.html";
})