| 1 |
/* global beyondwordsPreselect */ |
| 2 |
|
| 3 |
// Keeps the Generate audio caption in step with the checkbox and, in term-gated |
| 4 |
// preselect mode, auto-syncs the checkbox from watched terms (mirrors block editor). |
| 5 |
( function () { |
| 6 |
'use strict'; |
| 7 |
|
| 8 |
const generateAudioCheckbox = () => |
| 9 |
document.getElementById( 'beyondwords_generate_audio' ); |
| 10 |
|
| 11 |
const captionEl = () => |
| 12 |
document.getElementById( 'beyondwords-generate-audio-label' ); |
| 13 |
|
| 14 |
const syncCaption = () => { |
| 15 |
const caption = captionEl(); |
| 16 |
const checkbox = generateAudioCheckbox(); |
| 17 |
if ( ! caption || ! checkbox ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
const label = checkbox.checked |
| 21 |
? caption.getAttribute( 'data-label-enabled' ) |
| 22 |
: caption.getAttribute( 'data-label-disabled' ); |
| 23 |
if ( label !== null ) { |
| 24 |
caption.textContent = label; |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
const data = |
| 29 |
typeof beyondwordsPreselect !== 'undefined' |
| 30 |
? beyondwordsPreselect |
| 31 |
: null; |
| 32 |
|
| 33 |
const termGating = !! ( data && data.mode === 'terms' && data.terms ); |
| 34 |
|
| 35 |
const inputNameFor = ( taxonomy ) => |
| 36 |
taxonomy === 'category' |
| 37 |
? 'post_category[]' |
| 38 |
: `tax_input[${ taxonomy }][]`; |
| 39 |
|
| 40 |
const watched = termGating |
| 41 |
? Object.keys( data.terms ).map( ( taxonomy ) => ( { |
| 42 |
inputName: inputNameFor( taxonomy ), |
| 43 |
wanted: new Set( |
| 44 |
( data.terms[ taxonomy ] || [] ).map( String ) |
| 45 |
), |
| 46 |
} ) ) |
| 47 |
: []; |
| 48 |
|
| 49 |
// Once the editor toggles Generate audio by hand, stop auto-managing it. |
| 50 |
let userToggled = false; |
| 51 |
|
| 52 |
const anyTermMatches = () => |
| 53 |
watched.some( ( { inputName, wanted } ) => { |
| 54 |
const checked = document.querySelectorAll( |
| 55 |
`input[name="${ inputName }"]:checked` |
| 56 |
); |
| 57 |
return Array.prototype.some.call( checked, ( input ) => |
| 58 |
wanted.has( String( input.value ) ) |
| 59 |
); |
| 60 |
} ); |
| 61 |
|
| 62 |
const syncFromTerms = () => { |
| 63 |
if ( userToggled ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
const checkbox = generateAudioCheckbox(); |
| 67 |
if ( checkbox ) { |
| 68 |
checkbox.checked = anyTermMatches(); |
| 69 |
syncCaption(); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
const isWatchedInput = ( target ) => |
| 74 |
!! target && |
| 75 |
target.type === 'checkbox' && |
| 76 |
watched.some( ( { inputName } ) => target.name === inputName ); |
| 77 |
|
| 78 |
// Align the caption with the server-rendered checkbox state up front. |
| 79 |
syncCaption(); |
| 80 |
|
| 81 |
// Delegated so terms added via the "+ Add New Category" UI are covered too. |
| 82 |
document.addEventListener( 'change', ( event ) => { |
| 83 |
const target = event.target; |
| 84 |
|
| 85 |
if ( target && target.id === 'beyondwords_generate_audio' ) { |
| 86 |
userToggled = true; |
| 87 |
syncCaption(); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if ( termGating && isWatchedInput( target ) ) { |
| 92 |
syncFromTerms(); |
| 93 |
} |
| 94 |
} ); |
| 95 |
} )(); |
| 96 |
|