tab-status.css
3 months ago
tools.css
4 weeks ago
tools.js
3 months ago
version-control.js
3 months ago
version-control.js
112 lines
| 1 | /* eslint-disable no-console */ |
| 2 | import { endpoints } from '@advancedAds'; |
| 3 | |
| 4 | /** |
| 5 | * Get usable versions. |
| 6 | */ |
| 7 | function getUsableVersions() { |
| 8 | const nonceField = document.getElementById( 'version-control-nonce' ); |
| 9 | |
| 10 | fetch( endpoints.ajaxUrl, { |
| 11 | method: 'POST', |
| 12 | body: new URLSearchParams( { |
| 13 | action: 'advads_get_usable_versions', |
| 14 | nonce: nonceField ? nonceField.value : '', |
| 15 | } ), |
| 16 | } ) |
| 17 | .then( ( response ) => response.json() ) |
| 18 | .then( ( response ) => { |
| 19 | const versions = []; |
| 20 | const versionSelect = document.getElementById( 'plugin-version' ); |
| 21 | |
| 22 | response.data.order.forEach( ( number, index ) => { |
| 23 | const option = document.createElement( 'option' ); |
| 24 | option.value = `${ number }|${ response.data.versions[ number ] }`; |
| 25 | option.textContent = number; |
| 26 | if ( index === 0 ) { |
| 27 | option.selected = true; |
| 28 | } |
| 29 | versions.push( option ); |
| 30 | } ); |
| 31 | |
| 32 | versionSelect.innerHTML = ''; |
| 33 | versions.forEach( ( option ) => |
| 34 | versionSelect.appendChild( option ) |
| 35 | ); |
| 36 | versionSelect.disabled = false; |
| 37 | |
| 38 | const installButton = document.getElementById( 'install-version' ); |
| 39 | if ( installButton ) { |
| 40 | installButton.disabled = false; |
| 41 | } |
| 42 | } ) |
| 43 | .catch( ( error ) => { |
| 44 | console.error( error ); |
| 45 | } ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Launch the installation process |
| 50 | * |
| 51 | * @param {FormData} formData Form data instance. |
| 52 | */ |
| 53 | function installVersion( formData ) { |
| 54 | const versionSelect = document.getElementById( 'plugin-version' ); |
| 55 | const installButton = document.getElementById( 'install-version' ); |
| 56 | const spinner = installButton |
| 57 | ? installButton.parentElement.querySelector( '.spinner' ) |
| 58 | : null; |
| 59 | |
| 60 | if ( versionSelect ) { |
| 61 | versionSelect.disabled = true; |
| 62 | } |
| 63 | if ( installButton ) { |
| 64 | installButton.disabled = true; |
| 65 | } |
| 66 | if ( spinner ) { |
| 67 | spinner.style.visibility = 'visible'; |
| 68 | } |
| 69 | |
| 70 | formData.append( 'action', 'advads_install_alternate_version' ); |
| 71 | |
| 72 | fetch( endpoints.ajaxUrl, { |
| 73 | method: 'POST', |
| 74 | body: formData, |
| 75 | } ) |
| 76 | .then( ( response ) => response.json() ) |
| 77 | .then( ( response ) => { |
| 78 | if ( response.data && response.data.redirect ) { |
| 79 | document.location.href = response.data.redirect; |
| 80 | } |
| 81 | } ) |
| 82 | .catch( ( error ) => { |
| 83 | console.error( error ); |
| 84 | } ) |
| 85 | .finally( () => { |
| 86 | if ( versionSelect ) { |
| 87 | versionSelect.disabled = false; |
| 88 | } |
| 89 | if ( installButton ) { |
| 90 | installButton.disabled = false; |
| 91 | } |
| 92 | if ( spinner ) { |
| 93 | spinner.style.visibility = 'hidden'; |
| 94 | } |
| 95 | } ); |
| 96 | } |
| 97 | |
| 98 | export function versionControl() { |
| 99 | const form = document.getElementById( 'alternative-version' ); |
| 100 | if ( form ) { |
| 101 | form.addEventListener( 'submit', ( event ) => { |
| 102 | event.preventDefault(); |
| 103 | installVersion( new FormData( form ) ); |
| 104 | } ); |
| 105 | } |
| 106 | |
| 107 | const pluginVersion = document.getElementById( 'plugin-version' ); |
| 108 | if ( pluginVersion && ! pluginVersion.value ) { |
| 109 | getUsableVersions(); |
| 110 | } |
| 111 | } |
| 112 |