2019-11-02 18:06:16 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2020-2021 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-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 {
|
2021-04-03 17:02:49 +00:00
|
|
|
constructor(dictionaryController, node, index, dictionaryInfo) {
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaryController = dictionaryController;
|
|
|
|
this._node = node;
|
2021-04-03 17:02:49 +00:00
|
|
|
this._index = index;
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaryInfo = dictionaryInfo;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
2020-10-11 21:31:58 +00:00
|
|
|
this._detailsContainer = null;
|
2020-10-18 23:35:09 +00:00
|
|
|
this._hasDetails = false;
|
|
|
|
this._hasCounts = false;
|
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;
|
2021-04-03 17:02:49 +00:00
|
|
|
const index = this._index;
|
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 priorityInput = node.querySelector('.dictionary-priority');
|
2020-10-18 22:26:44 +00:00
|
|
|
const menuButton = node.querySelector('.dictionary-menu-button');
|
2020-10-11 21:31:58 +00:00
|
|
|
const detailsTable = node.querySelector('.dictionary-details-table');
|
|
|
|
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');
|
|
|
|
|
2020-12-13 17:32:43 +00:00
|
|
|
const hasDetails = (detailsTable !== null && this._setupDetails(detailsTable));
|
2020-10-18 23:35:09 +00:00
|
|
|
this._hasDetails = hasDetails;
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
titleNode.textContent = title;
|
|
|
|
versionNode.textContent = `rev.${revision}`;
|
2020-12-13 17:32:43 +00:00
|
|
|
if (wildcardSupportedCheckbox !== null) {
|
|
|
|
wildcardSupportedCheckbox.checked = !!prefixWildcardsSupported;
|
|
|
|
}
|
|
|
|
if (outdatedContainer !== null) {
|
|
|
|
outdatedContainer.hidden = (version >= 3);
|
|
|
|
}
|
|
|
|
if (enabledCheckbox !== null) {
|
2021-04-03 17:02:49 +00:00
|
|
|
enabledCheckbox.dataset.setting = `dictionaries[${index}].enabled`;
|
2020-12-20 18:59:30 +00:00
|
|
|
this._eventListeners.addEventListener(enabledCheckbox, 'settingChanged', this._onEnabledChanged.bind(this), false);
|
2020-12-13 17:32:43 +00:00
|
|
|
}
|
|
|
|
if (priorityInput !== null) {
|
2021-04-03 17:02:49 +00:00
|
|
|
priorityInput.dataset.setting = `dictionaries[${index}].priority`;
|
2020-12-13 17:32:43 +00:00
|
|
|
}
|
2020-10-18 22:26:44 +00:00
|
|
|
if (menuButton !== null) {
|
2021-01-20 01:52:57 +00:00
|
|
|
this._eventListeners.addEventListener(menuButton, 'menuOpen', this._onMenuOpen.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(menuButton, 'menuClose', this._onMenuClose.bind(this), false);
|
2020-10-18 22:26:44 +00:00
|
|
|
}
|
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;
|
2020-10-18 23:35:09 +00:00
|
|
|
this._hasCounts = true;
|
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
|
|
|
|
2021-01-20 01:52:57 +00:00
|
|
|
_onMenuOpen(e) {
|
2021-01-24 02:07:45 +00:00
|
|
|
const bodyNode = e.detail.menu.bodyNode;
|
|
|
|
const showDetails = bodyNode.querySelector('.popup-menu-item[data-menu-action="showDetails"]');
|
|
|
|
const hideDetails = bodyNode.querySelector('.popup-menu-item[data-menu-action="hideDetails"]');
|
2020-12-20 18:59:30 +00:00
|
|
|
const hasDetails = (this._detailsContainer !== null);
|
2020-10-18 23:35:09 +00:00
|
|
|
const detailsVisible = (hasDetails && !this._detailsContainer.hidden);
|
|
|
|
if (showDetails !== null) {
|
|
|
|
showDetails.hidden = detailsVisible;
|
|
|
|
showDetails.disabled = !hasDetails;
|
|
|
|
}
|
|
|
|
if (hideDetails !== null) {
|
|
|
|
hideDetails.hidden = !detailsVisible;
|
|
|
|
hideDetails.disabled = !hasDetails;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 01:52:57 +00:00
|
|
|
_onMenuClose(e) {
|
|
|
|
switch (e.detail.action) {
|
2020-10-18 23:35:09 +00:00
|
|
|
case 'delete':
|
2020-10-18 22:26:44 +00:00
|
|
|
this._delete();
|
|
|
|
break;
|
2020-10-18 23:35:09 +00:00
|
|
|
case 'showDetails':
|
|
|
|
if (this._detailsContainer !== null) { this._detailsContainer.hidden = false; }
|
|
|
|
break;
|
|
|
|
case 'hideDetails':
|
|
|
|
if (this._detailsContainer !== null) { this._detailsContainer.hidden = true; }
|
|
|
|
break;
|
2020-10-18 22:26:44 +00:00
|
|
|
}
|
2020-07-03 15:56:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 18:59:30 +00:00
|
|
|
_onEnabledChanged(e) {
|
2020-09-15 23:35:44 +00:00
|
|
|
const {detail: {value}} = e;
|
2020-12-20 18:59:30 +00:00
|
|
|
this._node.dataset.enabled = `${value}`;
|
2021-01-25 03:32:29 +00:00
|
|
|
this._dictionaryController.updateDictionariesEnabled();
|
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-10-18 22:26:44 +00:00
|
|
|
|
|
|
|
_delete() {
|
|
|
|
this._dictionaryController.deleteDictionary(this.dictionaryTitle);
|
|
|
|
}
|
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 {
|
2021-03-03 01:27:51 +00:00
|
|
|
constructor(settingsController, modalController, statusFooter) {
|
2020-09-15 23:35:44 +00:00
|
|
|
this._settingsController = settingsController;
|
2020-10-11 00:58:38 +00:00
|
|
|
this._modalController = modalController;
|
2020-10-18 22:26:44 +00:00
|
|
|
this._statusFooter = statusFooter;
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaries = null;
|
|
|
|
this._dictionaryEntries = [];
|
|
|
|
this._databaseStateToken = null;
|
|
|
|
this._checkingIntegrity = false;
|
|
|
|
this._checkIntegrityButton = null;
|
|
|
|
this._dictionaryEntryContainer = null;
|
|
|
|
this._integrityExtraInfoContainer = null;
|
2020-10-28 01:20:26 +00:00
|
|
|
this._dictionaryInstallCountNode = null;
|
2021-01-25 03:32:29 +00:00
|
|
|
this._dictionaryEnabledCountNode = null;
|
|
|
|
this._noDictionariesInstalledWarnings = null;
|
|
|
|
this._noDictionariesEnabledWarnings = null;
|
2020-09-15 23:35:44 +00:00
|
|
|
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._checkIntegrityButton = document.querySelector('#dictionary-check-integrity');
|
|
|
|
this._dictionaryEntryContainer = document.querySelector('#dictionary-list');
|
|
|
|
this._integrityExtraInfoContainer = document.querySelector('#dictionary-list-extra');
|
2020-10-28 01:20:26 +00:00
|
|
|
this._dictionaryInstallCountNode = document.querySelector('#dictionary-install-count');
|
2021-01-25 03:32:29 +00:00
|
|
|
this._dictionaryEnabledCountNode = document.querySelector('#dictionary-enabled-count');
|
2020-10-28 01:20:26 +00:00
|
|
|
this._noDictionariesInstalledWarnings = document.querySelectorAll('.no-dictionaries-installed-warning');
|
2021-01-25 03:32:29 +00:00
|
|
|
this._noDictionariesEnabledWarnings = document.querySelectorAll('.no-dictionaries-enabled-warning');
|
2020-10-11 21:31:58 +00:00
|
|
|
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));
|
2021-01-25 03:32:29 +00:00
|
|
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.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-12-13 17:32:43 +00:00
|
|
|
if (this._checkIntegrityButton !== null) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-01-25 03:32:29 +00:00
|
|
|
async updateDictionariesEnabled() {
|
|
|
|
const options = await this._settingsController.getOptions();
|
|
|
|
this._updateDictionariesEnabledWarnings(options);
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
static createDefaultDictionarySettings(name, enabled) {
|
2021-03-31 22:17:28 +00:00
|
|
|
return {
|
2021-04-03 17:02:49 +00:00
|
|
|
name,
|
2021-03-31 22:17:28 +00:00
|
|
|
priority: 0,
|
|
|
|
enabled,
|
|
|
|
allowSecondarySearches: false,
|
|
|
|
definitionsCollapsible: 'not-collapsible'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
static async ensureDictionarySettings(settingsController, dictionaries, optionsFull, modifyGlobalSettings, newDictionariesEnabled) {
|
2021-03-31 22:32:17 +00:00
|
|
|
if (typeof dictionaries === 'undefined') {
|
|
|
|
dictionaries = await settingsController.getDictionaryInfo();
|
|
|
|
}
|
|
|
|
if (typeof optionsFull === 'undefined') {
|
|
|
|
optionsFull = await settingsController.getOptionsFull();
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
const installedDictionaries = new Set();
|
2021-03-31 22:32:17 +00:00
|
|
|
for (const {title} of dictionaries) {
|
2021-04-03 17:02:49 +00:00
|
|
|
installedDictionaries.add(title);
|
|
|
|
}
|
2021-03-31 22:32:17 +00:00
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
const targets = [];
|
|
|
|
const {profiles} = optionsFull;
|
|
|
|
for (let i = 0, ii = profiles.length; i < ii; ++i) {
|
|
|
|
let modified = false;
|
|
|
|
const missingDictionaries = new Set([...installedDictionaries]);
|
|
|
|
const dictionaryOptionsArray = profiles[i].options.dictionaries;
|
|
|
|
for (let j = dictionaryOptionsArray.length - 1; j >= 0; --j) {
|
|
|
|
const {name} = dictionaryOptionsArray[j];
|
|
|
|
if (installedDictionaries.has(name)) {
|
|
|
|
missingDictionaries.delete(name);
|
2021-03-31 22:32:17 +00:00
|
|
|
} else {
|
2021-04-03 17:02:49 +00:00
|
|
|
dictionaryOptionsArray.splice(j, 1);
|
|
|
|
modified = true;
|
2021-03-31 22:32:17 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-03 17:02:49 +00:00
|
|
|
|
|
|
|
for (const name of missingDictionaries) {
|
|
|
|
const value = DictionaryController.createDefaultDictionarySettings(name, newDictionariesEnabled);
|
|
|
|
dictionaryOptionsArray.push(value);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modified) {
|
|
|
|
targets.push({
|
|
|
|
action: 'set',
|
|
|
|
path: `profiles[${i}].options.dictionaries`,
|
|
|
|
value: dictionaryOptionsArray
|
|
|
|
});
|
|
|
|
}
|
2021-03-31 22:32:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
if (modifyGlobalSettings && targets.length > 0) {
|
2021-03-31 22:32:17 +00:00
|
|
|
await settingsController.modifyGlobalSettings(targets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
// Private
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2021-01-25 03:32:29 +00:00
|
|
|
_onOptionsChanged({options}) {
|
|
|
|
this._updateDictionariesEnabledWarnings(options);
|
2021-04-03 17:02:49 +00:00
|
|
|
if (this._dictionaries !== null) {
|
|
|
|
this._updateEntries();
|
|
|
|
}
|
2021-01-25 03:32:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
async _onDatabaseUpdated() {
|
|
|
|
const token = {};
|
|
|
|
this._databaseStateToken = token;
|
|
|
|
this._dictionaries = null;
|
2020-11-05 23:45:57 +00:00
|
|
|
const dictionaries = await this._settingsController.getDictionaryInfo();
|
2020-09-15 23:35:44 +00:00
|
|
|
if (this._databaseStateToken !== token) { return; }
|
|
|
|
this._dictionaries = dictionaries;
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
await this._updateEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateEntries() {
|
|
|
|
const dictionaries = this._dictionaries;
|
2020-09-15 23:35:44 +00:00
|
|
|
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-28 01:20:26 +00:00
|
|
|
if (this._dictionaryInstallCountNode !== null) {
|
|
|
|
this._dictionaryInstallCountNode.textContent = `${dictionaries.length}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasDictionary = (dictionaries.length > 0);
|
|
|
|
for (const node of this._noDictionariesInstalledWarnings) {
|
|
|
|
node.hidden = hasDictionary;
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
await DictionaryController.ensureDictionarySettings(this._settingsController, dictionaries, void 0, true, false);
|
|
|
|
|
|
|
|
const options = await this._settingsController.getOptions();
|
2021-01-25 03:32:29 +00:00
|
|
|
this._updateDictionariesEnabledWarnings(options);
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
const dictionaryInfoMap = new Map();
|
|
|
|
for (const dictionary of this._dictionaries) {
|
|
|
|
dictionaryInfoMap.set(dictionary.title, dictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
const dictionaryOptionsArray = options.dictionaries;
|
|
|
|
for (let i = 0, ii = dictionaryOptionsArray.length; i < ii; ++i) {
|
|
|
|
const {name} = dictionaryOptionsArray[i];
|
|
|
|
const dictionaryInfo = dictionaryInfoMap.get(name);
|
|
|
|
if (typeof dictionaryInfo === 'undefined') { continue; }
|
|
|
|
this._createDictionaryEntry(i, dictionaryInfo);
|
2020-09-15 23:35:44 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
2019-11-02 20:21:06 +00:00
|
|
|
|
2021-01-25 03:32:29 +00:00
|
|
|
_updateDictionariesEnabledWarnings(options) {
|
|
|
|
let enabledCount = 0;
|
|
|
|
if (this._dictionaries !== null) {
|
2021-04-03 17:02:49 +00:00
|
|
|
const enabledDictionaries = new Set();
|
|
|
|
for (const {name, enabled} of options.dictionaries) {
|
|
|
|
if (enabled) {
|
|
|
|
enabledDictionaries.add(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 03:32:29 +00:00
|
|
|
for (const {title} of this._dictionaries) {
|
2021-04-03 17:02:49 +00:00
|
|
|
if (enabledDictionaries.has(title)) {
|
|
|
|
++enabledCount;
|
2021-01-25 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasEnabledDictionary = (enabledCount > 0);
|
|
|
|
for (const node of this._noDictionariesEnabledWarnings) {
|
|
|
|
node.hidden = hasEnabledDictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._dictionaryEnabledCountNode !== null) {
|
|
|
|
this._dictionaryEnabledCountNode.textContent = `${enabledCount}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2021-02-14 20:53:35 +00:00
|
|
|
const {counts, total} = await yomichan.api.getDictionaryCounts(dictionaryTitles, true);
|
2020-09-15 23:35:44 +00:00
|
|
|
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
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
_createDictionaryEntry(index, dictionaryInfo) {
|
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
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
const entry = new DictionaryEntry(this, node, index, dictionaryInfo);
|
2020-09-15 23:35:44 +00:00
|
|
|
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; }
|
|
|
|
|
2020-10-18 22:26:44 +00:00
|
|
|
const statusFooter = this._statusFooter;
|
|
|
|
const {node} = this._dictionaryEntries[index];
|
|
|
|
const progressSelector = '.dictionary-delete-progress';
|
|
|
|
const progressContainers = [
|
|
|
|
...node.querySelectorAll('.progress-container'),
|
2021-01-31 00:47:16 +00:00
|
|
|
...document.querySelectorAll(`#dictionaries-modal ${progressSelector}`)
|
2020-10-18 22:26:44 +00:00
|
|
|
];
|
|
|
|
const progressBars = [
|
|
|
|
...node.querySelectorAll('.progress-bar'),
|
|
|
|
...document.querySelectorAll(`${progressSelector} .progress-bar`)
|
|
|
|
];
|
|
|
|
const infoLabels = document.querySelectorAll(`${progressSelector} .progress-info`);
|
|
|
|
const statusLabels = document.querySelectorAll(`${progressSelector} .progress-status`);
|
2020-09-15 23:35:44 +00:00
|
|
|
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
|
|
|
const onProgress = ({processed, count, storeCount, storesProcesed}) => {
|
2020-10-18 22:26:44 +00:00
|
|
|
const percent = (
|
|
|
|
(count > 0 && storesProcesed > 0) ?
|
|
|
|
(processed / count) * (storesProcesed / storeCount) * 100.0 :
|
|
|
|
0.0
|
|
|
|
);
|
|
|
|
const cssString = `${percent}%`;
|
|
|
|
const statusString = `${percent.toFixed(0)}%`;
|
|
|
|
for (const progressBar of progressBars) { progressBar.style.width = cssString; }
|
|
|
|
for (const label of statusLabels) { label.textContent = statusString; }
|
2020-09-15 23:35:44 +00:00
|
|
|
};
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2020-10-18 22:26:44 +00:00
|
|
|
onProgress({processed: 0, count: 1, storeCount: 1, storesProcesed: 0});
|
|
|
|
|
|
|
|
for (const progress of progressContainers) { progress.hidden = false; }
|
|
|
|
for (const label of infoLabels) { label.textContent = 'Deleting dictionary...'; }
|
|
|
|
if (statusFooter !== null) { statusFooter.setTaskActive(progressSelector, true); }
|
|
|
|
|
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) {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(e);
|
2020-09-15 23:35:44 +00:00
|
|
|
} finally {
|
|
|
|
prevention.end();
|
2020-10-18 22:26:44 +00:00
|
|
|
for (const progress of progressContainers) { progress.hidden = true; }
|
|
|
|
if (statusFooter !== null) { statusFooter.setTaskActive(progressSelector, false); }
|
2020-09-15 23:35:44 +00:00
|
|
|
this._setButtonsEnabled(true);
|
|
|
|
this._isDeleting = false;
|
2021-03-03 01:27:51 +00:00
|
|
|
this._triggerStorageChanged();
|
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);
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.api.triggerDatabaseUpdated('dictionary', 'delete');
|
2020-09-19 21:17:33 +00:00
|
|
|
} 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];
|
2021-04-03 17:02:49 +00:00
|
|
|
for (let j = 0, jj = dictionaries.length; j < jj; ++j) {
|
|
|
|
if (dictionaries[j].name !== dictionaryTitle) { continue; }
|
|
|
|
const path = `profiles[${i}].options.dictionaries`;
|
|
|
|
targets.push({
|
|
|
|
action: 'splice',
|
|
|
|
path,
|
|
|
|
start: j,
|
|
|
|
deleteCount: 1,
|
|
|
|
items: []
|
|
|
|
});
|
2020-10-07 03:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
await this._settingsController.modifyGlobalSettings(targets);
|
|
|
|
}
|
2020-10-14 23:38:50 +00:00
|
|
|
|
2021-03-03 01:27:51 +00:00
|
|
|
_triggerStorageChanged() {
|
|
|
|
yomichan.trigger('storageChanged');
|
|
|
|
}
|
2019-11-24 03:54:06 +00:00
|
|
|
}
|