Files
zbiorki_app/static/js/przelaczniki_zabezpieczenie.js
Mateusz Gruszczyński a673fe99f8 fix w formularzu
2025-09-26 23:19:57 +02:00

72 lines
1.9 KiB
JavaScript

// static/js/postepy_guard.js
(function () {
'use strict';
function ready(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
} else {
fn();
}
}
ready(function () {
var boxes = Array.prototype.slice.call(
document.querySelectorAll('input.form-check-input[type="checkbox"][data-group="postepy"]')
);
if (!boxes.length) return;
var warning = document.getElementById('postepyWarning');
function atLeastOneChecked() {
return boxes.some(function (b) { return b.checked; });
}
function updateWarning() {
var show = !atLeastOneChecked();
if (warning) {
warning.classList.toggle('d-none', !show);
if (show) {
// delikatny highlight
warning.classList.add('animate__animated', 'animate__headShake');
setTimeout(function () {
warning.classList.remove('animate__animated', 'animate__headShake');
}, 700);
}
}
}
// Blokuj odznaczenie ostatniego i pokaż ostrzeżenie
boxes.forEach(function (box) {
box.addEventListener('change', function () {
if (!atLeastOneChecked()) {
// przywróć zaznaczenie i pokaż ostrzeżenie
box.checked = true;
updateWarning();
// krótka wizualna informacja na samym przycisku
box.classList.add('is-invalid');
setTimeout(function(){ box.classList.remove('is-invalid'); }, 500);
return;
}
updateWarning();
});
});
// Walidacja przy submit — pokaż blok zamiast alertu
var form = boxes[0].closest('form');
if (form) {
form.addEventListener('submit', function (e) {
if (!atLeastOneChecked()) {
e.preventDefault();
updateWarning();
// focus na pierwszy przełącznik
boxes[0].focus();
}
});
}
// Inicjalny stan
updateWarning();
});
})();