Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
336 lines (277 loc) · 12.1 KB
/
Copy pathscript.js
File metadata and controls
336 lines (277 loc) · 12.1 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Variables
let person1 = "Person 1";
let person2 = "Person 2";
let timeData = { person1: 0, person2: 0 };
let completedChores = 0;
let chores = [];
let namesAreSaved = false;
document.getElementById("weekNumber").textContent = getWeekNumber(new Date()).toString().padStart(2, "0");
initCustomSelects();
initModal();
// Save Names
document.getElementById('saveNames').addEventListener('click', () => {
person1 = document.getElementById('person1').value || "Person 1";
person2 = document.getElementById('person2').value || "Person 2";
document.getElementById('saveNames').style.display = "none";
document.getElementById('personSelect').options[0].text = person1;
document.getElementById('personSelect').options[1].text = person2;
refreshCustomSelect('personSelect');
namesAreSaved = true;
unlockChoreForm();
updateComparison();
});
// Add Task with Enter Key
document.addEventListener('keydown', (event) => {
if (event.key === "Enter" && namesAreSaved) {
document.getElementById('addTask').click();
}
});
// Add Task
document.getElementById('addTask').addEventListener('click', () => {
if (!namesAreSaved) {
showModal("One thing first", "Save the household names before adding chores.");
return;
}
const taskSelect = document.getElementById('taskSelect');
const task = taskSelect.value;
const taskLabel = taskSelect.options[taskSelect.selectedIndex].text;
const time = parseInt(document.getElementById('taskTime').value, 10);
const person = document.getElementById('personSelect').value;
const date = new Date().toLocaleDateString();
if (!time || time < 1) {
showModal("Missing minutes", "Add how many minutes this chore took before logging it.");
return;
}
timeData[person] += time;
completedChores += 1;
chores.push({ task, taskLabel, time, person, date });
const taskList = document.getElementById('taskList');
const listItem = document.createElement('li');
const personName = person === "person1" ? person1 : person2;
listItem.textContent = `${date}: ${taskLabel} (${time} minutes) - ${personName}`;
taskList.appendChild(listItem);
document.getElementById('taskTime').value = "";
updateComparison();
});
function unlockChoreForm() {
const nameForm = document.querySelector('.name-grid');
const choreForm = document.getElementById('choreForm');
const formNote = nameForm.querySelector('.form-note');
formNote.textContent = `${person1} and ${person2}`;
nameForm.classList.add('is-complete');
choreForm.classList.remove('is-locked');
choreForm.classList.add('is-unlocked');
choreForm.setAttribute('aria-hidden', 'false');
}
function initCustomSelects() {
document.querySelectorAll('.custom-select').forEach((customSelect) => {
const select = document.getElementById(customSelect.dataset.select);
const trigger = document.createElement('button');
const optionsList = document.createElement('ul');
trigger.type = "button";
trigger.className = "custom-select-trigger";
optionsList.className = "custom-select-options";
customSelect.appendChild(trigger);
customSelect.appendChild(optionsList);
trigger.addEventListener('click', () => {
closeCustomSelects(customSelect);
customSelect.classList.toggle('is-open');
});
refreshCustomSelect(select.id);
});
document.addEventListener('click', (event) => {
if (!event.target.closest('.custom-select')) {
closeCustomSelects();
}
});
}
function refreshCustomSelect(selectId) {
const select = document.getElementById(selectId);
const customSelect = document.querySelector(`[data-select="${selectId}"]`);
if (!customSelect) {
return;
}
const trigger = customSelect.querySelector('.custom-select-trigger');
const optionsList = customSelect.querySelector('.custom-select-options');
const selectedOption = select.options[select.selectedIndex];
trigger.textContent = selectedOption.text;
optionsList.innerHTML = "";
Array.from(select.options).forEach((option) => {
const optionItem = document.createElement('li');
const optionButton = document.createElement('button');
optionButton.type = "button";
optionButton.className = "custom-select-option";
optionButton.textContent = option.text;
if (option.value === select.value) {
optionButton.classList.add('is-selected');
}
optionButton.addEventListener('click', () => {
select.value = option.value;
refreshCustomSelect(selectId);
closeCustomSelects();
});
optionItem.appendChild(optionButton);
optionsList.appendChild(optionItem);
});
}
function closeCustomSelects(exceptSelect) {
document.querySelectorAll('.custom-select').forEach((customSelect) => {
if (customSelect !== exceptSelect) {
customSelect.classList.remove('is-open');
}
});
}
function initModal() {
document.getElementById('modalClose').addEventListener('click', hideModal);
document.getElementById('appModal').addEventListener('click', (event) => {
if (event.target.id === 'appModal') {
hideModal();
}
});
}
function showModal(title, message) {
document.getElementById('modalTitle').textContent = title;
document.getElementById('modalMessage').textContent = message;
document.getElementById('appModal').hidden = false;
}
function hideModal() {
document.getElementById('appModal').hidden = true;
}
// Update Comparison
function updateComparison() {
const { person1: person1Time, person2: person2Time } = timeData;
const totalTime = person1Time + person2Time;
const person1Percent = totalTime ? Math.round((person1Time / totalTime) * 100) : 50;
const person2Percent = totalTime ? 100 - person1Percent : 50;
const balance = Math.abs(person1Time - person2Time);
let comparisonText = "";
if (person1Time > person2Time) {
comparisonText = `${person2} needs to add ${person1Time - person2Time} more minutes to match ${person1}.`;
} else if (person2Time > person1Time) {
comparisonText = `${person1} needs to add ${person2Time - person1Time} more minutes to match ${person2}.`;
} else {
comparisonText = "Both have contributed equally in terms of time!";
}
document.getElementById('comparison').textContent = comparisonText;
document.getElementById('totalMinutes').textContent = totalTime;
document.getElementById('completedCount').textContent = completedChores;
document.getElementById('balanceDue').textContent = `${balance} min`;
document.getElementById('person1Percent').textContent = `${person1Percent}%`;
document.getElementById('person2Percent').textContent = `${person2Percent}%`;
document.getElementById('person1AllocationLabel').textContent = person1;
document.getElementById('person2AllocationLabel').textContent = person2;
document.getElementById('person1GraphLabel').textContent = person1;
document.getElementById('person2GraphLabel').textContent = person2;
document.getElementById('person1GraphMinutes').textContent = `${person1Time} min`;
document.getElementById('person2GraphMinutes').textContent = `${person2Time} min`;
renderAllocationMap();
}
function renderAllocationMap() {
const personOneMap = document.getElementById('person1ChoreMap');
const personTwoMap = document.getElementById('person2ChoreMap');
personOneMap.innerHTML = "";
personTwoMap.innerHTML = "";
const personOneChores = chores.filter((chore) => chore.person === "person1");
const personTwoChores = chores.filter((chore) => chore.person === "person2");
if (!namesAreSaved) {
personOneMap.classList.add('is-empty');
personOneMap.classList.remove('has-chores');
personTwoMap.classList.add('is-empty');
personTwoMap.classList.remove('has-chores');
personOneMap.appendChild(createEmptyAllocation("+", "Add names to begin"));
personTwoMap.appendChild(createEmptyAllocation("+", "Add names to begin"));
return;
}
if (!personOneChores.length && !personTwoChores.length) {
personOneMap.classList.add('is-empty');
personOneMap.classList.remove('has-chores');
personTwoMap.classList.add('is-empty');
personTwoMap.classList.remove('has-chores');
personOneMap.appendChild(createEmptyAllocation(getInitial(person1)));
personTwoMap.appendChild(createEmptyAllocation(getInitial(person2)));
return;
}
renderPersonChores(personOneMap, personOneChores, "person1");
renderPersonChores(personTwoMap, personTwoChores, "person2");
}
function renderPersonChores(container, personChores, personKey) {
const mapHeight = getMapHeight(personChores);
container.classList.toggle('is-empty', !personChores.length);
container.classList.toggle('has-chores', Boolean(personChores.length));
container.style.setProperty('--map-height', `${mapHeight}px`);
if (!personChores.length) {
container.appendChild(createEmptyAllocation(getInitial(personKey === "person1" ? person1 : person2), "No chores yet"));
return;
}
personChores.forEach((chore, index) => {
container.appendChild(createChoreBlock(chore, index));
});
}
function createChoreBlock(chore, index) {
const block = document.createElement('div');
const choreName = document.createElement('strong');
const choreMeta = document.createElement('span');
const blockSize = Math.min(215, Math.max(118, 96 + chore.time * 1.9));
const mapPosition = getMapPosition(index);
block.className = `chore-block ${chore.person} task-${chore.task}`;
block.style.setProperty('--block-size', `${blockSize}px`);
block.style.setProperty('--block-left', mapPosition.left);
block.style.setProperty('--block-top', mapPosition.top);
block.style.setProperty('--block-radius', mapPosition.radius);
choreName.textContent = chore.taskLabel;
choreMeta.textContent = `${chore.time} min`;
block.appendChild(choreName);
block.appendChild(choreMeta);
return block;
}
function getMapHeight(personChores) {
if (!personChores.length) {
return 480;
}
return personChores.reduce((height, chore, index) => {
const blockSize = Math.min(215, Math.max(118, 96 + chore.time * 1.9));
const position = getMapPosition(index);
const blockBottom = position.topRem * 16 + blockSize + 48;
return Math.max(height, blockBottom);
}, 480);
}
function getMapPosition(index) {
const pattern = [
{ left: "4%", topRem: 1, radius: "999px" },
{ left: "38%", topRem: 0.5, radius: "47% / 40%" },
{ left: "7%", topRem: 15, radius: "42% / 34%" },
{ left: "46%", topRem: 19, radius: "999px" },
{ left: "18%", topRem: 35, radius: "46% / 38%" },
{ left: "42%", topRem: 41, radius: "999px" },
];
const row = Math.floor(index / pattern.length);
const position = pattern[index % pattern.length];
const rowOffset = row * 48;
return {
left: position.left,
top: `${position.topRem + rowOffset}rem`,
topRem: position.topRem + rowOffset,
radius: position.radius,
};
}
function createEmptyAllocation(initial, label = "Equal share") {
const emptyState = document.createElement('div');
const initialElement = document.createElement('span');
const labelElement = document.createElement('p');
emptyState.className = "empty-allocation";
initialElement.textContent = initial;
labelElement.textContent = label;
emptyState.appendChild(initialElement);
emptyState.appendChild(labelElement);
return emptyState;
}
function getInitial(name) {
return name.trim().charAt(0).toUpperCase() || "?";
}
function getWeekNumber(date) {
const currentDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
currentDate.setUTCDate(currentDate.getUTCDate() + 4 - (currentDate.getUTCDay() || 7));
const yearStart = new Date(Date.UTC(currentDate.getUTCFullYear(), 0, 1));
return Math.ceil((((currentDate - yearStart) / 86400000) + 1) / 7);
}
updateComparison();