diff options
Diffstat (limited to 'ext/js/pages/settings/collapsible-dictionary-controller.js')
-rw-r--r-- | ext/js/pages/settings/collapsible-dictionary-controller.js | 86 |
1 files changed, 73 insertions, 13 deletions
diff --git a/ext/js/pages/settings/collapsible-dictionary-controller.js b/ext/js/pages/settings/collapsible-dictionary-controller.js index c8ce5e4f..a508bae4 100644 --- a/ext/js/pages/settings/collapsible-dictionary-controller.js +++ b/ext/js/pages/settings/collapsible-dictionary-controller.js @@ -20,18 +20,29 @@ import {EventListenerCollection} from '../../core.js'; import {yomitan} from '../../yomitan.js'; export class CollapsibleDictionaryController { + /** + * @param {SettingsController} settingsController + */ constructor(settingsController) { + /** @type {SettingsController} */ this._settingsController = settingsController; + /** @type {?import('core').TokenObject} */ this._getDictionaryInfoToken = null; + /** @type {Map<string, import('dictionary-importer').Summary>} */ this._dictionaryInfoMap = new Map(); + /** @type {EventListenerCollection} */ this._eventListeners = new EventListenerCollection(); + /** @type {?HTMLElement} */ this._container = null; + /** @type {HTMLSelectElement[]} */ this._selects = []; + /** @type {?HTMLSelectElement} */ this._allSelect = null; } + /** */ async prepare() { - this._container = document.querySelector('#collapsible-dictionary-list'); + this._container = /** @type {HTMLElement} */ (document.querySelector('#collapsible-dictionary-list')); await this._onDatabaseUpdated(); @@ -42,7 +53,9 @@ export class CollapsibleDictionaryController { // Private + /** */ async _onDatabaseUpdated() { + /** @type {?import('core').TokenObject} */ const token = {}; this._getDictionaryInfoToken = token; const dictionaries = await this._settingsController.getDictionaryInfo(); @@ -54,10 +67,12 @@ export class CollapsibleDictionaryController { this._dictionaryInfoMap.set(entry.title, entry); } - const options = await this._settingsController.getOptions(); - this._onOptionsChanged({options}); + await this._onDictionarySettingsReordered(); } + /** + * @param {import('settings-controller').OptionsChangedEvent} details + */ _onOptionsChanged({options}) { this._eventListeners.removeAllEventListeners(); this._selects = []; @@ -79,25 +94,37 @@ export class CollapsibleDictionaryController { this._selects.push(select); } - this._container.textContent = ''; - this._container.appendChild(fragment); + const container = /** @type {HTMLElement} */ (this._container); + container.textContent = ''; + container.appendChild(fragment); } + /** */ _onDefinitionsCollapsibleChange() { this._updateAllSelectFresh(); } + /** + * @param {Event} e + */ _onAllSelectChange(e) { - const {value} = e.currentTarget; - if (value === 'varies') { return; } - this._setDefinitionsCollapsibleAll(value); + const {value} = /** @type {HTMLSelectElement} */ (e.currentTarget); + const value2 = this._normalizeDictionaryDefinitionsCollapsible(value); + if (value2 === null) { return; } + this._setDefinitionsCollapsibleAll(value2); } + /** */ async _onDictionarySettingsReordered() { const options = await this._settingsController.getOptions(); - this._onOptionsChanged({options}); + const optionsContext = this._settingsController.getOptionsContext(); + this._onOptionsChanged({options, optionsContext}); } + /** + * @param {DocumentFragment} fragment + * @param {import('settings').ProfileOptions} options + */ _setupAllSelect(fragment, options) { const select = this._addSelect(fragment, 'All', ''); @@ -113,23 +140,33 @@ export class CollapsibleDictionaryController { this._updateAllSelect(options); } + /** + * @param {DocumentFragment} fragment + * @param {string} dictionary + * @param {string} version + * @returns {HTMLSelectElement} + */ _addSelect(fragment, dictionary, version) { const node = this._settingsController.instantiateTemplate('collapsible-dictionary-item'); fragment.appendChild(node); - const nameNode = node.querySelector('.dictionary-title'); + const nameNode = /** @type {HTMLElement} */ (node.querySelector('.dictionary-title')); nameNode.textContent = dictionary; - const versionNode = node.querySelector('.dictionary-version'); + const versionNode = /** @type {HTMLElement} */ (node.querySelector('.dictionary-version')); versionNode.textContent = version; - return node.querySelector('.definitions-collapsible'); + return /** @type {HTMLSelectElement} */ (node.querySelector('.definitions-collapsible')); } + /** */ async _updateAllSelectFresh() { this._updateAllSelect(await this._settingsController.getOptions()); } + /** + * @param {import('settings').ProfileOptions} options + */ _updateAllSelect(options) { let value = null; let varies = false; @@ -142,11 +179,17 @@ export class CollapsibleDictionaryController { } } - this._allSelect.value = (varies || value === null ? 'varies' : value); + if (this._allSelect !== null) { + this._allSelect.value = (varies || value === null ? 'varies' : value); + } } + /** + * @param {import('settings').DictionaryDefinitionsCollapsible} value + */ async _setDefinitionsCollapsibleAll(value) { const options = await this._settingsController.getOptions(); + /** @type {import('settings-modifications').Modification[]} */ const targets = []; const {dictionaries} = options; for (let i = 0, ii = dictionaries.length; i < ii; ++i) { @@ -158,4 +201,21 @@ export class CollapsibleDictionaryController { select.value = value; } } + + /** + * @param {string} value + * @returns {?import('settings').DictionaryDefinitionsCollapsible} + */ + _normalizeDictionaryDefinitionsCollapsible(value) { + switch (value) { + case 'not-collapsible': + case 'expanded': + case 'collapsed': + case 'force-collapsed': + case 'force-expanded': + return value; + default: + return null; + } + } } |