sitche_form
This commit is contained in:
@@ -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());
|
||||
});
|
||||
})();
|
||||
|
@@ -297,10 +297,6 @@
|
||||
</div>
|
||||
|
||||
<div class="row g-3 mt-2">
|
||||
|
||||
<div id="postepyWarning" class="alert alert-warning d-none mt-2" role="alert">
|
||||
Nie można wyłączyć wszystkich wskaźników postępu. Pozostaw przynajmniej jeden włączony.
|
||||
</div>
|
||||
<div class="col-12 col-md-4">
|
||||
<div class="form-check form-switch">
|
||||
<input
|
||||
@@ -343,7 +339,9 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="postepyWarning" class="alert alert-warning d-none mt-2" role="alert">
|
||||
Nie można wyłączyć wszystkich wskaźników postępu. Pozostaw przynajmniej jeden włączony.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user