dr
This commit is contained in:
parent
8f53647f17
commit
989597ebeb
|
|
@ -130,38 +130,57 @@
|
|||
|
||||
<script>
|
||||
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 => {
|
||||
collapse.addEventListener('show.bs.collapse', function () {
|
||||
const fnName = this.dataset.openFn;
|
||||
if (fnName && typeof window[fnName] === 'function') {
|
||||
window[fnName]();
|
||||
}
|
||||
const headerBtn = document.querySelector('[data-bs-target="#' + this.id + '"]');
|
||||
const icon = headerBtn?.querySelector('.accordion-icon');
|
||||
// Synchroniser les icônes une fois l'animation terminée
|
||||
document.querySelectorAll('.accordion-collapse').forEach(collapse => {
|
||||
collapse.addEventListener('shown.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-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 () {
|
||||
const fnName = this.dataset.closeFn;
|
||||
if (fnName && typeof window[fnName] === 'function') {
|
||||
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');
|
||||
try { if (fnName && typeof window[fnName] === 'function') window[fnName](); } catch(e) { /* no-op */ }
|
||||
});
|
||||
});
|
||||
|
||||
// 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 => {
|
||||
btn.addEventListener('click', function(e) {
|
||||
const target = document.querySelector(this.dataset.bsTarget);
|
||||
if (target.classList.contains('show')) {
|
||||
e.preventDefault(); // empêche Bootstrap de forcer l'ouverture
|
||||
bootstrap.Collapse.getInstance(target).hide();
|
||||
const targetSel = this.getAttribute('data-bs-target') || this.dataset.bsTarget;
|
||||
const target = document.querySelector(targetSel);
|
||||
if (!target) return;
|
||||
|
||||
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