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 4 commits into
base: 4.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 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,63 @@
(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_SELECTOR = '.ibexa-multi-collapse';
const toggleMultiCollapseButton = () => {
collapseAll.classList.toggle('d-none');
expandAll.classList.toggle('d-none');
};
const toggleMultiCollapseIfNeeded = () => {
const allGroups = doc.querySelectorAll(MULTI_COLLAPSE_BODY_SELECTOR);

if (singleElementClicked.length === allGroups.length || singleElementClicked.length === 0) {
toggleMultiCollapseButton();
}
};
const handleCollapseAction = (expandAction) => {
singleElementClicked = [];
doc.querySelectorAll('.ibexa-collapse').forEach((collapseNode) => {
const isElementCollapsed = collapseNode.classList.contains('ibexa-collapse--collapsed');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here's some bigger problem - it should be contextualized in some way. Now you'll collapse ALL elements on page that has this class, even if they're in some totally different place and are used for something totally different.

Probably instead of doc you should use some container to limit to some specific part of site


if (expandAction === isElementCollapsed) {
bootstrap.Collapse.getOrCreateInstance(collapseNode.querySelector(MULTI_COLLAPSE_BODY_SELECTOR)).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 uniqueName = toggleButton.getAttribute('data-bs-target');

collapseNode.addEventListener('click', (event) => {
event.stopPropagation();

const toggleIndex = singleElementClicked.findIndex((elementIndex) => elementIndex === uniqueName);

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(uniqueName);
toggleMultiCollapseIfNeeded();
}, 200);
});
}

collapseNode.addEventListener('hide.bs.collapse', (event) => {
event.stopPropagation();
collapseNode.classList.add('ibexa-collapse--collapsed');
Expand All @@ -18,4 +70,19 @@
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