forked from Subhash2807/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (109 loc) · 2.58 KB
/
app.js
File metadata and controls
133 lines (109 loc) · 2.58 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
// Budget contoller
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;
if(data.allItems[type].lenth>0)
{
ID=data.allItems[type][data.allItems[type].length-1]id+1;
}
else ID=0;
if(type==='exp'){
newItem new Expense(ID,des, val);
}
else if(type==='inc')
{
newItem new Expense(ID,des, val);
}
}
data.allItems[type].push(newItem);
return newItem;
}
})();
// UI Controller
var UIController = (function()
{
var DOMstrings =
{
inputType : '.add__type',
inputDescription : '.add__description',
inputValue : '.add__valu',
inputBtn: '.add__btn'
};
return
{
getinput: function()
{
return
{
type : document.querySelector(DOMstrings.inputType).value,
description : document.querySelector(DOMstrings.inputDescription).value,
value : document.querySelector(DOMstrings.inputValue).value;
}
},
getDOMstrings : function()
{
return DOMstrings
}
};
})();
// Global APP
var controller = (function(budgetCtrl, UICtrl)
{
var DOM =UICtrl.getDOMstrings();
var setupEventListeners=function()
{
document.querySelector.(DOM.inputBtn).addEventListener('click',ctrlAddItem);
document.addEventListener('keypress', function(event)
{
if(event.keyCode === 13)
{
ctrlAddItem();
}
});
}
var ctrlAddItem = function()
{
// 1. Get input data
var input = UICtrl.getInput();
// 2. Add the item to the budget controller
budgetCtrl.addItem(input.type, input.description, input.value);
// 3. add to UI
// 4. Calculate Budget.
// 5. Display Budget.
};
return
{
init: function()
{
setupEventListeners();
}
}
})(budgetController, UIController);
controller.init();