-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
265 lines (245 loc) · 7.94 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
function selector(select) {
return document.querySelector(select)
}
// counter for finding elements in array
var counterInc,
counterExp;
counterInc = 1;
counterExp = 1;
/*!!! Data - save values !!!*/
var budgetController = (function() {
var Expence = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
}
var Income = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
}
var data = {
allItams: {
exp: [],
inc: []
},
total: {
exp: 0,
inc: 0
},
percent: 0,
percentOfEachExp: [],
budget: 0
}
return {
addItam: function(typ, des, vel) {
var newItam,
id;
// generate unic id
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
id = guid();
// add new object
if (typ === 'inc') {
newItam = new Income(id, des, vel);
data.allItams.inc.push(newItam);
// console.log(newItam);
// console.log(data);
} else if (typ === 'exp') {
newItam = new Expence(id, des, vel);
data.allItams.exp.push(newItam);
// console.log(newItam);
// console.log(data);
}
},
retAllItams: function() {
return data.allItams;
},
returnData: function() {
return data;
},
returnIdOfElementIMustDelete: function(id) {
var checkInc = function(ind) {
data.total.inc -= data.allItams.inc[ind].value;
data.budget = data.total.inc - data.total.exp;
}
var checkExp = function(ind){
data.total.exp -= data.allItams.exp[ind].value;
data.budget = data.total.inc - data.total.exp;
//data.allItams.exp.splice(ind, 1);
}
data.allItams.inc.forEach(function(cur, ind) {
if (cur.id === id) {
checkInc(ind)
}
})
data.allItams.exp.forEach(function(cur, ind) {
if (cur.id === id) {
checkExp(ind)
}
})
},
// addPercentes: function(){
// for (var i = 0; i < data.allItams.exp.length; i++) {
// data.percentOfEachExp[i] = Math.round(data.allItams.exp[i].value / data.total.exp * 100);
// console.log(data.percentOfEachExp[i]);
// }
// },
}
})();
/*!!! Update UI !!!*/
var UIController = (function() {
// get class
var UIComponents = {
type: ".add__type",
desk: ".add__description",
money: ".add__value",
updateInc: '.income__list',
updateExp: '.expenses__list'
}
return {
// read values
readUIvalues: function() {
return {
typeOfValue: selector(UIComponents.type).value,
description: selector(UIComponents.desk).value,
valueOfMoney: selector(UIComponents.money).value
}
},
// add new html in list
addString: function(obj, dat) {
var html;
if (obj.inc.length === counterInc) {
html = `
<div class="item clearfix" id="${obj.inc[obj.inc.length - 1].id}">
<div class="item__description">${obj.inc[obj.inc.length - 1].description}</div>
<div class="right clearfix">
<div class="item__value">${UIController.workWithNums(obj.inc[obj.inc.length - 1].value)}</div>
<div class="item__delete">
<button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button>
</div>
</div>
</div>`;
counterInc++;
selector('.income__list').insertAdjacentHTML('beforeend', html);
//calculate total inc
dat.total.inc += obj.inc[obj.inc.length - 1].value;
//console.log(dat.total.inc);
} else if (obj.exp.length === counterExp) {
html = `<div class="item clearfix" id="${obj.exp[obj.exp.length - 1].id}">
<div class="item__description">${obj.exp[obj.exp.length - 1].description}</div>
<div class="right clearfix">
<div class="item__value">${UIController.workWithNums(obj.exp[obj.exp.length - 1].value)}</div>
<div class="item__delete">
<button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button>
</div>
</div>
</div>`;
counterExp++;
selector('.expenses__list').insertAdjacentHTML('beforeend', html);
// calculate total exp
dat.total.exp += obj.exp[obj.exp.length - 1].value;
}
//calculate budget
dat.total.budget = dat.total.inc - dat.total.exp;
// calculate percent
if (dat.total.inc > 0) {
dat.total.percent = Math.round((dat.total.exp / dat.total.inc) * 100);
}
},
// clear fields after add new element
clearFilds : function(){
var filds, arrFilds;
filds = document.querySelectorAll(`.add__description` + ', ' + '.add__value');
arrFilds = Array.prototype.slice.call(filds);
arrFilds.forEach(function (cur, ind, ar) {
cur.value = '';
});
arrFilds[0].focus();
},
// display budget
displayBudget : function(dat) {
selector('.budget__expenses--value').textContent = UIController.workWithNums(dat.total.exp);
selector('.budget__income--value').textContent = UIController.workWithNums(dat.total.inc);
selector('.budget__value').textContent = UIController.workWithNums(dat.total.inc - dat.total.exp);
if (dat.total.percent > 0) {
selector('.budget__expenses--percentage').textContent = Math.round(dat.total.exp / dat.total.inc * 100) + '%';
} else {
selector('.budget__expenses--percentage').textContent = '---';
}
},
// remove items from ui
removeItamsUI : function(id) {
var selected = document.getElementById(id)
selected.parentNode.removeChild(selected);
},
// make numbers nice look
workWithNums : function(num) {
var numSplit;
num = Math.abs(num);
num = num.toFixed(2);
numSplit = num.split('.');
int = numSplit[0]
if (int.length > 3) {
int = int.substr(0, int.length - 3) + "," + int.substr(int.length-3, 3);
}
num = int + '.' + numSplit[1];
return num;
},
// display date
displayDate : function(){
var now, year;
now = new Date();
year = now.getFullYear();
selector('.budget__title--month').textContent = year;
}
};
})();
/*!!! controller !!!*/
var controller = (function(budgetCtrl, UICtrl) {
function events() {
// event listeners
selector('.add__btn').addEventListener('click', addValue);
// on key press - enter
document.addEventListener('keypress', function(event) {
if (event.keyCode === 13) {
addValue();
}
});
// remove itams
selector('.container').addEventListener('click', deletItamFromData);
}
// remove itams
var deletItamFromData = function(event) {
var ID;
ID = event.target.parentNode.parentNode.parentNode.parentNode.id;
budgetCtrl.returnIdOfElementIMustDelete(ID);
UICtrl.removeItamsUI(ID);
UICtrl.displayBudget(budgetCtrl.returnData());
//budgetCtrl.addPercentes();
}
//save values
var counter = 1;
var addValue = function() {
if (UICtrl.readUIvalues().description !== '' && UICtrl.readUIvalues().valueOfMoney !== '' && UICtrl.readUIvalues().valueOfMoney !== '0') {
budgetCtrl.addItam(UICtrl.readUIvalues().typeOfValue, UICtrl.readUIvalues().description, parseFloat(UICtrl.readUIvalues().valueOfMoney));
UICtrl.addString(budgetCtrl.retAllItams(), budgetCtrl.returnData());
UICtrl.clearFilds();
UICtrl.displayBudget(budgetCtrl.returnData());
//budgetCtrl.addPercentes();
}
};
UICtrl.displayDate();
// let`s make light!
return {
returnEvents: function() {
return events();
}
}
})(budgetController, UIController);
// start events
controller.returnEvents();