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
78 changes: 76 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,53 @@
(function (global, doc) {
(function (global, doc, bootstrap, Translator) {
let toggleAllTimeout;
const MULTI_COLLAPSE_BTN_TAG = 'data-multi-collapse-btn-id';
bozatko marked this conversation as resolved.
Show resolved Hide resolved
const COLLAPSE_SECTION_BODY_TAG = 'data-multi-collapse-body';
const toggleAllBtns = [...doc.querySelectorAll(`[${MULTI_COLLAPSE_BTN_TAG}]`)];
const multiCollapsBodies = [...doc.querySelectorAll(`[${COLLAPSE_SECTION_BODY_TAG}]`)];

bozatko marked this conversation as resolved.
Show resolved Hide resolved
const toggleMultiCollapseButton = (btn, changeToCollapseAll) => {
bozatko marked this conversation as resolved.
Show resolved Hide resolved
const displayedText = changeToCollapseAll
? /*@Desc("Collapse all)*/ 'product_type.edit.section.attribute_collapse_all'
: /*@Desc("Expand all)*/ 'product_type.edit.section.attribute_expand_all';

btn.innerHTML = Translator.trans(displayedText, {}, 'ibexa_product_catalog');
btn.classList.toggle('ibexa-label-expand-all');
};
bozatko marked this conversation as resolved.
Show resolved Hide resolved

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 (toggleAllBtns.length > 0) {
const multiCollapseNode = collapseNode.closest(`[${COLLAPSE_SECTION_BODY_TAG}]`);
bozatko marked this conversation as resolved.
Show resolved Hide resolved

if (!!multiCollapseNode) {
const currentToggleAllButton = toggleAllBtns.find(
(button) => button.getAttribute(MULTI_COLLAPSE_BTN_TAG) === multiCollapseNode.getAttribute(COLLAPSE_SECTION_BODY_TAG),
);
bozatko marked this conversation as resolved.
Show resolved Hide resolved

collapseNode.querySelector('.ibexa-collapse__toggle-btn--status').addEventListener('click', (event) => {
event.stopPropagation();

const myToggleButtons = [...multiCollapseNode.querySelectorAll('.ibexa-collapse__toggle-btn--status')];

bozatko marked this conversation as resolved.
Show resolved Hide resolved
window.clearTimeout(toggleAllTimeout);

toggleAllTimeout = window.setTimeout(() => {
const countCollapsed = myToggleButtons.filter((button) => button.classList.contains('collapsed')).length;
const needToBeToggled = countCollapsed === myToggleButtons.length || countCollapsed === 0;
bozatko marked this conversation as resolved.
Show resolved Hide resolved

bozatko marked this conversation as resolved.
Show resolved Hide resolved
if (needToBeToggled) {
toggleMultiCollapseButton(currentToggleAllButton, countCollapsed === 0);
}
}, 200);
});
}
}

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

const handleCollapseAction = (multiCollapseNode, expandAction) => {
multiCollapseNode.querySelectorAll('.ibexa-collapse').forEach((collapseNode) => {
const isElementCollapsed = collapseNode.classList.contains('ibexa-collapse--collapsed');
const needToBeToggled = expandAction === isElementCollapsed;

bozatko marked this conversation as resolved.
Show resolved Hide resolved
if (needToBeToggled) {
const element = bootstrap.Collapse.getOrCreateInstance(collapseNode.querySelector('.ibexa-multi-collapse-single-item'));
expandAction ? element.show() : element.hide();
bozatko marked this conversation as resolved.
Show resolved Hide resolved
}
});
};
const attachAllElementsToggler = (btn) =>
btn.addEventListener('click', () => {
const collapseSelector = btn.getAttribute(MULTI_COLLAPSE_BTN_TAG);
if (!!collapseSelector) {
const multiCollapseNode = multiCollapsBodies.find(
bozatko marked this conversation as resolved.
Show resolved Hide resolved
(node) => node.getAttribute(COLLAPSE_SECTION_BODY_TAG) === collapseSelector,
);
bozatko marked this conversation as resolved.
Show resolved Hide resolved

window.clearTimeout(toggleAllTimeout);

toggleAllTimeout = window.setTimeout(() => {
const isExpandingAction = btn.classList.contains('ibexa-label-expand-all');

handleCollapseAction(multiCollapseNode, isExpandingAction);
toggleMultiCollapseButton(btn, isExpandingAction);
}, 200);
}
});

toggleAllBtns.forEach((btn) => attachAllElementsToggler(btn));
})(window, window.document, window.bootstrap, window.Translator);
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
32 changes: 32 additions & 0 deletions src/bundle/Resources/public/scss/_multi-collapse.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.ibexa-multi-collapse {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved styles from product-catalog. Based on previous review comment let me know if I still should change class name to ibexa-pc-...

&__header {
display: flex;
align-items: center;
justify-content: space-between;
}

&__content {
width: 100%;
margin: calculateRem(16px) 0;
}

&__input {
margin-top: calculateRem(16px);
}

&__group {
.ibexa-collapse {
&__header {
display: flex;
border-bottom: calculateRem(1px) solid $ibexa-color-light;
}

&__toggle-btn:not(.ibexa-collapse__toggle-btn--status) {
font-size: $ibexa-text-font-size-large;
font-weight: bold;
margin-right: auto;
padding-left: 0;
}
}
}
}
1 change: 1 addition & 0 deletions src/bundle/Resources/public/scss/ibexa.scss
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
@import 'autosave';
@import 'side-menu';
@import 'collapse';
@import 'multi-collapse';
@import 'tag-view-select';
@import 'grid-view';
@import 'grid-view-item';
Expand Down
Loading