Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-3456 Collapse all button support #1394

Open
wants to merge 12 commits into
base: 4.6
Choose a base branch
from
66 changes: 64 additions & 2 deletions src/bundle/Resources/public/js/scripts/core/collapse.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
(function (global, doc) {
(function (global, doc, bootstrap) {
let toggleAllTimeout;
let singleElementClicked = [];
bozatko marked this conversation as resolved.
Show resolved Hide resolved
const toggleAllBtn = doc.querySelector('.ibexa-multi-collapse-btn');
const expandAll = toggleAllBtn?.querySelector('.ibexa-attribute-group__toggler-expand');
const collapseAll = toggleAllBtn?.querySelector('.ibexa-attribute-group__toggler-collapse');
const MULTI_COLLAPSE_BODY_CLASS = '.ibexa-multi-collapse';

const toggleMultiCollapseButton = () => {
collapseAll.classList.toggle('d-none');
expandAll.classList.toggle('d-none');
};
const toggleMultiCollapseIfNeeded = () => {
const allGroups = doc.querySelectorAll(MULTI_COLLAPSE_BODY_CLASS);

bozatko marked this conversation as resolved.
Show resolved Hide resolved
if (singleElementClicked.length === allGroups.length || singleElementClicked.length === 0) {
bozatko marked this conversation as resolved.
Show resolved Hide resolved
toggleMultiCollapseButton();
}
};
const handleCollapseAction = (expandAction) => {
singleElementClicked = [];
doc.querySelectorAll('.ibexa-collapse').forEach((collapseNode) => {
const isElementCollapsed = collapseNode.classList.contains('ibexa-collapse--collapsed');
bozatko marked this conversation as resolved.
Show resolved Hide resolved

if (expandAction === isElementCollapsed) {
bootstrap.Collapse.getOrCreateInstance(collapseNode.querySelector(MULTI_COLLAPSE_BODY_CLASS)).toggle();
}
});
};

doc.querySelectorAll('.ibexa-collapse').forEach((collapseNode) => {
const toggleButton = collapseNode.querySelector('.ibexa-collapse__toggle-btn');
const isCollapsed = toggleButton.classList.contains('collapsed');

collapseNode.classList.toggle('ibexa-collapse--collapsed', isCollapsed);
collapseNode.dataset.collapsed = isCollapsed;

if (toggleAllBtn) {
const uniqeName = toggleButton.getAttribute('data-bs-target');

bozatko marked this conversation as resolved.
Show resolved Hide resolved
bozatko marked this conversation as resolved.
Show resolved Hide resolved
collapseNode.addEventListener('click', (event) => {
event.stopPropagation();
const toggleIndex = singleElementClicked.findIndex((elementIndex) => elementIndex === uniqeName);

bozatko marked this conversation as resolved.
Show resolved Hide resolved
window.clearTimeout(toggleAllTimeout);
toggleAllTimeout = window.setTimeout(() => {
if (toggleIndex !== -1) {
singleElementClicked.splice(toggleIndex, 1);
toggleMultiCollapseIfNeeded();
return;
}
bozatko marked this conversation as resolved.
Show resolved Hide resolved
singleElementClicked.push(uniqeName);
toggleMultiCollapseIfNeeded();
}, 200);
});
}

collapseNode.addEventListener('hide.bs.collapse', (event) => {
event.stopPropagation();
collapseNode.classList.add('ibexa-collapse--collapsed');
Expand All @@ -18,4 +67,17 @@
collapseNode.dataset.collapsed = false;
});
});
})(window, window.document);

if (toggleAllBtn) {
toggleAllBtn.addEventListener('click', (event) => {
event.stopPropagation();
window.clearTimeout(toggleAllTimeout);
toggleAllTimeout = window.setTimeout(() => {
const isExpanding = collapseAll.classList.contains('d-none');

handleCollapseAction(isExpanding);
toggleMultiCollapseButton();
}, 200);
});
}
})(window, window.document, window.bootstrap);
4 changes: 4 additions & 0 deletions src/bundle/Resources/public/scss/_anchor-navigation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
&:last-child {
border-bottom: none;
}

&--no-border {
border-bottom: none;
}
}

&__section-group {
Expand Down
Loading