2019-11-02 18:06:16 +00:00
|
|
|
/*
|
2020-09-15 23:35:44 +00:00
|
|
|
* Copyright (C) 2020 Yomichan Authors
|
2019-11-02 18:06:16 +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-11-02 18:06:16 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
2020-09-19 21:17:33 +00:00
|
|
|
* DictionaryDatabase
|
2020-09-19 21:14:51 +00:00
|
|
|
* Modal
|
2020-09-15 23:35:44 +00:00
|
|
|
* ObjectPropertyAccessor
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
class DictionaryEntry {
|
|
|
|
constructor(dictionaryController, node, dictionaryInfo) {
|
|
|
|
this._dictionaryController = dictionaryController;
|
|
|
|
this._node = node;
|
|
|
|
this._dictionaryInfo = dictionaryInfo;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
2020-10-11 21:31:58 +00:00
|
|
|
this._detailsContainer = null;
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
get node() {
|
|
|
|
return this._node;
|
2019-11-13 01:13:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
get dictionaryTitle() {
|
2020-10-11 21:31:58 +00:00
|
|
|
return this._dictionaryInfo.title;
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
prepare() {
|
|
|
|
const node = this._node;
|
2020-10-11 21:31:58 +00:00
|
|
|
const {title, revision, prefixWildcardsSupported, version} = this._dictionaryInfo;
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
this._detailsContainer = node.querySelector('.dictionary-details');
|
|
|
|
|
|
|
|
const enabledCheckbox = node.querySelector('.dictionary-enabled');
|
|
|
|
const allowSecondarySearchesCheckbox = node.querySelector('.dictionary-allow-secondary-searches');
|
|
|
|
const priorityInput = node.querySelector('.dictionary-priority');
|
|
|
|
const deleteButton = node.querySelector('.dictionary-delete-button');
|
|
|
|
const detailsTable = node.querySelector('.dictionary-details-table');
|
|
|
|
const detailsToggleLink = node.querySelector('.dictionary-details-toggle-link');
|
|
|
|
const outdatedContainer = node.querySelector('.dictionary-outdated-notification');
|
|
|
|
const titleNode = node.querySelector('.dictionary-title');
|
|
|
|
const versionNode = node.querySelector('.dictionary-version');
|
|
|
|
const wildcardSupportedCheckbox = node.querySelector('.dictionary-prefix-wildcard-searches-supported');
|
|
|
|
|
|
|
|
const hasDetails = this._setupDetails(detailsTable);
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
titleNode.textContent = title;
|
|
|
|
versionNode.textContent = `rev.${revision}`;
|
|
|
|
wildcardSupportedCheckbox.checked = !!prefixWildcardsSupported;
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
outdatedContainer.hidden = (version >= 3);
|
|
|
|
detailsToggleLink.hidden = !hasDetails;
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
enabledCheckbox.dataset.setting = ObjectPropertyAccessor.getPathString(['dictionaries', title, 'enabled']);
|
|
|
|
allowSecondarySearchesCheckbox.dataset.setting = ObjectPropertyAccessor.getPathString(['dictionaries', title, 'allowSecondarySearches']);
|
|
|
|
priorityInput.dataset.setting = ObjectPropertyAccessor.getPathString(['dictionaries', title, 'priority']);
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
this._eventListeners.addEventListener(deleteButton, 'click', this._onDeleteButtonClicked.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(detailsToggleLink, 'click', this._onDetailsToggleLinkClicked.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(priorityInput, 'settingChanged', this._onPriorityChanged.bind(this), false);
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
cleanup() {
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
const node = this._node;
|
|
|
|
if (node.parentNode !== null) {
|
|
|
|
node.parentNode.removeChild(node);
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
setCounts(counts) {
|
2020-10-11 21:31:58 +00:00
|
|
|
const node = this._node.querySelector('.dictionary-counts');
|
2020-09-15 23:35:44 +00:00
|
|
|
node.textContent = JSON.stringify({info: this._dictionaryInfo, counts}, null, 4);
|
|
|
|
node.hidden = false;
|
2019-11-03 14:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
// Private
|
2019-11-02 20:21:06 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_onDeleteButtonClicked(e) {
|
|
|
|
e.preventDefault();
|
2020-10-11 21:31:58 +00:00
|
|
|
this._dictionaryController.deleteDictionary(this.dictionaryTitle);
|
2020-07-03 15:56:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_onDetailsToggleLinkClicked(e) {
|
2019-11-02 20:21:06 +00:00
|
|
|
e.preventDefault();
|
2020-09-15 23:35:44 +00:00
|
|
|
this._detailsContainer.hidden = !this._detailsContainer.hidden;
|
2019-11-02 20:21:06 +00:00
|
|
|
}
|
2020-04-05 18:46:21 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_onPriorityChanged(e) {
|
|
|
|
const {detail: {value}} = e;
|
|
|
|
this._node.style.order = `${-value}`;
|
2020-04-05 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
_setupDetails(detailsTable) {
|
2020-04-05 18:46:21 +00:00
|
|
|
const targets = [
|
|
|
|
['Author', 'author'],
|
|
|
|
['URL', 'url'],
|
|
|
|
['Description', 'description'],
|
|
|
|
['Attribution', 'attribution']
|
|
|
|
];
|
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
const dictionaryInfo = this._dictionaryInfo;
|
2020-09-15 23:35:44 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
2020-10-11 21:31:58 +00:00
|
|
|
let any = false;
|
2020-04-05 18:46:21 +00:00
|
|
|
for (const [label, key] of targets) {
|
|
|
|
const info = dictionaryInfo[key];
|
|
|
|
if (typeof info !== 'string') { continue; }
|
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
const details = this._dictionaryController.instantiateTemplate('dictionary-details-entry');
|
|
|
|
details.dataset.type = key;
|
|
|
|
details.querySelector('.dictionary-details-entry-label').textContent = `${label}:`;
|
|
|
|
details.querySelector('.dictionary-details-entry-info').textContent = info;
|
|
|
|
fragment.appendChild(details);
|
2020-04-05 18:46:21 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
any = true;
|
2020-04-05 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
detailsTable.appendChild(fragment);
|
|
|
|
return any;
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
2020-09-15 23:35:44 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
class DictionaryController {
|
2020-10-11 00:58:38 +00:00
|
|
|
constructor(settingsController, modalController) {
|
2020-09-15 23:35:44 +00:00
|
|
|
this._settingsController = settingsController;
|
2020-10-11 00:58:38 +00:00
|
|
|
this._modalController = modalController;
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaries = null;
|
|
|
|
this._dictionaryEntries = [];
|
|
|
|
this._databaseStateToken = null;
|
|
|
|
this._checkingIntegrity = false;
|
|
|
|
this._warningNode = null;
|
|
|
|
this._checkIntegrityButton = null;
|
|
|
|
this._dictionaryEntryContainer = null;
|
|
|
|
this._integrityExtraInfoContainer = null;
|
|
|
|
this._deleteDictionaryModal = null;
|
|
|
|
this._integrityExtraInfoNode = null;
|
|
|
|
this._isDeleting = false;
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
async prepare() {
|
2020-10-11 21:31:58 +00:00
|
|
|
this._warningNode = document.querySelector('#dictionary-warning');
|
|
|
|
this._checkIntegrityButton = document.querySelector('#dictionary-check-integrity');
|
|
|
|
this._dictionaryEntryContainer = document.querySelector('#dictionary-list');
|
|
|
|
this._integrityExtraInfoContainer = document.querySelector('#dictionary-list-extra');
|
|
|
|
this._deleteDictionaryModal = this._modalController.getModal('dictionary-confirm-delete');
|
2019-11-02 20:21:06 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
yomichan.on('databaseUpdated', this._onDatabaseUpdated.bind(this));
|
2019-11-02 20:21:06 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
document.querySelector('#dictionary-confirm-delete-button').addEventListener('click', this._onDictionaryConfirmDelete.bind(this), false);
|
2020-09-15 23:35:44 +00:00
|
|
|
this._checkIntegrityButton.addEventListener('click', this._onCheckIntegrityButtonClick.bind(this), false);
|
2019-11-02 20:21:06 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
await this._onDatabaseUpdated();
|
2019-11-02 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
deleteDictionary(dictionaryTitle) {
|
|
|
|
if (this._isDeleting) { return; }
|
|
|
|
const modal = this._deleteDictionaryModal;
|
2020-09-19 21:14:51 +00:00
|
|
|
modal.node.dataset.dictionaryTitle = dictionaryTitle;
|
2020-10-11 21:31:58 +00:00
|
|
|
modal.node.querySelector('#dictionary-confirm-delete-name').textContent = dictionaryTitle;
|
2020-09-19 21:14:51 +00:00
|
|
|
modal.setVisible(true);
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
instantiateTemplate(name) {
|
|
|
|
return this._settingsController.instantiateTemplate(name);
|
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
// Private
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
async _onDatabaseUpdated() {
|
|
|
|
const token = {};
|
|
|
|
this._databaseStateToken = token;
|
|
|
|
this._dictionaries = null;
|
|
|
|
const dictionaries = await api.getDictionaryInfo();
|
|
|
|
if (this._databaseStateToken !== token) { return; }
|
|
|
|
this._dictionaries = dictionaries;
|
|
|
|
|
|
|
|
this._warningNode.hidden = (dictionaries.length > 0);
|
|
|
|
this._updateMainDictionarySelectOptions(dictionaries);
|
|
|
|
|
|
|
|
for (const entry of this._dictionaryEntries) {
|
|
|
|
entry.cleanup();
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaryEntries = [];
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-14 23:38:50 +00:00
|
|
|
await this._ensureDictionarySettings(dictionaries);
|
2020-09-15 23:35:44 +00:00
|
|
|
for (const dictionary of dictionaries) {
|
|
|
|
this._createDictionaryEntry(dictionary);
|
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
2019-11-02 20:21:06 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_onDictionaryConfirmDelete(e) {
|
2019-11-02 20:21:06 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
const modal = this._deleteDictionaryModal;
|
2020-09-19 21:14:51 +00:00
|
|
|
modal.setVisible(false);
|
2020-09-15 23:35:44 +00:00
|
|
|
|
2020-09-19 21:14:51 +00:00
|
|
|
const title = modal.node.dataset.dictionaryTitle;
|
2020-09-15 23:35:44 +00:00
|
|
|
if (typeof title !== 'string') { return; }
|
2020-09-19 21:14:51 +00:00
|
|
|
delete modal.node.dataset.dictionaryTitle;
|
2019-11-02 20:21:06 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
this._deleteDictionary(title);
|
2019-11-02 20:21:06 +00:00
|
|
|
}
|
2020-04-05 18:46:21 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_onCheckIntegrityButtonClick(e) {
|
2020-04-05 18:46:21 +00:00
|
|
|
e.preventDefault();
|
2020-09-15 23:35:44 +00:00
|
|
|
this._checkIntegrity();
|
2020-04-05 18:46:21 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_updateMainDictionarySelectOptions(dictionaries) {
|
2020-10-11 21:31:58 +00:00
|
|
|
for (const select of document.querySelectorAll('[data-setting="general.mainDictionary"]')) {
|
|
|
|
const fragment = document.createDocumentFragment();
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
let option = document.createElement('option');
|
|
|
|
option.className = 'text-muted';
|
|
|
|
option.value = '';
|
|
|
|
option.textContent = 'Not selected';
|
2020-09-15 23:35:44 +00:00
|
|
|
fragment.appendChild(option);
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
for (const {title, sequenced} of dictionaries) {
|
|
|
|
if (!sequenced) { continue; }
|
|
|
|
option = document.createElement('option');
|
|
|
|
option.value = title;
|
|
|
|
option.textContent = title;
|
|
|
|
fragment.appendChild(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
select.textContent = ''; // Empty
|
|
|
|
select.appendChild(fragment);
|
|
|
|
}
|
2020-05-30 00:25:22 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
async _checkIntegrity() {
|
|
|
|
if (this._dictionaries === null || this._checkingIntegrity || this._isDeleting) { return; }
|
2020-02-01 16:20:50 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
try {
|
|
|
|
this._checkingIntegrity = true;
|
|
|
|
this._setButtonsEnabled(false);
|
2019-11-24 03:54:06 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
const token = this._databaseStateToken;
|
|
|
|
const dictionaryTitles = this._dictionaries.map(({title}) => title);
|
|
|
|
const {counts, total} = await api.getDictionaryCounts(dictionaryTitles, true);
|
|
|
|
if (this._databaseStateToken !== token) { return; }
|
2020-09-13 22:43:44 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
for (let i = 0, ii = Math.min(counts.length, this._dictionaryEntries.length); i < ii; ++i) {
|
|
|
|
const entry = this._dictionaryEntries[i];
|
|
|
|
entry.setCounts(counts[i]);
|
|
|
|
}
|
2020-05-30 13:33:13 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
this._setCounts(counts, total);
|
|
|
|
} finally {
|
|
|
|
this._setButtonsEnabled(true);
|
|
|
|
this._checkingIntegrity = false;
|
|
|
|
}
|
2020-05-30 00:25:22 +00:00
|
|
|
}
|
2020-02-01 16:48:24 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_setCounts(dictionaryCounts, totalCounts) {
|
|
|
|
const remainders = Object.assign({}, totalCounts);
|
|
|
|
const keys = Object.keys(remainders);
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
for (const counts of dictionaryCounts) {
|
|
|
|
for (const key of keys) {
|
|
|
|
remainders[key] -= counts[key];
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 18:39:37 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
let totalRemainder = 0;
|
|
|
|
for (const key of keys) {
|
|
|
|
totalRemainder += remainders[key];
|
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
this._cleanupExtra();
|
|
|
|
if (totalRemainder > 0) {
|
|
|
|
this.extra = this._createExtra(totalCounts, remainders, totalRemainder);
|
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_createExtra(totalCounts, remainders, totalRemainder) {
|
2020-10-11 21:31:58 +00:00
|
|
|
const node = this.instantiateTemplate('dictionary-extra');
|
2020-09-15 23:35:44 +00:00
|
|
|
this._integrityExtraInfoNode = node;
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
node.querySelector('.dictionary-total-count').textContent = `${totalRemainder} item${totalRemainder !== 1 ? 's' : ''}`;
|
2020-02-01 16:40:09 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
const n = node.querySelector('.dictionary-counts');
|
2020-09-15 23:35:44 +00:00
|
|
|
n.textContent = JSON.stringify({counts: totalCounts, remainders}, null, 4);
|
|
|
|
n.hidden = false;
|
2020-02-01 16:40:09 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
this._integrityExtraInfoContainer.appendChild(node);
|
2020-02-01 16:40:09 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_cleanupExtra() {
|
|
|
|
const node = this._integrityExtraInfoNode;
|
|
|
|
if (node === null) { return; }
|
|
|
|
this._integrityExtraInfoNode = null;
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
const parent = node.parentNode;
|
|
|
|
if (parent === null) { return; }
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
parent.removeChild(node);
|
|
|
|
}
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_createDictionaryEntry(dictionary) {
|
2020-10-11 21:31:58 +00:00
|
|
|
const node = this.instantiateTemplate('dictionary');
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaryEntryContainer.appendChild(node);
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
const entry = new DictionaryEntry(this, node, dictionary);
|
|
|
|
this._dictionaryEntries.push(entry);
|
|
|
|
entry.prepare();
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
async _deleteDictionary(dictionaryTitle) {
|
|
|
|
if (this._isDeleting || this._checkingIntegrity) { return; }
|
|
|
|
|
|
|
|
const index = this._dictionaryEntries.findIndex((entry) => entry.dictionaryTitle === dictionaryTitle);
|
|
|
|
if (index < 0) { return; }
|
|
|
|
|
|
|
|
const entry = this._dictionaryEntries[index];
|
|
|
|
const node = entry.node;
|
2020-10-11 21:31:58 +00:00
|
|
|
const progress = node.querySelector('.progress-container');
|
2020-09-15 23:35:44 +00:00
|
|
|
const progressBar = node.querySelector('.progress-bar');
|
|
|
|
const prevention = this._settingsController.preventPageExit();
|
2020-05-30 00:25:22 +00:00
|
|
|
try {
|
2020-09-15 23:35:44 +00:00
|
|
|
this._isDeleting = true;
|
|
|
|
this._setButtonsEnabled(false);
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
progress.hidden = false;
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
const onProgress = ({processed, count, storeCount, storesProcesed}) => {
|
|
|
|
let percent = 0.0;
|
|
|
|
if (count > 0 && storesProcesed > 0) {
|
|
|
|
percent = (processed / count) * (storesProcesed / storeCount) * 100.0;
|
|
|
|
}
|
|
|
|
progressBar.style.width = `${percent}%`;
|
|
|
|
};
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-09-19 21:17:33 +00:00
|
|
|
await this._deleteDictionaryInternal(dictionaryTitle, onProgress);
|
2020-10-07 03:00:00 +00:00
|
|
|
await this._deleteDictionarySettings(dictionaryTitle);
|
2020-05-30 00:25:22 +00:00
|
|
|
} catch (e) {
|
2020-09-04 21:54:34 +00:00
|
|
|
yomichan.logError(e);
|
2020-09-15 23:35:44 +00:00
|
|
|
} finally {
|
|
|
|
prevention.end();
|
|
|
|
progress.hidden = true;
|
|
|
|
this._setButtonsEnabled(true);
|
|
|
|
this._isDeleting = false;
|
2020-05-30 00:25:22 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
_setButtonsEnabled(value) {
|
|
|
|
value = !value;
|
2020-10-11 21:31:58 +00:00
|
|
|
for (const node of document.querySelectorAll('.dictionary-database-mutating-input')) {
|
2020-09-15 23:35:44 +00:00
|
|
|
node.disabled = value;
|
2020-05-30 00:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-03 15:56:26 +00:00
|
|
|
|
2020-09-19 21:17:33 +00:00
|
|
|
async _deleteDictionaryInternal(dictionaryTitle, onProgress) {
|
|
|
|
const dictionaryDatabase = await this._getPreparedDictionaryDatabase();
|
|
|
|
try {
|
|
|
|
await dictionaryDatabase.deleteDictionary(dictionaryTitle, {rate: 1000}, onProgress);
|
|
|
|
api.triggerDatabaseUpdated('dictionary', 'delete');
|
|
|
|
} finally {
|
|
|
|
dictionaryDatabase.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getPreparedDictionaryDatabase() {
|
|
|
|
const dictionaryDatabase = new DictionaryDatabase();
|
|
|
|
await dictionaryDatabase.prepare();
|
|
|
|
return dictionaryDatabase;
|
|
|
|
}
|
2020-10-07 03:00:00 +00:00
|
|
|
|
|
|
|
async _deleteDictionarySettings(dictionaryTitle) {
|
|
|
|
const optionsFull = await this._settingsController.getOptionsFull();
|
|
|
|
const {profiles} = optionsFull;
|
|
|
|
const targets = [];
|
|
|
|
for (let i = 0, ii = profiles.length; i < ii; ++i) {
|
|
|
|
const {options: {dictionaries}} = profiles[i];
|
|
|
|
if (Object.prototype.hasOwnProperty.call(dictionaries, dictionaryTitle)) {
|
|
|
|
const path = ObjectPropertyAccessor.getPathString(['profiles', i, 'options', 'dictionaries', dictionaryTitle]);
|
|
|
|
targets.push({action: 'delete', path});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await this._settingsController.modifyGlobalSettings(targets);
|
|
|
|
}
|
2020-10-14 23:38:50 +00:00
|
|
|
|
|
|
|
async _ensureDictionarySettings(dictionaries2) {
|
|
|
|
const optionsFull = await this._settingsController.getOptionsFull();
|
|
|
|
const {profiles} = optionsFull;
|
|
|
|
const targets = [];
|
|
|
|
for (const {title} of dictionaries2) {
|
|
|
|
for (let i = 0, ii = profiles.length; i < ii; ++i) {
|
|
|
|
const {options: {dictionaries: dictionaryOptions}} = profiles[i];
|
|
|
|
if (Object.prototype.hasOwnProperty.call(dictionaryOptions, title)) { continue; }
|
|
|
|
|
|
|
|
const path = ObjectPropertyAccessor.getPathString(['profiles', i, 'options', 'dictionaries', title]);
|
|
|
|
targets.push({
|
|
|
|
action: 'set',
|
|
|
|
path,
|
|
|
|
value: {
|
|
|
|
enabled: false,
|
|
|
|
allowSecondarySearches: false,
|
|
|
|
priority: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targets.length > 0) {
|
2020-10-15 22:10:29 +00:00
|
|
|
await this._settingsController.modifyGlobalSettings(targets);
|
2020-10-14 23:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 03:54:06 +00:00
|
|
|
}
|