aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/profiles.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/settings/profiles.js')
-rw-r--r--ext/bg/js/settings/profiles.js419
1 files changed, 201 insertions, 218 deletions
diff --git a/ext/bg/js/settings/profiles.js b/ext/bg/js/settings/profiles.js
index bdf5a13d..2449ab44 100644
--- a/ext/bg/js/settings/profiles.js
+++ b/ext/bg/js/settings/profiles.js
@@ -17,288 +17,271 @@
/* global
* ConditionsUI
- * apiOptionsGetFull
* conditionsClearCaches
- * formWrite
- * getOptionsFullMutable
- * getOptionsMutable
* profileConditionsDescriptor
* profileConditionsDescriptorPromise
- * settingsSaveOptions
* utilBackgroundIsolate
*/
-let currentProfileIndex = 0;
-let profileConditionsContainer = null;
-
-function getOptionsContext() {
- return {
- index: currentProfileIndex
- };
-}
-
-
-async function profileOptionsSetup() {
- const optionsFull = await getOptionsFullMutable();
- currentProfileIndex = optionsFull.profileCurrent;
-
- profileOptionsSetupEventListeners();
- await profileOptionsUpdateTarget(optionsFull);
-}
-
-function profileOptionsSetupEventListeners() {
- $('#profile-target').change(onTargetProfileChanged);
- $('#profile-name').change(onProfileNameChanged);
- $('#profile-add').click(onProfileAdd);
- $('#profile-remove').click(onProfileRemove);
- $('#profile-remove-confirm').click(onProfileRemoveConfirm);
- $('#profile-copy').click(onProfileCopy);
- $('#profile-copy-confirm').click(onProfileCopyConfirm);
- $('#profile-move-up').click(() => onProfileMove(-1));
- $('#profile-move-down').click(() => onProfileMove(1));
- $('.profile-form').find('input, select, textarea').not('.profile-form-manual').change(onProfileOptionsChanged);
-}
+class ProfileController {
+ constructor(settingsController) {
+ this._settingsController = settingsController;
+ this._conditionsContainer = null;
+ }
-function tryGetIntegerValue(selector, min, max) {
- const value = parseInt($(selector).val(), 10);
- return (
- typeof value === 'number' &&
- Number.isFinite(value) &&
- Math.floor(value) === value &&
- value >= min &&
- value < max
- ) ? value : null;
-}
+ async prepare() {
+ $('#profile-target').change(this._onTargetProfileChanged.bind(this));
+ $('#profile-name').change(this._onNameChanged.bind(this));
+ $('#profile-add').click(this._onAdd.bind(this));
+ $('#profile-remove').click(this._onRemove.bind(this));
+ $('#profile-remove-confirm').click(this._onRemoveConfirm.bind(this));
+ $('#profile-copy').click(this._onCopy.bind(this));
+ $('#profile-copy-confirm').click(this._onCopyConfirm.bind(this));
+ $('#profile-move-up').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));
+
+ this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
+
+ this._onOptionsChanged();
+ }
-async function profileFormRead(optionsFull) {
- const profile = optionsFull.profiles[currentProfileIndex];
+ // Private
- // Current profile
- const index = tryGetIntegerValue('#profile-active', 0, optionsFull.profiles.length);
- if (index !== null) {
- optionsFull.profileCurrent = index;
+ async _onOptionsChanged() {
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ await this._formWrite(optionsFull);
}
- // Profile name
- profile.name = $('#profile-name').val();
-}
-
-async function profileFormWrite(optionsFull) {
- const profile = optionsFull.profiles[currentProfileIndex];
+ _tryGetIntegerValue(selector, min, max) {
+ const value = parseInt($(selector).val(), 10);
+ return (
+ typeof value === 'number' &&
+ Number.isFinite(value) &&
+ Math.floor(value) === value &&
+ value >= min &&
+ value < max
+ ) ? value : null;
+ }
- profileOptionsPopulateSelect($('#profile-active'), optionsFull.profiles, optionsFull.profileCurrent, null);
- profileOptionsPopulateSelect($('#profile-target'), optionsFull.profiles, currentProfileIndex, null);
- $('#profile-remove').prop('disabled', optionsFull.profiles.length <= 1);
- $('#profile-copy').prop('disabled', optionsFull.profiles.length <= 1);
- $('#profile-move-up').prop('disabled', currentProfileIndex <= 0);
- $('#profile-move-down').prop('disabled', currentProfileIndex >= optionsFull.profiles.length - 1);
+ async _formRead(optionsFull) {
+ const currentProfileIndex = this._settingsController.profileIndex;
+ const profile = optionsFull.profiles[currentProfileIndex];
- $('#profile-name').val(profile.name);
+ // Current profile
+ const index = this._tryGetIntegerValue('#profile-active', 0, optionsFull.profiles.length);
+ if (index !== null) {
+ optionsFull.profileCurrent = index;
+ }
- if (profileConditionsContainer !== null) {
- profileConditionsContainer.cleanup();
+ // Profile name
+ profile.name = $('#profile-name').val();
}
- await profileConditionsDescriptorPromise;
- profileConditionsContainer = new ConditionsUI.Container(
- profileConditionsDescriptor,
- 'popupLevel',
- profile.conditionGroups,
- $('#profile-condition-groups'),
- $('#profile-add-condition-group')
- );
- profileConditionsContainer.save = () => {
- settingsSaveOptions();
- conditionsClearCaches(profileConditionsDescriptor);
- };
- profileConditionsContainer.isolate = utilBackgroundIsolate;
-}
+ async _formWrite(optionsFull) {
+ const currentProfileIndex = this._settingsController.profileIndex;
+ const profile = optionsFull.profiles[currentProfileIndex];
-function profileOptionsPopulateSelect(select, profiles, currentValue, ignoreIndices) {
- select.empty();
+ this._populateSelect($('#profile-active'), optionsFull.profiles, optionsFull.profileCurrent, null);
+ this._populateSelect($('#profile-target'), optionsFull.profiles, currentProfileIndex, null);
+ $('#profile-remove').prop('disabled', optionsFull.profiles.length <= 1);
+ $('#profile-copy').prop('disabled', optionsFull.profiles.length <= 1);
+ $('#profile-move-up').prop('disabled', currentProfileIndex <= 0);
+ $('#profile-move-down').prop('disabled', currentProfileIndex >= optionsFull.profiles.length - 1);
+ $('#profile-name').val(profile.name);
- for (let i = 0; i < profiles.length; ++i) {
- if (ignoreIndices !== null && ignoreIndices.indexOf(i) >= 0) {
- continue;
+ if (this._conditionsContainer !== null) {
+ this._conditionsContainer.cleanup();
}
- const profile = profiles[i];
- select.append($(`<option value="${i}">${profile.name}</option>`));
- }
- select.val(`${currentValue}`);
-}
+ await profileConditionsDescriptorPromise;
+ this._conditionsContainer = new ConditionsUI.Container(
+ profileConditionsDescriptor,
+ 'popupLevel',
+ profile.conditionGroups,
+ $('#profile-condition-groups'),
+ $('#profile-add-condition-group')
+ );
+ this._conditionsContainer.save = () => {
+ this._settingsController.save();
+ conditionsClearCaches(profileConditionsDescriptor);
+ };
+ this._conditionsContainer.isolate = utilBackgroundIsolate;
+ }
-async function profileOptionsUpdateTarget(optionsFull) {
- await profileFormWrite(optionsFull);
+ _populateSelect(select, profiles, currentValue, ignoreIndices) {
+ select.empty();
- const optionsContext = getOptionsContext();
- const options = await getOptionsMutable(optionsContext);
- await formWrite(options);
-}
-function profileOptionsCreateCopyName(name, profiles, maxUniqueAttempts) {
- let space, index, prefix, suffix;
- const match = /^([\w\W]*\(Copy)((\s+)(\d+))?(\)\s*)$/.exec(name);
- if (match === null) {
- prefix = `${name} (Copy`;
- space = '';
- index = '';
- suffix = ')';
- } else {
- prefix = match[1];
- suffix = match[5];
- if (typeof match[2] === 'string') {
- space = match[3];
- index = parseInt(match[4], 10) + 1;
- } else {
- space = ' ';
- index = 2;
+ for (let i = 0; i < profiles.length; ++i) {
+ if (ignoreIndices !== null && ignoreIndices.indexOf(i) >= 0) {
+ continue;
+ }
+ const profile = profiles[i];
+ select.append($(`<option value="${i}">${profile.name}</option>`));
}
+
+ select.val(`${currentValue}`);
}
- let i = 0;
- while (true) {
- const newName = `${prefix}${space}${index}${suffix}`;
- if (i++ >= maxUniqueAttempts || profiles.findIndex((profile) => profile.name === newName) < 0) {
- return newName;
- }
- if (typeof index !== 'number') {
- index = 2;
- space = ' ';
+ _createCopyName(name, profiles, maxUniqueAttempts) {
+ let space, index, prefix, suffix;
+ const match = /^([\w\W]*\(Copy)((\s+)(\d+))?(\)\s*)$/.exec(name);
+ if (match === null) {
+ prefix = `${name} (Copy`;
+ space = '';
+ index = '';
+ suffix = ')';
} else {
- ++index;
+ prefix = match[1];
+ suffix = match[5];
+ if (typeof match[2] === 'string') {
+ space = match[3];
+ index = parseInt(match[4], 10) + 1;
+ } else {
+ space = ' ';
+ index = 2;
+ }
}
- }
-}
-async function onProfileOptionsChanged(e) {
- if (!e.originalEvent && !e.isTrigger) {
- return;
+ let i = 0;
+ while (true) {
+ const newName = `${prefix}${space}${index}${suffix}`;
+ if (i++ >= maxUniqueAttempts || profiles.findIndex((profile) => profile.name === newName) < 0) {
+ return newName;
+ }
+ if (typeof index !== 'number') {
+ index = 2;
+ space = ' ';
+ } else {
+ ++index;
+ }
+ }
}
- const optionsFull = await getOptionsFullMutable();
- await profileFormRead(optionsFull);
- await settingsSaveOptions();
-}
+ async _onInputChanged(e) {
+ if (!e.originalEvent && !e.isTrigger) {
+ return;
+ }
-async function onTargetProfileChanged() {
- const optionsFull = await getOptionsFullMutable();
- const index = tryGetIntegerValue('#profile-target', 0, optionsFull.profiles.length);
- if (index === null || currentProfileIndex === index) {
- return;
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ await this._formRead(optionsFull);
+ await this._settingsController.save();
}
- currentProfileIndex = index;
+ async _onTargetProfileChanged() {
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ const currentProfileIndex = this._settingsController.profileIndex;
+ const index = this._tryGetIntegerValue('#profile-target', 0, optionsFull.profiles.length);
+ if (index === null || currentProfileIndex === index) {
+ return;
+ }
- await profileOptionsUpdateTarget(optionsFull);
+ this._settingsController.profileIndex = index;
+ }
- yomichan.trigger('modifyingProfileChange');
-}
+ async _onAdd() {
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ const currentProfileIndex = this._settingsController.profileIndex;
+ const profile = utilBackgroundIsolate(optionsFull.profiles[currentProfileIndex]);
+ profile.name = this._createCopyName(profile.name, optionsFull.profiles, 100);
+ optionsFull.profiles.push(profile);
-async function onProfileAdd() {
- const optionsFull = await getOptionsFullMutable();
- const profile = utilBackgroundIsolate(optionsFull.profiles[currentProfileIndex]);
- profile.name = profileOptionsCreateCopyName(profile.name, optionsFull.profiles, 100);
- optionsFull.profiles.push(profile);
+ this._settingsController.profileIndex = optionsFull.profiles.length - 1;
- currentProfileIndex = optionsFull.profiles.length - 1;
+ await this._settingsController.save();
+ }
- await profileOptionsUpdateTarget(optionsFull);
- await settingsSaveOptions();
+ async _onRemove(e) {
+ if (e.shiftKey) {
+ return await this._onRemoveConfirm();
+ }
- yomichan.trigger('modifyingProfileChange');
-}
+ const optionsFull = await this._settingsController.getOptionsFull();
+ if (optionsFull.profiles.length <= 1) {
+ return;
+ }
-async function onProfileRemove(e) {
- if (e.shiftKey) {
- return await onProfileRemoveConfirm();
- }
+ const currentProfileIndex = this._settingsController.profileIndex;
+ const profile = optionsFull.profiles[currentProfileIndex];
- const optionsFull = await apiOptionsGetFull();
- if (optionsFull.profiles.length <= 1) {
- return;
+ $('#profile-remove-modal-profile-name').text(profile.name);
+ $('#profile-remove-modal').modal('show');
}
- const profile = optionsFull.profiles[currentProfileIndex];
+ async _onRemoveConfirm() {
+ $('#profile-remove-modal').modal('hide');
- $('#profile-remove-modal-profile-name').text(profile.name);
- $('#profile-remove-modal').modal('show');
-}
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ if (optionsFull.profiles.length <= 1) {
+ return;
+ }
-async function onProfileRemoveConfirm() {
- $('#profile-remove-modal').modal('hide');
+ const currentProfileIndex = this._settingsController.profileIndex;
+ optionsFull.profiles.splice(currentProfileIndex, 1);
- const optionsFull = await getOptionsFullMutable();
- if (optionsFull.profiles.length <= 1) {
- return;
- }
+ if (currentProfileIndex >= optionsFull.profiles.length) {
+ this._settingsController.profileIndex = optionsFull.profiles.length - 1;
+ }
- optionsFull.profiles.splice(currentProfileIndex, 1);
+ if (optionsFull.profileCurrent >= optionsFull.profiles.length) {
+ optionsFull.profileCurrent = optionsFull.profiles.length - 1;
+ }
- if (currentProfileIndex >= optionsFull.profiles.length) {
- --currentProfileIndex;
+ await this._settingsController.save();
}
- if (optionsFull.profileCurrent >= optionsFull.profiles.length) {
- optionsFull.profileCurrent = optionsFull.profiles.length - 1;
+ _onNameChanged() {
+ const currentProfileIndex = this._settingsController.profileIndex;
+ $('#profile-active, #profile-target').find(`[value="${currentProfileIndex}"]`).text(this.value);
}
- await profileOptionsUpdateTarget(optionsFull);
- await settingsSaveOptions();
-
- yomichan.trigger('modifyingProfileChange');
-}
+ async _onMove(offset) {
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ const currentProfileIndex = this._settingsController.profileIndex;
+ const index = currentProfileIndex + offset;
+ if (index < 0 || index >= optionsFull.profiles.length) {
+ return;
+ }
-function onProfileNameChanged() {
- $('#profile-active, #profile-target').find(`[value="${currentProfileIndex}"]`).text(this.value);
-}
+ const profile = optionsFull.profiles[currentProfileIndex];
+ optionsFull.profiles.splice(currentProfileIndex, 1);
+ optionsFull.profiles.splice(index, 0, profile);
-async function onProfileMove(offset) {
- const optionsFull = await getOptionsFullMutable();
- const index = currentProfileIndex + offset;
- if (index < 0 || index >= optionsFull.profiles.length) {
- return;
- }
+ if (optionsFull.profileCurrent === currentProfileIndex) {
+ optionsFull.profileCurrent = index;
+ }
- const profile = optionsFull.profiles[currentProfileIndex];
- optionsFull.profiles.splice(currentProfileIndex, 1);
- optionsFull.profiles.splice(index, 0, profile);
+ this._settingsController.profileIndex = index;
- if (optionsFull.profileCurrent === currentProfileIndex) {
- optionsFull.profileCurrent = index;
+ await this._settingsController.save();
}
- currentProfileIndex = index;
-
- await profileOptionsUpdateTarget(optionsFull);
- await settingsSaveOptions();
-
- yomichan.trigger('modifyingProfileChange');
-}
+ async _onCopy() {
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ if (optionsFull.profiles.length <= 1) {
+ return;
+ }
-async function onProfileCopy() {
- const optionsFull = await apiOptionsGetFull();
- if (optionsFull.profiles.length <= 1) {
- return;
+ const currentProfileIndex = this._settingsController.profileIndex;
+ this._populateSelect($('#profile-copy-source'), optionsFull.profiles, currentProfileIndex === 0 ? 1 : 0, [currentProfileIndex]);
+ $('#profile-copy-modal').modal('show');
}
- profileOptionsPopulateSelect($('#profile-copy-source'), optionsFull.profiles, currentProfileIndex === 0 ? 1 : 0, [currentProfileIndex]);
- $('#profile-copy-modal').modal('show');
-}
-
-async function onProfileCopyConfirm() {
- $('#profile-copy-modal').modal('hide');
+ async _onCopyConfirm() {
+ $('#profile-copy-modal').modal('hide');
- const optionsFull = await getOptionsFullMutable();
- const index = tryGetIntegerValue('#profile-copy-source', 0, optionsFull.profiles.length);
- if (index === null || index === currentProfileIndex) {
- return;
- }
+ const optionsFull = await this._settingsController.getOptionsFullMutable();
+ const index = this._tryGetIntegerValue('#profile-copy-source', 0, optionsFull.profiles.length);
+ const currentProfileIndex = this._settingsController.profileIndex;
+ if (index === null || index === currentProfileIndex) {
+ return;
+ }
- const profileOptions = utilBackgroundIsolate(optionsFull.profiles[index].options);
- optionsFull.profiles[currentProfileIndex].options = profileOptions;
+ const profileOptions = utilBackgroundIsolate(optionsFull.profiles[index].options);
+ optionsFull.profiles[currentProfileIndex].options = profileOptions;
- await profileOptionsUpdateTarget(optionsFull);
- await settingsSaveOptions();
+ await this._settingsController.save();
+ }
}