-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
209 lines (184 loc) · 6.14 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
document
.getElementById('addNamesButton')
.addEventListener('click', function () {
const namesInput = document.getElementById('namesInput');
const names = namesInput.value
.split(',')
.map(name => name.trim())
.filter(name => name !== '');
names.forEach(name => {
if (name) {
addNameToList(name);
}
});
namesInput.value = ''; // Clear the input field
});
document.getElementById('addNameButton').addEventListener('click', function () {
const nameInput = document.getElementById('nameInput');
const name = nameInput.value.trim();
if (name) {
addNameToList(name);
nameInput.value = '';
}
});
function addNameToList(name) {
const nameList = document.getElementById('nameList');
const listItem = document.createElement('li');
listItem.textContent = name;
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', function () {
nameList.removeChild(listItem);
});
const unavailableDaysInput = document.createElement('input');
unavailableDaysInput.type = 'text';
unavailableDaysInput.placeholder = 'Unavailable days (e.g., [1-10],25)';
listItem.appendChild(unavailableDaysInput);
listItem.appendChild(removeButton);
nameList.appendChild(listItem);
}
document
.getElementById('inputForm')
.addEventListener('submit', function (event) {
event.preventDefault();
// Validate that at least one name has been added
const nameListItems = document.getElementById('nameList').children;
if (nameListItems.length === 0) {
alert('Please add at least one name.');
return;
}
// Validate that a month has been selected
const month = parseInt(document.getElementById('month').value);
if (isNaN(month) || month < 1 || month > 12) {
alert('Please select a valid month.');
return;
}
// Validate that a year has been selected
const year = parseInt(document.getElementById('year').value);
if (isNaN(year) || year < 2023 || year > 2100) {
alert('Please select a valid year.');
return;
}
// Validate that at least one day of the week has been selected
const selectedDays = Array.from(
document.querySelectorAll('input[name="days"]:checked')
).map(input => parseInt(input.value));
if (selectedDays.length === 0) {
alert('Please select at least one day of the week.');
return;
}
// Collect names and their unavailable days
const names = [];
for (let item of nameListItems) {
const name = item.firstChild.textContent;
const unavailableDaysInput = item.querySelector('input').value;
const unavailableDays = parseUnavailableDays(unavailableDaysInput);
names.push({ name, unavailableDays });
}
const resultTableBody = document
.getElementById('resultTable')
.querySelector('tbody');
resultTableBody.innerHTML = ''; // Clear previous results
// Set the caption with the selected month and year
const monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
const caption = document
.getElementById('resultTable')
.querySelector('caption');
caption.textContent = `Rotation of ${monthNames[month - 1]} ${year}`;
const daysInMonth = new Date(year, month, 0).getDate(); // Get number of days in the month
const weekdays = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
let nameIndex = 0;
for (let day = 1; day <= daysInMonth; day++) {
const date = new Date(year, month - 1, day);
const dayOfWeek = date.getDay();
// Skip days not selected by the user
if (!selectedDays.includes(dayOfWeek)) {
continue;
}
// Find the next available name
while (names[nameIndex].unavailableDays.includes(day)) {
nameIndex = (nameIndex + 1) % names.length;
}
const row = document.createElement('tr');
const dayOfWeekCell = document.createElement('td');
const dayOfMonthCell = document.createElement('td');
const nameCell = document.createElement('td');
dayOfWeekCell.textContent = weekdays[dayOfWeek];
dayOfMonthCell.textContent = day;
nameCell.textContent = names[nameIndex].name;
row.appendChild(dayOfWeekCell);
row.appendChild(dayOfMonthCell);
row.appendChild(nameCell);
resultTableBody.appendChild(row);
// Use uma IIFE para capturar o valor correto de nameIndex
(function (capturedNameIndex) {
row.addEventListener('click', function () {
highlightRowsWithName(names[capturedNameIndex].name);
});
})(nameIndex);
nameIndex = (nameIndex + 1) % names.length; // Cycle through names
}
});
document
.getElementById('resetTableButton')
.addEventListener('click', function () {
const resultTableBody = document
.getElementById('resultTable')
.querySelector('tbody');
resultTableBody.innerHTML = ''; // Clear the table body
const caption = document
.getElementById('resultTable')
.querySelector('caption');
caption.textContent = ''; // Clear the caption
});
function parseUnavailableDays(input) {
const unavailableDays = [];
const parts = input.split(',');
parts.forEach(part => {
part = part.trim();
if (part.startsWith('[') && part.endsWith(']')) {
const range = part.slice(1, -1).split('-').map(Number);
if (range.length === 2 && !isNaN(range[0]) && !isNaN(range[1])) {
for (let i = range[0]; i <= range[1]; i++) {
unavailableDays.push(i);
}
}
} else {
const day = parseInt(part);
if (!isNaN(day)) {
unavailableDays.push(day);
}
}
});
return unavailableDays;
}
function highlightRowsWithName(name) {
const rows = document.querySelectorAll('#resultTable tbody tr');
rows.forEach(row => {
const nameCell = row.cells[2];
if (nameCell && nameCell.textContent === name) {
row.classList.toggle('selected-row');
}
});
}