yomichan/ext/bg/js/options.js

405 lines
14 KiB
JavaScript
Raw Normal View History

2016-04-04 03:05:22 +00:00
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2017-03-03 05:01:49 +00:00
/*
* General
*/
2016-04-04 03:05:22 +00:00
2017-03-05 23:54:03 +00:00
function formRead() {
2017-03-03 05:01:49 +00:00
return optionsLoad().then(optionsOld => {
const optionsNew = $.extend(true, {}, optionsOld);
2017-04-09 01:01:36 +00:00
optionsNew.general.showGuide = $('#show-usage-guide').prop('checked');
optionsNew.general.audioSource = $('#audio-playback-source').val();
optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val());
2017-03-03 05:01:49 +00:00
optionsNew.general.groupResults = $('#group-terms-results').prop('checked');
2017-05-24 03:54:03 +00:00
optionsNew.general.debugInfo = $('#show-debug-info').prop('checked');
2017-03-03 05:01:49 +00:00
optionsNew.general.showAdvanced = $('#show-advanced-options').prop('checked');
optionsNew.general.maxResults = parseInt($('#max-displayed-results').val(), 10);
optionsNew.general.popupWidth = parseInt($('#popup-width').val(), 10);
optionsNew.general.popupHeight = parseInt($('#popup-height').val(), 10);
optionsNew.general.popupOffset = parseInt($('#popup-offset').val(), 10);
2017-03-03 05:01:49 +00:00
2017-03-06 00:03:14 +00:00
optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked');
2017-03-03 05:01:49 +00:00
optionsNew.scanning.selectText = $('#select-matched-text').prop('checked');
optionsNew.scanning.alphanumeric = $('#search-alphanumeric').prop('checked');
2017-03-03 05:01:49 +00:00
optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10);
optionsNew.scanning.length = parseInt($('#scan-length').val(), 10);
optionsNew.scanning.modifier = $('#scan-modifier-key').val();
2017-03-03 05:01:49 +00:00
optionsNew.anki.enable = $('#anki-enable').prop('checked');
optionsNew.anki.tags = $('#card-tags').val().split(/[,; ]+/);
optionsNew.anki.htmlCards = $('#generate-html-cards').prop('checked');
optionsNew.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10);
optionsNew.anki.server = $('#interface-server').val();
2017-03-05 23:54:03 +00:00
2017-03-07 03:42:59 +00:00
if (optionsOld.anki.enable && !$('#anki-error').is(':visible')) {
2017-03-03 05:01:49 +00:00
optionsNew.anki.terms.deck = $('#anki-terms-deck').val();
optionsNew.anki.terms.model = $('#anki-terms-model').val();
optionsNew.anki.terms.fields = ankiFieldsToDict($('#terms .anki-field-value'));
optionsNew.anki.kanji.deck = $('#anki-kanji-deck').val();
optionsNew.anki.kanji.model = $('#anki-kanji-model').val();
optionsNew.anki.kanji.fields = ankiFieldsToDict($('#kanji .anki-field-value'));
2016-04-06 05:18:55 +00:00
}
2017-03-03 05:01:49 +00:00
$('.dict-group').each((index, element) => {
const dictionary = $(element);
const title = dictionary.data('title');
2017-03-05 23:54:03 +00:00
const priority = parseInt(dictionary.find('.dict-priority').val(), 10);
2017-03-03 05:01:49 +00:00
const enabled = dictionary.find('.dict-enabled').prop('checked');
optionsNew.dictionaries[title] = {priority, enabled};
});
return {optionsNew, optionsOld};
});
}
function updateVisibility(options) {
const general = $('#anki-general');
if (options.anki.enable) {
general.show();
} else {
general.hide();
}
const advanced = $('.options-advanced');
if (options.general.showAdvanced) {
advanced.show();
} else {
advanced.hide();
}
2017-05-25 02:13:56 +00:00
const debug = $('#debug');
if (options.general.debugInfo) {
const text = JSON.stringify(options, null, 4);
debug.html(handlebarsEscape(text));
debug.show();
} else {
debug.hide();
}
2017-03-03 05:01:49 +00:00
}
2017-03-05 23:54:03 +00:00
function onOptionsChanged(e) {
if (!e.originalEvent && !e.isTrigger) {
return;
}
formRead().then(({optionsNew, optionsOld}) => {
return optionsSave(optionsNew).then(() => {
updateVisibility(optionsNew);
const ankiUpdated =
optionsNew.anki.enable !== optionsOld.anki.enable ||
optionsNew.anki.server !== optionsOld.anki.server;
if (ankiUpdated) {
ankiErrorShow(null);
ankiSpinnerShow(true);
return ankiDeckAndModelPopulate(optionsNew);
}
});
}).catch(ankiErrorShow).then(() => ankiSpinnerShow(false));
}
2017-03-03 05:01:49 +00:00
$(document).ready(() => {
2017-03-05 19:12:48 +00:00
handlebarsRegister();
2017-03-03 05:01:49 +00:00
optionsLoad().then(options => {
2017-04-09 01:01:36 +00:00
$('#show-usage-guide').prop('checked', options.general.showGuide);
$('#audio-playback-source').val(options.general.audioSource);
$('#audio-playback-volume').val(options.general.audioVolume);
2017-03-03 05:01:49 +00:00
$('#group-terms-results').prop('checked', options.general.groupResults);
2017-05-24 03:54:03 +00:00
$('#show-debug-info').prop('checked', options.general.debugInfo);
2017-03-03 05:01:49 +00:00
$('#show-advanced-options').prop('checked', options.general.showAdvanced);
$('#max-displayed-results').val(options.general.maxResults);
$('#popup-width').val(options.general.popupWidth);
$('#popup-height').val(options.general.popupHeight);
$('#popup-offset').val(options.general.popupOffset);
2017-03-03 05:01:49 +00:00
2017-03-06 00:03:14 +00:00
$('#middle-mouse-button-scan').prop('checked', options.scanning.middleMouse);
2017-03-03 05:01:49 +00:00
$('#select-matched-text').prop('checked', options.scanning.selectText);
$('#search-alphanumeric').prop('checked', options.scanning.alphanumeric);
2017-03-03 05:01:49 +00:00
$('#scan-delay').val(options.scanning.delay);
$('#scan-length').val(options.scanning.length);
$('#scan-modifier-key').val(options.scanning.modifier);
2017-03-03 05:01:49 +00:00
$('#dict-purge').click(onDictionaryPurge);
2017-06-13 05:58:02 +00:00
$('#dict-file').change(onDictionaryImport);
2017-03-03 05:01:49 +00:00
$('#anki-enable').prop('checked', options.anki.enable);
$('#card-tags').val(options.anki.tags.join(' '));
$('#generate-html-cards').prop('checked', options.anki.htmlCards);
$('#sentence-detection-extent').val(options.anki.sentenceExt);
$('#interface-server').val(options.anki.server);
$('input, select').not('.anki-model').change(onOptionsChanged);
$('.anki-model').change(onAnkiModelChanged);
2017-03-05 23:54:03 +00:00
dictionaryGroupsPopulate(options);
ankiDeckAndModelPopulate(options);
2017-03-03 05:01:49 +00:00
updateVisibility(options);
});
});
/*
* Dictionary
*/
2017-03-05 23:54:03 +00:00
function dictionaryErrorShow(error) {
2017-03-03 05:01:49 +00:00
const dialog = $('#dict-error');
if (error) {
dialog.show().find('span').text(error);
} else {
dialog.hide();
}
}
2017-03-05 23:54:03 +00:00
function dictionarySpinnerShow(show) {
2017-03-03 05:01:49 +00:00
const spinner = $('#dict-spinner');
if (show) {
spinner.show();
} else {
spinner.hide();
}
}
2017-03-05 23:54:03 +00:00
function dictionaryGroupsSort() {
const dictGroups = $('#dict-groups');
const dictGroupChildren = dictGroups.children('.dict-group').sort((ca, cb) => {
const pa = parseInt($(ca).find('.dict-priority').val(), 10);
const pb = parseInt($(cb).find('.dict-priority').val(), 10);
if (pa < pb) {
return 1;
} else if (pa > pb) {
return -1;
} else {
return 0;
}
});
dictGroups.append(dictGroupChildren);
}
function dictionaryGroupsPopulate(options) {
dictionaryErrorShow(null);
dictionarySpinnerShow(true);
2017-03-03 05:01:49 +00:00
const dictGroups = $('#dict-groups').empty();
const dictWarning = $('#dict-warning').hide();
return instDb().getDictionaries().then(rows => {
2017-03-05 23:54:03 +00:00
if (rows.length === 0) {
dictWarning.show();
}
for (const row of dictRowsSort(rows, options)) {
2017-03-06 00:27:29 +00:00
const dictOptions = options.dictionaries[row.title] || {enabled: false, priority: 0};
2017-03-05 20:35:58 +00:00
const dictHtml = handlebarsRender('dictionary.html', {
2017-03-03 05:01:49 +00:00
title: row.title,
version: row.version,
revision: row.revision,
priority: dictOptions.priority,
enabled: dictOptions.enabled
});
2017-03-05 20:35:58 +00:00
dictGroups.append($(dictHtml));
2017-03-05 23:54:03 +00:00
}
2017-03-03 05:01:49 +00:00
updateVisibility(options);
2017-03-05 23:54:03 +00:00
$('.dict-enabled, .dict-priority').change(e => {
dictionaryGroupsSort();
2017-03-05 20:35:58 +00:00
onOptionsChanged(e);
});
2017-03-05 23:54:03 +00:00
}).catch(dictionaryErrorShow).then(() => dictionarySpinnerShow(false));
2017-03-03 05:01:49 +00:00
}
function onDictionaryPurge(e) {
e.preventDefault();
2017-03-05 23:54:03 +00:00
dictionaryErrorShow(null);
dictionarySpinnerShow(true);
2017-01-14 04:47:40 +00:00
2017-03-03 05:01:49 +00:00
const dictControls = $('#dict-importer, #dict-groups').hide();
const dictProgress = $('#dict-purge-progress').show();
2017-03-05 23:54:03 +00:00
instDb().purge().catch(dictionaryErrorShow).then(() => {
dictionarySpinnerShow(false);
2017-03-03 05:01:49 +00:00
dictControls.show();
dictProgress.hide();
return optionsLoad();
}).then(options => {
options.dictionaries = {};
2017-03-05 23:54:03 +00:00
optionsSave(options).then(() => dictionaryGroupsPopulate(options));
2017-03-03 05:01:49 +00:00
});
}
2017-06-25 22:36:28 +00:00
function onDictionaryImport(e) {
dictionaryErrorShow(null);
dictionarySpinnerShow(true);
2017-06-25 22:43:29 +00:00
const dictFile = $('#dict-file');
2017-06-25 22:36:28 +00:00
const dictImporter = $('#dict-importer').hide();
const dictProgress = $('#dict-import-progress').show();
const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`);
2017-06-25 22:43:29 +00:00
const updateProgress = (total, current) => setProgress(current / total * 100.0);
2017-06-25 22:36:28 +00:00
setProgress(0.0);
optionsLoad().then(options => {
2017-06-25 22:43:29 +00:00
return instDb().importDictionary(e.target.files[0], updateProgress).then(summary => {
2017-06-25 22:36:28 +00:00
options.dictionaries[summary.title] = {enabled: true, priority: 0};
return optionsSave(options);
}).then(() => dictionaryGroupsPopulate(options));
}).catch(dictionaryErrorShow).then(() => {
2017-06-25 22:43:29 +00:00
dictFile.val('');
2017-06-25 22:36:28 +00:00
dictionarySpinnerShow(false);
dictProgress.hide();
dictImporter.show();
});
2017-03-03 05:01:49 +00:00
}
/*
* Anki
*/
2017-03-05 23:54:03 +00:00
function ankiSpinnerShow(show) {
2017-03-03 05:01:49 +00:00
const spinner = $('#anki-spinner');
if (show) {
spinner.show();
} else {
spinner.hide();
}
}
2017-03-05 23:54:03 +00:00
function ankiErrorShow(error) {
2017-03-03 05:01:49 +00:00
const dialog = $('#anki-error');
if (error) {
dialog.show().find('span').text(error);
}
else {
dialog.hide();
2016-04-04 03:05:22 +00:00
}
2017-03-03 05:01:49 +00:00
}
function ankiFieldsToDict(selection) {
const result = {};
selection.each((index, element) => {
result[$(element).data('field')] = $(element).val();
});
2016-04-04 03:05:22 +00:00
2017-03-03 05:01:49 +00:00
return result;
2016-04-04 03:05:22 +00:00
}
2017-03-05 23:54:03 +00:00
function ankiDeckAndModelPopulate(options) {
ankiErrorShow(null);
ankiSpinnerShow(true);
2017-03-03 05:01:49 +00:00
const ankiFormat = $('#anki-format').hide();
return Promise.all([instAnki().getDeckNames(), instAnki().getModelNames()]).then(([deckNames, modelNames]) => {
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 ankiModel = $('.anki-model');
ankiModel.find('option').remove();
modelNames.sort().forEach(name => ankiModel.append($('<option/>', {value: name, text: name})));
return Promise.all([
2017-03-05 23:54:03 +00:00
ankiFieldsPopulate($('#anki-terms-model').val(options.anki.terms.model), options),
ankiFieldsPopulate($('#anki-kanji-model').val(options.anki.kanji.model), options)
2017-03-03 05:01:49 +00:00
]);
2017-03-05 23:54:03 +00:00
}).then(() => ankiFormat.show()).catch(ankiErrorShow).then(() => ankiSpinnerShow(false));
2017-03-03 05:01:49 +00:00
}
2017-03-05 23:54:03 +00:00
function ankiFieldsPopulate(element, options) {
2017-03-03 05:01:49 +00:00
const tab = element.closest('.tab-pane');
const tabId = tab.attr('id');
const container = tab.find('tbody').empty();
const modelName = element.val();
if (modelName === null) {
return Promise.resolve();
}
const markers = {
2017-03-29 03:49:26 +00:00
'terms': [
'audio',
'cloze-body',
'cloze-prefix',
'cloze-suffix',
'dictionary',
'expression',
'furigana',
'glossary',
'reading',
'sentence',
'tags',
'url'
],
'kanji': [
'character',
'dictionary',
'glossary',
'kunyomi',
'onyomi',
'sentence',
'tags',
'url'
]
2017-03-03 05:01:49 +00:00
}[tabId] || {};
return instAnki().getModelFieldNames(modelName).then(names => {
names.forEach(name => {
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 => {
e.preventDefault();
const link = e.target;
$(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change');
});
2016-09-16 03:10:11 +00:00
});
2016-04-04 03:05:22 +00:00
}
2017-03-03 05:01:49 +00:00
function onAnkiModelChanged(e) {
if (!e.originalEvent) {
return;
}
2017-03-05 23:54:03 +00:00
ankiErrorShow(null);
ankiSpinnerShow(true);
2017-03-03 05:01:49 +00:00
const element = $(this);
2017-03-05 23:54:03 +00:00
formRead().then(({optionsNew, optionsOld}) => {
2017-03-03 05:01:49 +00:00
const tab = element.closest('.tab-pane');
const tabId = tab.attr('id');
optionsNew.anki[tabId].fields = {};
2017-03-05 23:54:03 +00:00
ankiFieldsPopulate(element, optionsNew).then(() => {
2017-03-05 03:16:19 +00:00
optionsSave(optionsNew);
2017-03-05 23:54:03 +00:00
}).catch(ankiErrorShow).then(() => ankiSpinnerShow(false));
2016-09-16 03:10:11 +00:00
});
2016-04-04 03:05:22 +00:00
}