refactor options page
This commit is contained in:
parent
39f1f30dc9
commit
a22facec98
@ -21,8 +21,8 @@
|
||||
* General
|
||||
*/
|
||||
|
||||
function formRead() {
|
||||
return optionsLoad().then(optionsOld => {
|
||||
async function formRead() {
|
||||
const optionsOld = await optionsLoad();
|
||||
const optionsNew = $.extend(true, {}, optionsOld);
|
||||
|
||||
optionsNew.general.showGuide = $('#show-usage-guide').prop('checked');
|
||||
@ -67,7 +67,6 @@ function formRead() {
|
||||
});
|
||||
|
||||
return {optionsNew, optionsOld};
|
||||
});
|
||||
}
|
||||
|
||||
function updateVisibility(options) {
|
||||
@ -95,13 +94,18 @@ function updateVisibility(options) {
|
||||
}
|
||||
}
|
||||
|
||||
function onOptionsChanged(e) {
|
||||
async function onOptionsChanged(e) { (async () => {
|
||||
if (!e.originalEvent && !e.isTrigger) {
|
||||
return;
|
||||
}
|
||||
|
||||
formRead().then(({optionsNew, optionsOld}) => {
|
||||
return optionsSave(optionsNew).then(() => {
|
||||
try {
|
||||
ankiErrorShow();
|
||||
ankiSpinnerShow(true);
|
||||
|
||||
const {optionsNew, optionsOld} = await formRead();
|
||||
await optionsSave(optionsNew);
|
||||
|
||||
updateVisibility(optionsNew);
|
||||
|
||||
const ankiUpdated =
|
||||
@ -109,18 +113,20 @@ function onOptionsChanged(e) {
|
||||
optionsNew.anki.server !== optionsOld.anki.server;
|
||||
|
||||
if (ankiUpdated) {
|
||||
ankiErrorShow(null);
|
||||
ankiSpinnerShow(true);
|
||||
return ankiDeckAndModelPopulate(optionsNew);
|
||||
await ankiDeckAndModelPopulate(optionsNew);
|
||||
}
|
||||
});
|
||||
}).catch(ankiErrorShow).then(() => ankiSpinnerShow(false));
|
||||
}
|
||||
} catch (e) {
|
||||
ankiErrorShow(e);
|
||||
} finally {
|
||||
ankiSpinnerShow(false);
|
||||
}
|
||||
})();}
|
||||
|
||||
$(document).ready(() => {
|
||||
function onReady() {(async () => {
|
||||
handlebarsRegister();
|
||||
|
||||
optionsLoad().then(options => {
|
||||
const options = await optionsLoad();
|
||||
|
||||
$('#show-usage-guide').prop('checked', options.general.showGuide);
|
||||
$('#audio-playback-source').val(options.general.audioSource);
|
||||
$('#audio-playback-volume').val(options.general.audioVolume);
|
||||
@ -150,11 +156,22 @@ $(document).ready(() => {
|
||||
$('input, select').not('.anki-model').change(onOptionsChanged);
|
||||
$('.anki-model').change(onAnkiModelChanged);
|
||||
|
||||
dictionaryGroupsPopulate(options);
|
||||
ankiDeckAndModelPopulate(options);
|
||||
try {
|
||||
await dictionaryGroupsPopulate(options);
|
||||
} catch (e) {
|
||||
dictionaryErrorShow(e);
|
||||
}
|
||||
|
||||
try {
|
||||
await ankiDeckAndModelPopulate(options);
|
||||
} catch (e) {
|
||||
ankiErrorShow(e);
|
||||
}
|
||||
|
||||
updateVisibility(options);
|
||||
});
|
||||
});
|
||||
})();}
|
||||
|
||||
$(document).ready(onReady);
|
||||
|
||||
|
||||
/*
|
||||
@ -196,24 +213,21 @@ function dictionaryGroupsSort() {
|
||||
dictGroups.append(dictGroupChildren);
|
||||
}
|
||||
|
||||
function dictionaryGroupsPopulate(options) {
|
||||
dictionaryErrorShow(null);
|
||||
dictionarySpinnerShow(true);
|
||||
|
||||
async function dictionaryGroupsPopulate(options) {
|
||||
const dictGroups = $('#dict-groups').empty();
|
||||
const dictWarning = $('#dict-warning').hide();
|
||||
|
||||
return instDb().getDictionaries().then(rows => {
|
||||
if (rows.length === 0) {
|
||||
const dictRows = await instDb().getDictionaries();
|
||||
if (dictRows.length === 0) {
|
||||
dictWarning.show();
|
||||
}
|
||||
|
||||
for (const row of dictRowsSort(rows, options)) {
|
||||
const dictOptions = options.dictionaries[row.title] || {enabled: false, priority: 0};
|
||||
for (const dictRow of dictRowsSort(dictRows, options)) {
|
||||
const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0};
|
||||
const dictHtml = handlebarsRender('dictionary.html', {
|
||||
title: row.title,
|
||||
version: row.version,
|
||||
revision: row.revision,
|
||||
title: dictRow.title,
|
||||
version: dictRow.version,
|
||||
revision: dictRow.revision,
|
||||
priority: dictOptions.priority,
|
||||
enabled: dictOptions.enabled
|
||||
});
|
||||
@ -227,53 +241,63 @@ function dictionaryGroupsPopulate(options) {
|
||||
dictionaryGroupsSort();
|
||||
onOptionsChanged(e);
|
||||
});
|
||||
}).catch(dictionaryErrorShow).then(() => dictionarySpinnerShow(false));
|
||||
}
|
||||
|
||||
function onDictionaryPurge(e) {
|
||||
async function onDictionaryPurge(e) { (async () => {
|
||||
e.preventDefault();
|
||||
|
||||
dictionaryErrorShow(null);
|
||||
dictionarySpinnerShow(true);
|
||||
|
||||
const dictControls = $('#dict-importer, #dict-groups').hide();
|
||||
const dictProgress = $('#dict-purge-progress').show();
|
||||
|
||||
instDb().purge().catch(dictionaryErrorShow).then(() => {
|
||||
dictionarySpinnerShow(false);
|
||||
dictControls.show();
|
||||
dictProgress.hide();
|
||||
return optionsLoad();
|
||||
}).then(options => {
|
||||
options.dictionaries = {};
|
||||
optionsSave(options).then(() => dictionaryGroupsPopulate(options));
|
||||
});
|
||||
}
|
||||
|
||||
function onDictionaryImport(e) {
|
||||
dictionaryErrorShow(null);
|
||||
try {
|
||||
dictionaryErrorShow();
|
||||
dictionarySpinnerShow(true);
|
||||
|
||||
await instDb().purge();
|
||||
const options = await optionsLoad();
|
||||
options.dictionaries = {};
|
||||
await optionsSave(options);
|
||||
|
||||
await dictionaryGroupsPopulate(options);
|
||||
} catch (e) {
|
||||
dictionaryErrorShow(e);
|
||||
} finally {
|
||||
dictionarySpinnerShow(false);
|
||||
|
||||
dictControls.show();
|
||||
dictProgress.hide();
|
||||
}
|
||||
})();}
|
||||
|
||||
function onDictionaryImport(e) { (async () => {
|
||||
const dictFile = $('#dict-file');
|
||||
const dictImporter = $('#dict-importer').hide();
|
||||
const dictControls = $('#dict-importer').hide();
|
||||
const dictProgress = $('#dict-import-progress').show();
|
||||
|
||||
try {
|
||||
dictionaryErrorShow();
|
||||
dictionarySpinnerShow(true);
|
||||
|
||||
const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`);
|
||||
const updateProgress = (total, current) => setProgress(current / total * 100.0);
|
||||
|
||||
setProgress(0.0);
|
||||
|
||||
optionsLoad().then(options => {
|
||||
return instDb().importDictionary(e.target.files[0], updateProgress).then(summary => {
|
||||
const options = await optionsLoad();
|
||||
const summary = await instDb().importDictionary(e.target.files[0], updateProgress);
|
||||
options.dictionaries[summary.title] = {enabled: true, priority: 0};
|
||||
return optionsSave(options);
|
||||
}).then(() => dictionaryGroupsPopulate(options));
|
||||
}).catch(dictionaryErrorShow).then(() => {
|
||||
dictFile.val('');
|
||||
await optionsSave(options);
|
||||
|
||||
await dictionaryGroupsPopulate(options);
|
||||
} catch (e) {
|
||||
dictionaryErrorShow(e);
|
||||
} finally {
|
||||
dictionarySpinnerShow(false);
|
||||
|
||||
dictFile.val('');
|
||||
dictControls.show();
|
||||
dictProgress.hide();
|
||||
dictImporter.show();
|
||||
});
|
||||
}
|
||||
}
|
||||
})();}
|
||||
|
||||
/*
|
||||
* Anki
|
||||
@ -307,40 +331,38 @@ function ankiFieldsToDict(selection) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function ankiDeckAndModelPopulate(options) {
|
||||
ankiErrorShow(null);
|
||||
ankiSpinnerShow(true);
|
||||
|
||||
async function ankiDeckAndModelPopulate(options) {
|
||||
const ankiFormat = $('#anki-format').hide();
|
||||
return Promise.all([instAnki().getDeckNames(), instAnki().getModelNames()]).then(([deckNames, modelNames]) => {
|
||||
|
||||
const deckNames = await instAnki().getDeckNames();
|
||||
const ankiDeck = $('.anki-deck');
|
||||
ankiDeck.find('option').remove();
|
||||
deckNames.sort().forEach(name => ankiDeck.append($('<option/>', {value: name, text: name})));
|
||||
|
||||
$('#anki-terms-deck').val(options.anki.terms.deck);
|
||||
$('#anki-kanji-deck').val(options.anki.kanji.deck);
|
||||
|
||||
const modelNames = await instAnki().getModelNames();
|
||||
const ankiModel = $('.anki-model');
|
||||
ankiModel.find('option').remove();
|
||||
modelNames.sort().forEach(name => ankiModel.append($('<option/>', {value: name, text: name})));
|
||||
|
||||
return Promise.all([
|
||||
ankiFieldsPopulate($('#anki-terms-model').val(options.anki.terms.model), options),
|
||||
ankiFieldsPopulate($('#anki-kanji-model').val(options.anki.kanji.model), options)
|
||||
]);
|
||||
}).then(() => ankiFormat.show()).catch(ankiErrorShow).then(() => ankiSpinnerShow(false));
|
||||
$('#anki-terms-deck').val(options.anki.terms.deck);
|
||||
await ankiFieldsPopulate($('#anki-terms-model').val(options.anki.terms.model), options);
|
||||
|
||||
$('#anki-kanji-deck').val(options.anki.kanji.deck);
|
||||
await ankiFieldsPopulate($('#anki-kanji-model').val(options.anki.kanji.model), options);
|
||||
|
||||
ankiFormat.show();
|
||||
}
|
||||
|
||||
function ankiFieldsPopulate(element, options) {
|
||||
async function ankiFieldsPopulate(element, options) {
|
||||
const modelName = element.val();
|
||||
if (!modelName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const tab = element.closest('.tab-pane');
|
||||
const tabId = tab.attr('id');
|
||||
const container = tab.find('tbody').empty();
|
||||
|
||||
const modelName = element.val();
|
||||
if (!modelName) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const markers = {
|
||||
'terms': [
|
||||
'audio',
|
||||
@ -369,12 +391,11 @@ function ankiFieldsPopulate(element, options) {
|
||||
]
|
||||
}[tabId] || {};
|
||||
|
||||
return instAnki().getModelFieldNames(modelName).then(names => {
|
||||
names.forEach(name => {
|
||||
for (const name of await instAnki().getModelFieldNames(modelName)) {
|
||||
const value = options.anki[tabId].fields[name] || '';
|
||||
const html = Handlebars.templates['model.html']({name, markers, value});
|
||||
container.append($(html));
|
||||
});
|
||||
}
|
||||
|
||||
tab.find('.anki-field-value').change(onOptionsChanged);
|
||||
tab.find('.marker-link').click(e => {
|
||||
@ -382,24 +403,29 @@ function ankiFieldsPopulate(element, options) {
|
||||
const link = e.target;
|
||||
$(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function onAnkiModelChanged(e) {
|
||||
function onAnkiModelChanged(e) { (async () => {
|
||||
if (!e.originalEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
ankiErrorShow(null);
|
||||
try {
|
||||
ankiErrorShow();
|
||||
ankiSpinnerShow(true);
|
||||
|
||||
const element = $(this);
|
||||
formRead().then(({optionsNew, optionsOld}) => {
|
||||
const tab = element.closest('.tab-pane');
|
||||
const tabId = tab.attr('id');
|
||||
|
||||
const {optionsNew, optionsOld} = await formRead();
|
||||
optionsNew.anki[tabId].fields = {};
|
||||
ankiFieldsPopulate(element, optionsNew).then(() => {
|
||||
optionsSave(optionsNew);
|
||||
}).catch(ankiErrorShow).then(() => ankiSpinnerShow(false));
|
||||
});
|
||||
}
|
||||
await optionsSave(optionsNew);
|
||||
|
||||
await ankiFieldsPopulate(element, optionsNew);
|
||||
} catch (e) {
|
||||
ankiErrorShow(e);
|
||||
} finally {
|
||||
ankiSpinnerShow(false);
|
||||
}
|
||||
})();}
|
||||
|
Loading…
Reference in New Issue
Block a user