This commit is contained in:
Alex Yatskov 2017-01-14 20:38:11 -08:00
parent 8fe82573cc
commit 6ad860bd72
2 changed files with 23 additions and 24 deletions

View File

@ -57,24 +57,23 @@ function getFormValues() {
optsNew.dictionaries[title] = {enableTerms, enableKanji};
});
return {
optsNew: sanitizeOptions(optsNew),
optsOld: sanitizeOptions(optsOld)
};
return {optsNew, optsOld};
});
}
function updateVisibility(opts) {
const general = $('#anki-general');
if (opts.anki.enable) {
$('#anki-general').show();
general.show();
} else {
$('#anki-general').hide();
general.hide();
}
const advanced = $('.options-advanced');
if (opts.general.showAdvanced) {
$('.options-advanced').show();
advanced.show();
} else {
$('.options-advanced').hide();
advanced.hide();
}
}
@ -93,18 +92,17 @@ $(document).ready(() => {
$('#scan-delay').val(opts.scanning.delay);
$('#scan-length').val(opts.scanning.length);
$('#anki-enable').prop('checked', opts.anki.enable);
$('#anki-card-tags').val(opts.anki.tags.join(' '));
$('#sentence-extent').val(opts.anki.sentenceExt);
$('input, select').not('.anki-model').change(onOptionsChanged);
$('.anki-model').change(onAnkiModelChanged);
$('#dict-purge').click(onDictionaryPurge);
$('#dict-importer a').click(onDictionarySetUrl);
$('#dict-import').click(onDictionaryImport);
$('#dict-url').on('input', onDictionaryUpdateUrl);
$('#anki-enable').prop('checked', opts.anki.enable);
$('#anki-card-tags').val(opts.anki.tags.join(' '));
$('#sentence-extent').val(opts.anki.sentenceExt);
$('input, select').not('.anki-model').change(onOptionsChanged);
$('.anki-model').change(onAnkiModelChanged);
populateDictionaries(opts);
populateAnkiDeckAndModel(opts);
updateVisibility(opts);
@ -301,10 +299,10 @@ function fieldsToDict(selection) {
return result;
}
function modelIdToFieldOptKey(id) {
function modelIdToCard(id) {
return {
'anki-term-model': 'anki.terms.fields',
'anki-kanji-model': 'anki.kanji.fields'
'anki-term-model': 'terms',
'anki-kanji-model': 'kanji'
}[id];
}
@ -374,12 +372,12 @@ function populateAnkiFields(element, opts) {
}
const modelId = element.attr('id');
const optKey = modelIdToFieldOptKey(modelId);
const card = modelIdToCard(modelId);
const markers = modelIdToMarkers(modelId);
return anki().getModelFieldNames(modelName).then(names => {
names.forEach(name => {
const html = Handlebars.templates['model.html']({name, markers, value: opts[optKey][name] || ''});
const html = Handlebars.templates['model.html']({name, markers, value: opts.anki[card].fields[name] || ''});
container.append($(html));
});
@ -401,7 +399,8 @@ function onAnkiModelChanged(e) {
showAnkiSpinner(true);
getFormValues().then(({optsNew, optsOld}) => {
optsNew[modelIdToFieldOptKey($(this).id)] = {};
const card = modelIdToCard($(this).id);
optsNew.anki[card].fields = {};
populateAnkiFields($(this), optsNew).then(() => {
optionsSave(optsNew).then(() => yomichan().setOptions(optsNew));
}).catch(error => {

View File

@ -69,7 +69,7 @@ function optionsVersion(options) {
targetDict[targetKey] = sourceDict[sourceKey] || targetDict[targetKey];
};
const version = options.version || 0;
options.version = options.version || 0;
const fixups = [
() => {
optionsSetDefaults(options);
@ -98,8 +98,8 @@ function optionsVersion(options) {
},
];
if (version < fixups.length) {
fixups[version]();
if (options.version < fixups.length) {
fixups[options.version]();
++options.version;
optionsVersion(options);
}