| 1 |
/* global jQuery, TomSelect, beyondwordsData */ |
| 2 |
'use strict'; |
| 3 |
( function ( $ ) { |
| 4 |
$( document ).ready( function () { |
| 5 |
const originalLanguageCode = $( |
| 6 |
'#beyondwords_project_language_code' |
| 7 |
).value; |
| 8 |
|
| 9 |
if ( $( '#beyondwords_project_language_code' ).length ) { |
| 10 |
const select = new TomSelect( |
| 11 |
'#beyondwords_project_language_code', |
| 12 |
{ |
| 13 |
maxOptions: null, |
| 14 |
sortField: { |
| 15 |
field: 'text', |
| 16 |
direction: 'asc', |
| 17 |
}, |
| 18 |
} |
| 19 |
); |
| 20 |
|
| 21 |
select.on( 'change', async function ( languageCode ) { |
| 22 |
const $voicesSelects = $( '.beyondwords_project_voice' ); |
| 23 |
const $titleVoicesSelect = $( |
| 24 |
'#beyondwords_project_title_voice_id' |
| 25 |
); |
| 26 |
const $bodyVoicesSelect = $( |
| 27 |
'#beyondwords_project_body_voice_id' |
| 28 |
); |
| 29 |
// eslint-disable-next-line max-len |
| 30 |
const endpoint = `${ beyondwordsData.root }beyondwords/v1/languages/${ languageCode }/voices`; |
| 31 |
|
| 32 |
$( '.beyondwords-settings__loader-default-language' ).show(); |
| 33 |
$( 'select.beyondwords_project_voice' ).hide(); |
| 34 |
$( 'select.beyondwords_project_voice' ) |
| 35 |
.attr( 'value', '' ) |
| 36 |
.attr( 'disabled', 'disabled' ); |
| 37 |
$( |
| 38 |
'.beyondwords-setting__title-voice .beyondwords-settings__loader' |
| 39 |
).show(); |
| 40 |
$( |
| 41 |
'.beyondwords-setting__body-voice .beyondwords-settings__loader' |
| 42 |
).show(); |
| 43 |
$( '.beyondwords_speaking_rate' ).attr( |
| 44 |
'disabled', |
| 45 |
'disabled' |
| 46 |
); |
| 47 |
|
| 48 |
jQuery |
| 49 |
.ajax( { |
| 50 |
url: endpoint, |
| 51 |
method: 'GET', |
| 52 |
beforeSend( xhr ) { |
| 53 |
xhr.setRequestHeader( |
| 54 |
'X-WP-Nonce', |
| 55 |
beyondwordsData.nonce |
| 56 |
); |
| 57 |
}, |
| 58 |
} ) |
| 59 |
.done( function ( voices ) { |
| 60 |
$voicesSelects.each( function () { |
| 61 |
$( this ) |
| 62 |
.empty() |
| 63 |
.append( |
| 64 |
voices.map( ( voice ) => { |
| 65 |
return $( '<option></option>' ) |
| 66 |
.val( voice.id ) |
| 67 |
.text( voice.name ); |
| 68 |
} ) |
| 69 |
) |
| 70 |
.attr( 'disabled', false ); |
| 71 |
} ); |
| 72 |
|
| 73 |
const defaultVoices = $( |
| 74 |
`#beyondwords_project_language_code option[value="${ languageCode }"]` |
| 75 |
).data( 'voices' ); |
| 76 |
|
| 77 |
if ( defaultVoices ) { |
| 78 |
if ( |
| 79 |
defaultVoices.title && |
| 80 |
defaultVoices.title.id |
| 81 |
) { |
| 82 |
$( $titleVoicesSelect ) |
| 83 |
.find( |
| 84 |
`option[value="${ defaultVoices.title.id }"]` |
| 85 |
) |
| 86 |
.prop( 'selected', true ); |
| 87 |
} |
| 88 |
if ( defaultVoices.body && defaultVoices.body.id ) { |
| 89 |
$( $bodyVoicesSelect ) |
| 90 |
.find( |
| 91 |
`option[value="${ defaultVoices.body.id }"]` |
| 92 |
) |
| 93 |
.prop( 'selected', true ); |
| 94 |
} |
| 95 |
if ( |
| 96 |
defaultVoices.title && |
| 97 |
defaultVoices.title.speaking_rate |
| 98 |
) { |
| 99 |
$( |
| 100 |
'#beyondwords_project_title_voice_speaking_rate' |
| 101 |
).val( defaultVoices.title.speaking_rate ); |
| 102 |
} |
| 103 |
if ( |
| 104 |
defaultVoices.body && |
| 105 |
defaultVoices.body.speaking_rate |
| 106 |
) { |
| 107 |
$( |
| 108 |
'#beyondwords_project_body_voice_speaking_rate' |
| 109 |
).val( defaultVoices.body.speaking_rate ); |
| 110 |
} |
| 111 |
} |
| 112 |
} ) |
| 113 |
.fail( function ( xhr ) { |
| 114 |
// eslint-disable-next-line no-console |
| 115 |
console.log( '🔊 Unable to load voices', xhr ); |
| 116 |
$( '#beyondwords_project_language_code' ).setValue( |
| 117 |
originalLanguageCode |
| 118 |
); |
| 119 |
} ) |
| 120 |
.always( function () { |
| 121 |
$( |
| 122 |
'.beyondwords-setting__title-voice .beyondwords-settings__loader' |
| 123 |
).hide(); |
| 124 |
$( |
| 125 |
'.beyondwords-setting__body-voice .beyondwords-settings__loader' |
| 126 |
).hide(); |
| 127 |
$( 'select.beyondwords_project_voice' ).show(); |
| 128 |
$( 'select.beyondwords_project_voice' ) |
| 129 |
.attr( 'value', '' ) |
| 130 |
.attr( 'disabled', false ); |
| 131 |
$( '.beyondwords_speaking_rate' ).attr( |
| 132 |
'disabled', |
| 133 |
false |
| 134 |
); |
| 135 |
} ); |
| 136 |
} ); |
| 137 |
} |
| 138 |
} ); |
| 139 |
} )( jQuery ); |
| 140 |
|