-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathform.js
More file actions
200 lines (134 loc) · 5.7 KB
/
form.js
File metadata and controls
200 lines (134 loc) · 5.7 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
//EVENT HANDLING SECTION
function newBillEvent(event){
event.preventDefault();
var inputBillName = document.getElementById("billName");
var inputBillAmount = document.getElementById("billAmount");
var currentBillsList = document.getElementById("billList");
var currentMonth = document.getElementById("monthName");
var billFrequency = document.getElementById("billFrequency");
// console.log(inputBillName.value.toLowerCase());
// console.log(billOptions);
// var inputBillNameOption = inputBillName.value.toLowerCase();
// if (inputBillNameOption === billOptions.indexOf || inputBillName.value !== null) {
// billOptions.push(inputBillNameOption);
// }
var bill = new BillObject(currentMonth.value, inputBillAmount.value, billFrequency.value, inputBillName.value);
bill.findAndUpdateMonth();
var billSelector = document.getElementById('selectedBill');
var billOption = document.createElement('option');
billOption.value = inputBillName.value;
billOption.textContent = inputBillName.value + " - " + currentMonth.value;
billOption.id = inputBillName.value;
billSelector.appendChild(billOption);
var newListItem = document.createElement("li");
newListItem.textContent = inputBillName.value + " $" + inputBillAmount.value + " - " + currentMonth.value;
newListItem.id = inputBillName.value + '-li';
currentBillsList.appendChild(newListItem);
inputBillName.value = "";
inputBillAmount.value = "";
helperFunctions.pushToLocalStorage();
}
function newRoommateEvent(event){
event.preventDefault();
var targetMonth = document.getElementById('monthName');
var inputRoommate = document.getElementById("newRoomate");
var currentRoommateList = document.getElementById("roommateList");
var roommateSelector = document.getElementById('selectedRoomate');
var child = document.createElement("li");
child.textContent = inputRoommate.value + " - " + targetMonth.value;
child.id = inputRoommate.value + '-li';
var roomateOption = document.createElement('option');
roomateOption.value = inputRoommate.value;
roomateOption.id = inputRoommate.value
roomateOption.textContent = inputRoommate.value + " - " + targetMonth.value;
for (var i = 0; i < allMonths.length; i++) {
if(allMonths[i].monthName === targetMonth.value){
allMonths[i].roommateNameArray.push(inputRoommate.value);
}
}
roommateSelector.appendChild(roomateOption);
currentRoommateList.appendChild(child);
inputRoommate.value = "";
helperFunctions.pushToLocalStorage
}
function removeRoomate(event){
event.preventDefault();
var selectedRoomate = document.getElementById('selectedRoomate');
var targetMonth = document.getElementById('monthName');
var currentRoommateList = document.getElementById("roommateList");
for (var i = 0; i < allMonths.length; i++) {
var nameLocation = allMonths[i].roommateNameArray.indexOf(selectedRoomate.value);
if(targetMonth.value === allMonths[i].monthName){
if (nameLocation > -1) {
allMonths[i].roommateNameArray.splice(nameLocation, 1);
var child = document.getElementById(selectedRoomate.value + '-li')
currentRoommateList.removeChild(child);
child = document.getElementById(selectedRoomate.value);
selectedRoomate.removeChild(child);
break;
}
else{
break;
}
}
}
helperFunctions.pushToLocalStorage();
}
function removeBill(event) {
event.preventDefault();
var selectedBill = document.getElementById('selectedBill');
var targetMonth = document.getElementById('monthName');
var currentBillList = document.getElementById("billList");
for (var i = 0; i < allMonths.length; i++) {
var billObjectLocation = allMonths[i].billNameArray.indexOf(selectedBill.value);
for (var j = 0; j < allMonths[i].billObjectArray.length; j++) {
if(selectedBill.value === allMonths[i].billNameArray[j]){
if (billObjectLocation > -1) {
allMonths[i].grandTotal -= allMonths[i].billAmountArray[j];
allMonths[i].billObjectArray.splice(billObjectLocation, 1);
allMonths[i].billNameArray.splice(billObjectLocation, 1);
allMonths[i].billAmountArray.splice(billObjectLocation, 1);
var child = document.getElementById(selectedBill.value + '-li')
currentBillList.removeChild(child);
child = document.getElementById(selectedBill.value);
selectedBill.removeChild(child);
break;
}
}
}
}
helperFunctions.pushToLocalStorage();
}
function refreshGraphs(event, location) {
event.preventDefault();
var targetMonth = document.getElementById('whichGraph');
for (var i = 0; i < allMonths.length; i++) {
if(targetMonth.value === allMonths[i].monthName){
var parent = document.getElementById('graphDiv1');
var child = document.getElementById('graph1');
parent.removeChild(child);
child = document.createElement('canvas');
child.id = 'graph1';
parent.appendChild(child);
makeBarChart(allMonths[i], 1);
var parent = document.getElementById('graphDiv2');
var child = document.getElementById('graph2');
parent.removeChild(child);
child = document.createElement('canvas');
child.id = 'graph2';
parent.appendChild(child);
makeDoughnutChart(allMonths[i], 2);
}
}
}
startUpCheckStorage();
var newBillButton = document.getElementById("nb");
newBillButton.addEventListener("click", newBillEvent);
var newRoommateButton = document.getElementById("nr");
newRoommateButton.addEventListener("click", newRoommateEvent);
var refreshButton = document.getElementById('refGraphs');
refreshButton.addEventListener("click", refreshGraphs);
var removeRoomateButton = document.getElementById('removeRoomateBbutton');
removeRoomateButton.addEventListener('click', removeRoomate);
var removeBillButton = document.getElementById('removeBillButton');
removeBillButton.addEventListener('click', removeBill);