Files
zbiorki_app/static/js/przelaczniki_zabezpieczenie.js
Mateusz Gruszczyński ee05bf74b5 sitche_form
2025-09-26 23:25:13 +02:00

75 lines
2.0 KiB
JavaScript

// static/js/przelaczniki_zabezpieczenie.js
(function () {
'use strict';
function onReady(cb) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', cb);
} else {
cb();
}
}
onReady(function () {
var boxes = Array.prototype.slice.call(
document.querySelectorAll('input.form-check-input[type="checkbox"][data-group="postepy"]')
);
var warning = document.getElementById('postepyWarning');
if (!boxes.length || !warning) {
// Nic do zrobienia, brak elementów
return;
}
function atLeastOneChecked() {
return boxes.some(function (b) { return !!b.checked; });
}
function showWarning(show) {
warning.classList.toggle('d-none', !show);
if (show) {
// dyskretny highlight
warning.classList.add('border', 'border-warning');
warning.style.transition = 'box-shadow 0.2s ease';
warning.style.boxShadow = '0 0 0.25rem rgba(255,193,7,.6)';
setTimeout(function () {
warning.style.boxShadow = '';
}, 300);
}
}
function enforceAtLeastOne(e) {
// Jeżeli po zmianie byłaby 0/3, przywróć zaznaczenie klikniętego i pokaż ostrzeżenie
if (!atLeastOneChecked()) {
e.target.checked = true;
showWarning(true);
e.target.classList.add('is-invalid');
setTimeout(function () { e.target.classList.remove('is-invalid'); }, 400);
return;
}
// Jeśli >=1, ostrzeżenie ukryj
showWarning(false);
}
// Podpinka zdarzeń
boxes.forEach(function (box) {
box.addEventListener('change', enforceAtLeastOne);
});
// Walidacja przy submit (na wszelki wypadek)
var form = boxes[0].closest('form');
if (form) {
form.addEventListener('submit', function (e) {
if (!atLeastOneChecked()) {
e.preventDefault();
showWarning(true);
boxes[0].focus();
}
});
}
// Inicjalny stan (np. po rerenderze z błędem)
showWarning(!atLeastOneChecked());
});
})();