summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/dictionaries.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-30 09:33:13 -0400
committerGitHub <noreply@github.com>2020-05-30 09:33:13 -0400
commit63a3e56367b95f7ea64a5701d17179de60ed8718 (patch)
treefc811028b127abf9c1ff1dbdbb4663ba70b60649 /ext/bg/js/settings/dictionaries.js
parent1a5a37c9e47dc4d1f1e1b3ffaf990e792140b912 (diff)
Use SettingsController (#576)
* Use settingsController internally in settings/main.js * Replace modifyingProfileChange with SettingsController.optionsContextChanged * Update ClipboardPopupsController to use SettingsController * Store reference to checkbox * Use this._settingsController for everything * Change where current profile is initially assigned from * Remove some unnecessary async calls * Move setup calls * Update AnkiTemplatesController to use SettingsController * Cache default field templates * Update AnkiController to use SettingsController * Update AudioController to use SettingsController * Update SettingsBackup to use SettingsController * Update DictionaryController to use SettingsController * Update GenericSettingController to use SettingsController * Update ProfileController to use SettingsController * Remove unused * Remove unused * Replace some uses of api.options* functions * Fix missing awaits * Fix invalid function
Diffstat (limited to 'ext/bg/js/settings/dictionaries.js')
-rw-r--r--ext/bg/js/settings/dictionaries.js54
1 files changed, 26 insertions, 28 deletions
diff --git a/ext/bg/js/settings/dictionaries.js b/ext/bg/js/settings/dictionaries.js
index dd6dd1c1..94a71233 100644
--- a/ext/bg/js/settings/dictionaries.js
+++ b/ext/bg/js/settings/dictionaries.js
@@ -18,15 +18,12 @@
/* global
* PageExitPrevention
* api
- * getOptionsContext
- * getOptionsFullMutable
- * getOptionsMutable
- * settingsSaveOptions
* utilBackgroundIsolate
*/
-class SettingsDictionaryListUI {
+class SettingsDictionaryListUI extends EventDispatcher {
constructor(container, template, extraContainer, extraTemplate) {
+ super();
this.container = container;
this.template = template;
this.extraContainer = extraContainer;
@@ -309,7 +306,7 @@ class SettingsDictionaryEntryUI {
this.isDeleting = false;
progress.hidden = true;
- this.onDatabaseUpdated();
+ this.parent.trigger('databaseUpdated');
}
}
@@ -384,7 +381,8 @@ class SettingsDictionaryExtraUI {
}
class DictionaryController {
- constructor(storageController) {
+ constructor(settingsController, storageController) {
+ this._settingsController = settingsController;
this._storageController = storageController;
this._dictionaryUI = null;
this._dictionaryErrorToStringOverrides = [
@@ -410,7 +408,8 @@ class DictionaryController {
document.querySelector('#dict-groups-extra'),
document.querySelector('#dict-extra-template')
);
- this._dictionaryUI.save = settingsSaveOptions;
+ this._dictionaryUI.save = () => this._settingsController.save();
+ this._dictionaryUI.on('databaseUpdated', this._onDatabaseUpdated.bind(this));
document.querySelector('#dict-purge-button').addEventListener('click', this._onPurgeButtonClick.bind(this), false);
document.querySelector('#dict-purge-confirm').addEventListener('click', this._onPurgeConfirmButtonClick.bind(this), false);
@@ -419,26 +418,25 @@ class DictionaryController {
document.querySelector('#dict-main').addEventListener('change', this._onDictionaryMainChanged.bind(this), false);
document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', this._onDatabaseEnablePrefixWildcardSearchesChanged.bind(this), false);
- await this.optionsChanged();
+ this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
+
+ await this._onOptionsChanged();
await this._onDatabaseUpdated();
}
- async optionsChanged() {
- if (this._dictionaryUI === null) { return; }
+ // Private
- const optionsContext = getOptionsContext();
- const options = await getOptionsMutable(optionsContext);
+ async _onOptionsChanged() {
+ const options = await this._settingsController.getOptionsMutable();
this._dictionaryUI.setOptionsDictionaries(options.dictionaries);
- const optionsFull = await api.optionsGetFull();
+ const optionsFull = await this._settingsController.getOptionsFull();
document.querySelector('#database-enable-prefix-wildcard-searches').checked = optionsFull.global.database.prefixWildcardsSupported;
await this._updateMainDictionarySelectValue();
}
- // Private
-
_updateMainDictionarySelectOptions(dictionaries) {
const select = document.querySelector('#dict-main');
select.textContent = ''; // Empty
@@ -460,8 +458,7 @@ class DictionaryController {
}
async _updateMainDictionarySelectValue() {
- const optionsContext = getOptionsContext();
- const options = await api.optionsGet(optionsContext);
+ const options = await this._settingsController.getOptions();
const value = options.general.mainDictionary;
@@ -589,10 +586,9 @@ class DictionaryController {
missingNodeOption.parentNode.removeChild(missingNodeOption);
}
- const optionsContext = getOptionsContext();
- const options = await getOptionsMutable(optionsContext);
+ const options = await this._settingsController.getOptionsMutable();
options.general.mainDictionary = value;
- await settingsSaveOptions();
+ await this._settingsController.save();
}
_onImportButtonClick() {
@@ -622,11 +618,12 @@ class DictionaryController {
this._dictionarySpinnerShow(true);
await api.purgeDatabase();
- for (const {options} of toIterable((await getOptionsFullMutable()).profiles)) {
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ for (const {options} of toIterable(optionsFull.profiles)) {
options.dictionaries = utilBackgroundIsolate({});
options.general.mainDictionary = '';
}
- await settingsSaveOptions();
+ await this._settingsController.save();
this._onDatabaseUpdated();
} catch (err) {
@@ -665,7 +662,7 @@ class DictionaryController {
this._storageController.updateStats();
};
- const optionsFull = await api.optionsGetFull();
+ const optionsFull = await this._settingsController.getOptionsFull();
const importDetails = {
prefixWildcardsSupported: optionsFull.global.database.prefixWildcardsSupported
@@ -680,7 +677,8 @@ class DictionaryController {
const archiveContent = await this._dictReadFile(files[i]);
const {result, errors} = await api.importDictionaryArchive(archiveContent, importDetails, updateProgress);
- for (const {options} of toIterable((await getOptionsFullMutable()).profiles)) {
+ const optionsFull2 = await this._settingsController.getOptionsFullMutable();
+ for (const {options} of toIterable(optionsFull2.profiles)) {
const dictionaryOptions = SettingsDictionaryListUI.createDictionaryOptions();
dictionaryOptions.enabled = true;
options.dictionaries[result.title] = dictionaryOptions;
@@ -689,7 +687,7 @@ class DictionaryController {
}
}
- await settingsSaveOptions();
+ await this._settingsController.save();
if (errors.length > 0) {
const errors2 = errors.map((error) => jsonToError(error));
@@ -714,10 +712,10 @@ class DictionaryController {
}
async _onDatabaseEnablePrefixWildcardSearchesChanged(e) {
- const optionsFull = await getOptionsFullMutable();
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
const v = !!e.target.checked;
if (optionsFull.global.database.prefixWildcardsSupported === v) { return; }
optionsFull.global.database.prefixWildcardsSupported = !!e.target.checked;
- await settingsSaveOptions();
+ await this._settingsController.save();
}
}