-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
272 lines (206 loc) · 7.8 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
import ItemCtrl from "./modules/itemctrl.js";
import StorageCtrl from "./modules/storagectrl.js";
import UICtrl from "./modules/uictrl.js";
import ChartCtrl from "./modules/chartctrl.js";
// Main module
// Event listeners
const loadEventListeners = function () {
const UISelectors = UICtrl.getSelectors();
document.querySelector("#ru").addEventListener("click", toRussianClick);
document.querySelector("#eng").addEventListener("click", toEnglishClick);
document
.querySelector(UISelectors.addBtn)
.addEventListener("click", itemAddSubmit);
document.addEventListener("keypress", function (e) {
if (e.key === "Enter") {
e.preventDefault();
return false;
}
});
document
.querySelector(UISelectors.itemList)
.addEventListener("click", itemEditClick);
document
.querySelector(UISelectors.updateBtn)
.addEventListener("click", itemUpdateSubmit);
document
.querySelector(UISelectors.backBtn)
.addEventListener("click", backBtnClick);
document
.querySelector(UISelectors.deleteBtn)
.addEventListener("click", itemDeleteSubmit);
document
.querySelector(UISelectors.clearBtn)
.addEventListener("click", clearAllItemsClick);
};
const checkLang = function () {
if (JSON.parse(localStorage.getItem("language")) === "ru") {
toRussian();
} else {
toEnglish();
}
};
const toRussian = function () {
UICtrl.changeTextContent(".brand-logo", "Счетчик калорий");
UICtrl.changeTextContent("#clear-all-button", "Очистить все");
UICtrl.changeTextContent(".card-title", "Добавить блюдо и калорийность");
document.querySelector("#item-name").placeholder = "Добавить блюдо";
document.querySelector("#item-calories").placeholder = "Добавить калории";
UICtrl.changeTextContent("#food-label", "Блюдо");
UICtrl.changeTextContent("#calories-label", "Калорийность");
UICtrl.changeTextContent("#add-button", "Добавить");
UICtrl.changeTextContent("#update-button", "Обновить");
UICtrl.changeTextContent("#delete-button", "Удалить");
UICtrl.changeTextContent("#back-button", "Назад");
UICtrl.changeTextContent(
"#total-calories-text",
`Всего калорий: ${ItemCtrl.data.totalCalories}`
);
};
const toEnglish = function () {
UICtrl.changeTextContent(".brand-logo", "Calories tracker App");
UICtrl.changeTextContent("#clear-all-button", "CLEAR ALL");
UICtrl.changeTextContent(".card-title", "Add Meal & Calories");
document.querySelector("#item-name").placeholder = "Add food";
document.querySelector("#item-calories").placeholder = "Add calories";
UICtrl.changeTextContent("#food-label", "Food");
UICtrl.changeTextContent("#calories-label", "Calories");
UICtrl.changeTextContent("#add-button", "ADD MEAL");
UICtrl.changeTextContent("#update-button", "UPDATE MEAL");
UICtrl.changeTextContent("#delete-button", "DELETE MEAL");
UICtrl.changeTextContent("#back-button", "BACK");
UICtrl.changeTextContent(
"#total-calories-text",
`Total Calories: ${ItemCtrl.data.totalCalories}`
);
};
// switch to russian lang
const toRussianClick = function (e) {
UICtrl.changeTextContent(".brand-logo", "Счетчик калорий");
UICtrl.changeTextContent("#clear-all-button", "Очистить все");
UICtrl.changeTextContent(".card-title", "Добавить блюдо и калорийность");
document.querySelector("#item-name").placeholder = "Добавить блюдо";
document.querySelector("#item-calories").placeholder = "Добавить калории";
UICtrl.changeTextContent("#food-label", "Блюдо");
UICtrl.changeTextContent("#calories-label", "Калорийность");
UICtrl.changeTextContent("#add-button", "Добавить");
UICtrl.changeTextContent("#update-button", "Обновить");
UICtrl.changeTextContent("#delete-button", "Удалить");
UICtrl.changeTextContent("#back-button", "Назад");
UICtrl.changeTextContent(
"#total-calories-text",
`Всего калорий: ${ItemCtrl.data.totalCalories}`
);
localStorage.setItem("language", JSON.stringify("ru"));
e.preventDefault();
};
// switch to english lang
const toEnglishClick = function (e) {
UICtrl.changeTextContent(".brand-logo", "Calories tracker App");
UICtrl.changeTextContent("#clear-all-button", "CLEAR ALL");
UICtrl.changeTextContent(".card-title", "Add Meal & Calories");
document.querySelector("#item-name").placeholder = "Add food";
document.querySelector("#item-calories").placeholder = "Add calories";
UICtrl.changeTextContent("#food-label", "Food");
UICtrl.changeTextContent("#calories-label", "Calories");
UICtrl.changeTextContent("#add-button", "ADD MEAL");
UICtrl.changeTextContent("#update-button", "UPDATE MEAL");
UICtrl.changeTextContent("#delete-button", "DELETE MEAL");
UICtrl.changeTextContent("#back-button", "BACK");
UICtrl.changeTextContent(
"#total-calories-text",
`Total Calories: ${ItemCtrl.data.totalCalories}`
);
localStorage.setItem("language", JSON.stringify("eng"));
e.preventDefault();
};
const backBtnClick = function (e) {
UICtrl.clearEditState();
e.preventDefault();
};
const itemAddSubmit = function (e) {
const input = UICtrl.getItemInput();
if (
input.name !== "" && input.calories !== "" && input.specifiedDate !== ""
) {
const newItem = ItemCtrl.addItem(
input.name,
input.calories,
input.specifiedDate
);
UICtrl.addListItem(newItem);
const totalCalories = ItemCtrl.getTotalCalories();
UICtrl.showTotalCalories(totalCalories);
StorageCtrl.storeItem(newItem);
ChartCtrl.initChart(StorageCtrl.getItemsFromStorage());
UICtrl.clearInput();
}
e.preventDefault();
};
const itemEditClick = function (e) {
if (e.target.classList.contains("edit-item")) {
const listID = e.target.parentNode.parentNode.id;
const listIDArr = listID.split("-");
const ID = parseInt(listIDArr[1]);
const itemToEdit = ItemCtrl.getItemById(ID);
ItemCtrl.setCurrentItem(itemToEdit);
UICtrl.addItemToForm();
}
e.preventDefault();
};
const itemUpdateSubmit = function (e) {
const input = UICtrl.getItemInput();
const updatedItem = ItemCtrl.updateItem(
input.name,
input.calories,
input.specifiedDate
);
UICtrl.updateListItem(updatedItem);
const totalCalories = ItemCtrl.getTotalCalories();
UICtrl.showTotalCalories(totalCalories);
StorageCtrl.updateItemStorage(updatedItem);
ChartCtrl.initChart(StorageCtrl.getItemsFromStorage());
UICtrl.clearEditState();
e.preventDefault();
};
const itemDeleteSubmit = function (e) {
const currentItem = ItemCtrl.getCurrentItem();
ItemCtrl.deleteItem(currentItem.id);
UICtrl.deleteListItem(currentItem.id);
const totalCalories = ItemCtrl.getTotalCalories();
UICtrl.showTotalCalories(totalCalories);
StorageCtrl.deleteItemFromStorage(currentItem.id);
UICtrl.clearEditState();
const items = ItemCtrl.getItems();
if (items.length === 0) {
UICtrl.hideList();
}
ChartCtrl.initChart(StorageCtrl.getItemsFromStorage());
e.preventDefault();
};
const clearAllItemsClick = function (e) {
ItemCtrl.clearAllItems();
const totalCalories = ItemCtrl.getTotalCalories();
UICtrl.showTotalCalories(totalCalories);
UICtrl.removeItems();
StorageCtrl.clearItemsFromStorage();
UICtrl.hideList();
ChartCtrl.initChart(StorageCtrl.getItemsFromStorage());
e.preventDefault();
};
function init() {
UICtrl.clearEditState();
const items = ItemCtrl.getItems();
if (items.length === 0) {
UICtrl.hideList();
} else {
UICtrl.populateItemList(items);
}
checkLang();
const totalCalories = ItemCtrl.getTotalCalories();
UICtrl.showTotalCalories(totalCalories);
ChartCtrl.initChart(StorageCtrl.getItemsFromStorage());
loadEventListeners();
}
// Initialize App
init();