-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
302 lines (243 loc) · 9.7 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
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
// Get the user's current month for the month header
function getUserMonth() {
const months = [
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
];
return months[new Date().getMonth()];
}
let selectionMode = false;
function toggleSelectionMode() {
selectionMode = !selectionMode;
const button = document.getElementById('toggleSelectionMode');
button.textContent = selectionMode ? 'finish striking' : 'strike a mission';
}
let habits = [];
let completedHabits = 0;
let totalDaysInMonth = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();
/* Load calendar */
document.addEventListener('DOMContentLoaded', function() {
// Set the month header
const header = document.getElementById('month');
const progress = document.getElementById('progressBase');
progress.style.width = '0%';
header.textContent = getUserMonth();
// Initialize the calendar
const calendar = document.getElementById('calendar');
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
const daysInMonth = new Date(year, month + 1, 0).getDate();
for (let i = 1; i <= daysInMonth; i++) {
const square = document.createElement('div');
square.classList.add('day-square');
square.setAttribute('day', i);
const dayNumber = document.createElement('span');
dayNumber.classList.add('day-number');
dayNumber.textContent = i;
if (i === today.getDate()) {
square.classList.add('today');
square.style.backgroundColor = "#E1D6EC";
}
square.appendChild(dayNumber);
square.addEventListener('click', function() {
openHabits(i);
});
calendar.appendChild(square);
}
function updateProgressBar() {
const progressFill = document.getElementById('progressFill');
const progressPercentage = (completedHabits / totalDaysInMonth) * 100;
const clampedPercentage = Math.min(100, Math.max(0, progressPercentage));
// Update the width of the progress bar's fill
progressFill.style.width = `${clampedPercentage}%`; // Update fill width based on progress
}
// Initialize habit list
// Function to add habits
document.getElementById("input").addEventListener("keydown", function(event) {
if (event.key == "Enter") {
var input = document.getElementById("input").value;
if (input.trim() !== "") {
const item = document.createElement("li");
const text = document.createElement("span");
text.textContent = input;
const colourCircle = document.createElement("div");
colourCircle.classList.add("colourOptions");
const colourInput = document.createElement("input");
colourInput.type = "color";
colourInput.value = "#7E8287"; // Default colour
colourInput.addEventListener("input", function() {
colourCircle.style.backgroundColor = colourInput.value;
});
colourCircle.addEventListener("click", function() {
colourInput.click();
});
item.appendChild(colourCircle);
item.appendChild(text);
item.appendChild(colourInput);
colourInput.addEventListener("change", function() {
storeHabit(input, colourInput.value);
});
document.getElementById("habits").appendChild(item);
document.getElementById("input").value = ""; // Clear input after adding
}
}
});
// Function to store habit and its color
function storeHabit(habitName, color) {
const habitIndex = habits.findIndex(h => h.name === habitName);
if (habitIndex != -1) {
habits[habitIndex].color = color;
} else {
habits.push({
name: habitName,
color: color
});
}
}
// Function to open habit selection popup
function openHabits(day) {
const popup = document.createElement("div");
popup.classList.add("popup");
const habitList = document.createElement("ol");
habits.forEach(function(habit) {
const habitItem = document.createElement("li");
const colourCircle = document.createElement("div");
colourCircle.classList.add("colourOptions");
colourCircle.style.backgroundColor = habit.color;
habitItem.appendChild(colourCircle);
habitItem.appendChild(document.createTextNode(habit.name));
habitItem.addEventListener("click", function() {
selectHabit(day, habit.color);
document.body.removeChild(popup); // Close the popup
});
habitList.appendChild(habitItem);
});
popup.appendChild(habitList);
document.body.appendChild(popup); // Add the popup to the body
}
// Function to select a habit for a day and change the background color
function selectHabit(day, color) {
const daySquare = document.querySelector(`[day="${day}"]`);
if (daySquare) {
daySquare.style.backgroundColor = color;
completedHabits++;
updateProgressBar();
}
}
});
// Add task functionality when 'Enter' is pressed
document.getElementById('addTask').addEventListener('click', function() {
this.blur(); // Prevent focus on click
addBlankTask(); // Add a blank task input when clicked
});
// Add toggle selection mode functionality
document.getElementById('toggleSelectionMode').addEventListener('click', toggleSelectionMode);
// Function to add a blank task input (every time clicking 'Add a Task')
function addBlankTask() {
const taskList = document.getElementById('taskList');
// Create a new blank task input
const taskItem = document.createElement('li');
const inputSpan = document.createElement('span');
inputSpan.className = 'task-text';
inputSpan.textContent = 'new task...'; // Default placeholder text
taskItem.appendChild(inputSpan);
const deleteButton = document.createElement('button');
deleteButton.className = 'delete-task';
deleteButton.textContent = '🗑️';
deleteButton.addEventListener('click', function() {
taskItem.remove(); // Remove task on delete
});
taskItem.appendChild(deleteButton);
// Call function to initialize the task with editability and strike-through ability
makeTaskEditable(taskItem);
taskList.insertBefore(taskItem, document.getElementById('addTask')); // Add the new task before the "Add a new task" button
// Ensure the "Add a new task" button remains at the bottom
addAddNewTaskButton();
}
// Make tasks editable and enable strike-through functionality
function makeTaskEditable(taskItem) {
taskItem.setAttribute('contenteditable', 'true');
// Ensure that the delete button is not editable
const deleteButton = taskItem.querySelector('.delete-task');
if (deleteButton) {
deleteButton.setAttribute('contenteditable', 'false');
}
// Allow striking through tasks when clicked in selection mode
taskItem.addEventListener('click', function() {
if (selectionMode) {
toggleTaskCompletion(taskItem); // Toggle strikethrough when selection mode is active
}
});
// Ensure the task is not editable once 'Enter' key is pressed
taskItem.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
taskItem.blur(); // Exit editing mode on Enter key
// After editing, add a new "Add a new task" button
addAddNewTaskButton();
}
});
}
// Toggle task completion (strikethrough effect)
function toggleTaskCompletion(taskItem) {
taskItem.classList.toggle('completed');
const taskText = taskItem.querySelector('.task-text');
if(taskText) {
taskText.setAttribute('contenteditable', taskItem.classList.contains('completed') ? 'false' : 'true');
} // Toggle 'completed' class for strikethrough effect
}
// Add the "Add a new task" button at the bottom
function addAddNewTaskButton() {
const taskList = document.getElementById('taskList');
// Ensure there's only one "Add a new task" button
let existingAddTaskButton = document.getElementById('addTask');
if (!existingAddTaskButton) {
const addTaskButton = document.createElement('li');
addTaskButton.id = 'addTask';
addTaskButton.textContent = '➕ Add a new task';
addTaskButton.className = 'add-task-button'; // Add a class to style the button
addTaskButton.addEventListener('click', function() {
addBlankTask(); // Add task when the button is clicked
});
addTaskButton.setAttribute('contenteditable', 'false');
taskList.appendChild(addTaskButton);
}
}
// Reinitialize tasks when the page is loaded to allow for new tasks to be editable
document.addEventListener('DOMContentLoaded', function() {
// Initial "Add a new task" button
addAddNewTaskButton();
const taskItems = document.querySelectorAll('#taskList li');
taskItems.forEach(item => {
if(!item.classList.contains('completed')) {
makeTaskEditable(item); // Reinitialize tasks as editable
}
const deleteButton = item.querySelector('.delete-task');
if (deleteButton) {
deleteButton.addEventListener('click', function() {
item.remove(); // Ensure old tasks are deletable
});
}
});
});
const toggleButton = document.getElementById('toggleSelectionMode');
const body = document.body;
toggleButton.addEventListener('click', () => {
body.classList.toggle('selection-mode');
});
document.addEventListener("DOMContentLoaded", function() {
// Select all delete-task buttons
const deleteButtons = document.querySelectorAll('.delete-task');
// Access the audio element
const strikeSound = document.getElementById('strikeSound');
// Add event listeners to each delete button
deleteButtons.forEach(button => {
button.addEventListener('click', function() {
// Play the sound
strikeSound.play();
// Remove the task
const task = button.closest('li');
task.remove();
});
});
});