dr
This commit is contained in:
parent
8f53647f17
commit
989597ebeb
|
|
@ -130,38 +130,57 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
const collapses = document.querySelectorAll('.accordion-collapse');
|
// Utilitaire : récupérer ou créer l'instance Collapse sans toggle auto
|
||||||
|
function getCollapseInstance(el) {
|
||||||
|
return bootstrap.Collapse.getInstance(el) || new bootstrap.Collapse(el, { toggle: false });
|
||||||
|
}
|
||||||
|
|
||||||
collapses.forEach(collapse => {
|
// Synchroniser les icônes une fois l'animation terminée
|
||||||
collapse.addEventListener('show.bs.collapse', function () {
|
document.querySelectorAll('.accordion-collapse').forEach(collapse => {
|
||||||
const fnName = this.dataset.openFn;
|
collapse.addEventListener('shown.bs.collapse', function () {
|
||||||
if (fnName && typeof window[fnName] === 'function') {
|
const btn = document.querySelector('[data-bs-target="#' + this.id + '"]');
|
||||||
window[fnName]();
|
const icon = btn?.querySelector('.accordion-icon');
|
||||||
}
|
|
||||||
const headerBtn = document.querySelector('[data-bs-target="#' + this.id + '"]');
|
|
||||||
const icon = headerBtn?.querySelector('.accordion-icon');
|
|
||||||
if (icon) icon.classList.replace('bi-chevron-down', 'bi-chevron-up');
|
if (icon) icon.classList.replace('bi-chevron-down', 'bi-chevron-up');
|
||||||
});
|
});
|
||||||
|
collapse.addEventListener('hidden.bs.collapse', function () {
|
||||||
|
const btn = document.querySelector('[data-bs-target="#' + this.id + '"]');
|
||||||
|
const icon = btn?.querySelector('.accordion-icon');
|
||||||
|
if (icon) icon.classList.replace('bi-chevron-up', 'bi-chevron-down');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Tes callbacks open/close sécurisés
|
||||||
|
collapse.addEventListener('show.bs.collapse', function () {
|
||||||
|
const fnName = this.dataset.openFn;
|
||||||
|
try { if (fnName && typeof window[fnName] === 'function') window[fnName](); } catch(e) { /* no-op */ }
|
||||||
|
});
|
||||||
collapse.addEventListener('hide.bs.collapse', function () {
|
collapse.addEventListener('hide.bs.collapse', function () {
|
||||||
const fnName = this.dataset.closeFn;
|
const fnName = this.dataset.closeFn;
|
||||||
if (fnName && typeof window[fnName] === 'function') {
|
try { if (fnName && typeof window[fnName] === 'function') window[fnName](); } catch(e) { /* no-op */ }
|
||||||
window[fnName]();
|
|
||||||
}
|
|
||||||
const headerBtn = document.querySelector('[data-bs-target="#' + this.id + '"]');
|
|
||||||
const icon = headerBtn?.querySelector('.accordion-icon');
|
|
||||||
if (icon) icon.classList.replace('bi-chevron-up', 'bi-chevron-down');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Patch pour permettre le toggle même avec data-bs-parent
|
// Toggle explicite au clic, même avec data-bs-parent
|
||||||
document.querySelectorAll('.accordion-button').forEach(btn => {
|
document.querySelectorAll('.accordion-button').forEach(btn => {
|
||||||
btn.addEventListener('click', function(e) {
|
btn.addEventListener('click', function(e) {
|
||||||
const target = document.querySelector(this.dataset.bsTarget);
|
const targetSel = this.getAttribute('data-bs-target') || this.dataset.bsTarget;
|
||||||
if (target.classList.contains('show')) {
|
const target = document.querySelector(targetSel);
|
||||||
e.preventDefault(); // empêche Bootstrap de forcer l'ouverture
|
if (!target) return;
|
||||||
bootstrap.Collapse.getInstance(target).hide();
|
|
||||||
|
e.preventDefault(); // on prend la main sur le toggle
|
||||||
|
const isOpen = target.classList.contains('show');
|
||||||
|
|
||||||
|
// Exclusivité : fermer les autres si data-bs-parent est défini
|
||||||
|
const parentSel = target.getAttribute('data-bs-parent');
|
||||||
|
if (parentSel) {
|
||||||
|
document.querySelectorAll(parentSel + ' .accordion-collapse.show').forEach(c => {
|
||||||
|
if (c !== target) getCollapseInstance(c).hide();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Toggle explicite
|
||||||
|
const instance = getCollapseInstance(target);
|
||||||
|
if (isOpen) instance.hide();
|
||||||
|
else instance.show();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user