|
| 1 | +/* global prplInteractiveTaskFormListener, progressPlanner, prplDocumentReady */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Set page settings (About, Contact, FAQ, etc.) |
| 5 | + * |
| 6 | + * Dependencies: progress-planner/recommendations/interactive-task |
| 7 | + */ |
| 8 | + |
| 9 | +// Initialize custom submit handlers for all set-page tasks. |
| 10 | +prplDocumentReady( function () { |
| 11 | + // Find all set-page popovers. |
| 12 | + const popovers = document.querySelectorAll( |
| 13 | + '[id^="prpl-popover-set-page-"]' |
| 14 | + ); |
| 15 | + |
| 16 | + popovers.forEach( function ( popover ) { |
| 17 | + // Extract page name from popover ID (e.g., "prpl-popover-set-page-about" -> "about") |
| 18 | + const popoverId = popover.id; |
| 19 | + const match = popoverId.match( /prpl-popover-set-page-(.+)/ ); |
| 20 | + if ( ! match ) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const pageName = match[ 1 ]; |
| 25 | + const taskId = 'set-page-' + pageName; |
| 26 | + |
| 27 | + // Skip if already initialized. |
| 28 | + if ( popover.dataset.setPageInitialized ) { |
| 29 | + return; |
| 30 | + } |
| 31 | + popover.dataset.setPageInitialized = 'true'; |
| 32 | + |
| 33 | + prplInteractiveTaskFormListener.customSubmit( { |
| 34 | + taskId, |
| 35 | + popoverId, |
| 36 | + callback: () => { |
| 37 | + return new Promise( ( resolve, reject ) => { |
| 38 | + const pageValue = document.querySelector( |
| 39 | + '#' + |
| 40 | + popoverId + |
| 41 | + ' input[name="pages[' + |
| 42 | + pageName + |
| 43 | + '][have_page]"]:checked' |
| 44 | + ); |
| 45 | + |
| 46 | + if ( ! pageValue ) { |
| 47 | + reject( { |
| 48 | + success: false, |
| 49 | + error: new Error( 'Page value not found' ), |
| 50 | + } ); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const pageId = document.querySelector( |
| 55 | + '#' + |
| 56 | + popoverId + |
| 57 | + ' select[name="pages[' + |
| 58 | + pageName + |
| 59 | + '][id]"]' |
| 60 | + ); |
| 61 | + |
| 62 | + fetch( progressPlanner.ajaxUrl, { |
| 63 | + method: 'POST', |
| 64 | + headers: { |
| 65 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 66 | + }, |
| 67 | + body: new URLSearchParams( { |
| 68 | + action: 'prpl_interactive_task_submit_set-page', |
| 69 | + nonce: progressPlanner.nonce, |
| 70 | + have_page: pageValue.value, |
| 71 | + id: pageId ? pageId.value : '', |
| 72 | + task_id: taskId, |
| 73 | + } ), |
| 74 | + } ) |
| 75 | + .then( ( response ) => response.json() ) |
| 76 | + .then( ( data ) => { |
| 77 | + if ( data.success ) { |
| 78 | + resolve( { response: data, success: true } ); |
| 79 | + } else { |
| 80 | + reject( { success: false, error: data } ); |
| 81 | + } |
| 82 | + } ) |
| 83 | + .catch( ( error ) => { |
| 84 | + reject( { success: false, error } ); |
| 85 | + } ); |
| 86 | + } ); |
| 87 | + }, |
| 88 | + } ); |
| 89 | + } ); |
| 90 | +} ); |
| 91 | + |
| 92 | +const prplTogglePageSelectorSettingVisibility = function ( page, value ) { |
| 93 | + const itemRadiosWrapperEl = document.querySelector( |
| 94 | + `.prpl-pages-item-${ page } .radios` |
| 95 | + ); |
| 96 | + |
| 97 | + if ( ! itemRadiosWrapperEl ) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const selectPageWrapper = |
| 102 | + itemRadiosWrapperEl.querySelector( '.prpl-select-page' ); |
| 103 | + |
| 104 | + if ( ! selectPageWrapper ) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // Show only create button. |
| 109 | + if ( 'no' === value || 'not-applicable' === value ) { |
| 110 | + // Hide <select> wrapper. |
| 111 | + selectPageWrapper.style.visibility = 'hidden'; |
| 112 | + } |
| 113 | + |
| 114 | + // Show only select and edit button. |
| 115 | + if ( 'yes' === value ) { |
| 116 | + // Show <select> wrapper. |
| 117 | + selectPageWrapper.style.visibility = 'visible'; |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +prplDocumentReady( function () { |
| 122 | + document |
| 123 | + .querySelectorAll( 'input[type="radio"][data-page]' ) |
| 124 | + .forEach( function ( radio ) { |
| 125 | + const page = radio.getAttribute( 'data-page' ), |
| 126 | + value = radio.value; |
| 127 | + |
| 128 | + if ( radio ) { |
| 129 | + // Show/hide the page selector setting if radio is checked. |
| 130 | + if ( radio.checked ) { |
| 131 | + prplTogglePageSelectorSettingVisibility( page, value ); |
| 132 | + } |
| 133 | + |
| 134 | + // Add listeners for all radio buttons. |
| 135 | + radio.addEventListener( 'change', function () { |
| 136 | + prplTogglePageSelectorSettingVisibility( page, value ); |
| 137 | + } ); |
| 138 | + } |
| 139 | + } ); |
| 140 | +} ); |
0 commit comments