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-9248: The "Discard" button in the "Create content" does not discard anything #1438

Open
wants to merge 1 commit into
base: 4.6
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions src/bundle/Resources/public/js/scripts/sidebar/extra.actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getInstance } from '@ibexa-admin-ui/src/bundle/Resources/public/js/scripts/helpers/object.instances';

(function (global, doc, ibexa) {
const CLASS_HIDDEN = 'ibexa-extra-actions--hidden';
const CLASS_EXPANDED = 'ibexa-context-menu--expanded';
Expand All @@ -8,6 +10,46 @@
const btns = [...doc.querySelectorAll('.ibexa-btn--extra-actions')];
const menu = doc.querySelector('.ibexa-context-menu');
const backdrop = new ibexa.core.Backdrop();
const formsInitialData = new Map();
const saveInitialFormData = (extraActionsContainer) => {
const extraActionsInputs = extraActionsContainer.querySelectorAll('input, select');

extraActionsInputs.forEach((node) => {
const value = node.type === 'radio' || node.type === 'checkbox' ? node.checked : node.value;

formsInitialData.set(node, value);
});
};
const restoreInitialFormData = (extraActionsContainer) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately not, I was looking into it, but for some strange reason, form.reset() doesn't trigger any events on inputs (mainly change ones) :/

const extraActionsInputs = extraActionsContainer.querySelectorAll('input, select');

extraActionsInputs.forEach((node) => {
const value = formsInitialData.get(node);
let prevValue = node.value;

if (node.type === 'radio' || node.type === 'checkbox') {
prevValue = node.checked;

node.checked = value;
} else if (node.tagName === 'SELECT') {
const dropdownContainer = node.closest('.ibexa-dropdown');

if (dropdownContainer) {
const dropdownInstance = getInstance(dropdownContainer);

dropdownInstance.selectOption(value);
} else {
node.value = value;
}
} else {
node.value = value;
}

if (value !== prevValue) {
node.dispatchEvent(new CustomEvent('change'));
}
});
};
const haveHiddenPart = (element) => element.classList.contains(CLASS_HIDDEN) && !element.classList.contains(CLASS_PREVENT_SHOW);
const removeBackdrop = () => {
backdrop.hide();
Expand All @@ -23,6 +65,7 @@
doc.body.dispatchEvent(new CustomEvent('ibexa-extra-actions:after-close'));

removeBackdrop();
restoreInitialFormData(actions);
};
const toggleExtraActionsWidget = (widgetData) => {
const actions = doc.querySelector(`.ibexa-extra-actions[data-actions="${widgetData.actions}"]`);
Expand Down Expand Up @@ -50,9 +93,11 @@
backdrop.show();
doc.body.addEventListener('click', detectClickOutside, false);
doc.body.classList.add('ibexa-scroll-disabled');
saveInitialFormData(actions);
} else {
doc.body.removeEventListener('click', detectClickOutside);
removeBackdrop();
restoreInitialFormData(actions);
}

if (focusElement) {
Expand Down
Loading