-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
95 lines (90 loc) · 2.6 KB
/
index.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
let totalInsertions = [];
let noft = document.getElementById("not");
let notfText = document.getElementById("not-text");
//Clear all button
$("#clearAll").on("click", () => {
$("tbody").html("");
form_clear();
});
// Clears the input areas
function form_clear() {
$("#title").val("");
$("#desc").val("");
}
//Adds a new row if it's not null
$("#add").on("click", (e) => {
e.preventDefault();
title = $("#title").val();
desc = $("#desc").val();
let innerStr = "";
if (title.trim() === "") {
alert("Title can't be null. Please enter a title");
return;
}
nofify("Task Added.", "orange");
//Creates a json object in order to save the input
currentInsertion = {
curTitle: title,
curDesc: desc,
status: "done",
};
totalInsertions.push(currentInsertion);
$("tbody").fadeOut(400, () => {
let i = 0;
totalInsertions.forEach((element) => {
innerStr += `<tr id = "tr${i++}">
<th scope="row">${i}</th>
<td>${element.curTitle}</td>
<td>${element.curDesc}</td>
<td><button type="submit" class="btn btn-success btn-sm replace">${
element.status
}</button></td>
<td><button type="submit" class="btn btn-danger btn-sm delete">Delete</button></td>
</tr>`;
});
$("tbody").html(innerStr);
$("tbody").fadeIn(400);
});
});
$(document).on("click", ".replace", (e) => {
rowId = getCurrentRow(e);
totalInsertions[rowId].status = "✔️";
$(e.currentTarget).html("✔️");
nofify("Task Completed.", "green");
});
$(document).on("click", ".delete", (e) => {
e.preventDefault();
rowId = getCurrentRow(e);
totalInsertions.splice(rowId, 1);
let i = 0;
let innerStr = "";
// Because splice does not work on splice (1, 1) deletes the central element
$("tbody").fadeOut(400, () => {
totalInsertions.forEach((element) => {
innerStr += `<tr id = "tr${i++}">
<th scope="row">${i}</th>
<td>${element.curTitle}</td>
<td>${element.curDesc}</td>
<td><button type="submit" class="btn btn-success btn-sm replace">${
element.status
}</button></td>
<td><button type="submit" class="btn btn-danger btn-sm delete">Delete</button></td>
</tr>`;
});
$("tbody").html(innerStr);
$("tbody").fadeIn(400);
});
});
function getCurrentRow(event) {
currRow = $(event.currentTarget).parent().parent();
rowId = $(currRow).attr("id");
return rowId.replace(/[^0-9]/g, "");
}
function nofify(message, color) {
noft.innerHTML = message;
noft.style.visibility = "visible";
noft.style.borderColor = color;
setTimeout(() => {
noft.style.visibility = "hidden";
}, 3000);
}