-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
238 lines (182 loc) · 6.68 KB
/
app.js
File metadata and controls
238 lines (182 loc) · 6.68 KB
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
//Budget Controller
var budgetController = (function() {
var Expense = 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 calculateTotal = function(type) {
var sum = 0;
data.allItems[type].forEach(function(current){
sum = sum + current.value;
});
data.totals[type] = sum;
};
var data = {
allItems: {
exp: [],
inc: [],
},
totals: {
exp: 0,
inc: 0,
},
budget: 0,
percentage: -1
};
return {
addItem: function(type, des, val){
var newItem, ID;
//create new // IDEA:
if (data.allItems[type].length > 0){
ID = data.allItems[type][data.allItems[type].length-1].id + 1;
} else {
ID = 0;
};
//create new item based on inc or exp
if (type === 'exp'){
newItem = new Expense (ID, des, val);
} else if (type === 'inc') {
newItem = new Income(ID, des, val);
};
//push it into data structure
data.allItems[type].push(newItem);
//return new element
return newItem;
},
calculateBudget: function(){
// calc total income and expenses
calculateTotal('exp');
calculateTotal('inc');
//calc budget: income - expenses
data.budget = data.totals.inc - data.totals.exp;
//calc perct of income that is spent
if (data.totals.inc > 0){
data.percentage = Math.round((data.totals.exp / data.totals.inc) * 100);
} else {
data.percentage = -1;
}
},
getBudget: function(){
return {
budget: data.budget,
totalInc: data.totals.inc,
totalExp: data.totals.exp,
percentage: data.percentage
};
}
};
}());
//UI Controller
var UIController = (function() {
var DOMstrings = {
inputType: '.add__type',
inputDescription: '.add__description',
inputValue: '.add__value',
inputBtn: '.add__btn',
incomeContainer: '.income__list',
expenseContainer: '.expenses__list',
budgetLabel: '.budget__value',
incomeLabel: '.budget__income--value',
expensesLabel: '.budget__expenses--value',
percentageLabel: '.budget__expenses--percentage',
itemPercent: '.item__percentage'
}
return {
getInput: function(){
return {
type: document.querySelector(DOMstrings.inputType).value, //Will be inc or exp
description: document.querySelector(DOMstrings.inputDescription).value, //Description of input
value: parseFloat(document.querySelector(DOMstrings.inputValue).value), //Gets input value
};
},
addListItem: function (obj, type){
var html, newHtml, element;
//create html string with placeholder text
if (type === 'inc'){
element = DOMstrings.incomeContainer;
html = '<div class="item clearfix" id="income-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
} else {
element = DOMstrings.expenseContainer;
html = '<div class="item clearfix" id="expense-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__percentage">21%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
}
//replace placeholder text with data
newHtml = html.replace('%id%', obj.id);
newHtml = newHtml.replace('%description%', obj.description);
newHtml = newHtml.replace('%value%', obj.value);
//insert html into DOM
document.querySelector(element).insertAdjacentHTML('beforeend', newHtml);
},
clearFields: function(){
var fields, fieldsArray;
fields = document.querySelectorAll(DOMstrings.inputDescription+ ', ' +
DOMstrings.inputValue);
fieldsArray = Array.prototype.slice.call(fields);
fieldsArray.forEach(function(current, index, array){
current.value = "";
});
fieldsArray[0].focus();
},
displayBudget: function(obj){
document.querySelector(DOMstrings.budgetLabel).textContent = obj.budget;
document.querySelector(DOMstrings.incomeLabel).textContent = obj.totalInc;
document.querySelector(DOMstrings.expensesLabel).textContent = obj.totalExp;
if (obj.percentage > 0){
document.querySelector(DOMstrings.percentageLabel).textContent = obj.percentage;
// document.querySelector(DOMstrings.itemPercent).textContent = obj.percentage;
} else {
document.querySelector(DOMstrings.percentageLabel).textContent = "--";
}
},
getDOMstrings: function (){
return DOMstrings;
}
};
}());
//Global app controller
var controller = (function(budgetCtrl, uiCtrl) {
var setupEventListeners = function (){
var DOM = UIController.getDOMstrings();
document.querySelector(DOM.inputBtn).addEventListener('click', ctrlAddItem);
document.addEventListener('keypress', function(event){
if (event.keyCode === 13 || event.which === 13){
ctrlAddItem();
}
});
};
var updateBudget = function(){
//calculate budget
budgetCtrl.calculateBudget();
//return budget
var budget = budgetCtrl.getBudget();
//display budget on ui
uiCtrl.displayBudget(budget);
};
var ctrlAddItem = function (){
var input, newItem;
//get input data
input = uiCtrl.getInput();
if (input.description !=="" && !isNaN(input.value) && input.value > 0 ){
//add item to budget Controller
newItem = budgetCtrl.addItem(input.type, input.description, input.value);
//add new item to ui
uiCtrl.addListItem(newItem, input.type);
//clear fields
uiCtrl.clearFields();
//calc and update budget
updateBudget();
}
};
return {
init: function(){
console.log('app started');
setupEventListeners();
}
};
})(budgetController,UIController);
controller.init();