-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
118 lines (99 loc) · 2.61 KB
/
app.js
File metadata and controls
118 lines (99 loc) · 2.61 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
//jshint esversion:6
// Budget
// BUDGET CONTROLLER
let budgetController = (function () {
let Expense = function (id, description, value) {
this.id = id;
this.description = description;
this.value = value;
};
let Income = function (id, description, value) {
this.id = id;
this.description = description;
this.value = value;
};
let data = {
allItems: {
exp: [],
inc: [],
},
totals: {
exp: 0,
inc: 0
}
};
return {
addItem: function (type, des, val){
let newItem, ID;
// Create new ID
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'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 the new element
return newItem;
},
testing:function(){
console.log(data)
}
};
})();
//UI CONTROLLER
let UIController = (function () {
let DomStrings = {
inputType: ".addType",
inputDescription: ".addDescription",
inputValue: ".addValue",
inputBtn: ".addBtn",
};
return {
getinput: function () {
return {
type: document.querySelector(DomStrings.inputType).value, //will be either in or exp
description: document.querySelector(DomStrings.inputDescription).value,
value: document.querySelector(DomStrings.inputValue).value,
};
},
getDomStrings: function () {
return DomStrings;
},
};
})();
// GLOBAL APP CONTROLLER
let controller = (function (budgetCtrl, UICtrl) {
let setupEventListeners = function () {
let DOM = UICtrl.getDomStrings();
document.querySelector(DOM.inputBtn).addEventListener("click", ctrlAddItem);
document.addEventListener("keypress", function (e) {
if (e.keyCode == 13 || e.which == 13) {
ctrlAddItem();
}
});
};
let ctrlAddItem = function () {
// TodoLis
// 1. Get the field input data
let input = UICtrl.getinput();
// 2. Add item to the budget controller
let newItem = budgetCtrl.addItem(input.type, input.description, input.value);
// 3. Add item to the UI
// 4. Calculate the budget
// 5. Display the budget on the UI
};
return {
init: function () {
console.log("Application has started!");
setupEventListeners();
},
};
})(budgetController, UIController);
controller.init();