-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
305 lines (232 loc) · 8.56 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
(function(){
// App main object and namespace
var App = {};
// increment number for id's
App.incIdNumber = 1;
// ingredients list
App.ingredients = [];
// initial ingredients in list
App.initialIngredientsInList = 1;
// ingredients properties blueprint
App.ingredientProperties = {
properties: {
n: {},
name: {},
amount: {},
},
nutrients: {
energy: {},
carbs: {},
sugars: {},
protein: {},
fat: {},
saturated: {},
trans: {},
sodium: {},
},
};
// app buttons
App.addIngredientButton = document.getElementById("add-ingredient-button");
App.removeLastIngredientButton = document.getElementById("remove-last-ingredient-button");
App.calculateButton = document.getElementById("calculate-button");
/*******************************
Ingredient
params: name, amount, energy, carbs, sugars, protein, fat, saturatedFats, transFats, sodium
*******************************/
App.Ingredient = function(properties = {}, nutrients = {}){
//var properties = properties == null ? App.ingredientProperties.properties : properties;
//var nutrients = nutrients == null ? App.ingredientProperties.nutrients : nutrients;
// @Properties
this.id = App.incIdNumber;
this.properties = {};
this.nutrients = {};
this.tds = {};
this.inputFields = {};
// set ingredient properties
for(var prop in App.ingredientProperties.properties){
this.properties[prop] = properties[prop] ? properties[prop] : 0;
}
// set ingredient nutrients
for(var nutrient in App.ingredientProperties.nutrients){
this.nutrients[nutrient] = nutrients[nutrient] ? nutrients[nutrient] : 0;
}
// push to app array
App.ingredients.push(this);
// inc app number
App.incIdNumber++;
// create and set ingredient's tr element
this.tr = document.createElement("tr");
this.tr.setAttribute("app-ingredient-id", this.id);
/***************************************************
create td and input fields based on properties and nutrients
***************************************************/
for(var prop in this.properties){
var attributeId = this.id + "-" + prop;
var td = document.createElement("td");
var input = document.createElement("input");
td.id = "td-" + attributeId;
input.id = "input-" + attributeId;
input.className = "form-control";
this.tds[prop] = td;
this.inputFields[prop] = input;
this.tr.appendChild(td);
td.appendChild(input);
};
for(var nutrient in this.nutrients){
var attributeId = this.id + "-" + nutrient;
var td = document.createElement("td");
var input = document.createElement("input");
td.id = "td-" + attributeId;
input.id = "input-" + attributeId;
input.className = "form-control";
this.tds[nutrient] = td;
this.inputFields[nutrient] = input;
this.tr.appendChild(td);
td.appendChild(input);
};
/***************************************************
get total nutrients based on ingredient amount
***************************************************/
this.getTotalNutrientsByAmount = function(){
return {
totalEnergy: (this.nutrients.energy * this.properties.amount / 100),
totalCarbs: (this.nutrients.carbs * this.properties.amount / 100),
totalSugars: (this.nutrients.sugars * this.properties.amount / 100),
totalProtein: (this.nutrients.protein * this.properties.amount / 100),
totalFat: (this.nutrients.fat * this.properties.amount / 100),
totalSaturated: (this.nutrients.saturated * this.properties.amount / 100),
totalTrans: (this.nutrients.trans * this.properties.amount / 100),
totalSodium: (this.nutrients.sodium * this.properties.amount / 100)
}
};
/*****************************************
get properties from input fields
*****************************************/
this.getPropertiesFromInputs = function(){
for(var prop in this.properties){
var input = this.inputFields[prop];
var inputValue = input.value;
if(prop == "name"){
this.properties.name = inputValue;
}else{
this.properties[prop] = !isNaN(parseFloat(inputValue)) ? parseFloat(inputValue) : 0;
}
}
for(var nutrient in this.nutrients){
var input = this.inputFields[nutrient];
var inputValue = input.value;
this.nutrients[nutrient] = !isNaN(parseFloat(inputValue)) ? parseFloat(inputValue) : 0;
}
};
/**********************************
add to table
**********************************/
this.addToTable = function(){
var listTableBody = document.getElementById('list-table-body');
this.tds['n'].innerHTML = listTableBody.getElementsByTagName("tr").length + 1;
listTableBody.appendChild(this.tr);
};
/**********************************
get list index
**********************************/
this.getListIndex = function(){
return App.ingredients.indexOf(this);
};
/**********************************
remove
**********************************/
this.remove = function(){
var listTableBody = document.getElementById('list-table-body');
App.ingredients.splice(this.getListIndex(), 1);
listTableBody.removeChild(this.tr);
};
// log ingredient
console.log(this);
};
/**********************************
calculate
(need some fixes)
**********************************/
App.calculate = function(){
var totalAmount = 0;
var totalEnergy = 0;
var totalCarbs = 0;
var totalSugars = 0;
var totalProtein = 0;
var totalFat = 0;
var totalFatSaturated = 0;
var totalFatTrans = 0;
var totalSodium = 0;
for(var i=0; i<App.ingredients.length; i++){
var ing = App.ingredients[i];
ing.getPropertiesFromInputs();
var totals = ing.getTotalNutrientsByAmount();
totalAmount += (ing.properties.amount);
totalEnergy += totals.totalEnergy;
totalCarbs += totals.totalCarbs;
totalSugars += totals.totalSugars;
totalProtein += totals.totalProtein;
totalFat += totals.totalFat;
totalFatSaturated += totals.totalSaturated;
totalFatTrans += totals.totalTrans;
totalSodium += totals.totalSodium;
};
if(totalAmount <= 0){
return;
};
document.getElementById("total-amount").innerHTML = (totalAmount).toFixed(2);
document.getElementById("total-energy").innerHTML = (totalEnergy).toFixed(2);
document.getElementById("total-carbs").innerHTML = (totalCarbs).toFixed(2);
document.getElementById("total-sugars").innerHTML = (totalSugars).toFixed(2);
document.getElementById("total-protein").innerHTML = (totalProtein).toFixed(2);
document.getElementById("total-fat").innerHTML = (totalFat).toFixed(2);
document.getElementById("total-fat-saturated").innerHTML = (totalFatSaturated).toFixed(2);
document.getElementById("total-fat-trans").innerHTML = (totalFatTrans).toFixed(2);
document.getElementById("total-sodium").innerHTML = (totalSodium).toFixed(2);
document.getElementById("total-energy-percent").innerHTML = (100 * totalEnergy / totalAmount).toFixed(2);
document.getElementById("total-carbs-percent").innerHTML = (100 * totalCarbs / totalAmount).toFixed(2);
document.getElementById("total-sugars-percent").innerHTML = (100 * totalSugars / totalAmount).toFixed(2);
document.getElementById("total-protein-percent").innerHTML = (100 * totalProtein / totalAmount).toFixed(2);
document.getElementById("total-fat-percent").innerHTML = (100 * totalFat / totalAmount).toFixed(2);
document.getElementById("total-fat-saturated-percent").innerHTML = (100 * totalFatSaturated / totalAmount).toFixed(2);
document.getElementById("total-fat-trans-percent").innerHTML = (100 * totalFatTrans / totalAmount).toFixed(2);
document.getElementById("total-sodium-percent").innerHTML = (100 * totalSodium / totalAmount).toFixed(2);
};
/**********************************
add ingredient to the table
**********************************/
App.addListIngredient = function(){
var ingredient = new App.Ingredient();
ingredient.addToTable();
};
/**********************************
remove last ingredient from the table
**********************************/
App.removeLastListIngredient = function(){
var last = App.ingredients[App.ingredients.length - 1];
if(last){
last.remove();
}
};
/**********************************
setup
**********************************/
App.setup = function(){
App.addIngredientButton.onclick = function(){
App.addListIngredient();
};
App.removeLastIngredientButton.onclick = function(){
App.removeLastListIngredient();
};
App.calculateButton.onclick = function(){
App.calculate();
};
for(var i=0; i<App.initialIngredientsInList; i++){
App.addListIngredient();
};
};
/**********************************
export to window
**********************************/
window.App = App;
})();