-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpenses.js
191 lines (149 loc) · 4.91 KB
/
expenses.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
//@ aviGeta sololern
window.onload = function () {
var items;
var bolTog = true;
var editBtn;
var totalSum = 0;
var delEl;
var dataFromlocal = [];
var n = "";
var a = "";
var itm = document.getElementById("items");
var btns = document.getElementById("btns");
var addItem = document.getElementById("add");
//localStorage.removeItem("savedExpenses");
//itm.addEventListener("input",changeWidth);
try{
loadSavedData();
}
catch{
}
function htmlAdd() {
return (
`<div class="ex">
<input type="text"
class="data"
value = ${n}>
<input type="number"
class="item"
value = ${a} >
<div class = "menu">
<span class="menuSimbol">⁝</span>
<button class = "edit"> SAVE </button>
<button class = "del"> X </button>
</div>
</div>`
);
}
function getEl() {
items = document.getElementsByClassName("item");
editBtn = document.getElementsByClassName("edit");
delEl = document.getElementsByClassName("del");
menuSimbol = document.getElementsByClassName("menuSimbol");
}
var tg = true;
function menuItem (){
var pn = this.parentNode.children;
if(tg){
pn[1].style.display="block";
pn[2].style.display="block";
tg=false;
}
else{
pn[1].style.display="none";
pn[2].style.display="none";
tg=true;
}
}
function saveItem(event) {
var ct = event.currentTarget;
/*
for(var i = 0 ; i < (pn.childNodes.length);i++)
alert(pn.childNodes[i].value);
*/
if (bolTog) {
ct.innerHTML = "EDIT";
ct.parentNode.childNodes[1].setAttribute("disabled", "disabled");
ct.parentNode.childNodes[3].setAttribute("disabled", "disabled");
bolTog = !bolTog;
}
else {
ct.innerHTML = "SAVE";
ct.parentNode.childNodes[1].
removeAttribute("disabled");
ct.innerHTML = "SAVE";
ct.parentNode.childNodes[3].
removeAttribute("disabled");
bolTog = !bolTog
}
}
function deleteData(event) {
var dt = event.currentTarget;
dt.parentNode.parentNode.parentNode.removeChild(dt.parentNode.parentNode);
sumExpenses();
}
function sumExpenses() {
getEl();
for (var i = 0; i < (items.length); i++)
totalSum += Number(items[i].value);
document.getElementById("totalSum").innerHTML = totalSum;
totalSum = 0;
}
function saveData() {
try{
var myExpenses = [];
var expensesAmount = document.getElementsByClassName("item");
var expeneseName = document.getElementsByClassName("data");
for (var i = 0; i < (expensesAmount.length); i++) {
var expense = {
expN: expeneseName[i].value,
expA: expensesAmount[i].value
};
myExpenses.push(expense);
}
localStorage.setItem("savedExpenses", JSON.stringify(myExpenses));
}
catch{
}
}
function loadSavedData() {
dataFromlocal = JSON.parse(localStorage.getItem("savedExpenses"));
if (dataFromlocal == null) {
var myExpenses = [];
var expensesAmount = "";
var expeneseName = "";
var expense = {
expN: expeneseName,
expA: expensesAmount
};
myExpenses.push(expense);
localStorage.setItem("savedExpenses", JSON.stringify(myExpenses));
}
else {
for (var i = 0; i < (dataFromlocal.length); i++) {
a = dataFromlocal[i].expA;
n = dataFromlocal[i].expN;
document.getElementById("items").innerHTML += htmlAdd();
}
}
getEl();
listner();
sumExpenses();
}
document.getElementById("saveAll").addEventListener("click", saveData);
function listner() {
for (var i = 0; i < items.length; i++) {
items[i].addEventListener("input", sumExpenses);
editBtn[i].addEventListener("click", saveItem);
delEl[i].addEventListener("click", deleteData);
menuSimbol[i].addEventListener("click", menuItem);
}
}
addItem.addEventListener("click", function () {
n = "";
a = "";
document.getElementById("items").innerHTML += htmlAdd();
getEl();
listner();
});
}