-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
166 lines (136 loc) · 5.24 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
// Module Pattern
// Encapsulation : Hides the data from outside scope and expose only public function also known as API.
//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 data = {
allItems: {
exp: [],
inc: []
},
totals: {
exp: 0,
inc: 0
}
}
return {
addItem: function(type, des, val) {
var newItem, ID;
//Crate new ID
if (data.allItems[type].length > 0) {
ID = data.allItems[type][data.allItems[type].length -1].id;
} else {
ID = 0;
}
//Create new item based on type
if(type === 'exp') {
newItem = new Expense(ID, des, val);
} else if(type === 'inc') {
newItem = new Income(ID, des, val);
}
//Push it into our data structure
data.allItems[type].push(newItem);
//return new item
return newItem;
},
testing: function() {
console.log(data);
}
}
})();
//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'
}
return {
getInput: function() {
return {
type: document.querySelector(DOMstrings.inputType).value, // will be either 'inc' or 'exp'
description: document.querySelector(DOMstrings.inputDescription).value,
value: document.querySelector(DOMstrings.inputValue).value
};
},
addListItem: function(obj, type) {
var html, newHtml, element;
//Create HTML strings 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 the placeHolder text with some actual 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);
},
getDOMstrings: function() {
return DOMstrings;
}
};
})();
//Global app Controller
var controller = (function(budgetCtrl, UICtrl) {
var setupEventListeners = function() {
var DOM = UICtrl.getDOMstrings();
document.querySelector(DOM.inputBtn).addEventListener('click', ctrlAddItem);
document.addEventListener('keypress', function(event) {
if(event.keyCode === 13 || event.which === 13) {
ctrlAddItem();
}
});
};
var ctrlAddItem = function() {
var input, newItem;
// 1. Get field input data
input = UICtrl.getInput();
// 2. add the item to the budget controller
newItem = budgetCtrl.addItem(input.type, input.description, input.value);
// 3. add item to the UI
UICtrl.addListItem(newItem, input.type);
// 4. Calculate the budget
// 5. Display the budget to UI
};
return {
init: function() {
console.log('Application has started!!');
setupEventListeners();
}
}
})(budgetController, UIController);
controller.init();