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
This commit is contained in:
parent
1a5a37c9e4
commit
63a3e56367
@ -18,19 +18,20 @@
|
|||||||
/* global
|
/* global
|
||||||
* AnkiNoteBuilder
|
* AnkiNoteBuilder
|
||||||
* api
|
* api
|
||||||
* getOptionsContext
|
|
||||||
* getOptionsMutable
|
|
||||||
* settingsSaveOptions
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class AnkiTemplatesController {
|
class AnkiTemplatesController {
|
||||||
constructor(ankiController) {
|
constructor(settingsController, ankiController) {
|
||||||
|
this._settingsController = settingsController;
|
||||||
this._ankiController = ankiController;
|
this._ankiController = ankiController;
|
||||||
this._cachedDefinitionValue = null;
|
this._cachedDefinitionValue = null;
|
||||||
this._cachedDefinitionText = null;
|
this._cachedDefinitionText = null;
|
||||||
|
this._defaultFieldTemplates = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
prepare() {
|
async prepare() {
|
||||||
|
this._defaultFieldTemplates = await api.getDefaultAnkiFieldTemplates();
|
||||||
|
|
||||||
const markers = new Set([
|
const markers = new Set([
|
||||||
...this._ankiController.getFieldMarkers('terms'),
|
...this._ankiController.getFieldMarkers('terms'),
|
||||||
...this._ankiController.getFieldMarkers('kanji')
|
...this._ankiController.getFieldMarkers('kanji')
|
||||||
@ -48,21 +49,22 @@ class AnkiTemplatesController {
|
|||||||
$('#field-templates-reset').on('click', this._onReset.bind(this));
|
$('#field-templates-reset').on('click', this._onReset.bind(this));
|
||||||
$('#field-templates-reset-confirm').on('click', this._onResetConfirm.bind(this));
|
$('#field-templates-reset-confirm').on('click', this._onResetConfirm.bind(this));
|
||||||
|
|
||||||
this.updateValue();
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
||||||
|
|
||||||
|
const options = await this._settingsController.getOptions();
|
||||||
|
this._onOptionsChanged({options});
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateValue() {
|
// Private
|
||||||
const optionsContext = getOptionsContext();
|
|
||||||
const options = await api.optionsGet(optionsContext);
|
_onOptionsChanged({options}) {
|
||||||
let templates = options.anki.fieldTemplates;
|
let templates = options.anki.fieldTemplates;
|
||||||
if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
|
if (typeof templates !== 'string') { templates = this._defaultFieldTemplates; }
|
||||||
$('#field-templates').val(templates);
|
$('#field-templates').val(templates);
|
||||||
|
|
||||||
this._onValidateCompile();
|
this._onValidateCompile();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private
|
|
||||||
|
|
||||||
_onReset(e) {
|
_onReset(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
$('#field-template-reset-modal').modal('show');
|
$('#field-template-reset-modal').modal('show');
|
||||||
@ -89,10 +91,9 @@ class AnkiTemplatesController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite
|
// Overwrite
|
||||||
const optionsContext = getOptionsContext();
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
const options = await getOptionsMutable(optionsContext);
|
|
||||||
options.anki.fieldTemplates = templates;
|
options.anki.fieldTemplates = templates;
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
|
|
||||||
// Compile
|
// Compile
|
||||||
this._onValidateCompile();
|
this._onValidateCompile();
|
||||||
@ -133,10 +134,10 @@ class AnkiTemplatesController {
|
|||||||
const exceptions = [];
|
const exceptions = [];
|
||||||
let result = `No definition found for ${text}`;
|
let result = `No definition found for ${text}`;
|
||||||
try {
|
try {
|
||||||
const optionsContext = getOptionsContext();
|
const optionsContext = this._settingsController.getOptionsContext();
|
||||||
const definition = await this._getDefinition(text, optionsContext);
|
const definition = await this._getDefinition(text, optionsContext);
|
||||||
if (definition !== null) {
|
if (definition !== null) {
|
||||||
const options = await api.optionsGet(optionsContext);
|
const options = await this._settingsController.getOptions();
|
||||||
const context = {
|
const context = {
|
||||||
document: {
|
document: {
|
||||||
title: document.title
|
title: document.title
|
||||||
|
@ -17,38 +17,25 @@
|
|||||||
|
|
||||||
/* global
|
/* global
|
||||||
* api
|
* api
|
||||||
* getOptionsContext
|
|
||||||
* getOptionsMutable
|
|
||||||
* settingsSaveOptions
|
|
||||||
* utilBackgroundIsolate
|
* utilBackgroundIsolate
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class AnkiController {
|
class AnkiController {
|
||||||
prepare() {
|
constructor(settingsController) {
|
||||||
|
this._settingsController = settingsController;
|
||||||
|
}
|
||||||
|
|
||||||
|
async prepare() {
|
||||||
$('#anki-fields-container input,#anki-fields-container select,#anki-fields-container textarea').change(this._onFieldsChanged.bind(this));
|
$('#anki-fields-container input,#anki-fields-container select,#anki-fields-container textarea').change(this._onFieldsChanged.bind(this));
|
||||||
|
|
||||||
for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) {
|
for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) {
|
||||||
node.addEventListener('change', this._onModelChanged.bind(this), false);
|
node.addEventListener('change', this._onModelChanged.bind(this), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.optionsChanged();
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
||||||
}
|
|
||||||
|
|
||||||
async optionsChanged(options=null) {
|
const options = await this._settingsController.getOptions();
|
||||||
if (options === null) {
|
this._onOptionsChanged({options});
|
||||||
const optionsContext = getOptionsContext();
|
|
||||||
options = await getOptionsMutable(optionsContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!options.anki.enable) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this._deckAndModelPopulate(options);
|
|
||||||
await Promise.all([
|
|
||||||
this._fieldsPopulate('terms', options),
|
|
||||||
this._fieldsPopulate('kanji', options)
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getFieldMarkers(type) {
|
getFieldMarkers(type) {
|
||||||
@ -103,6 +90,18 @@ class AnkiController {
|
|||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
|
async _onOptionsChanged({options}) {
|
||||||
|
if (!options.anki.enable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this._deckAndModelPopulate(options);
|
||||||
|
await Promise.all([
|
||||||
|
this._fieldsPopulate('terms', options),
|
||||||
|
this._fieldsPopulate('kanji', options)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
_fieldsToDict(elements) {
|
_fieldsToDict(elements) {
|
||||||
const result = {};
|
const result = {};
|
||||||
for (const element of elements) {
|
for (const element of elements) {
|
||||||
@ -277,17 +276,15 @@ class AnkiController {
|
|||||||
fields[name] = '';
|
fields[name] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionsContext = getOptionsContext();
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
const options = await getOptionsMutable(optionsContext);
|
|
||||||
options.anki[tabId].fields = utilBackgroundIsolate(fields);
|
options.anki[tabId].fields = utilBackgroundIsolate(fields);
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
|
|
||||||
await this._fieldsPopulate(tabId, options);
|
await this._fieldsPopulate(tabId, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onFieldsChanged() {
|
async _onFieldsChanged() {
|
||||||
const optionsContext = getOptionsContext();
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
const options = await getOptionsMutable(optionsContext);
|
|
||||||
|
|
||||||
options.anki.terms.deck = $('#anki-terms-deck').val();
|
options.anki.terms.deck = $('#anki-terms-deck').val();
|
||||||
options.anki.terms.model = $('#anki-terms-model').val();
|
options.anki.terms.model = $('#anki-terms-model').val();
|
||||||
@ -296,8 +293,6 @@ class AnkiController {
|
|||||||
options.anki.kanji.model = $('#anki-kanji-model').val();
|
options.anki.kanji.model = $('#anki-kanji-model').val();
|
||||||
options.anki.kanji.fields = utilBackgroundIsolate(this._fieldsToDict(document.querySelectorAll('#kanji .anki-field-value')));
|
options.anki.kanji.fields = utilBackgroundIsolate(this._fieldsToDict(document.querySelectorAll('#kanji .anki-field-value')));
|
||||||
|
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
|
|
||||||
await this.optionsChanged(options);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,11 @@
|
|||||||
|
|
||||||
/* global
|
/* global
|
||||||
* AudioSystem
|
* AudioSystem
|
||||||
* getOptionsContext
|
|
||||||
* getOptionsMutable
|
|
||||||
* settingsSaveOptions
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class AudioController {
|
class AudioController {
|
||||||
constructor() {
|
constructor(settingsController) {
|
||||||
|
this._settingsController = settingsController;
|
||||||
this._audioSystem = null;
|
this._audioSystem = null;
|
||||||
this._settingsAudioSources = null;
|
this._settingsAudioSources = null;
|
||||||
this._audioSourceContainer = null;
|
this._audioSourceContainer = null;
|
||||||
@ -37,27 +35,36 @@ class AudioController {
|
|||||||
useCache: true
|
useCache: true
|
||||||
});
|
});
|
||||||
|
|
||||||
const optionsContext = getOptionsContext();
|
|
||||||
const options = await getOptionsMutable(optionsContext);
|
|
||||||
|
|
||||||
this._settingsAudioSources = options.audio.sources;
|
|
||||||
this._audioSourceContainer = document.querySelector('.audio-source-list');
|
this._audioSourceContainer = document.querySelector('.audio-source-list');
|
||||||
this._audioSourceAddButton = document.querySelector('.audio-source-add');
|
this._audioSourceAddButton = document.querySelector('.audio-source-add');
|
||||||
this._audioSourceContainer.textContent = '';
|
this._audioSourceContainer.textContent = '';
|
||||||
|
|
||||||
this._audioSourceAddButton.addEventListener('click', this._onAddAudioSource.bind(this), false);
|
this._audioSourceAddButton.addEventListener('click', this._onAddAudioSource.bind(this), false);
|
||||||
|
|
||||||
for (const audioSource of toIterable(this._settingsAudioSources)) {
|
|
||||||
this._createAudioSourceEntry(audioSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
this._prepareTextToSpeech();
|
this._prepareTextToSpeech();
|
||||||
|
|
||||||
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
||||||
|
|
||||||
|
this._onOptionsChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
|
async _onOptionsChanged() {
|
||||||
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
|
|
||||||
|
for (const entry of [...this._audioSourceEntries]) {
|
||||||
|
this._removeAudioSourceEntry(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._settingsAudioSources = options.audio.sources;
|
||||||
|
for (const audioSource of toIterable(this._settingsAudioSources)) {
|
||||||
|
this._createAudioSourceEntry(audioSource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async _save() {
|
async _save() {
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
_prepareTextToSpeech() {
|
_prepareTextToSpeech() {
|
||||||
|
@ -19,13 +19,11 @@
|
|||||||
* api
|
* api
|
||||||
* optionsGetDefault
|
* optionsGetDefault
|
||||||
* optionsUpdateVersion
|
* optionsUpdateVersion
|
||||||
* utilBackend
|
|
||||||
* utilBackgroundIsolate
|
|
||||||
* utilIsolate
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class SettingsBackup {
|
class SettingsBackup {
|
||||||
constructor() {
|
constructor(settingsController) {
|
||||||
|
this._settingsController = settingsController;
|
||||||
this._settingsExportToken = null;
|
this._settingsExportToken = null;
|
||||||
this._settingsExportRevoke = null;
|
this._settingsExportRevoke = null;
|
||||||
this._currentVersion = 0;
|
this._currentVersion = 0;
|
||||||
@ -59,7 +57,7 @@ class SettingsBackup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _getSettingsExportData(date) {
|
async _getSettingsExportData(date) {
|
||||||
const optionsFull = await api.optionsGetFull();
|
const optionsFull = await this._settingsController.getOptionsFull();
|
||||||
const environment = await api.getEnvironmentInfo();
|
const environment = await api.getEnvironmentInfo();
|
||||||
const fieldTemplatesDefault = await api.getDefaultAnkiFieldTemplates();
|
const fieldTemplatesDefault = await api.getDefaultAnkiFieldTemplates();
|
||||||
|
|
||||||
@ -143,9 +141,7 @@ class SettingsBackup {
|
|||||||
// Importing
|
// Importing
|
||||||
|
|
||||||
async _settingsImportSetOptionsFull(optionsFull) {
|
async _settingsImportSetOptionsFull(optionsFull) {
|
||||||
return utilIsolate(utilBackend().setFullOptions(
|
await this._settingsController.setOptionsFull(optionsFull);
|
||||||
utilBackgroundIsolate(optionsFull)
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_showSettingsImportError(error) {
|
_showSettingsImportError(error) {
|
||||||
|
@ -15,29 +15,37 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* globals
|
|
||||||
* getOptionsContext
|
|
||||||
* getOptionsMutable
|
|
||||||
* settingsSaveOptions
|
|
||||||
*/
|
|
||||||
|
|
||||||
class ClipboardPopupsController {
|
class ClipboardPopupsController {
|
||||||
prepare() {
|
constructor(settingsController) {
|
||||||
document.querySelector('#enable-clipboard-popups').addEventListener('change', this._onEnableClipboardPopupsChanged.bind(this), false);
|
this._settingsController = settingsController;
|
||||||
|
this._checkbox = document.querySelector('#enable-clipboard-popups');
|
||||||
|
}
|
||||||
|
|
||||||
|
async prepare() {
|
||||||
|
this._checkbox.addEventListener('change', this._onEnableClipboardPopupsChanged.bind(this), false);
|
||||||
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
||||||
|
|
||||||
|
const options = await this._settingsController.getOptions();
|
||||||
|
this._onOptionsChanged({options});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private
|
||||||
|
|
||||||
|
_onOptionsChanged({options}) {
|
||||||
|
this._checkbox.checked = options.general.enableClipboardPopups;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onEnableClipboardPopupsChanged(e) {
|
async _onEnableClipboardPopupsChanged(e) {
|
||||||
const optionsContext = getOptionsContext();
|
|
||||||
const options = await getOptionsMutable(optionsContext);
|
|
||||||
|
|
||||||
const enableClipboardPopups = e.target.checked;
|
const enableClipboardPopups = e.target.checked;
|
||||||
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
|
|
||||||
if (enableClipboardPopups) {
|
if (enableClipboardPopups) {
|
||||||
options.general.enableClipboardPopups = await new Promise((resolve) => {
|
options.general.enableClipboardPopups = await new Promise((resolve) => {
|
||||||
chrome.permissions.request(
|
chrome.permissions.request(
|
||||||
{permissions: ['clipboardRead']},
|
{permissions: ['clipboardRead']},
|
||||||
(granted) => {
|
(granted) => {
|
||||||
if (!granted) {
|
if (!granted) {
|
||||||
$('#enable-clipboard-popups').prop('checked', false);
|
this._checkbox.checked = false;
|
||||||
}
|
}
|
||||||
resolve(granted);
|
resolve(granted);
|
||||||
}
|
}
|
||||||
@ -47,6 +55,6 @@ class ClipboardPopupsController {
|
|||||||
options.general.enableClipboardPopups = false;
|
options.general.enableClipboardPopups = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,15 +18,12 @@
|
|||||||
/* global
|
/* global
|
||||||
* PageExitPrevention
|
* PageExitPrevention
|
||||||
* api
|
* api
|
||||||
* getOptionsContext
|
|
||||||
* getOptionsFullMutable
|
|
||||||
* getOptionsMutable
|
|
||||||
* settingsSaveOptions
|
|
||||||
* utilBackgroundIsolate
|
* utilBackgroundIsolate
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class SettingsDictionaryListUI {
|
class SettingsDictionaryListUI extends EventDispatcher {
|
||||||
constructor(container, template, extraContainer, extraTemplate) {
|
constructor(container, template, extraContainer, extraTemplate) {
|
||||||
|
super();
|
||||||
this.container = container;
|
this.container = container;
|
||||||
this.template = template;
|
this.template = template;
|
||||||
this.extraContainer = extraContainer;
|
this.extraContainer = extraContainer;
|
||||||
@ -309,7 +306,7 @@ class SettingsDictionaryEntryUI {
|
|||||||
this.isDeleting = false;
|
this.isDeleting = false;
|
||||||
progress.hidden = true;
|
progress.hidden = true;
|
||||||
|
|
||||||
this.onDatabaseUpdated();
|
this.parent.trigger('databaseUpdated');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,7 +381,8 @@ class SettingsDictionaryExtraUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DictionaryController {
|
class DictionaryController {
|
||||||
constructor(storageController) {
|
constructor(settingsController, storageController) {
|
||||||
|
this._settingsController = settingsController;
|
||||||
this._storageController = storageController;
|
this._storageController = storageController;
|
||||||
this._dictionaryUI = null;
|
this._dictionaryUI = null;
|
||||||
this._dictionaryErrorToStringOverrides = [
|
this._dictionaryErrorToStringOverrides = [
|
||||||
@ -410,7 +408,8 @@ class DictionaryController {
|
|||||||
document.querySelector('#dict-groups-extra'),
|
document.querySelector('#dict-groups-extra'),
|
||||||
document.querySelector('#dict-extra-template')
|
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-button').addEventListener('click', this._onPurgeButtonClick.bind(this), false);
|
||||||
document.querySelector('#dict-purge-confirm').addEventListener('click', this._onPurgeConfirmButtonClick.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('#dict-main').addEventListener('change', this._onDictionaryMainChanged.bind(this), false);
|
||||||
document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', this._onDatabaseEnablePrefixWildcardSearchesChanged.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();
|
await this._onDatabaseUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
async optionsChanged() {
|
// Private
|
||||||
if (this._dictionaryUI === null) { return; }
|
|
||||||
|
|
||||||
const optionsContext = getOptionsContext();
|
async _onOptionsChanged() {
|
||||||
const options = await getOptionsMutable(optionsContext);
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
|
|
||||||
this._dictionaryUI.setOptionsDictionaries(options.dictionaries);
|
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;
|
document.querySelector('#database-enable-prefix-wildcard-searches').checked = optionsFull.global.database.prefixWildcardsSupported;
|
||||||
|
|
||||||
await this._updateMainDictionarySelectValue();
|
await this._updateMainDictionarySelectValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private
|
|
||||||
|
|
||||||
_updateMainDictionarySelectOptions(dictionaries) {
|
_updateMainDictionarySelectOptions(dictionaries) {
|
||||||
const select = document.querySelector('#dict-main');
|
const select = document.querySelector('#dict-main');
|
||||||
select.textContent = ''; // Empty
|
select.textContent = ''; // Empty
|
||||||
@ -460,8 +458,7 @@ class DictionaryController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _updateMainDictionarySelectValue() {
|
async _updateMainDictionarySelectValue() {
|
||||||
const optionsContext = getOptionsContext();
|
const options = await this._settingsController.getOptions();
|
||||||
const options = await api.optionsGet(optionsContext);
|
|
||||||
|
|
||||||
const value = options.general.mainDictionary;
|
const value = options.general.mainDictionary;
|
||||||
|
|
||||||
@ -589,10 +586,9 @@ class DictionaryController {
|
|||||||
missingNodeOption.parentNode.removeChild(missingNodeOption);
|
missingNodeOption.parentNode.removeChild(missingNodeOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionsContext = getOptionsContext();
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
const options = await getOptionsMutable(optionsContext);
|
|
||||||
options.general.mainDictionary = value;
|
options.general.mainDictionary = value;
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
_onImportButtonClick() {
|
_onImportButtonClick() {
|
||||||
@ -622,11 +618,12 @@ class DictionaryController {
|
|||||||
this._dictionarySpinnerShow(true);
|
this._dictionarySpinnerShow(true);
|
||||||
|
|
||||||
await api.purgeDatabase();
|
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.dictionaries = utilBackgroundIsolate({});
|
||||||
options.general.mainDictionary = '';
|
options.general.mainDictionary = '';
|
||||||
}
|
}
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
|
|
||||||
this._onDatabaseUpdated();
|
this._onDatabaseUpdated();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -665,7 +662,7 @@ class DictionaryController {
|
|||||||
this._storageController.updateStats();
|
this._storageController.updateStats();
|
||||||
};
|
};
|
||||||
|
|
||||||
const optionsFull = await api.optionsGetFull();
|
const optionsFull = await this._settingsController.getOptionsFull();
|
||||||
|
|
||||||
const importDetails = {
|
const importDetails = {
|
||||||
prefixWildcardsSupported: optionsFull.global.database.prefixWildcardsSupported
|
prefixWildcardsSupported: optionsFull.global.database.prefixWildcardsSupported
|
||||||
@ -680,7 +677,8 @@ class DictionaryController {
|
|||||||
|
|
||||||
const archiveContent = await this._dictReadFile(files[i]);
|
const archiveContent = await this._dictReadFile(files[i]);
|
||||||
const {result, errors} = await api.importDictionaryArchive(archiveContent, importDetails, updateProgress);
|
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();
|
const dictionaryOptions = SettingsDictionaryListUI.createDictionaryOptions();
|
||||||
dictionaryOptions.enabled = true;
|
dictionaryOptions.enabled = true;
|
||||||
options.dictionaries[result.title] = dictionaryOptions;
|
options.dictionaries[result.title] = dictionaryOptions;
|
||||||
@ -689,7 +687,7 @@ class DictionaryController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
|
|
||||||
if (errors.length > 0) {
|
if (errors.length > 0) {
|
||||||
const errors2 = errors.map((error) => jsonToError(error));
|
const errors2 = errors.map((error) => jsonToError(error));
|
||||||
@ -714,10 +712,10 @@ class DictionaryController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _onDatabaseEnablePrefixWildcardSearchesChanged(e) {
|
async _onDatabaseEnablePrefixWildcardSearchesChanged(e) {
|
||||||
const optionsFull = await getOptionsFullMutable();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
const v = !!e.target.checked;
|
const v = !!e.target.checked;
|
||||||
if (optionsFull.global.database.prefixWildcardsSupported === v) { return; }
|
if (optionsFull.global.database.prefixWildcardsSupported === v) { return; }
|
||||||
optionsFull.global.database.prefixWildcardsSupported = !!e.target.checked;
|
optionsFull.global.database.prefixWildcardsSupported = !!e.target.checked;
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
/* global
|
/* global
|
||||||
* DOMDataBinder
|
* DOMDataBinder
|
||||||
* api
|
* api
|
||||||
* getOptionsContext
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class DOMSettingsBinder {
|
class DOMSettingsBinder {
|
||||||
|
@ -16,24 +16,26 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* globals
|
/* globals
|
||||||
* getOptionsContext
|
|
||||||
* getOptionsMutable
|
|
||||||
* settingsSaveOptions
|
|
||||||
* utilBackgroundIsolate
|
* utilBackgroundIsolate
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class GenericSettingController {
|
class GenericSettingController {
|
||||||
prepare() {
|
constructor(settingsController) {
|
||||||
$('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(this._onFormOptionsChanged.bind(this));
|
this._settingsController = settingsController;
|
||||||
}
|
}
|
||||||
|
|
||||||
optionsChanged(options) {
|
async prepare() {
|
||||||
this._formWrite(options);
|
$('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(this._onFormOptionsChanged.bind(this));
|
||||||
|
|
||||||
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
||||||
|
|
||||||
|
const options = await this._settingsController.getOptions();
|
||||||
|
this._onOptionsChanged({options});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
async _formWrite(options) {
|
_onOptionsChanged({options}) {
|
||||||
$('#enable').prop('checked', options.general.enable);
|
$('#enable').prop('checked', options.general.enable);
|
||||||
$('#show-usage-guide').prop('checked', options.general.showGuide);
|
$('#show-usage-guide').prop('checked', options.general.showGuide);
|
||||||
$('#compact-tags').prop('checked', options.general.compactTags);
|
$('#compact-tags').prop('checked', options.general.compactTags);
|
||||||
@ -107,7 +109,7 @@ class GenericSettingController {
|
|||||||
this._formUpdateVisibility(options);
|
this._formUpdateVisibility(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _formRead(options) {
|
_formRead(options) {
|
||||||
options.general.enable = $('#enable').prop('checked');
|
options.general.enable = $('#enable').prop('checked');
|
||||||
options.general.showGuide = $('#show-usage-guide').prop('checked');
|
options.general.showGuide = $('#show-usage-guide').prop('checked');
|
||||||
options.general.compactTags = $('#compact-tags').prop('checked');
|
options.general.compactTags = $('#compact-tags').prop('checked');
|
||||||
@ -180,12 +182,10 @@ class GenericSettingController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _onFormOptionsChanged() {
|
async _onFormOptionsChanged() {
|
||||||
const optionsContext = getOptionsContext();
|
const options = await this._settingsController.getOptionsMutable();
|
||||||
const options = await getOptionsMutable(optionsContext);
|
this._formRead(options);
|
||||||
|
|
||||||
await this._formRead(options);
|
|
||||||
await settingsSaveOptions();
|
|
||||||
this._formUpdateVisibility(options);
|
this._formUpdateVisibility(options);
|
||||||
|
await this._settingsController.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
_formUpdateVisibility(options) {
|
_formUpdateVisibility(options) {
|
||||||
|
@ -28,71 +28,8 @@
|
|||||||
* SettingsController
|
* SettingsController
|
||||||
* StorageController
|
* StorageController
|
||||||
* api
|
* api
|
||||||
* utilBackend
|
|
||||||
* utilBackgroundIsolate
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let profileIndex = 0;
|
|
||||||
|
|
||||||
function getOptionsContext() {
|
|
||||||
return {index: getProfileIndex()};
|
|
||||||
}
|
|
||||||
|
|
||||||
function getProfileIndex() {
|
|
||||||
return profileIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setProfileIndex(value) {
|
|
||||||
profileIndex = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function getOptionsMutable(optionsContext) {
|
|
||||||
return utilBackend().getOptions(
|
|
||||||
utilBackgroundIsolate(optionsContext)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getOptionsFullMutable() {
|
|
||||||
return utilBackend().getFullOptions();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function settingsGetSource() {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
chrome.tabs.getCurrent((tab) => resolve(`settings${tab ? tab.id : ''}`));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function settingsSaveOptions() {
|
|
||||||
const source = await settingsGetSource();
|
|
||||||
await api.optionsSave(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function onOptionsUpdated({source}) {
|
|
||||||
const thisSource = await settingsGetSource();
|
|
||||||
if (source === thisSource) { return; }
|
|
||||||
|
|
||||||
const optionsContext = getOptionsContext();
|
|
||||||
const options = await getOptionsMutable(optionsContext);
|
|
||||||
|
|
||||||
document.querySelector('#enable-clipboard-popups').checked = options.general.enableClipboardPopups;
|
|
||||||
if (ankiTemplatesController !== null) {
|
|
||||||
ankiTemplatesController.updateValue();
|
|
||||||
}
|
|
||||||
if (dictionaryController !== null) {
|
|
||||||
dictionaryController.optionsChanged();
|
|
||||||
}
|
|
||||||
if (ankiController !== null) {
|
|
||||||
ankiController.optionsChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (genericSettingController !== null) {
|
|
||||||
genericSettingController.optionsChanged(options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function showExtensionInformation() {
|
function showExtensionInformation() {
|
||||||
const node = document.getElementById('extension-info');
|
const node = document.getElementById('extension-info');
|
||||||
if (node === null) { return; }
|
if (node === null) { return; }
|
||||||
@ -124,40 +61,34 @@ async function setupEnvironmentInfo() {
|
|||||||
document.documentElement.dataset.operatingSystem = platform.os;
|
document.documentElement.dataset.operatingSystem = platform.os;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ankiController = null;
|
|
||||||
let ankiTemplatesController = null;
|
|
||||||
let dictionaryController = null;
|
|
||||||
let genericSettingController = null;
|
|
||||||
|
|
||||||
async function onReady() {
|
async function onReady() {
|
||||||
api.forwardLogsToBackend();
|
api.forwardLogsToBackend();
|
||||||
await yomichan.prepare();
|
await yomichan.prepare();
|
||||||
|
|
||||||
const settingsController = new SettingsController();
|
|
||||||
settingsController.prepare();
|
|
||||||
|
|
||||||
setupEnvironmentInfo();
|
setupEnvironmentInfo();
|
||||||
showExtensionInformation();
|
showExtensionInformation();
|
||||||
|
settingsPopulateModifierKeys();
|
||||||
|
|
||||||
|
const optionsFull = await api.optionsGetFull();
|
||||||
|
const settingsController = new SettingsController(optionsFull.profileCurrent);
|
||||||
|
settingsController.prepare();
|
||||||
|
|
||||||
const storageController = new StorageController();
|
const storageController = new StorageController();
|
||||||
storageController.prepare();
|
storageController.prepare();
|
||||||
|
|
||||||
await settingsPopulateModifierKeys();
|
const genericSettingController = new GenericSettingController(settingsController);
|
||||||
genericSettingController = new GenericSettingController();
|
|
||||||
genericSettingController.prepare();
|
genericSettingController.prepare();
|
||||||
new ClipboardPopupsController().prepare();
|
new ClipboardPopupsController(settingsController).prepare();
|
||||||
new PopupPreviewController().prepare();
|
new PopupPreviewController(settingsController).prepare();
|
||||||
new AudioController().prepare();
|
new AudioController(settingsController).prepare();
|
||||||
await (new ProfileController()).prepare();
|
new ProfileController(settingsController).prepare();
|
||||||
dictionaryController = new DictionaryController(storageController);
|
const dictionaryController = new DictionaryController(settingsController, storageController);
|
||||||
dictionaryController.prepare();
|
dictionaryController.prepare();
|
||||||
ankiController = new AnkiController();
|
const ankiController = new AnkiController(settingsController);
|
||||||
ankiController.prepare();
|
ankiController.prepare();
|
||||||
ankiTemplatesController = new AnkiTemplatesController(ankiController);
|
new AnkiTemplatesController(settingsController, ankiController).prepare();
|
||||||
ankiTemplatesController.prepare();
|
new SettingsBackup(settingsController).prepare();
|
||||||
new SettingsBackup().prepare();
|
|
||||||
|
|
||||||
yomichan.on('optionsUpdated', onOptionsUpdated);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$(document).ready(() => onReady());
|
$(document).ready(() => onReady());
|
||||||
|
@ -16,12 +16,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* global
|
/* global
|
||||||
* getOptionsContext
|
|
||||||
* wanakana
|
* wanakana
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class PopupPreviewController {
|
class PopupPreviewController {
|
||||||
constructor() {
|
constructor(settingsController) {
|
||||||
|
this._settingsController = settingsController;
|
||||||
this._previewVisible = false;
|
this._previewVisible = false;
|
||||||
this._targetOrigin = chrome.runtime.getURL('/').replace(/\/$/, '');
|
this._targetOrigin = chrome.runtime.getURL('/').replace(/\/$/, '');
|
||||||
this._frame = null;
|
this._frame = null;
|
||||||
@ -58,7 +58,7 @@ class PopupPreviewController {
|
|||||||
text.addEventListener('input', this._onTextChange.bind(this), false);
|
text.addEventListener('input', this._onTextChange.bind(this), false);
|
||||||
customCss.addEventListener('input', this._onCustomCssChange.bind(this), false);
|
customCss.addEventListener('input', this._onCustomCssChange.bind(this), false);
|
||||||
customOuterCss.addEventListener('input', this._onCustomOuterCssChange.bind(this), false);
|
customOuterCss.addEventListener('input', this._onCustomOuterCssChange.bind(this), false);
|
||||||
yomichan.on('modifyingProfileChange', this._onOptionsContextChange.bind(this));
|
this._settingsController.on('optionsContextChanged', this._onOptionsContextChange.bind(this));
|
||||||
|
|
||||||
frame.src = '/bg/settings-popup-preview.html';
|
frame.src = '/bg/settings-popup-preview.html';
|
||||||
frame.id = 'settings-popup-preview-frame';
|
frame.id = 'settings-popup-preview-frame';
|
||||||
@ -88,7 +88,8 @@ class PopupPreviewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_onOptionsContextChange() {
|
_onOptionsContextChange() {
|
||||||
this._invoke('updateOptionsContext', {optionsContext: getOptionsContext()});
|
const optionsContext = this._settingsController.getOptionsContext();
|
||||||
|
this._invoke('updateOptionsContext', {optionsContext});
|
||||||
}
|
}
|
||||||
|
|
||||||
_setText(text) {
|
_setText(text) {
|
||||||
|
@ -17,34 +17,19 @@
|
|||||||
|
|
||||||
/* global
|
/* global
|
||||||
* ConditionsUI
|
* ConditionsUI
|
||||||
* api
|
|
||||||
* conditionsClearCaches
|
* conditionsClearCaches
|
||||||
* getOptionsFullMutable
|
|
||||||
* getProfileIndex
|
|
||||||
* onOptionsUpdated
|
|
||||||
* profileConditionsDescriptor
|
* profileConditionsDescriptor
|
||||||
* profileConditionsDescriptorPromise
|
* profileConditionsDescriptorPromise
|
||||||
* setProfileIndex
|
|
||||||
* settingsSaveOptions
|
|
||||||
* utilBackgroundIsolate
|
* utilBackgroundIsolate
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class ProfileController {
|
class ProfileController {
|
||||||
constructor() {
|
constructor(settingsController) {
|
||||||
|
this._settingsController = settingsController;
|
||||||
this._conditionsContainer = null;
|
this._conditionsContainer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
const optionsFull = await getOptionsFullMutable();
|
|
||||||
setProfileIndex(optionsFull.profileCurrent);
|
|
||||||
|
|
||||||
this._setupEventListeners();
|
|
||||||
await this._updateTarget(optionsFull);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Private
|
|
||||||
|
|
||||||
_setupEventListeners() {
|
|
||||||
$('#profile-target').change(this._onTargetProfileChanged.bind(this));
|
$('#profile-target').change(this._onTargetProfileChanged.bind(this));
|
||||||
$('#profile-name').change(this._onNameChanged.bind(this));
|
$('#profile-name').change(this._onNameChanged.bind(this));
|
||||||
$('#profile-add').click(this._onAdd.bind(this));
|
$('#profile-add').click(this._onAdd.bind(this));
|
||||||
@ -55,6 +40,17 @@ class ProfileController {
|
|||||||
$('#profile-move-up').click(() => this._onMove(-1));
|
$('#profile-move-up').click(() => this._onMove(-1));
|
||||||
$('#profile-move-down').click(() => this._onMove(1));
|
$('#profile-move-down').click(() => this._onMove(1));
|
||||||
$('.profile-form').find('input, select, textarea').not('.profile-form-manual').change(this._onInputChanged.bind(this));
|
$('.profile-form').find('input, select, textarea').not('.profile-form-manual').change(this._onInputChanged.bind(this));
|
||||||
|
|
||||||
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
||||||
|
|
||||||
|
this._onOptionsChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private
|
||||||
|
|
||||||
|
async _onOptionsChanged() {
|
||||||
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
|
await this._formWrite(optionsFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
_tryGetIntegerValue(selector, min, max) {
|
_tryGetIntegerValue(selector, min, max) {
|
||||||
@ -69,7 +65,7 @@ class ProfileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _formRead(optionsFull) {
|
async _formRead(optionsFull) {
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
const profile = optionsFull.profiles[currentProfileIndex];
|
const profile = optionsFull.profiles[currentProfileIndex];
|
||||||
|
|
||||||
// Current profile
|
// Current profile
|
||||||
@ -83,7 +79,7 @@ class ProfileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _formWrite(optionsFull) {
|
async _formWrite(optionsFull) {
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
const profile = optionsFull.profiles[currentProfileIndex];
|
const profile = optionsFull.profiles[currentProfileIndex];
|
||||||
|
|
||||||
this._populateSelect($('#profile-active'), optionsFull.profiles, optionsFull.profileCurrent, null);
|
this._populateSelect($('#profile-active'), optionsFull.profiles, optionsFull.profileCurrent, null);
|
||||||
@ -108,7 +104,7 @@ class ProfileController {
|
|||||||
$('#profile-add-condition-group')
|
$('#profile-add-condition-group')
|
||||||
);
|
);
|
||||||
this._conditionsContainer.save = () => {
|
this._conditionsContainer.save = () => {
|
||||||
settingsSaveOptions();
|
this._settingsController.save();
|
||||||
conditionsClearCaches(profileConditionsDescriptor);
|
conditionsClearCaches(profileConditionsDescriptor);
|
||||||
};
|
};
|
||||||
this._conditionsContainer.isolate = utilBackgroundIsolate;
|
this._conditionsContainer.isolate = utilBackgroundIsolate;
|
||||||
@ -129,11 +125,6 @@ class ProfileController {
|
|||||||
select.val(`${currentValue}`);
|
select.val(`${currentValue}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _updateTarget(optionsFull) {
|
|
||||||
await this._formWrite(optionsFull);
|
|
||||||
await onOptionsUpdated({source: null});
|
|
||||||
}
|
|
||||||
|
|
||||||
_createCopyName(name, profiles, maxUniqueAttempts) {
|
_createCopyName(name, profiles, maxUniqueAttempts) {
|
||||||
let space, index, prefix, suffix;
|
let space, index, prefix, suffix;
|
||||||
const match = /^([\w\W]*\(Copy)((\s+)(\d+))?(\)\s*)$/.exec(name);
|
const match = /^([\w\W]*\(Copy)((\s+)(\d+))?(\)\s*)$/.exec(name);
|
||||||
@ -174,39 +165,32 @@ class ProfileController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionsFull = await getOptionsFullMutable();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
await this._formRead(optionsFull);
|
await this._formRead(optionsFull);
|
||||||
await settingsSaveOptions();
|
await this._settingsController.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onTargetProfileChanged() {
|
async _onTargetProfileChanged() {
|
||||||
const optionsFull = await getOptionsFullMutable();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
const index = this._tryGetIntegerValue('#profile-target', 0, optionsFull.profiles.length);
|
const index = this._tryGetIntegerValue('#profile-target', 0, optionsFull.profiles.length);
|
||||||
if (index === null || currentProfileIndex === index) {
|
if (index === null || currentProfileIndex === index) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setProfileIndex(index);
|
this._settingsController.profileIndex = index;
|
||||||
|
|
||||||
await this._updateTarget(optionsFull);
|
|
||||||
|
|
||||||
yomichan.trigger('modifyingProfileChange');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onAdd() {
|
async _onAdd() {
|
||||||
const optionsFull = await getOptionsFullMutable();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
const profile = utilBackgroundIsolate(optionsFull.profiles[currentProfileIndex]);
|
const profile = utilBackgroundIsolate(optionsFull.profiles[currentProfileIndex]);
|
||||||
profile.name = this._createCopyName(profile.name, optionsFull.profiles, 100);
|
profile.name = this._createCopyName(profile.name, optionsFull.profiles, 100);
|
||||||
optionsFull.profiles.push(profile);
|
optionsFull.profiles.push(profile);
|
||||||
|
|
||||||
setProfileIndex(optionsFull.profiles.length - 1);
|
this._settingsController.profileIndex = optionsFull.profiles.length - 1;
|
||||||
|
|
||||||
await this._updateTarget(optionsFull);
|
await this._settingsController.save();
|
||||||
await settingsSaveOptions();
|
|
||||||
|
|
||||||
yomichan.trigger('modifyingProfileChange');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onRemove(e) {
|
async _onRemove(e) {
|
||||||
@ -214,12 +198,12 @@ class ProfileController {
|
|||||||
return await this._onRemoveConfirm();
|
return await this._onRemoveConfirm();
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionsFull = await api.optionsGetFull();
|
const optionsFull = await this._settingsController.getOptionsFull();
|
||||||
if (optionsFull.profiles.length <= 1) {
|
if (optionsFull.profiles.length <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
const profile = optionsFull.profiles[currentProfileIndex];
|
const profile = optionsFull.profiles[currentProfileIndex];
|
||||||
|
|
||||||
$('#profile-remove-modal-profile-name').text(profile.name);
|
$('#profile-remove-modal-profile-name').text(profile.name);
|
||||||
@ -229,36 +213,33 @@ class ProfileController {
|
|||||||
async _onRemoveConfirm() {
|
async _onRemoveConfirm() {
|
||||||
$('#profile-remove-modal').modal('hide');
|
$('#profile-remove-modal').modal('hide');
|
||||||
|
|
||||||
const optionsFull = await getOptionsFullMutable();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
if (optionsFull.profiles.length <= 1) {
|
if (optionsFull.profiles.length <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
optionsFull.profiles.splice(currentProfileIndex, 1);
|
optionsFull.profiles.splice(currentProfileIndex, 1);
|
||||||
|
|
||||||
if (currentProfileIndex >= optionsFull.profiles.length) {
|
if (currentProfileIndex >= optionsFull.profiles.length) {
|
||||||
setProfileIndex(optionsFull.profiles.length - 1);
|
this._settingsController.profileIndex = optionsFull.profiles.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optionsFull.profileCurrent >= optionsFull.profiles.length) {
|
if (optionsFull.profileCurrent >= optionsFull.profiles.length) {
|
||||||
optionsFull.profileCurrent = optionsFull.profiles.length - 1;
|
optionsFull.profileCurrent = optionsFull.profiles.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this._updateTarget(optionsFull);
|
await this._settingsController.save();
|
||||||
await settingsSaveOptions();
|
|
||||||
|
|
||||||
yomichan.trigger('modifyingProfileChange');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_onNameChanged() {
|
_onNameChanged() {
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
$('#profile-active, #profile-target').find(`[value="${currentProfileIndex}"]`).text(this.value);
|
$('#profile-active, #profile-target').find(`[value="${currentProfileIndex}"]`).text(this.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onMove(offset) {
|
async _onMove(offset) {
|
||||||
const optionsFull = await getOptionsFullMutable();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
const index = currentProfileIndex + offset;
|
const index = currentProfileIndex + offset;
|
||||||
if (index < 0 || index >= optionsFull.profiles.length) {
|
if (index < 0 || index >= optionsFull.profiles.length) {
|
||||||
return;
|
return;
|
||||||
@ -272,21 +253,18 @@ class ProfileController {
|
|||||||
optionsFull.profileCurrent = index;
|
optionsFull.profileCurrent = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
setProfileIndex(index);
|
this._settingsController.profileIndex = index;
|
||||||
|
|
||||||
await this._updateTarget(optionsFull);
|
await this._settingsController.save();
|
||||||
await settingsSaveOptions();
|
|
||||||
|
|
||||||
yomichan.trigger('modifyingProfileChange');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onCopy() {
|
async _onCopy() {
|
||||||
const optionsFull = await api.optionsGetFull();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
if (optionsFull.profiles.length <= 1) {
|
if (optionsFull.profiles.length <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
this._populateSelect($('#profile-copy-source'), optionsFull.profiles, currentProfileIndex === 0 ? 1 : 0, [currentProfileIndex]);
|
this._populateSelect($('#profile-copy-source'), optionsFull.profiles, currentProfileIndex === 0 ? 1 : 0, [currentProfileIndex]);
|
||||||
$('#profile-copy-modal').modal('show');
|
$('#profile-copy-modal').modal('show');
|
||||||
}
|
}
|
||||||
@ -294,9 +272,9 @@ class ProfileController {
|
|||||||
async _onCopyConfirm() {
|
async _onCopyConfirm() {
|
||||||
$('#profile-copy-modal').modal('hide');
|
$('#profile-copy-modal').modal('hide');
|
||||||
|
|
||||||
const optionsFull = await getOptionsFullMutable();
|
const optionsFull = await this._settingsController.getOptionsFullMutable();
|
||||||
const index = this._tryGetIntegerValue('#profile-copy-source', 0, optionsFull.profiles.length);
|
const index = this._tryGetIntegerValue('#profile-copy-source', 0, optionsFull.profiles.length);
|
||||||
const currentProfileIndex = getProfileIndex();
|
const currentProfileIndex = this._settingsController.profileIndex;
|
||||||
if (index === null || index === currentProfileIndex) {
|
if (index === null || index === currentProfileIndex) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -304,7 +282,6 @@ class ProfileController {
|
|||||||
const profileOptions = utilBackgroundIsolate(optionsFull.profiles[index].options);
|
const profileOptions = utilBackgroundIsolate(optionsFull.profiles[index].options);
|
||||||
optionsFull.profiles[currentProfileIndex].options = profileOptions;
|
optionsFull.profiles[currentProfileIndex].options = profileOptions;
|
||||||
|
|
||||||
await this._updateTarget(optionsFull);
|
await this._settingsController.save();
|
||||||
await settingsSaveOptions();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,11 @@ class SettingsController extends EventDispatcher {
|
|||||||
return utilBackend().getFullOptions();
|
return utilBackend().getFullOptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async setOptionsFull(optionsFull) {
|
||||||
|
utilBackend().setFullOptions(utilBackgroundIsolate(optionsFull));
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
getOptionsContext() {
|
getOptionsContext() {
|
||||||
return {index: this._profileIndex};
|
return {index: this._profileIndex};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user