|
1 | | -document.addEventListener('DOMContentLoaded', function() { |
2 | | - const sanitizationMethodSelect = document.querySelector("#data_sanitization_method"); |
3 | | - if (!sanitizationMethodSelect) { |
4 | | - return; |
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
| 2 | + function listenForSanitizationMethodChange() { |
| 3 | + const sanitizationMethodSelect = document.querySelector( |
| 4 | + "#data_sanitization_method" |
| 5 | + ); |
| 6 | + |
| 7 | + if (!sanitizationMethodSelect) { |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + function toggleCustomFields() { |
| 12 | + const isCustom = sanitizationMethodSelect.value === "custom"; |
| 13 | + const customElements = document.querySelectorAll( |
| 14 | + ".wpgraphql-logging-custom" |
| 15 | + ); |
| 16 | + |
| 17 | + customElements.forEach((el) => { |
| 18 | + if (isCustom) { |
| 19 | + el.classList.add("block"); |
| 20 | + } else { |
| 21 | + el.classList.remove("block"); |
| 22 | + } |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + toggleCustomFields(); |
| 27 | + sanitizationMethodSelect.addEventListener("change", toggleCustomFields); |
5 | 28 | } |
6 | 29 |
|
7 | | - function toggleCustomFields() { |
8 | | - const isCustom = sanitizationMethodSelect.value === 'custom'; |
9 | | - const customElements = document.querySelectorAll('.wpgraphql-logging-custom'); |
| 30 | + function listenForUnsavedChanges() { |
| 31 | + const formElement = document.querySelector("form"); |
| 32 | + |
| 33 | + function getFormState(form) { |
| 34 | + const data = new FormData(form); |
| 35 | + return JSON.stringify(Array.from(data.entries())); |
| 36 | + } |
10 | 37 |
|
11 | | - customElements.forEach((el) => { |
12 | | - if (isCustom) { |
13 | | - el.classList.add('block'); |
14 | | - } else { |
15 | | - el.classList.remove('block'); |
| 38 | + // Save the initial state of the form for later comparison |
| 39 | + const initialState = getFormState(formElement); |
| 40 | + |
| 41 | + // Warn the user if they try to leave with unsaved changes |
| 42 | + function beforeUnload(e) { |
| 43 | + const formState = getFormState(formElement); |
| 44 | + |
| 45 | + if (formState !== initialState) { |
| 46 | + e.preventDefault(); |
| 47 | + e.returnValue = true; |
16 | 48 | } |
| 49 | + } |
| 50 | + |
| 51 | + window.addEventListener("beforeunload", beforeUnload); |
| 52 | + |
| 53 | + // Remove the warning on submit so it doesn't appear when saving |
| 54 | + formElement.addEventListener("submit", function () { |
| 55 | + window.removeEventListener("beforeunload", beforeUnload); |
17 | 56 | }); |
18 | 57 | } |
19 | 58 |
|
20 | | - toggleCustomFields(); |
21 | | - sanitizationMethodSelect.addEventListener('change', toggleCustomFields); |
| 59 | + function listenForLogPointsSelection() { |
| 60 | + const logPointsInput = document.querySelector("#event_log_selection"); |
| 61 | + const enableCheckbox = document.querySelector( |
| 62 | + "input[name='wpgraphql_logging_settings[basic_configuration][enabled]']" |
| 63 | + ); |
| 64 | + |
| 65 | + function checkLogPointsSelection() { |
| 66 | + const anyLogPointsSelected = logPointsInput.selectedOptions.length > 0; |
| 67 | + |
| 68 | + const existingDescription = |
| 69 | + logPointsInput.parentElement.querySelector(".description"); |
| 70 | + if (existingDescription) { |
| 71 | + existingDescription.remove(); |
| 72 | + } |
| 73 | + |
| 74 | + // If the logging is enabled and no log points are selected, show a description |
| 75 | + if (enableCheckbox?.checked && !anyLogPointsSelected) { |
| 76 | + const description = document.createElement("p"); |
| 77 | + |
| 78 | + if (!logPointsInput.parentElement.querySelector(".description")) { |
| 79 | + description.className = "description"; |
| 80 | + description.textContent = |
| 81 | + "If you don't select any log points, no data will be logged."; |
| 82 | + description.style.marginLeft = "25px"; |
| 83 | + logPointsInput.parentElement.appendChild(description); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + logPointsInput.addEventListener("change", checkLogPointsSelection); |
| 89 | + enableCheckbox.addEventListener("change", checkLogPointsSelection); |
| 90 | + checkLogPointsSelection(); |
| 91 | + } |
| 92 | + |
| 93 | + listenForSanitizationMethodChange(); |
| 94 | + listenForUnsavedChanges(); |
| 95 | + listenForLogPointsSelection(); |
22 | 96 | }); |
0 commit comments