|
| 1 | +import { getInstance } from '@ibexa-admin-ui/src/bundle/Resources/public/js/scripts/helpers/object.instances'; |
| 2 | + |
1 | 3 | (function (global, doc, ibexa) { |
2 | 4 | const CLASS_HIDDEN = 'ibexa-extra-actions--hidden'; |
3 | 5 | const CLASS_EXPANDED = 'ibexa-context-menu--expanded'; |
|
8 | 10 | const btns = [...doc.querySelectorAll('.ibexa-btn--extra-actions')]; |
9 | 11 | const menu = doc.querySelector('.ibexa-context-menu'); |
10 | 12 | const backdrop = new ibexa.core.Backdrop(); |
| 13 | + const formsInitialData = new Map(); |
| 14 | + const saveInitialFormData = (extraActionsContainer) => { |
| 15 | + const extraActionsInputs = extraActionsContainer.querySelectorAll('input, select'); |
| 16 | + |
| 17 | + extraActionsInputs.forEach((node) => { |
| 18 | + const value = node.type === 'radio' || node.type === 'checkbox' ? node.checked : node.value; |
| 19 | + |
| 20 | + formsInitialData.set(node, value); |
| 21 | + }); |
| 22 | + }; |
| 23 | + const restoreInitialFormData = (extraActionsContainer) => { |
| 24 | + if (formsInitialData.size === 0) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const extraActionsInputs = extraActionsContainer.querySelectorAll('input, select'); |
| 29 | + |
| 30 | + extraActionsInputs.forEach((node) => { |
| 31 | + const value = formsInitialData.get(node); |
| 32 | + let prevValue = node.value; |
| 33 | + |
| 34 | + if (node.type === 'radio' || node.type === 'checkbox') { |
| 35 | + prevValue = node.checked; |
| 36 | + |
| 37 | + node.checked = value; |
| 38 | + } else if (node.tagName === 'SELECT') { |
| 39 | + const dropdownContainer = node.closest('.ibexa-dropdown'); |
| 40 | + |
| 41 | + if (dropdownContainer) { |
| 42 | + const dropdownInstance = getInstance(dropdownContainer); |
| 43 | + |
| 44 | + dropdownInstance.selectOption(value); |
| 45 | + } else { |
| 46 | + node.value = value; |
| 47 | + } |
| 48 | + } else { |
| 49 | + node.value = value; |
| 50 | + } |
| 51 | + |
| 52 | + if (value !== prevValue) { |
| 53 | + node.dispatchEvent(new CustomEvent('change')); |
| 54 | + } |
| 55 | + }); |
| 56 | + }; |
11 | 57 | const haveHiddenPart = (element) => element.classList.contains(CLASS_HIDDEN) && !element.classList.contains(CLASS_PREVENT_SHOW); |
12 | 58 | const removeBackdrop = () => { |
13 | 59 | backdrop.hide(); |
|
23 | 69 | doc.body.dispatchEvent(new CustomEvent('ibexa-extra-actions:after-close')); |
24 | 70 |
|
25 | 71 | removeBackdrop(); |
| 72 | + restoreInitialFormData(actions); |
26 | 73 | }; |
27 | 74 | const toggleExtraActionsWidget = (widgetData) => { |
28 | 75 | const actions = doc.querySelector(`.ibexa-extra-actions[data-actions="${widgetData.actions}"]`); |
|
50 | 97 | backdrop.show(); |
51 | 98 | doc.body.addEventListener('click', detectClickOutside, false); |
52 | 99 | doc.body.classList.add('ibexa-scroll-disabled'); |
| 100 | + saveInitialFormData(actions); |
53 | 101 | } else { |
54 | 102 | doc.body.removeEventListener('click', detectClickOutside); |
55 | 103 | removeBackdrop(); |
| 104 | + restoreInitialFormData(actions); |
56 | 105 | } |
57 | 106 |
|
58 | 107 | if (focusElement) { |
|
0 commit comments