const calendarTextarea = document.getElementById('calendar-textarea'); const untisTextarea = document.getElementById('untis-textarea'); const calendarcalendarCopyButton = document.getElementById('calendar-copy-button'); const untisCopyButton = document.getElementById('untis-copy-button'); const weekList = document.getElementById('week-list'); const exportButton = document.getElementById('export-button'); function loadWeeks() { fetch('/newweekavailable?week=' + weekList.options[0].innerHTML) .then(response => { if (response.ok) { return response.json(); } else { throw new Error('Error während des Abrufs'); } }).then(json => { if (json[0] == true) { weekList.innerHTML = ''; for (let i = 0; i < json[1].length; i++) { const option = document.createElement('option'); option.value = i; option.innerText = json[1][i]; weekList.appendChild(option); } } }).catch(error => { console.error(error); }); } loadWeeks(); setInterval(loadWeeks, 60000); let loadingInterval; function copyToClipboard(button, textarea) { navigator.clipboard.writeText(textarea.value).then(() => { button.innerText = 'Kopiert!'; setTimeout(() => { button.innerText = 'Kopieren'; }, 2000); }).catch(() => { button.innerText = 'Error!'; setTimeout(() => { button.innerText = 'Kopieren'; }, 2000); }); } function loading() { exportButton.disabled = true; calendarTextarea.value = 'Wird geladen'; untisTextarea.value = ''; loadingInterval = setInterval(() => { if (calendarTextarea.value != 'Wird geladen...') { calendarTextarea.value += '.'; } else { calendarTextarea.value = 'Wird geladen'; } }, 500); } function loaded() { exportButton.disabled = false; clearInterval(loadingInterval); calendarTextarea.value = ''; } function exportReport() { const week = weekList.value; loading(); fetch('/getreport?week=' + week) .then(response => { if (response.ok) { return response.text(); } else { calendarTextarea.value = 'Error während des Abrufs'; throw new Error('Error während des Abrufs'); } }).then(text => { loaded(); // parse json const json = JSON.parse(text); calendarTextarea.value = json.calendar; calendarTextarea.style.height = "auto"; calendarTextarea.style.height = calendarTextarea.scrollHeight + "px"; untisTextarea.value = json.untis; untisTextarea.style.height = "auto"; untisTextarea.style.height = untisTextarea.scrollHeight + "px"; }).catch(error => { loaded(); calendarTextarea.value = 'Error während des Abrufs'; console.error(error); }); } function autoResize(textarea) { textarea.style.height = "auto"; textarea.style.height = textarea.scrollHeight + "px"; } calendarTextarea.addEventListener('input', () => { autoResize(calendarTextarea); }); untisTextarea.addEventListener('input', () => { autoResize(untisTextarea); });