|
| 1 | +/* global prplInteractiveTaskFormListener, prplDocumentReady, progressPlanner */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Set the permalink structure. |
| 5 | + * |
| 6 | + * Dependencies: progress-planner/recommendations/interactive-task, progress-planner/document-ready |
| 7 | + */ |
| 8 | + |
| 9 | +prplInteractiveTaskFormListener.customSubmit( { |
| 10 | + taskId: 'core-permalink-structure', |
| 11 | + popoverId: 'prpl-popover-core-permalink-structure', |
| 12 | + callback: () => { |
| 13 | + const customPermalinkStructure = document.querySelector( |
| 14 | + '#prpl-popover-core-permalink-structure input[name="prpl_custom_permalink_structure"]' |
| 15 | + ); |
| 16 | + |
| 17 | + fetch( progressPlanner.ajaxUrl, { |
| 18 | + method: 'POST', |
| 19 | + headers: { |
| 20 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 21 | + }, |
| 22 | + body: new URLSearchParams( { |
| 23 | + action: 'prpl_interactive_task_submit_core-permalink-structure', |
| 24 | + nonce: progressPlanner.nonce, |
| 25 | + value: customPermalinkStructure.value, |
| 26 | + } ), |
| 27 | + } ); |
| 28 | + }, |
| 29 | +} ); |
| 30 | + |
| 31 | +prplDocumentReady( () => { |
| 32 | + // Handle custom date format input, this value is what is actually submitted to the server. |
| 33 | + const customPermalinkStructureInput = document.querySelector( |
| 34 | + '#prpl-popover-core-permalink-structure input[name="prpl_custom_permalink_structure"]' |
| 35 | + ); |
| 36 | + |
| 37 | + // If there is no custom permalink structure input, return. |
| 38 | + if ( ! customPermalinkStructureInput ) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Handle date format radio button clicks. |
| 43 | + document |
| 44 | + .querySelectorAll( |
| 45 | + '#prpl-popover-core-permalink-structure input[name="prpl_permalink_structure"]' |
| 46 | + ) |
| 47 | + .forEach( function ( input ) { |
| 48 | + input.addEventListener( 'click', function () { |
| 49 | + // Dont update the custom permalink structure input if the custom radio button is checked. |
| 50 | + if ( 'prpl_permalink_structure_custom_radio' !== this.id ) { |
| 51 | + customPermalinkStructureInput.value = this.value; |
| 52 | + } |
| 53 | + } ); |
| 54 | + } ); |
| 55 | + |
| 56 | + // If users clicks on the custom permalink structure input, check the custom radio button. |
| 57 | + customPermalinkStructureInput.addEventListener( 'click', function () { |
| 58 | + document.getElementById( |
| 59 | + 'prpl_permalink_structure_custom_radio' |
| 60 | + ).checked = true; |
| 61 | + } ); |
| 62 | +} ); |
0 commit comments