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-30 22:01:16 +00:00
|
|
|
constructor(dictionaryController, fragment, index, dictionaryInfo) {
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaryController = dictionaryController;
|
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();
|
2021-04-30 22:01:16 +00:00
|
|
|
this._counts = null;
|
|
|
|
this._nodes = [...fragment.childNodes];
|
|
|
|
this._enabledCheckbox = fragment.querySelector('.dictionary-enabled');
|
|
|
|
this._priorityInput = fragment.querySelector('.dictionary-priority');
|
|
|
|
this._menuButton = fragment.querySelector('.dictionary-menu-button');
|
|
|
|
this._outdatedButton = fragment.querySelector('.dictionary-outdated-button');
|
|
|
|
this._integrityButton = fragment.querySelector('.dictionary-integrity-button');
|
|
|
|
this._titleNode = fragment.querySelector('.dictionary-title');
|
|
|
|
this._versionNode = fragment.querySelector('.dictionary-version');
|
|
|
|
this._titleContainer = fragment.querySelector('.dictionary-item-title-container');
|
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() {
|
2021-04-03 17:02:49 +00:00
|
|
|
const index = this._index;
|
2021-04-30 22:01:16 +00:00
|
|
|
const {title, revision, version} = this._dictionaryInfo;
|
|
|
|
|
|
|
|
this._titleNode.textContent = title;
|
|
|
|
this._versionNode.textContent = `rev.${revision}`;
|
|
|
|
this._outdatedButton.hidden = (version >= 3);
|
|
|
|
this._priorityInput.dataset.setting = `dictionaries[${index}].priority`;
|
|
|
|
this._enabledCheckbox.dataset.setting = `dictionaries[${index}].enabled`;
|
|
|
|
this._eventListeners.addEventListener(this._enabledCheckbox, 'settingChanged', this._onEnabledChanged.bind(this), false);
|
2021-04-30 22:15:32 +00:00
|
|
|
this._eventListeners.addEventListener(this._menuButton, 'menuOpen', this._onMenuOpen.bind(this), false);
|
2021-04-30 22:01:16 +00:00
|
|
|
this._eventListeners.addEventListener(this._menuButton, 'menuClose', this._onMenuClose.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(this._outdatedButton, 'click', this._onOutdatedButtonClick.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(this._integrityButton, 'click', this._onIntegrityButtonClick.bind(this), false);
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
cleanup() {
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
2021-04-30 22:01:16 +00:00
|
|
|
for (const node of this._nodes) {
|
|
|
|
if (node.parentNode !== null) {
|
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
2021-04-30 22:01:16 +00:00
|
|
|
this._nodes = [];
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:35:44 +00:00
|
|
|
setCounts(counts) {
|
2021-04-30 22:01:16 +00:00
|
|
|
this._counts = counts;
|
|
|
|
this._integrityButton.hidden = false;
|
2019-11-03 14:56:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
setEnabled(value) {
|
|
|
|
this._enabledCheckbox.checked = value;
|
2020-10-18 23:35:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
// Private
|
|
|
|
|
2021-04-30 22:15:32 +00:00
|
|
|
_onMenuOpen(e) {
|
|
|
|
const bodyNode = e.detail.menu.bodyNode;
|
|
|
|
const count = this._dictionaryController.dictionaryOptionCount;
|
|
|
|
this._setMenuActionEnabled(bodyNode, 'moveUp', this._index > 0);
|
|
|
|
this._setMenuActionEnabled(bodyNode, 'moveDown', this._index < count - 1);
|
2021-05-03 01:04:39 +00:00
|
|
|
this._setMenuActionEnabled(bodyNode, 'moveTo', count > 1);
|
2021-04-30 22:15:32 +00:00
|
|
|
}
|
|
|
|
|
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':
|
2021-04-30 22:01:16 +00:00
|
|
|
this._showDetails();
|
2020-10-18 23:35:09 +00:00
|
|
|
break;
|
2021-04-30 22:15:32 +00:00
|
|
|
case 'moveUp':
|
|
|
|
this._move(-1);
|
|
|
|
break;
|
|
|
|
case 'moveDown':
|
|
|
|
this._move(1);
|
|
|
|
break;
|
2021-05-03 01:04:39 +00:00
|
|
|
case 'moveTo':
|
|
|
|
this._showMoveToModal();
|
|
|
|
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;
|
2021-04-30 22:01:16 +00:00
|
|
|
this._titleContainer.dataset.enabled = `${value}`;
|
2021-01-25 03:32:29 +00:00
|
|
|
this._dictionaryController.updateDictionariesEnabled();
|
2020-04-05 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
_onOutdatedButtonClick() {
|
|
|
|
this._showDetails();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onIntegrityButtonClick() {
|
|
|
|
this._showDetails();
|
|
|
|
}
|
|
|
|
|
|
|
|
_showDetails() {
|
2021-05-02 14:15:47 +00:00
|
|
|
const {title, revision, version, prefixWildcardsSupported} = this._dictionaryInfo;
|
2021-04-30 22:01:16 +00:00
|
|
|
|
|
|
|
const modal = this._dictionaryController.modalController.getModal('dictionary-details');
|
|
|
|
|
|
|
|
modal.node.querySelector('.dictionary-title').textContent = title;
|
|
|
|
modal.node.querySelector('.dictionary-version').textContent = `rev.${revision}`;
|
|
|
|
modal.node.querySelector('.dictionary-outdated-notification').hidden = (version >= 3);
|
|
|
|
modal.node.querySelector('.dictionary-counts').textContent = this._counts !== null ? JSON.stringify(this._counts, null, 4) : '';
|
2021-05-02 14:15:47 +00:00
|
|
|
modal.node.querySelector('.dictionary-prefix-wildcard-searches-supported').checked = prefixWildcardsSupported;
|
2021-04-30 22:01:16 +00:00
|
|
|
this._setupDetails(modal.node.querySelector('.dictionary-details-table'));
|
|
|
|
|
|
|
|
modal.setVisible(true);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
detailsTable.textContent = '';
|
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);
|
|
|
|
}
|
2021-04-30 22:15:32 +00:00
|
|
|
|
|
|
|
_move(offset) {
|
|
|
|
this._dictionaryController.swapDictionaryOptions(this._index, this._index + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setMenuActionEnabled(menu, action, enabled) {
|
|
|
|
const element = menu.querySelector(`[data-menu-action="${action}"]`);
|
|
|
|
if (element === null) { return; }
|
|
|
|
element.disabled = !enabled;
|
|
|
|
}
|
2021-05-03 01:04:39 +00:00
|
|
|
|
|
|
|
_showMoveToModal() {
|
|
|
|
const {title} = this._dictionaryInfo;
|
|
|
|
const count = this._dictionaryController.dictionaryOptionCount;
|
|
|
|
const modal = this._dictionaryController.modalController.getModal('dictionary-move-location');
|
|
|
|
const input = modal.node.querySelector('#dictionary-move-location');
|
|
|
|
|
|
|
|
modal.node.dataset.index = `${this._index}`;
|
|
|
|
modal.node.querySelector('.dictionary-title').textContent = title;
|
|
|
|
input.value = `${this._index + 1}`;
|
|
|
|
input.max = `${count}`;
|
|
|
|
|
|
|
|
modal.setVisible(true);
|
|
|
|
}
|
2020-09-15 23:35:44 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
class DictionaryExtraInfo {
|
|
|
|
constructor(parent, totalCounts, remainders, totalRemainder) {
|
|
|
|
this._parent = parent;
|
|
|
|
this._totalCounts = totalCounts;
|
|
|
|
this._remainders = remainders;
|
|
|
|
this._totalRemainder = totalRemainder;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
|
|
|
this._nodes = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare(container) {
|
|
|
|
const fragment = this._parent.instantiateTemplateFragment('dictionary-extra');
|
|
|
|
this._nodes = [...fragment.childNodes];
|
|
|
|
|
|
|
|
this._setTitle(fragment.querySelector('.dictionary-total-count'));
|
|
|
|
this._eventListeners.addEventListener(fragment.querySelector('.dictionary-integrity-button'), 'click', this._onIntegrityButtonClick.bind(this), false);
|
|
|
|
|
|
|
|
container.appendChild(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
for (const node of this._nodes) {
|
|
|
|
if (node.parentNode !== null) {
|
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._nodes = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_onIntegrityButtonClick() {
|
|
|
|
this._showDetails();
|
|
|
|
}
|
|
|
|
|
|
|
|
_showDetails() {
|
|
|
|
const modal = this._parent.modalController.getModal('dictionary-extra-data');
|
|
|
|
|
|
|
|
const info = {counts: this._totalCounts, remainders: this._remainders};
|
|
|
|
modal.node.querySelector('.dictionary-counts').textContent = JSON.stringify(info, null, 4);
|
|
|
|
this._setTitle(modal.node.querySelector('.dictionary-total-count'));
|
|
|
|
|
|
|
|
modal.setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setTitle(node) {
|
|
|
|
node.textContent = `${this._totalRemainder} item${this._totalRemainder !== 1 ? 's' : ''}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
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;
|
2021-04-30 22:01:16 +00:00
|
|
|
this._allCheckbox = null;
|
|
|
|
this._extraInfo = null;
|
2020-09-15 23:35:44 +00:00
|
|
|
this._isDeleting = false;
|
2019-11-02 18:06:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
get modalController() {
|
|
|
|
return this._modalController;
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:15:32 +00:00
|
|
|
get dictionaryOptionCount() {
|
|
|
|
return this._dictionaryEntries.length;
|
|
|
|
}
|
|
|
|
|
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');
|
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');
|
2021-04-30 22:01:16 +00:00
|
|
|
this._allCheckbox = document.querySelector('#all-dictionaries-enabled');
|
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));
|
2021-04-30 22:01:16 +00:00
|
|
|
this._allCheckbox.addEventListener('change', this._onAllCheckboxChange.bind(this), false);
|
2020-10-11 21:31:58 +00:00
|
|
|
document.querySelector('#dictionary-confirm-delete-button').addEventListener('click', this._onDictionaryConfirmDelete.bind(this), false);
|
2021-05-03 01:04:39 +00:00
|
|
|
document.querySelector('#dictionary-move-button').addEventListener('click', this._onDictionaryMoveButtonClick.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
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
this._updateDictionaryEntryCount();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-30 22:15:32 +00:00
|
|
|
async swapDictionaryOptions(index1, index2) {
|
|
|
|
const options = await this._settingsController.getOptions();
|
|
|
|
const {dictionaries} = options;
|
|
|
|
if (
|
|
|
|
index1 < 0 || index1 >= dictionaries.length ||
|
2021-05-03 01:04:39 +00:00
|
|
|
index2 < 0 || index2 >= dictionaries.length ||
|
|
|
|
index1 === index2
|
2021-04-30 22:15:32 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this._settingsController.modifyProfileSettings([{
|
|
|
|
action: 'swap',
|
|
|
|
path1: `dictionaries[${index1}]`,
|
|
|
|
path2: `dictionaries[${index2}]`
|
|
|
|
}]);
|
|
|
|
|
|
|
|
await this._updateEntries();
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:31:58 +00:00
|
|
|
instantiateTemplate(name) {
|
|
|
|
return this._settingsController.instantiateTemplate(name);
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
instantiateTemplateFragment(name) {
|
|
|
|
return this._settingsController.instantiateTemplateFragment(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();
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
_onAllCheckboxChange() {
|
|
|
|
const value = this._allCheckbox.checked;
|
|
|
|
this._allCheckbox.checked = !value;
|
|
|
|
this._setAllDictionariesEnabled(value);
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
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 = [];
|
2021-04-30 22:01:16 +00:00
|
|
|
this._updateDictionaryEntryCount();
|
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) {
|
2021-04-30 22:01:16 +00:00
|
|
|
const {dictionaries} = options;
|
|
|
|
let enabledDictionaryCountValid = 0;
|
|
|
|
let enabledDictionaryCount = 0;
|
|
|
|
const dictionaryCount = dictionaries.length;
|
2021-01-25 03:32:29 +00:00
|
|
|
if (this._dictionaries !== null) {
|
2021-04-03 17:02:49 +00:00
|
|
|
const enabledDictionaries = new Set();
|
2021-04-30 22:01:16 +00:00
|
|
|
for (const {name, enabled} of dictionaries) {
|
2021-04-03 17:02:49 +00:00
|
|
|
if (enabled) {
|
2021-04-30 22:01:16 +00:00
|
|
|
++enabledDictionaryCount;
|
2021-04-03 17:02:49 +00:00
|
|
|
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)) {
|
2021-04-30 22:01:16 +00:00
|
|
|
++enabledDictionaryCountValid;
|
2021-01-25 03:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
const hasEnabledDictionary = (enabledDictionaryCountValid > 0);
|
2021-01-25 03:32:29 +00:00
|
|
|
for (const node of this._noDictionariesEnabledWarnings) {
|
|
|
|
node.hidden = hasEnabledDictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._dictionaryEnabledCountNode !== null) {
|
2021-04-30 22:01:16 +00:00
|
|
|
this._dictionaryEnabledCountNode.textContent = `${enabledDictionaryCountValid}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._allCheckbox.checked = (enabledDictionaryCount >= dictionaryCount);
|
|
|
|
|
|
|
|
const entries = this._dictionaryEntries;
|
|
|
|
for (let i = 0, ii = Math.min(entries.length, dictionaryCount); i < ii; ++i) {
|
|
|
|
entries[i].setEnabled(dictionaries[i].enabled);
|
2021-01-25 03:32:29 +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
|
|
|
|
2021-05-03 01:04:39 +00:00
|
|
|
_onDictionaryMoveButtonClick() {
|
|
|
|
const modal = this._modalController.getModal('dictionary-move-location');
|
|
|
|
let {index} = modal.node.dataset;
|
|
|
|
index = Number.parseInt(index, 10);
|
|
|
|
|
|
|
|
let target = document.querySelector('#dictionary-move-location').value;
|
|
|
|
target = Number.parseInt(target, 10) - 1;
|
|
|
|
|
|
|
|
if (!Number.isFinite(target) || !Number.isFinite(index) || index === target) { return; }
|
|
|
|
|
|
|
|
this.swapDictionaryOptions(index, target);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
if (this._extraInfo !== null) {
|
|
|
|
this._extraInfo.cleanup();
|
|
|
|
this._extraInfo = null;
|
2020-09-15 23:35:44 +00:00
|
|
|
}
|
2019-11-02 18:06:16 +00:00
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
if (totalRemainder > 0) {
|
|
|
|
this._extraInfo = new DictionaryExtraInfo(this, totalCounts, remainders, totalRemainder);
|
|
|
|
this._extraInfo.prepare(this._dictionaryEntryContainer);
|
|
|
|
}
|
2020-09-15 23:35:44 +00:00
|
|
|
}
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2021-04-03 17:02:49 +00:00
|
|
|
_createDictionaryEntry(index, dictionaryInfo) {
|
2021-04-30 22:01:16 +00:00
|
|
|
const fragment = this.instantiateTemplateFragment('dictionary');
|
2020-05-30 00:25:22 +00:00
|
|
|
|
2021-04-30 22:01:16 +00:00
|
|
|
const entry = new DictionaryEntry(this, fragment, index, dictionaryInfo);
|
2020-09-15 23:35:44 +00:00
|
|
|
this._dictionaryEntries.push(entry);
|
|
|
|
entry.prepare();
|
2021-04-30 22:01:16 +00:00
|
|
|
|
|
|
|
const container = this._dictionaryEntryContainer;
|
|
|
|
const relative = container.querySelector('.dictionary-item-bottom');
|
|
|
|
container.insertBefore(fragment, relative);
|
|
|
|
|
|
|
|
this._updateDictionaryEntryCount();
|
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');
|
|
|
|
}
|
2021-04-30 22:01:16 +00:00
|
|
|
|
|
|
|
_updateDictionaryEntryCount() {
|
|
|
|
this._dictionaryEntryContainer.dataset.count = `${this._dictionaryEntries.length}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setAllDictionariesEnabled(value) {
|
|
|
|
const options = await this._settingsController.getOptions();
|
|
|
|
const {dictionaries} = options;
|
|
|
|
|
|
|
|
const targets = [];
|
|
|
|
for (let i = 0, ii = dictionaries.length; i < ii; ++i) {
|
|
|
|
targets.push({
|
|
|
|
action: 'set',
|
|
|
|
path: `dictionaries[${i}].enabled`,
|
|
|
|
value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await this._settingsController.modifyProfileSettings(targets);
|
|
|
|
|
|
|
|
await this.updateDictionariesEnabled();
|
|
|
|
}
|
2019-11-24 03:54:06 +00:00
|
|
|
}
|