Simplify audio settings UI

This commit is contained in:
toasted-nutbread 2019-12-05 22:36:59 -05:00
parent 63f3e94bb1
commit b418760a52
3 changed files with 53 additions and 32 deletions

View File

@ -21,7 +21,7 @@ class AudioSourceUI {
static instantiateTemplate(templateSelector) { static instantiateTemplate(templateSelector) {
const template = document.querySelector(templateSelector); const template = document.querySelector(templateSelector);
const content = document.importNode(template.content, true); const content = document.importNode(template.content, true);
return $(content.firstChild); return content.firstChild;
} }
} }
@ -32,13 +32,14 @@ AudioSourceUI.Container = class Container {
this.addButton = addButton; this.addButton = addButton;
this.children = []; this.children = [];
this.container.empty(); this.container.textContent = '';
for (const audioSource of toIterable(audioSources)) { for (const audioSource of toIterable(audioSources)) {
this.children.push(new AudioSourceUI.AudioSource(this, audioSource, this.children.length)); this.children.push(new AudioSourceUI.AudioSource(this, audioSource, this.children.length));
} }
this.addButton.on('click', () => this.onAddAudioSource()); this._clickListener = () => this.onAddAudioSource();
this.addButton.addEventListener('click', this._clickListener, false);
} }
cleanup() { cleanup() {
@ -46,8 +47,9 @@ AudioSourceUI.Container = class Container {
child.cleanup(); child.cleanup();
} }
this.addButton.off('click'); this.addButton.removeEventListener('click', this._clickListener, false);
this.container.empty(); this.container.textContent = '';
this._clickListener = null;
} }
save() { save() {
@ -98,20 +100,28 @@ AudioSourceUI.AudioSource = class AudioSource {
this.audioSource = audioSource; this.audioSource = audioSource;
this.index = index; this.index = index;
this.container = AudioSourceUI.instantiateTemplate('#audio-source-template').appendTo(parent.container); this.container = AudioSourceUI.instantiateTemplate('#audio-source-template');
this.select = this.container.find('.audio-source-select'); this.select = this.container.querySelector('.audio-source-select');
this.removeButton = this.container.find('.audio-source-remove'); this.removeButton = this.container.querySelector('.audio-source-remove');
this.select.val(audioSource); this.select.value = audioSource;
this.select.on('change', () => this.onSelectChanged()); this._selectChangeListener = () => this.onSelectChanged();
this.removeButton.on('click', () => this.onRemoveClicked()); this._removeClickListener = () => this.onRemoveClicked();
this.select.addEventListener('change', this._selectChangeListener, false);
this.removeButton.addEventListener('click', this._removeClickListener, false);
parent.container.appendChild(this.container);
} }
cleanup() { cleanup() {
this.select.off('change'); this.select.removeEventListener('change', this._selectChangeListener, false);
this.removeButton.off('click'); this.removeButton.removeEventListener('click', this._removeClickListener, false);
this.container.remove();
if (this.container.parentNode !== null) {
this.container.parentNode.removeChild(this.container);
}
} }
save() { save() {
@ -119,7 +129,7 @@ AudioSourceUI.AudioSource = class AudioSource {
} }
onSelectChanged() { onSelectChanged() {
this.audioSource = this.select.val(); this.audioSource = this.select.value;
this.parent.audioSources[this.index] = this.audioSource; this.parent.audioSources[this.index] = this.audioSource;
this.save(); this.save();
} }

View File

@ -22,7 +22,11 @@ let audioSourceUI = null;
async function audioSettingsInitialize() { async function audioSettingsInitialize() {
const optionsContext = getOptionsContext(); const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext); const options = await apiOptionsGet(optionsContext);
audioSourceUI = new AudioSourceUI.Container(options.audio.sources, $('.audio-source-list'), $('.audio-source-add')); audioSourceUI = new AudioSourceUI.Container(
options.audio.sources,
document.querySelector('.audio-source-list'),
document.querySelector('.audio-source-add')
);
audioSourceUI.save = () => settingsSaveOptions(); audioSourceUI.save = () => settingsSaveOptions();
textToSpeechInitialize(); textToSpeechInitialize();
@ -34,24 +38,33 @@ function textToSpeechInitialize() {
speechSynthesis.addEventListener('voiceschanged', () => updateTextToSpeechVoices(), false); speechSynthesis.addEventListener('voiceschanged', () => updateTextToSpeechVoices(), false);
updateTextToSpeechVoices(); updateTextToSpeechVoices();
$('#text-to-speech-voice-test').on('click', () => textToSpeechTest()); document.querySelector('#text-to-speech-voice-test').addEventListener('click', () => textToSpeechTest(), false);
} }
function updateTextToSpeechVoices() { function updateTextToSpeechVoices() {
const voices = Array.prototype.map.call(speechSynthesis.getVoices(), (voice, index) => ({voice, index})); const voices = Array.prototype.map.call(speechSynthesis.getVoices(), (voice, index) => ({voice, index}));
voices.sort(textToSpeechVoiceCompare); voices.sort(textToSpeechVoiceCompare);
if (voices.length > 0) {
$('#text-to-speech-voice-container').css('display', '');
}
const select = $('#text-to-speech-voice'); document.querySelector('#text-to-speech-voice-container').hidden = (voices.length === 0);
select.empty();
select.append($('<option>').val('').text('None')); const fragment = document.createDocumentFragment();
let option = document.createElement('option');
option.value = '';
option.textContent = 'None';
fragment.appendChild(option);
for (const {voice} of voices) { for (const {voice} of voices) {
select.append($('<option>').val(voice.voiceURI).text(`${voice.name} (${voice.lang})`)); option = document.createElement('option');
option.value = voice.voiceURI;
option.textContent = `${voice.name} (${voice.lang})`;
fragment.appendChild(option);
} }
select.val(select.attr('data-value')); const select = document.querySelector('#text-to-speech-voice');
select.textContent = '';
select.appendChild(fragment);
select.value = select.dataset.value;
} }
function languageTagIsJapanese(languageTag) { function languageTagIsJapanese(languageTag) {
@ -78,15 +91,13 @@ function textToSpeechVoiceCompare(a, b) {
if (bIsDefault) { return 1; } if (bIsDefault) { return 1; }
} }
if (a.index < b.index) { return -1; } return a.index - b.index;
if (a.index > b.index) { return 1; }
return 0;
} }
function textToSpeechTest() { function textToSpeechTest() {
try { try {
const text = $('#text-to-speech-voice-test').attr('data-speech-text') || ''; const text = document.querySelector('#text-to-speech-voice-test').dataset.speechText || '';
const voiceURI = $('#text-to-speech-voice').val(); const voiceURI = document.querySelector('#text-to-speech-voice').value;
const voice = audioGetTextToSpeechVoice(voiceURI); const voice = audioGetTextToSpeechVoice(voiceURI);
if (voice === null) { return; } if (voice === null) { return; }

View File

@ -299,7 +299,7 @@
<input type="number" min="0" max="100" id="audio-playback-volume" class="form-control"> <input type="number" min="0" max="100" id="audio-playback-volume" class="form-control">
</div> </div>
<div class="form-group" style="display: none;" id="text-to-speech-voice-container"> <div class="form-group" id="text-to-speech-voice-container" hidden>
<label for="text-to-speech-voice">Text-to-speech voice</label> <label for="text-to-speech-voice">Text-to-speech voice</label>
<div class="input-group"> <div class="input-group">
<select class="form-control" id="text-to-speech-voice"></select> <select class="form-control" id="text-to-speech-voice"></select>
@ -870,7 +870,6 @@
<script src="/bg/js/anki.js"></script> <script src="/bg/js/anki.js"></script>
<script src="/bg/js/api.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/audio-ui.js"></script>
<script src="/bg/js/conditions.js"></script> <script src="/bg/js/conditions.js"></script>
<script src="/bg/js/conditions-ui.js"></script> <script src="/bg/js/conditions-ui.js"></script>
<script src="/bg/js/dictionary.js"></script> <script src="/bg/js/dictionary.js"></script>
@ -885,6 +884,7 @@
<script src="/bg/js/settings/anki.js"></script> <script src="/bg/js/settings/anki.js"></script>
<script src="/bg/js/settings/anki-templates.js"></script> <script src="/bg/js/settings/anki-templates.js"></script>
<script src="/bg/js/settings/audio.js"></script> <script src="/bg/js/settings/audio.js"></script>
<script src="/bg/js/settings/audio-ui.js"></script>
<script src="/bg/js/settings/dictionaries.js"></script> <script src="/bg/js/settings/dictionaries.js"></script>
<script src="/bg/js/settings/popup-preview.js"></script> <script src="/bg/js/settings/popup-preview.js"></script>
<script src="/bg/js/settings/profiles.js"></script> <script src="/bg/js/settings/profiles.js"></script>