-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
266 lines (230 loc) · 8.07 KB
/
script.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
document.addEventListener('DOMContentLoaded', () => {
updateCategoryList();
updateCategorySelect();
updateScheduleList();
// Event listener for category input field to add category on Enter key press
const categoryInput = document.getElementById('categoryInput');
categoryInput.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
addCategory();
}
});
// Event listener for habit input field to add habit on Enter key press
const habitInput = document.getElementById('habitInput');
habitInput.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
addHabit();
}
});
});
class Habit {
constructor(name) {
this.name = name;
this.count = 0;
this.schedules = [];
}
increment() {
this.count += 1;
}
addSchedule(time) {
this.schedules.push(time);
}
}
class Category {
constructor(name) {
this.name = name;
this.habits = {};
}
addHabit(habit) {
if (!(habit.name in this.habits)) {
this.habits[habit.name] = habit;
return true;
}
return false;
}
removeHabit(name) {
if (name in this.habits) {
delete this.habits[name];
return true;
}
return false;
}
getHabits() {
return this.habits;
}
}
class HabitTracker {
constructor() {
this.categories = {};
}
addCategory(name) {
if (name && !(name in this.categories)) {
this.categories[name] = new Category(name);
return true;
}
return false;
}
removeCategory(name) {
if (name in this.categories) {
delete this.categories[name];
return true;
}
return false;
}
addHabit(categoryName, habitName) {
if (categoryName in this.categories) {
const habit = new Habit(habitName);
return this.categories[categoryName].addHabit(habit);
}
return false;
}
removeHabit(categoryName, habitName) {
if (categoryName in this.categories) {
return this.categories[categoryName].removeHabit(habitName);
}
return false;
}
getCategories() {
return this.categories;
}
}
const habitTracker = new HabitTracker();
function addCategory() {
const categoryInput = document.getElementById('categoryInput').value.trim();
if (habitTracker.addCategory(categoryInput)) {
updateCategoryList();
updateCategorySelect();
document.getElementById('categoryInput').value = '';
} else {
alert('Category already exists or invalid name.');
}
}
function removeCategory() {
const categorySelect = document.getElementById('categorySelect');
const categoryToRemove = categorySelect.value;
if (categoryToRemove && habitTracker.removeCategory(categoryToRemove)) {
updateCategoryList();
updateCategorySelect();
} else {
alert('Failed to remove category. Please ensure a category is selected.');
}
}
function addHabit() {
const habitInput = document.getElementById('habitInput').value.trim();
const categorySelect = document.getElementById('categorySelect').value;
if (habitTracker.addHabit(categorySelect, habitInput)) {
updateCategoryList();
document.getElementById('habitInput').value = '';
} else {
alert('Habit already exists or invalid name.');
}
}
function removeHabit() {
const habitInput = document.getElementById('habitInput').value.trim();
const categorySelect = document.getElementById('categorySelect').value;
if (habitTracker.removeHabit(categorySelect, habitInput)) {
updateCategoryList();
document.getElementById('habitInput').value = '';
} else {
alert('Habit not found or invalid name.');
}
}
function updateCategoryList() {
const categoriesList = document.getElementById('categoriesList');
categoriesList.innerHTML = '';
const categories = habitTracker.getCategories();
for (let categoryName in categories) {
const category = categories[categoryName];
const categoryDiv = document.createElement('div');
categoryDiv.className = 'category';
const categoryTitle = document.createElement('h3');
categoryTitle.textContent = categoryName;
categoryDiv.appendChild(categoryTitle);
const habitsList = document.createElement('ul');
const habits = category.getHabits();
for (let habitName in habits) {
const habit = habits[habitName];
const li = document.createElement('li');
const habitText = document.createElement('span');
habitText.textContent = `${habit.name} - Count: ${habit.count}`;
const scheduleText = document.createElement('span');
if (habit.schedules.length > 0) {
scheduleText.textContent = 'Time: ' + habit.schedules.join(', Time: ');
}
const button = document.createElement('button');
button.textContent = 'Finished';
button.onclick = () => {
habit.increment();
updateCategoryList();
};
li.appendChild(habitText);
if (habit.schedules.length > 0) {
li.appendChild(scheduleText);
}
li.appendChild(button);
habitsList.appendChild(li);
}
categoryDiv.appendChild(habitsList);
categoriesList.appendChild(categoryDiv);
}
}
function updateCategorySelect() {
const categorySelect = document.getElementById('categorySelect');
categorySelect.innerHTML = '';
const categories = habitTracker.getCategories();
for (let categoryName in categories) {
const option = document.createElement('option');
option.value = categoryName;
option.textContent = categoryName;
categorySelect.appendChild(option);
}
}
function showScheduleMaker() {
document.getElementById('scheduleMaker').style.display = 'block';
}
function hideScheduleMaker() {
document.getElementById('scheduleMaker').style.display = 'none';
}
function addSchedule() {
const habitName = document.getElementById('scheduleHabitInput').value.trim();
const time = document.getElementById('scheduleTimeInput').value;
const categories = habitTracker.getCategories();
for (let categoryName in categories) {
const category = categories[categoryName];
if (habitName in category.getHabits()) {
category.getHabits()[habitName].addSchedule(time);
updateCategoryList();
updateScheduleList();
document.getElementById('scheduleHabitInput').value = '';
document.getElementById('scheduleTimeInput').value = '';
return;
}
}
alert('Habit not found.');
}
function updateScheduleList() {
const scheduleList = document.getElementById('scheduleList');
scheduleList.innerHTML = '';
const categories = habitTracker.getCategories();
for (let categoryName in categories) {
const category = categories[categoryName];
const habits = category.getHabits();
for (let habitName in habits) {
const habit = habits[habitName];
habit.schedules.forEach(time => {
const li = document.createElement('li');
li.textContent = `${habitName} - Time: ${time}`;
scheduleList.appendChild(li);
});
}
}
}
function startTracking() {
document.getElementById('welcomeScreen').style.display = 'none';
document.getElementById('habitTracker').style.display = 'block';
}
document.addEventListener('DOMContentLoaded', () => {
updateCategoryList();
updateCategorySelect();
updateScheduleList();
});