-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
158 lines (142 loc) · 4.33 KB
/
script.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
// Get categories and entry arrays form localStorage
let categories = JSON.parse(localStorage.getItem("categories"));
if (categories === null) {
categories = [];
}
let entries = JSON.parse(localStorage.getItem("entries"));
if (entries === null) {
entries = [];
}
let numOfCats = 1;
const catcolors = ["#ff595e", "#ffca3a", "#8ac926", "#1982c4", "#6a4c93"];
catcolors.forEach((element, index) => {
let selector = "div#category-" + index;
$(selector).css("background-color", element);
});
initialize();
function initialize() {
$("#form-cat-indicator").css("background-color", catcolors[0]);
// Binding submission (pressing enter) to creating a new entry
$("#form").bind("submit", (e) => {
e.preventDefault();
addEntry(
$("#form-input").val(),
$("#form-input").parent().parent().attr("category")
); // Create new entry
$("#form-input")[0].value = ""; // Empty the form
changeCategory(0);
});
document.addEventListener("keydown", (e) => {
switch (e.key) {
case "[": // Selector for deleting all entries
e.preventDefault();
$(".entry:not(.template, .entry-form)").remove(); // Remove all entries
updateAll(); // Update LS and Total count
break;
case "]":
$("input.category").val("");
updateAll();
break;
case " ": // Selector changing category
e.preventDefault();
changeCategory();
updateAll();
break;
}
});
// Populate with stored entries
if (entries.length > 0) {
entries.forEach((element, index) =>
createEntry(element.value, element.category, index)
);
}
if (categories.length > 0) {
categories.forEach(
(element, index) => ($("input.category")[index].value = element)
);
}
$("input:not(#form-input)").change(updateAll);
updateAll();
}
/****************FUNCTIONS********************/
function updateAll() {
updateLS();
updateTotal();
}
// Function is called when entry form is submitted
function addEntry(value, category) {
createEntry(value, category, entries.length);
updateAll();
}
// Creating the new element after submission
function createEntry(value, category, index) {
const newEntry = $("#entry-template").clone();
newEntry.attr("id", "entry" + index).attr("category", category);
newEntry.removeClass("template");
newEntry.children().removeClass("template");
newEntry.children(".input")[0].value = value;
newEntry
.children(".category-indicator")
.css("background-color", catcolors[category]);
$(".entries-container").append(newEntry);
$("input:not(#form-input)").change(updateAll);
}
function updateTotal() {
// Update total sum at bottom
const allEntries = $("input:not(.template,.category,#form-input)");
let sum = 0;
let catSum = [0, 0, 0, 0, 0];
allEntries.each(function () {
if (!isNaN(parseFloat(this.value))) {
sum += parseFloat(this.value);
catSum[parseInt($(this).parents("div.entry").attr("category"))] +=
parseFloat(this.value);
}
});
sum = Math.round(sum * 100) / 100;
$("#total-count").html(sum);
catSum.forEach((element, index) => {
let selector = "#result-" + index;
$(selector).html(Math.round(element * 100) / 100);
});
// Update individual category totals
}
// Updating of localstorage so that entries persist over refreshes
function updateLS() {
const allEntries = $("input.input:not(.template, #form-input)");
const allCategories = $("input.category");
const entries = [];
allEntries.each(function () {
entries.push({
value: this.value,
category: $(this).parent().attr("category"),
});
});
const categories = [];
numOfCats = 0;
allCategories.each(function () {
categories.push(this.value);
if (this.value != "") {
numOfCats++;
}
});
if (numOfCats < 1) {
numOfCats = 1;
}
localStorage.setItem("entries", JSON.stringify(entries));
localStorage.setItem("categories", JSON.stringify(categories));
}
function changeCategory(catNum) {
let focusedDiv = $("input:focus:not(.category)").parents("div.entry");
let newCat = (parseInt(focusedDiv.attr("category")) + 1) % numOfCats;
if (catNum !== undefined) {
newCat = catNum % numOfCats;
}
if (isNaN(newCat)) {
return;
}
focusedDiv.attr("category", newCat);
focusedDiv
.children("div.category-indicator")
.css("background-color", catcolors[newCat]);
}