-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
653 lines (285 loc) · 10.2 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
380
//Data Controller
var dataController=(function(){
var Expense =function(id,description,value){
this.id=id;
this.description= description;
this.value= value;
this.percentage=-1;
};
Expense.prototype.calcPercentage=function(totalInc){
if(totalInc>0){
this.percentage=Math.round((this.value/totalInc)*100);
}else{
this.percentage=-1;
}
}
Expense.prototype.getPerc=function(){
return this.percentage;
}
var Income =function(id,description,value){
this.id=id;
this.description= description;
this.value= value
};
var data={
allItems:{
inc:[],
exp:[]
},
totals:{
exp:0,
inc:0
},
budget:0,
percentage: -1
};
var calculateBudget= function(type){
var sum=0;
data.allItems[type].forEach(function(cur){
sum+=cur.value;
});
data.totals[type]=sum;
} ;
return{
addItem: function(type,des,val){
var newItem,ID;
//creqate new id
if(data.allItems[type].length>0){
var ID=data.allItems[type][data.allItems[type].length-1].id+1;
}else{
ID=0;
}
//create new item
if(type==="exp"){
newItem=new Expense(ID,des,val);
}else{
newItem=new Income(ID,des,val);
}
//push into data structure
data.allItems[type].push(newItem);
return newItem;
},
deleteItem: function(type,id){
var ids=data.allItems[type].map(function(current){
return current.id;
});
console.log(ids);
var index=ids.indexOf(id);
console.log(index);
if(index!==-1){
data.allItems[type].splice(index,1);
}
},
calculateBudget: function(){
//calculate total income and expenses
calculateBudget("exp");
calculateBudget("inc");
//calculate the budget
data.budget=data.totals.inc-data.totals.exp;
//calculate the percentage of income that we spent
if(data.totals.inc>0){
data.percentage=Math.round((data.totals.exp/data.totals.inc)*100);
}else{
data.percentage=-1;
}
},
calculatePercentage: function(){
data.allItems.exp.forEach(function(cur){
cur.calcPercentage(data.totals.inc);
});
},
getPercentage:function(){
var allperc=data.allItems.exp.map(function(cur){
return cur.getPerc();
});
return allperc;
},
getBudget: function(){
return{
budget: data.budget,
totalInc:data.totals.inc,
totalExp:data.totals.exp,
percentage:data.percentage
};
},
testing:function(){
console.log(data);
}
};
})();
//UI controller
var UIController=(function(){
var DOMStrings={
inputType:".add__type",
inputDescription:".add__description",
inputValue:".add__value",
addButton: ".add__btn",
expensesContainer:".expenses__list",
incomeContainer:".income__list",
container:".container "
}
return{
getInput: function(){
return{
type: document.querySelector(DOMStrings.inputType).value,
description: document.querySelector(DOMStrings.inputDescription).value,
value: parseFloat(document.querySelector(DOMStrings.inputValue).value)
} ;
},
getDOMStrings: function(){
return DOMStrings;
},
addList: function(obj,type){
var html;
//html portion to be added
if(type=="inc"){
html =`<div class="item clearfix" id="inc-${obj.id}">
<div class="item__description">${obj.description}</div>
<div class="right clearfix">
<div class="item__value">${obj.value}</div>
<div class="item__delete">
<button class="item__delete--btn"><i class="ion-ios-close-outline"></i>del</button>
</div>
</div>
</div>`;
//Insert html into specified position
document.querySelector(DOMStrings.incomeContainer).insertAdjacentHTML("beforeend",html);
}else{
html=` <div class="item clearfix" id="exp-${obj.id}">
<div class="item__description">${obj.description}</div>
<div class="right clearfix">
<div class="item__value">${obj.value}</div>
<div class="item__percentage">21%</div>
<div class="item__delete">
<button class="item__delete--btn"><i class="ion-ios-close-outline"></i>del</button>
</div>
</div>
</div>`;
//Insert html into specified position
document.querySelector(DOMStrings.expensesContainer).insertAdjacentHTML("beforeend",html);
}
/*
//replace placeholdertext with some actual value
newHtml=html.replace("%id%",obj.id);
newHtml=newhtml.replace("%description%",obj.description);
newHtml=newhtml.replace("%value%",obj.value);
*/
},
deleteList: function(selectedId){
var list=document.getElementById(selectedId);
list.parentNode.removeChild(list);
},
clearinput: function(){
//document.querySelector(DOMStrings.inputType).value="";
document.querySelector(DOMStrings.inputDescription).value="";
document.querySelector(DOMStrings.inputValue).value="";
document.querySelector(DOMStrings.inputDescription).focus();
},
displayBudget: function(obj){
document.querySelector(".budget__value").innerHTML=obj.budget;
document.querySelector(".budget__income--value").innerHTML=obj.totalInc;
document.querySelector(".budget__expenses--value").innerHTML=obj.totalExp;
if(obj.totalInc>obj.totalExp){
document.querySelector(".budget__expenses--percentage").innerHTML=obj.percentage + "%";
}else{
document.querySelector(".budget__expenses--percentage").innerHTML="--";
}
},
displayMonth:function(){
now=new Date();
var months=['January','February','March','April','May','June','July','August','September','October','November','December'];
var month=now.getMonth();
var year=now.getFullYear();
document.querySelector(".budget__title--month").textContent=months[month]+" "+year;
},
displayPercentage: function(percentage){
var fields=document.querySelectorAll(".item__percentage");
NodeListforEach=function(list){
for(var i=0;i<list.length;i++){
if(percentage[i]>0){
list[i].textContent=percentage[i]+"%";
}
else{
list[i].textContent="--";
}
}
};
NodeListforEach(fields);
}
};
})();
//App Controller
var appController=(function(dataCtrl,UICtrl){
//var DOM=UICtrl.getDOMStrings();
var updateBudget= function(){
//1.calculate bhdget
dataCtrl.calculateBudget();
//2.return budget
var budget=dataCtrl.getBudget();
//3.display the budget on UI
console.log(budget);
UICtrl.displayBudget(budget);
};
var updatePercentages=function(){
//1.calculate percentage
dataCtrl.calculatePercentage();
//2.get percentage fron data controller
perc=dataCtrl.getPercentage();
// console.log(dataCtrl.getPercentage());
//3.display percentage in UI
UICtrl.displayPercentage(perc);
};
var addItem=function(){
//1.get input
var input= UICtrl.getInput();
//console.log(input);
//2.add the input to data controller
if(input.description!=="" && !isNaN(input.value) && input.value>0){
var newItem=dataCtrl.addItem(input.type,input.description,input.value);
//3.add the item to UI
UICtrl.addList(newItem,input.type);
//clear inputfields after addding item
UICtrl.clearinput();
//4.Calculate the budget
updateBudget();
//Update percentages
updatePercentages();
}
// console.log("works");
};
var ctrlDeleteItem=function(event){
var itemId=event.target.parentNode.parentNode.parentNode.id;
if (itemId){
splitID=itemId.split('-');
type=splitID[0];
ID=parseInt(splitID[1]);
//console.log(type);
//console.log(ID);
//delete item from data structure
dataCtrl.deleteItem(type,ID);
//delete item from UI
UICtrl.deleteList(itemId);
//update and show new budget
updateBudget();
//update percentage
updatePercentages();
}
};
return{
init:function(){
UICtrl.displayBudget( {budget: 0,totalInc: 0,totalExp: 0,percentage: 0});
UICtrl.displayMonth();
document.querySelector(".add__type").focus();
var DOM=UICtrl.getDOMStrings();
document.querySelector(DOM.addButton).addEventListener("click",addItem);
document.addEventListener("keypress",function(event){
if(event.keyCode===13 || event.which===13){
addItem();
}
});
document.querySelector(DOM.container).addEventListener("click",ctrlDeleteItem);
//UICtrl.displayBudget( {budget: 0,totalInc: 0,totalExp: 0,percentage: 0});
}
};
})(dataController,UIController);
appController.init();