-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbba.js
164 lines (137 loc) · 4.89 KB
/
bba.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
tasks = {};
notifications = [];
window.localStorage.setItem("page", "bba");
chrome.storage.local.set({"page": "bba"});
function checkIfEmpty() {
const inputs = document.querySelectorAll('input');
let notEmpty = true;
for(let i = 0; i < inputs.length; i++) {
if(inputs[i].value.length == 0) {
notEmpty = false;
}
}
if(inputs[0].value in tasks) {
document.getElementById('add').disabled = true;
}
else if(notEmpty) {
document.getElementById('add').disabled = false;
}
}
function addTask() {
const inputs = document.querySelectorAll('input');
let y = inputs[2].value.substring(0,4);
let m = inputs[2].value.substring(5,7);
let d = inputs[2].value.substring(8, 10);
let t = inputs[2].value.substring(11);
let date = m + "/" + d + "/" + y + " " + t;
tasks[inputs[0].value] = [inputs[1].value, date];
saveTasks();
renderRows();
inputs[0].value = "";
inputs[1].value = "";
document.getElementById('add').disabled = true;
chrome.alarms.create(String(Date.now()), {
when: new Date(inputs[2].value).getTime()
})
}
function saveTasks() {
window.localStorage.setItem("bbatasks", JSON.stringify(tasks));
}
function renderRows() {
tasks = JSON.parse(window.localStorage.getItem("bbatasks"));
if(tasks == null) {
tasks = {};
}
doneButtonVisible();
while (document.getElementById("tasks").firstChild) {
document.getElementById("tasks").removeChild(document.getElementById("tasks").firstChild);
}
for(let key in tasks) {
if(key) {
let tr = document.createElement("tr");
tr.classList.add("task");
let name = document.createElement("th");
name.scope ="row";
name.innerHTML = key;
let desc = document.createElement("td");
desc.innerHTML = tasks[key][0];
let date = document.createElement("td");
date.innerHTML = tasks[key][1];
let cb = document.createElement("input");
cb.type = "checkbox";
cb.classList.add("cb");
cb.classList.add("form-check-input");
cb.addEventListener("click", () => doneButton());
tr.appendChild(name);
tr.appendChild(desc);
tr.appendChild(date);
tr.appendChild(cb);
document.getElementById("tasks").appendChild(tr);
}
}
}
function doneButtonVisible() {
tasks = JSON.parse(window.localStorage.getItem("bbatasks"));
if((tasks == null) || (Object.keys(tasks).length === 0)) {
document.getElementById('done').style.visibility = "hidden";
}
else {
document.getElementById('done').style.visibility = "visible";
}
}
function removeTasks() {
rows = document.querySelectorAll('.task');
rows.forEach(row => {
if(row.lastChild.checked) {
document.getElementById("tasks").removeChild(row);
delete tasks[row.firstChild.innerHTML];
saveTasks();
document.getElementById('done').disabled = true;
}
});
doneButtonVisible();
}
function doneButton() {
let clicked = false;
rows = document.querySelectorAll('.task');
rows.forEach(row => {
if(row.lastChild.checked) {
clicked = true;
}
});
if(clicked) {
document.getElementById('done').disabled = false;
}
else {
document.getElementById('done').disabled = true;
}
}
function setMin() {
let currentDate = new Date();
let dd = String(currentDate.getDate()).padStart(2, '0');
let mm = String(currentDate.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = currentDate.getFullYear();
let mins = String(currentDate.getMinutes() + 5).padStart(2, '0');
let hh = String(currentDate.getHours()).padStart(2, '0');
let min = yyyy + "-" + mm + "-" + dd + "T" + hh + ":" + mins;
document.getElementById("tasktime").min = min;
// console.log(min)
// console.log(document.getElementById("tasktime").min)
}
function goBack() {
window.localStorage.setItem("page", "index");
window.location.href = "index.html";
}
window.onload = function() {
if(window.localStorage.getItem("bbatasks") === null) {
window.localStorage.setItem("bbatasks", JSON.stringify(tasks));
}
renderRows();
document.querySelectorAll('input').forEach(e => e.addEventListener("input", function(){checkIfEmpty();}));
document.getElementById('add').addEventListener("click", function(){addTask()});
document.getElementById('add').disabled = true;
document.getElementById('done').addEventListener("click", function(){removeTasks()});
document.getElementById('done').disabled = true;
document.getElementById('tasktime').addEventListener("click", function(){setMin()});
document.getElementById('back').addEventListener("click", function(){goBack()});
}