Files
datascape/assets/page/tasks.js
T
2026-05-23 08:44:19 +02:00

46 lines
1.5 KiB
JavaScript

(function () {
document.querySelectorAll('input.task-checkbox[data-task-index]').forEach(function (cb) {
cb.addEventListener('change', function () {
var idx = cb.dataset.taskIndex;
var checked = cb.checked;
cb.disabled = true;
fetch(window.location.pathname + '?toggle=' + idx, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'checked=' + checked
}).then(function (res) {
if (!res.ok) {
cb.checked = !checked;
alert('Failed to save task state (' + res.status + ')');
}
}).catch(function () {
cb.checked = !checked;
alert('Failed to save task state');
}).finally(function () {
cb.disabled = false;
});
});
});
var hasChecked = !!document.querySelector('input.task-checkbox:checked');
if (hasChecked) {
var btn = document.querySelector('[data-action="clean-tasks"]');
if (btn) btn.hidden = false;
}
})();
function cleanUpTasks() {
openModal({
title: 'Clean up tasks',
body: 'Remove all completed tasks from this page?',
confirm: {
label: 'CLEAN UP',
danger: true,
onConfirm: function () {
closeModal();
postReplace(window.location.pathname + '?cleantasks=1', null, window.location.pathname);
}
}
});
}