2019-12-01 20:41:09 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2019-2020 Yomichan Authors
|
2019-12-01 20:41:09 +00:00
|
|
|
*
|
|
|
|
* 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
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-01 20:41:09 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
2020-04-11 19:21:43 +00:00
|
|
|
* apiGetAnkiDeckNames
|
|
|
|
* apiGetAnkiModelFieldNames
|
|
|
|
* apiGetAnkiModelNames
|
2020-03-11 02:30:36 +00:00
|
|
|
* getOptionsContext
|
|
|
|
* getOptionsMutable
|
|
|
|
* onFormOptionsChanged
|
|
|
|
* settingsSaveOptions
|
|
|
|
* utilBackgroundIsolate
|
|
|
|
*/
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2019-12-02 03:21:10 +00:00
|
|
|
// Private
|
|
|
|
|
2019-12-02 03:16:58 +00:00
|
|
|
let _ankiDataPopulated = false;
|
|
|
|
|
|
|
|
|
2019-12-02 03:19:45 +00:00
|
|
|
function _ankiSpinnerShow(show) {
|
2019-12-01 20:41:09 +00:00
|
|
|
const spinner = $('#anki-spinner');
|
|
|
|
if (show) {
|
|
|
|
spinner.show();
|
|
|
|
} else {
|
|
|
|
spinner.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 03:26:23 +00:00
|
|
|
function _ankiSetError(error) {
|
|
|
|
const node = document.querySelector('#anki-error');
|
2020-02-02 19:53:32 +00:00
|
|
|
const node2 = document.querySelector('#anki-invalid-response-error');
|
2019-12-01 20:41:09 +00:00
|
|
|
if (error) {
|
2020-01-27 02:01:00 +00:00
|
|
|
const errorString = `${error}`;
|
2020-02-02 19:53:32 +00:00
|
|
|
if (node !== null) {
|
|
|
|
node.hidden = false;
|
|
|
|
node.textContent = errorString;
|
|
|
|
_ankiSetErrorData(node, error);
|
|
|
|
}
|
2020-01-27 02:01:00 +00:00
|
|
|
|
|
|
|
if (node2 !== null) {
|
|
|
|
node2.hidden = (errorString.indexOf('Invalid response') < 0);
|
|
|
|
}
|
2020-01-22 00:08:56 +00:00
|
|
|
} else {
|
2020-02-02 19:53:32 +00:00
|
|
|
if (node !== null) {
|
|
|
|
node.hidden = true;
|
|
|
|
node.textContent = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node2 !== null) {
|
|
|
|
node2.hidden = true;
|
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 00:08:56 +00:00
|
|
|
function _ankiSetErrorData(node, error) {
|
|
|
|
const data = error.data;
|
|
|
|
let message = '';
|
|
|
|
if (typeof data !== 'undefined') {
|
|
|
|
message += `${JSON.stringify(data, null, 4)}\n\n`;
|
|
|
|
}
|
|
|
|
message += `${error.stack}`.trimRight();
|
|
|
|
|
|
|
|
const button = document.createElement('a');
|
|
|
|
button.className = 'error-data-show-button';
|
|
|
|
|
|
|
|
const content = document.createElement('div');
|
|
|
|
content.className = 'error-data-container';
|
|
|
|
content.textContent = message;
|
|
|
|
content.hidden = true;
|
|
|
|
|
|
|
|
button.addEventListener('click', () => content.hidden = !content.hidden, false);
|
|
|
|
|
|
|
|
node.appendChild(button);
|
|
|
|
node.appendChild(content);
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
function _ankiSetDropdownOptions(dropdown, optionValues) {
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (const optionValue of optionValues) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = optionValue;
|
|
|
|
option.textContent = optionValue;
|
|
|
|
fragment.appendChild(option);
|
|
|
|
}
|
|
|
|
dropdown.textContent = '';
|
|
|
|
dropdown.appendChild(fragment);
|
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
async function _ankiDeckAndModelPopulate(options) {
|
|
|
|
const termsDeck = {value: options.anki.terms.deck, selector: '#anki-terms-deck'};
|
|
|
|
const kanjiDeck = {value: options.anki.kanji.deck, selector: '#anki-kanji-deck'};
|
|
|
|
const termsModel = {value: options.anki.terms.model, selector: '#anki-terms-model'};
|
|
|
|
const kanjiModel = {value: options.anki.kanji.model, selector: '#anki-kanji-model'};
|
|
|
|
try {
|
|
|
|
_ankiSpinnerShow(true);
|
2020-04-11 19:21:43 +00:00
|
|
|
const [deckNames, modelNames] = await Promise.all([apiGetAnkiDeckNames(), apiGetAnkiModelNames()]);
|
2019-12-03 03:17:45 +00:00
|
|
|
deckNames.sort();
|
|
|
|
modelNames.sort();
|
|
|
|
termsDeck.values = deckNames;
|
|
|
|
kanjiDeck.values = deckNames;
|
|
|
|
termsModel.values = modelNames;
|
|
|
|
kanjiModel.values = modelNames;
|
|
|
|
_ankiSetError(null);
|
|
|
|
} catch (error) {
|
|
|
|
_ankiSetError(error);
|
|
|
|
} finally {
|
|
|
|
_ankiSpinnerShow(false);
|
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
for (const {value, values, selector} of [termsDeck, kanjiDeck, termsModel, kanjiModel]) {
|
|
|
|
const node = document.querySelector(selector);
|
|
|
|
_ankiSetDropdownOptions(node, Array.isArray(values) ? values : [value]);
|
|
|
|
node.value = value;
|
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 03:19:45 +00:00
|
|
|
function _ankiCreateFieldTemplate(name, value, markers) {
|
2019-12-01 20:41:09 +00:00
|
|
|
const template = document.querySelector('#anki-field-template').content;
|
|
|
|
const content = document.importNode(template, true).firstChild;
|
|
|
|
|
|
|
|
content.querySelector('.anki-field-name').textContent = name;
|
|
|
|
|
|
|
|
const field = content.querySelector('.anki-field-value');
|
|
|
|
field.dataset.field = name;
|
|
|
|
field.value = value;
|
|
|
|
|
|
|
|
content.querySelector('.anki-field-marker-list').appendChild(ankiGetFieldMarkersHtml(markers));
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
async function _ankiFieldsPopulate(tabId, options) {
|
|
|
|
const tab = document.querySelector(`.tab-pane[data-anki-card-type=${tabId}]`);
|
|
|
|
const container = tab.querySelector('tbody');
|
2019-12-02 03:21:10 +00:00
|
|
|
const markers = ankiGetFieldMarkers(tabId);
|
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
const fields = options.anki[tabId].fields;
|
|
|
|
for (const name of Object.keys(fields)) {
|
|
|
|
const value = fields[name];
|
2019-12-02 03:21:10 +00:00
|
|
|
const html = _ankiCreateFieldTemplate(name, value, markers);
|
2019-12-03 03:17:45 +00:00
|
|
|
fragment.appendChild(html);
|
2019-12-02 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
container.textContent = '';
|
|
|
|
container.appendChild(fragment);
|
|
|
|
|
|
|
|
for (const node of container.querySelectorAll('.anki-field-value')) {
|
2020-02-27 02:17:01 +00:00
|
|
|
node.addEventListener('change', onFormOptionsChanged, false);
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
|
|
|
for (const node of container.querySelectorAll('.marker-link')) {
|
2020-02-27 02:17:01 +00:00
|
|
|
node.addEventListener('click', _onAnkiMarkerClicked, false);
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
2019-12-02 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _onAnkiMarkerClicked(e) {
|
|
|
|
e.preventDefault();
|
2019-12-03 03:17:45 +00:00
|
|
|
const link = e.currentTarget;
|
|
|
|
const input = $(link).closest('.input-group').find('.anki-field-value')[0];
|
|
|
|
input.value = `{${link.textContent}}`;
|
|
|
|
input.dispatchEvent(new Event('change'));
|
2019-12-02 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function _onAnkiModelChanged(e) {
|
2019-12-03 03:17:45 +00:00
|
|
|
const node = e.currentTarget;
|
|
|
|
let fieldNames;
|
2019-12-02 03:21:10 +00:00
|
|
|
try {
|
2019-12-03 03:17:45 +00:00
|
|
|
const modelName = node.value;
|
2020-04-11 19:21:43 +00:00
|
|
|
fieldNames = await apiGetAnkiModelFieldNames(modelName);
|
2019-12-02 03:26:23 +00:00
|
|
|
_ankiSetError(null);
|
2019-12-02 03:21:10 +00:00
|
|
|
} catch (error) {
|
2019-12-02 03:26:23 +00:00
|
|
|
_ankiSetError(error);
|
2019-12-03 03:17:45 +00:00
|
|
|
return;
|
2019-12-02 03:21:10 +00:00
|
|
|
} finally {
|
|
|
|
_ankiSpinnerShow(false);
|
|
|
|
}
|
2019-12-03 03:17:45 +00:00
|
|
|
|
|
|
|
const tabId = node.dataset.ankiCardType;
|
|
|
|
if (tabId !== 'terms' && tabId !== 'kanji') { return; }
|
|
|
|
|
|
|
|
const fields = {};
|
|
|
|
for (const name of fieldNames) {
|
|
|
|
fields[name] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const optionsContext = getOptionsContext();
|
2019-12-12 02:11:07 +00:00
|
|
|
const options = await getOptionsMutable(optionsContext);
|
2019-12-03 03:17:45 +00:00
|
|
|
options.anki[tabId].fields = utilBackgroundIsolate(fields);
|
|
|
|
await settingsSaveOptions();
|
|
|
|
|
|
|
|
await _ankiFieldsPopulate(tabId, options);
|
2019-12-02 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Public
|
|
|
|
|
|
|
|
function ankiErrorShown() {
|
2019-12-02 03:26:23 +00:00
|
|
|
const node = document.querySelector('#anki-error');
|
|
|
|
return node && !node.hidden;
|
2019-12-02 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
function ankiFieldsToDict(elements) {
|
2019-12-02 03:21:10 +00:00
|
|
|
const result = {};
|
2019-12-03 03:17:45 +00:00
|
|
|
for (const element of elements) {
|
|
|
|
result[element.dataset.field] = element.value;
|
|
|
|
}
|
2019-12-02 03:21:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-03 03:19:55 +00:00
|
|
|
function ankiGetFieldMarkersHtml(markers) {
|
2019-12-01 20:41:09 +00:00
|
|
|
const template = document.querySelector('#anki-field-marker-template').content;
|
2019-12-03 03:19:55 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
2019-12-01 20:41:09 +00:00
|
|
|
for (const marker of markers) {
|
|
|
|
const markerNode = document.importNode(template, true).firstChild;
|
|
|
|
markerNode.querySelector('.marker-link').textContent = marker;
|
|
|
|
fragment.appendChild(markerNode);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ankiGetFieldMarkers(type) {
|
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
return [
|
|
|
|
'audio',
|
|
|
|
'cloze-body',
|
|
|
|
'cloze-prefix',
|
|
|
|
'cloze-suffix',
|
|
|
|
'dictionary',
|
2020-03-28 01:58:24 +00:00
|
|
|
'document-title',
|
2019-12-01 20:41:09 +00:00
|
|
|
'expression',
|
|
|
|
'furigana',
|
|
|
|
'furigana-plain',
|
|
|
|
'glossary',
|
|
|
|
'glossary-brief',
|
|
|
|
'reading',
|
|
|
|
'screenshot',
|
|
|
|
'sentence',
|
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
case 'kanji':
|
|
|
|
return [
|
|
|
|
'character',
|
|
|
|
'dictionary',
|
2020-03-28 01:58:24 +00:00
|
|
|
'document-title',
|
2019-12-01 20:41:09 +00:00
|
|
|
'glossary',
|
|
|
|
'kunyomi',
|
|
|
|
'onyomi',
|
|
|
|
'screenshot',
|
|
|
|
'sentence',
|
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 03:16:58 +00:00
|
|
|
|
|
|
|
function ankiInitialize() {
|
|
|
|
for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) {
|
2020-02-27 02:17:01 +00:00
|
|
|
node.addEventListener('change', _onAnkiModelChanged, false);
|
2019-12-02 03:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onAnkiOptionsChanged(options) {
|
|
|
|
if (!options.anki.enable) {
|
|
|
|
_ankiDataPopulated = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_ankiDataPopulated) { return; }
|
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
await _ankiDeckAndModelPopulate(options);
|
|
|
|
_ankiDataPopulated = true;
|
|
|
|
await Promise.all([_ankiFieldsPopulate('terms', options), _ankiFieldsPopulate('kanji', options)]);
|
2019-12-02 03:16:58 +00:00
|
|
|
}
|