sitche_form

This commit is contained in:
Mateusz Gruszczyński
2025-09-26 23:25:13 +02:00
parent 052439b340
commit ee05bf74b5
2 changed files with 44 additions and 43 deletions

View File

@@ -1,71 +1,74 @@
// static/js/postepy_guard.js
// static/js/przelaczniki_zabezpieczenie.js
(function () {
'use strict';
function ready(fn) {
function onReady(cb) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
document.addEventListener('DOMContentLoaded', cb);
} else {
fn();
cb();
}
}
ready(function () {
onReady(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; });
if (!boxes.length || !warning) {
// Nic do zrobienia, brak elementów
return;
}
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);
}
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);
}
}
// Blokuj odznaczenie ostatniego i pokaż ostrzeżenie
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', 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();
});
box.addEventListener('change', enforceAtLeastOne);
});
// Walidacja przy submit — pokaż blok zamiast alertu
// Walidacja przy submit (na wszelki wypadek)
var form = boxes[0].closest('form');
if (form) {
form.addEventListener('submit', function (e) {
if (!atLeastOneChecked()) {
e.preventDefault();
updateWarning();
// focus na pierwszy przełącznik
showWarning(true);
boxes[0].focus();
}
});
}
// Inicjalny stan
updateWarning();
// Inicjalny stan (np. po rerenderze z błędem)
showWarning(!atLeastOneChecked());
});
})();