| 1 |
document.addEventListener( 'DOMContentLoaded', (): void => { |
| 2 |
setActiveTab(); |
| 3 |
window.addEventListener( 'hashchange', setActiveTab ); |
| 4 |
document.querySelector( '.media-single-image button.browse' )?.addEventListener( 'click', selectImage ); |
| 5 |
} ); |
| 6 |
|
| 7 |
function setActiveTab(): void { |
| 8 |
const activeTab = location.hash !== '' ? location.hash.substring( 1 ) : 'basic-section'; |
| 9 |
|
| 10 |
document.querySelectorAll( '.nav-tab' )?.forEach( ( t: Element ): void => { |
| 11 |
if ( t.classList.contains( activeTab + '-tab' ) ) { |
| 12 |
t.classList.add( 'nav-tab-active' ); |
| 13 |
} else { |
| 14 |
t.classList.remove( 'nav-tab-active' ); |
| 15 |
} |
| 16 |
} ); |
| 17 |
|
| 18 |
document.querySelectorAll( '.tab-content' )?.forEach( ( t: Element ): void => { |
| 19 |
if ( t.classList.contains( activeTab ) ) { |
| 20 |
t.setAttribute( 'style', 'display: initial' ); |
| 21 |
} else { |
| 22 |
t.setAttribute( 'style', 'display: none' ); |
| 23 |
} |
| 24 |
} ); |
| 25 |
|
| 26 |
const form = document.querySelector( 'form' ); |
| 27 |
if ( form ) { |
| 28 |
form.removeAttribute( 'hidden' ); |
| 29 |
form.setAttribute( 'action', `options.php#${ activeTab }` ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
function selectImage( event: Event ) { |
| 34 |
const optionName = ( event.target as HTMLButtonElement ).dataset.option; |
| 35 |
|
| 36 |
const imageFrame = window.wp.media( { |
| 37 |
multiple: false, |
| 38 |
library: { |
| 39 |
type: 'image', |
| 40 |
}, |
| 41 |
} ); |
| 42 |
|
| 43 |
imageFrame.on( 'select', function() { |
| 44 |
const url = imageFrame.state().get( 'selection' ).first().toJSON().url; |
| 45 |
const inputSelector: string = '#media-single-image-' + optionName + ' input.file-path'; |
| 46 |
|
| 47 |
const inputElement: HTMLInputElement | null = document.querySelector( inputSelector ); |
| 48 |
if ( inputElement ) { |
| 49 |
inputElement.value = url; |
| 50 |
} |
| 51 |
} ); |
| 52 |
|
| 53 |
imageFrame.open(); |
| 54 |
} |
| 55 |
|