-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
234 lines (191 loc) · 6.95 KB
/
main.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
var budgetController = (function () {
var Income = function (id,description,value) {
this.id = id,
this.description = description,
this.value = value
}
var Expense = function (id,description,value) {
this.id = id
this.description = description
this.value = value
}
var data = {
allItems : {
income : [],
expense : []
},
totals : {
income : 0,
expense : 0
},
budget : 0
}
var calculateTotal = (type) => {
var sum = 0
data.allItems[type].forEach((current) => {
sum += current.value
})
data.totals[type] = sum
return sum
}
return {
addData : (type,des,val) => {
var newItem,ID
// Generate ID
if(data.allItems[type].length > 0) {
ID = data.allItems[type][data.allItems[type].length - 1].id + 1
} else {
ID = 0
}
// Create new Expense or Income
if(type === 'income') {
newItem = new Income(ID,des,val)
} else if(type === 'expense') {
newItem = new Expense(ID,des,val)
}
// Add it to the array
data.allItems[type].push(newItem)
// Increment the number of items
data.totals[type]++
return newItem
},
deleteItem : (type,id) => {
data.allItems[type].forEach((item,index) => {
if(item.id == id){
data.allItems[type].splice(index,1)
return item
}
})
},
calculateBudget : (value,type) => {
calculateTotal('income')
calculateTotal('expense')
data.budget = data.totals.income - data.totals.expense
},
getBudget : () => {
return {
totalIncome : data.totals.income,
totalExpense : data.totals.expense,
totalBudget : data.budget
}
}
}
})()
var UIController = (function () {
// Declare all query selectors
var querySelectors = {
description : '.description',
value : '.value',
select : '.select',
submit : '.submit',
main_container : '.main_container',
income_container : '.income_container',
expense_container : '.expense_container',
total_income_number : '.budget_income-number',
total_expense_number : '.budget_expense-number',
total_budget : '.budget_total-number',
delete : '.delete'
}
return {
getInput : () => {
return {
description : document.querySelector(querySelectors.description).value,
value : parseFloat(document.querySelector(querySelectors.value).value),
type : document.querySelector(querySelectors.select).value
}
},
getQuerySelectors : () => {
return querySelectors
},
showItem : (newItem,type) => {
let output
if(type==='income') {
output = `
<div class="item_container ${type}" id="income-${newItem.id}">
<div class="item_description">${newItem.description}</div>
<div class="item_value">+ ${newItem.value}</div>
<ion-icon class="delete" name="close-circle-outline"></ion-icon>
</div>
`
document.querySelector(querySelectors.income_container).insertAdjacentHTML('beforeend',output)
} else if(type === 'expense') {
output = `
<div class="item_container ${type}" id="expense-${newItem.id}">
<div class="item_description">${newItem.description}</div>
<div class="item_value">- ${newItem.value}</div>
<ion-icon class="delete" name="close-circle-outline"></ion-icon>
</div>
`
document.querySelector(querySelectors.expense_container).insertAdjacentHTML('beforeend',output)
}
},
deleteItem : (type,id) => {
document.querySelectorAll('.item_container').forEach((item) => {
if(item.id == `${type}-${id}`){
item.remove()
}
})
},
updateBudget: (budget) => {
document.querySelector(querySelectors.total_income_number).textContent = budget.totalIncome
document.querySelector(querySelectors.total_expense_number).textContent = budget.totalExpense
document.querySelector(querySelectors.total_budget).textContent = budget.totalBudget
},
clearInput : () => {
document.querySelector(querySelectors.description).value = ''
document.querySelector(querySelectors.value).value = ''
document.querySelector(querySelectors.description).focus()
}
}
})()
var controller = (function (budgetCtrl,UICtrl) {
// Declare all event listeners
var eventListeners = function () {
var querySelectors = UIController.getQuerySelectors()
document.querySelector(querySelectors.submit).addEventListener('click',ctrlAddItem)
document.querySelector(querySelectors.description).addEventListener('keypress',function (event) {
if(event.keyCode === 13) {
ctrlAddItem()
}
})
document.querySelector(querySelectors.value).addEventListener('keypress',function (event) {
if(event.keyCode === 13) {
ctrlAddItem()
}
})
document.querySelector(querySelectors.main_container).addEventListener('click',ctrlDeleteItem)
}
// Update the budget
var updateBudget = function () {
budgetCtrl.calculateBudget()
var newBudget = budgetCtrl.getBudget()
UICtrl.updateBudget(newBudget)
}
// Delete an item
var ctrlDeleteItem = function (event) {
if(event.target.classList.contains('delete')) {
let type = event.target.parentElement.id.split('-')[0]
let id = event.target.parentElement.id.split('-')[1]
var deletedItem = budgetCtrl.deleteItem(type,id)
UICtrl.deleteItem(type,id)
updateBudget()
}
}
// Add an item
var ctrlAddItem = function () {
var input = UICtrl.getInput()
if(input.description !== "" && Number(input.value) && input.value > 0) {
var newItem = budgetCtrl.addData(input.type,input.description,input.value)
updateBudget()
UICtrl.showItem(newItem,input.type)
UICtrl.clearInput()
}
}
return {
init : () => {
console.log("App has started")
eventListeners()
}
}
})(budgetController,UIController)
controller.init()