Skip to content

Commit

Permalink
Disable enabled button when dictionary is already installed (#1336)
Browse files Browse the repository at this point in the history
* Disable enabled button when dictionary is already installed

* Disable button if downloadUrl exists in installed dictionaries

* lint
  • Loading branch information
khaitruong922 authored Aug 22, 2024
1 parent f5e76db commit 99710a5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ext/js/pages/settings/dictionary-import-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,20 @@ export class DictionaryImportController {
return;
}

const installedDictionaries = await this._settingsController.getDictionaryInfo();
/** @type {Set<string>} */
const installedDictionaryNames = new Set();
/** @type {Set<string>} */
const installedDictionaryDownloadUrls = new Set();
for (const dictionary of installedDictionaries) {
installedDictionaryNames.add(dictionary.title);
if (dictionary.downloadUrl) {
installedDictionaryDownloadUrls.add(dictionary.downloadUrl);
}
}

for (const {property, element} of recommendedDictionaryCategories) {
this._renderRecommendedDictionaryGroup(recommendedDictionaries[language][property], element);
this._renderRecommendedDictionaryGroup(recommendedDictionaries[language][property], element, installedDictionaryNames, installedDictionaryDownloadUrls);
}

/** @type {NodeListOf<HTMLElement>} */
Expand All @@ -191,8 +203,10 @@ export class DictionaryImportController {
*
* @param {import('dictionary-recommended.js').Dictionary[]} recommendedDictionaries
* @param {HTMLElement} dictionariesList
* @param {Set<string>} installedDictionaryNames
* @param {Set<string>} installedDictionaryDownloadUrls
*/
_renderRecommendedDictionaryGroup(recommendedDictionaries, dictionariesList) {
_renderRecommendedDictionaryGroup(recommendedDictionaries, dictionariesList, installedDictionaryNames, installedDictionaryDownloadUrls) {
const dictionariesListParent = dictionariesList.parentElement;
dictionariesList.innerHTML = '';
for (const dictionary of recommendedDictionaries) {
Expand All @@ -202,7 +216,9 @@ export class DictionaryImportController {
}
const template = this._settingsController.instantiateTemplate('recommended-dictionaries-list-item');
const label = querySelectorNotNull(template, '.settings-item-label');
/** @type {HTMLButtonElement} */
const button = querySelectorNotNull(template, '.action-button[data-action=import-recommended-dictionary]');
button.disabled = installedDictionaryNames.has(dictionary.name) || installedDictionaryDownloadUrls.has(dictionary.url);

const urlAttribute = document.createAttribute('data-import-url');
urlAttribute.value = dictionary.url;
Expand Down

0 comments on commit 99710a5

Please sign in to comment.