-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
379 lines (349 loc) · 12.4 KB
/
app.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//add month
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const d = new Date();
const month = monthNames[d.getMonth()];
const dateElement = document.getElementById("month");
dateElement.innerHTML = month;
// declaration
const rem_inc = document.getElementById("ilist");
const rem_exp = document.getElementById("elist");
var sum_inc = 0;
var sum_exp = 0;
var final_score = 0;
var perc_count = 0;
//main function
function run() {
//get users values
const typev = document.getElementById("type").value;
const descv = document.getElementById("desc").value;
var valuev = document.getElementById("val").value;
//validation
if (descv == "" || valuev < 1) {
alert("field should not remain empty");
}
else {
if (typev == "+") {
saveLocalInc(descv, valuev);
var desctxt = document.createTextNode(descv);
var valtxt = document.createTextNode("+" + valuev);
console.log(valtxt);
var node1 = document.createElement("i");
var node2 = document.createElement("div");
node2.appendChild(node1);
node1.setAttribute("class", "fa fa-close");
node2.setAttribute("class", "item__delete--btn");
var node3 = document.createElement("div");
node3.appendChild(node2);
var node4 = document.createElement("div");
node4.appendChild(valtxt);
var node5 = document.createElement("div");
node5.setAttribute("class", "right ");
node5.appendChild(node4);
node5.appendChild(node3);
var node6 = document.createElement("div");
node6.appendChild(desctxt);
var node7 = document.createElement("div");
node7.setAttribute("class", "item");
node7.appendChild(node6);
node7.appendChild(node5);
document.getElementById("ilist").appendChild(node7);
updateinc1(valuev);
document.getElementById("desc").value = "";
document.getElementById("val").value = "";
}
else {
saveLocalExp(descv, valuev);
var desctxt = document.createTextNode(descv);
var valtxt = document.createTextNode(valuev);
var perctxt = document.createTextNode(calc_perc(valuev) + "%");
var node1 = document.createElement("i");
var node2 = document.createElement("div");
node2.appendChild(node1);
node1.setAttribute("class", "fa fa-close");
node2.setAttribute("class", "item__delete--btn");
var node3 = document.createElement("div");
node3.appendChild(node2);
var node4 = document.createElement("div");
node4.setAttribute("class", "item__percentage");
node4.appendChild(perctxt);
var node5 = document.createElement("div");
node5.appendChild(valtxt);
var node6 = document.createElement("div");
node6.setAttribute("class", "right ");
node6.appendChild(node5);
node6.appendChild(node4);
node6.appendChild(node3);
var node7 = document.createElement("div");
node7.appendChild(desctxt);
var node8 = document.createElement("div");
node8.setAttribute("class", "item ");
node8.appendChild(node7);
node8.appendChild(node6);
document.getElementById("elist").appendChild(node8);
updateexp1(valuev);
document.getElementById("desc").value = "";
document.getElementById("val").value = "";
}
}
}
function remove_inc(element) {
element.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild(element.parentNode.parentNode.parentNode.parentNode);
}
function remove_exp(element) {
element.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild(element.parentNode.parentNode.parentNode.parentNode);
}
rem_inc.addEventListener("click", function (event) {
const element = event.target;
var ch = element.parentNode.parentNode.parentNode.firstChild;
var ch2 = element.parentNode.parentNode.parentNode.parentNode.firstChild;
var value = parseInt(ch.innerText);
var desc = ch2.innerText;
const elementjob = element.nodeName;
if (elementjob == "I") {
remove_inc(element);
removeLocalInc(value, desc);
updateinc2(value);
}
});
rem_exp.addEventListener("click", function (event) {
const element = event.target;
var ch = element.parentNode.parentNode.parentNode.firstChild;
var value = parseInt(ch.innerText);
var ch2 = element.parentNode.parentNode.parentNode.firstChild.nextSibling;
var ch3 = element.parentNode.parentNode.parentNode.parentNode.firstChild;
var percent = parseInt(ch2.innerText);
var desc = ch3.innerText;
elementjob = element.nodeName;
if (elementjob == "I") {
remove_exp(element);
updateexp2(value);
removeLocalExp(value, desc);
update_f_perc(percent);
}
});
function updateinc1(valuev) {
var a = parseInt(valuev);
sum_inc = sum_inc + a;
document.getElementById("upd_inc").innerHTML = "+ " + sum_inc;
update_total();
drawgraph();
}
function updateinc2(v) {
sum_inc = sum_inc - v;
document.getElementById("upd_inc").innerHTML = "+ " + sum_inc;
update_total();
drawgraph();
}
function updateexp1(valuev) {
var a = parseInt(valuev);
sum_exp = sum_exp + a;
document.getElementById("upd_exp").innerHTML = "- " + sum_exp;
update_total();
drawgraph();
}
function updateexp2(v) {
var a = parseInt(v);
sum_exp = sum_exp - a;
document.getElementById("upd_exp").innerHTML = "- " + sum_exp;
update_total();
drawgraph();
}
function update_total() {
var final_score = sum_inc - sum_exp;
if (final_score > 0) {
document.getElementById("f_scr").innerHTML = "+ " + final_score + " Rs.";
}
else {
document.getElementById("f_scr").innerHTML = final_score + " Rs.";
}
}
function calc_perc(valuev) {
var perc = Math.round((parseInt(valuev) / sum_inc) * 100);
if (perc_count == "Infinity") {
perc_count = 0;
}
perc_count = perc_count + perc;
document.getElementById("exp_fperc").innerHTML = perc_count + "%";
return perc;
}
function update_f_perc(v2) {
perc_count = perc_count - v2;
document.getElementById("exp_fperc").innerHTML = perc_count + "%";
}
document.addEventListener('keypress', function (event) {
if (event.keyCode === 13) {
run();
}
});
//function to save income at local storage
function saveLocalInc(desc, price) {
let locinc;
up1 = {
description: desc,
money: price
};
if (localStorage.getItem("locinc") === null) {
locinc = [];
} else {
locinc = JSON.parse(localStorage.getItem("locinc"));
}
locinc.push(up1);
localStorage.setItem('locinc', JSON.stringify(locinc));
}
//function to save expense at local storage
function saveLocalExp(desc, price) {
let locexp;
up2 = {
description: desc,
money: price
};
if (localStorage.getItem("locexp") === null) {
locexp = [];
} else {
locexp = JSON.parse(localStorage.getItem("locexp"));
}
locexp.push(up2);
localStorage.setItem('locexp', JSON.stringify(locexp));
}
//function to load previously stored income nd expenses
function getall() {
let savedinc, savedexp;
if (localStorage.getItem("locinc") === null) {
savedinc = [];
} else {
savedinc = JSON.parse(localStorage.getItem("locinc"));
}
if (localStorage.getItem("locexp") === null) {
savedexp = [];
} else {
savedexp = JSON.parse(localStorage.getItem("locexp"));
}
savedinc.forEach(function (inc) {
console.log(inc['description']);
console.log(inc['money']);
var desctxt = document.createTextNode(inc['description']);
var valtxt = document.createTextNode("+" + inc['money']);
var node1 = document.createElement("i");
var node2 = document.createElement("div");
node2.appendChild(node1);
node1.setAttribute("class", "fa fa-close");
node2.setAttribute("class", "item__delete--btn");
var node3 = document.createElement("div");
node3.appendChild(node2);
var node4 = document.createElement("div");
node4.appendChild(valtxt);
var node5 = document.createElement("div");
node5.setAttribute("class", "right");
node5.appendChild(node4);
node5.appendChild(node3);
var node6 = document.createElement("div");
node6.appendChild(desctxt);
var node7 = document.createElement("div");
node7.setAttribute("class", "item");
node7.appendChild(node6);
node7.appendChild(node5);
document.getElementById("ilist").appendChild(node7);
updateinc1(inc['money']);
document.getElementById("desc").value = "";
document.getElementById("val").value = "";
});
savedexp.forEach(function (inc) {
console.log(inc['description']);
console.log(inc['money']);
var desctxt = document.createTextNode(inc['description']);
var valtxt = document.createTextNode(inc['money']);
var perctxt = document.createTextNode(calc_perc(inc['money']) + "%");
var node1 = document.createElement("i");
var node2 = document.createElement("div");
node2.appendChild(node1);
node1.setAttribute("class", "fa fa-close");
node2.setAttribute("class", "item__delete--btn");
var node3 = document.createElement("div");
node3.appendChild(node2);
var node4 = document.createElement("div");
node4.setAttribute("class", "item__percentage");
node4.appendChild(perctxt);
var node5 = document.createElement("div");
node5.appendChild(valtxt);
var node6 = document.createElement("div");
node6.setAttribute("class", "right");
node6.appendChild(node5);
node6.appendChild(node4);
node6.appendChild(node3);
var node7 = document.createElement("div");
node7.appendChild(desctxt);
var node8 = document.createElement("div");
node8.setAttribute("class", "item");
node8.appendChild(node7);
node8.appendChild(node6);
document.getElementById("elist").appendChild(node8);
updateexp1(inc['money']);
document.getElementById("desc").value = "";
document.getElementById("val").value = "";
});
}
getall();
//remove expense from local storage
function removeLocalExp(value, desc) {
let locexp;
if (localStorage.getItem("locexp") === null) {
locexp = [];
} else {
locexp = JSON.parse(localStorage.getItem("locexp"));
}
let index = locexp.find(item => item.money == value && item.description == desc);
locexp.splice(locexp.indexOf(index), 1);
localStorage.setItem("locexp", JSON.stringify(locexp));
}
//remove Income from local storage
function removeLocalInc(value, desc) {
let locinc;
if (localStorage.getItem("locinc") === null) {
locinc = [];
} else {
locinc = JSON.parse(localStorage.getItem("locinc"));
}
let index = locinc.find(item => item.money == value && item.description == desc);
locinc.splice(locinc.indexOf(index), 1);
localStorage.setItem("locinc", JSON.stringify(locinc));
}
//Chart
Chart.defaults.global.defaultFontColor = "black";
Chart.defaults.global.defaultFontSize = 15;
function drawgraph() {
var ctx = document.getElementById('chart-area').getContext('2d');
var config = new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [{
data: [
sum_exp,
sum_inc
],
backgroundColor: [
"red",
"green",
],
label: 'Income and Expenses'
}],
labels: [
'Expense',
'Income'
]
},
options: {
responsive: true,
legend: {
position: 'top',
},
title: {
display: true,
text: 'Graphical Representation'
},
animation: {
animateScale: true,
animateRotate: true
}
}
});
}