mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
refactor: replace TSLint with ESLint
This commit is contained in:
committed by
Jovan Krunić
parent
67fb4a43c9
commit
d696215d08
@@ -13,11 +13,7 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Injectable} from '@angular/core';
|
||||
import {
|
||||
SCSetting,
|
||||
SCSettingValue,
|
||||
SCSettingValues,
|
||||
} from '@openstapps/core';
|
||||
import {SCSetting, SCSettingValue, SCSettingValues} from '@openstapps/core';
|
||||
import deepMerge from 'deepmerge';
|
||||
import {Subject} from 'rxjs';
|
||||
import {ConfigProvider} from '../config/config.provider';
|
||||
@@ -38,7 +34,7 @@ export interface CategoryWithSettings {
|
||||
/**
|
||||
* Settings that belong in this category
|
||||
*/
|
||||
settings: { [key: string]: SCSetting; };
|
||||
settings: {[key: string]: SCSetting};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,14 +95,17 @@ export class SettingsProvider {
|
||||
* Source of settings actions
|
||||
*/
|
||||
private settingsActionSource = new Subject<SettingsAction>();
|
||||
|
||||
/**
|
||||
* Order of the setting categories
|
||||
*/
|
||||
categoriesOrder: string[];
|
||||
|
||||
/**
|
||||
* Settings actions observable
|
||||
*/
|
||||
settingsActionChanged$ = this.settingsActionSource.asObservable();
|
||||
|
||||
/**
|
||||
* Cache for the imported settings
|
||||
*/
|
||||
@@ -114,6 +113,7 @@ export class SettingsProvider {
|
||||
|
||||
/**
|
||||
* Return true if all given values are valid to possible values in given settingInput
|
||||
*
|
||||
* @param possibleValues Possible values
|
||||
* @param enteredValues Entered value
|
||||
*/
|
||||
@@ -121,7 +121,7 @@ export class SettingsProvider {
|
||||
possibleValues: SCSettingValues | undefined,
|
||||
enteredValues: SCSettingValues,
|
||||
): boolean {
|
||||
if ( typeof possibleValues === 'undefined' ) {
|
||||
if (typeof possibleValues === 'undefined') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -136,6 +136,7 @@ export class SettingsProvider {
|
||||
|
||||
/**
|
||||
* Returns true if given value is valid to possible values in given settingInput
|
||||
*
|
||||
* @param possibleValues Possible values
|
||||
* @param enteredValue Entered value
|
||||
*/
|
||||
@@ -143,21 +144,27 @@ export class SettingsProvider {
|
||||
possibleValues: SCSettingValues | undefined,
|
||||
enteredValue: SCSettingValue,
|
||||
): boolean {
|
||||
if ( typeof possibleValues === 'undefined' ) {
|
||||
if (typeof possibleValues === 'undefined') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return possibleValues !== undefined
|
||||
&& (Array.isArray(possibleValues)
|
||||
&& possibleValues.includes(enteredValue));
|
||||
return (
|
||||
possibleValues !== undefined &&
|
||||
Array.isArray(possibleValues) &&
|
||||
possibleValues.includes(enteredValue)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates value for given settings inputType. Returns true if value is valid.
|
||||
*
|
||||
* @param setting setting to check value against
|
||||
* @param value value to validate
|
||||
*/
|
||||
public static validateValue(setting: SCSetting, value: SCSettingValue | SCSettingValues): boolean {
|
||||
public static validateValue(
|
||||
setting: SCSetting,
|
||||
value: SCSettingValue | SCSettingValues,
|
||||
): boolean {
|
||||
let isValueValid = false;
|
||||
switch (setting.inputType) {
|
||||
case 'number':
|
||||
@@ -166,11 +173,9 @@ export class SettingsProvider {
|
||||
}
|
||||
break;
|
||||
case 'multiple choice':
|
||||
if (!Array.isArray(value)) {
|
||||
isValueValid = false;
|
||||
} else {
|
||||
isValueValid = SettingsProvider.checkMultipleChoiceValue(setting.values, value);
|
||||
}
|
||||
isValueValid = !Array.isArray(value)
|
||||
? false
|
||||
: SettingsProvider.checkMultipleChoiceValue(setting.values, value);
|
||||
break;
|
||||
case 'password':
|
||||
case 'text':
|
||||
@@ -179,11 +184,9 @@ export class SettingsProvider {
|
||||
}
|
||||
break;
|
||||
case 'single choice':
|
||||
if (Array.isArray(value)) {
|
||||
isValueValid = false;
|
||||
} else {
|
||||
isValueValid = SettingsProvider.checkSingleChoiceValue(setting.values, value);
|
||||
}
|
||||
isValueValid = Array.isArray(value)
|
||||
? false
|
||||
: SettingsProvider.checkSingleChoiceValue(setting.values, value);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
@@ -196,14 +199,17 @@ export class SettingsProvider {
|
||||
* @param storage TODO
|
||||
* @param configProvider TODO
|
||||
*/
|
||||
constructor(private readonly storage: StorageProvider,
|
||||
private readonly configProvider: ConfigProvider) {
|
||||
constructor(
|
||||
private readonly storage: StorageProvider,
|
||||
private readonly configProvider: ConfigProvider,
|
||||
) {
|
||||
this.categoriesOrder = [];
|
||||
this.settingsCache = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an Setting to the Cache if not exist and set undefined value to defaultValue
|
||||
*
|
||||
* @param setting Setting with categories, defaultValue, name, input type and valid values
|
||||
*/
|
||||
private addSetting(setting: SCSetting): void {
|
||||
@@ -214,7 +220,8 @@ export class SettingsProvider {
|
||||
if (setting.value === undefined) {
|
||||
setting.value = setting.defaultValue;
|
||||
}
|
||||
this.settingsCache[setting.categories[0]].settings[setting.name] = setting;
|
||||
this.settingsCache[setting.categories[0]].settings[setting.name] =
|
||||
setting;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +233,9 @@ export class SettingsProvider {
|
||||
// iterate through keys of categories
|
||||
for (const categoryKey of Object.keys(this.settingsCache)) {
|
||||
// iterate through keys of settingValueContainer
|
||||
for (const settingKey of Object.keys(this.settingsCache[categoryKey].settings)) {
|
||||
for (const settingKey of Object.keys(
|
||||
this.settingsCache[categoryKey].settings,
|
||||
)) {
|
||||
if (typeof settingValuesContainer[categoryKey] === 'undefined') {
|
||||
settingValuesContainer[categoryKey] = {};
|
||||
}
|
||||
@@ -240,6 +249,7 @@ export class SettingsProvider {
|
||||
|
||||
/**
|
||||
* Add category if not exists
|
||||
*
|
||||
* @param category the category to provide
|
||||
*/
|
||||
private provideCategory(category: string): void {
|
||||
@@ -256,6 +266,7 @@ export class SettingsProvider {
|
||||
|
||||
/**
|
||||
* Returns true if category exists
|
||||
*
|
||||
* @param category Category key name
|
||||
*/
|
||||
public categoryExists(category: string): boolean {
|
||||
@@ -280,32 +291,39 @@ export class SettingsProvider {
|
||||
|
||||
/**
|
||||
* Returns copy of a setting if exist
|
||||
*
|
||||
* @param category the category of requested setting
|
||||
* @param name the name of requested setting
|
||||
*
|
||||
* @throws Exception if setting is not provided
|
||||
*/
|
||||
public async getSetting(category: string, name: string): Promise<SCSetting> {
|
||||
await this.init();
|
||||
if (this.settingExists(category, name)) {
|
||||
// return a copy of the settings
|
||||
return JSON.parse(JSON.stringify(this.settingsCache[category].settings[name]));
|
||||
return JSON.parse(
|
||||
JSON.stringify(this.settingsCache[category].settings[name]),
|
||||
);
|
||||
}
|
||||
throw new Error(`Setting "${name}" not provided`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns copy of a settings value if exist
|
||||
*
|
||||
* @param category the category of requested setting
|
||||
* @param name the name of requested setting
|
||||
*
|
||||
* @throws Exception if setting is not provided
|
||||
*/
|
||||
public async getValue(category: string, name: string): Promise<SCSettingValue | SCSettingValues> {
|
||||
public async getValue(
|
||||
category: string,
|
||||
name: string,
|
||||
): Promise<SCSettingValue | SCSettingValues> {
|
||||
await this.init();
|
||||
if (this.settingExists(category, name)) {
|
||||
// return a copy of the settings value
|
||||
return JSON.parse(JSON.stringify(this.settingsCache[category].settings[name].value));
|
||||
return JSON.parse(
|
||||
JSON.stringify(this.settingsCache[category].settings[name].value),
|
||||
);
|
||||
}
|
||||
throw new Error(`Setting "${name}" not provided`);
|
||||
}
|
||||
@@ -313,31 +331,39 @@ export class SettingsProvider {
|
||||
/**
|
||||
* Initializes settings from config and stored values if exist
|
||||
*/
|
||||
public async init(): Promise<void> {
|
||||
public async init(): Promise<void> {
|
||||
try {
|
||||
const settings: SCSetting[] = (await this.configProvider.getValue('settings')) as SCSetting[];
|
||||
settings.forEach((setting) => this.addSetting(setting));
|
||||
const settings: SCSetting[] = (await this.configProvider.getValue(
|
||||
'settings',
|
||||
)) as SCSetting[];
|
||||
for (const setting of settings) this.addSetting(setting);
|
||||
|
||||
for (const category of Object.keys(this.settingsCache)) {
|
||||
if (!this.categoriesOrder.includes(category)) {
|
||||
this.categoriesOrder.push(category);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
} catch {
|
||||
this.settingsCache = {};
|
||||
}
|
||||
|
||||
if (await this.storage.has(STORAGE_KEY_SETTING_VALUES)) {
|
||||
// get setting values from StorageProvider into settingsCache
|
||||
const valuesContainer: SettingValuesContainer =
|
||||
await this.storage.get<SettingValuesContainer>(STORAGE_KEY_SETTING_VALUES);
|
||||
await this.storage.get<SettingValuesContainer>(
|
||||
STORAGE_KEY_SETTING_VALUES,
|
||||
);
|
||||
// iterate through keys of categories
|
||||
for (const categoryKey of Object.keys(this.settingsCache)) {
|
||||
// iterate through setting keys of category
|
||||
for (const settingKey of Object.keys(this.settingsCache[categoryKey].settings)) {
|
||||
for (const settingKey of Object.keys(
|
||||
this.settingsCache[categoryKey].settings,
|
||||
)) {
|
||||
// if saved setting value exists set it, otherwise set to default value
|
||||
if (typeof valuesContainer[categoryKey] !== 'undefined'
|
||||
&& typeof valuesContainer[categoryKey][settingKey] !== 'undefined') {
|
||||
if (
|
||||
typeof valuesContainer[categoryKey] !== 'undefined' &&
|
||||
typeof valuesContainer[categoryKey][settingKey] !== 'undefined'
|
||||
) {
|
||||
this.settingsCache[categoryKey].settings[settingKey].value =
|
||||
valuesContainer[categoryKey][settingKey];
|
||||
} else {
|
||||
@@ -352,6 +378,7 @@ export class SettingsProvider {
|
||||
|
||||
/**
|
||||
* Adds given setting and its category if not exist
|
||||
*
|
||||
* @param setting the setting to add
|
||||
*/
|
||||
public provideSetting(setting: SCSetting): void {
|
||||
@@ -371,7 +398,9 @@ export class SettingsProvider {
|
||||
*/
|
||||
async resetDefault(): Promise<void> {
|
||||
for (const catKey of Object.keys(this.settingsCache)) {
|
||||
for (const settingKey of Object.keys(this.settingsCache[catKey].settings)) {
|
||||
for (const settingKey of Object.keys(
|
||||
this.settingsCache[catKey].settings,
|
||||
)) {
|
||||
const settingInput = this.settingsCache[catKey].settings[settingKey];
|
||||
settingInput.value = settingInput.defaultValue;
|
||||
}
|
||||
@@ -385,18 +414,29 @@ export class SettingsProvider {
|
||||
public async saveSettingValues(): Promise<void> {
|
||||
if (await this.storage.has(STORAGE_KEY_SETTING_VALUES)) {
|
||||
const savedSettingsValues: SettingValuesContainer =
|
||||
await this.storage.get<SettingValuesContainer>(STORAGE_KEY_SETTING_VALUES);
|
||||
await this.storage.get<SettingValuesContainer>(
|
||||
STORAGE_KEY_SETTING_VALUES,
|
||||
);
|
||||
const cacheSettingsValues = this.getSettingValuesFromCache();
|
||||
const mergedSettingValues = deepMerge(savedSettingsValues, cacheSettingsValues);
|
||||
await this.storage
|
||||
.put<SettingValuesContainer>(STORAGE_KEY_SETTING_VALUES, mergedSettingValues);
|
||||
const mergedSettingValues = deepMerge(
|
||||
savedSettingsValues,
|
||||
cacheSettingsValues,
|
||||
);
|
||||
await this.storage.put<SettingValuesContainer>(
|
||||
STORAGE_KEY_SETTING_VALUES,
|
||||
mergedSettingValues,
|
||||
);
|
||||
} else {
|
||||
await this.storage.put<SettingValuesContainer>(STORAGE_KEY_SETTING_VALUES, this.getSettingValuesFromCache());
|
||||
await this.storage.put<SettingValuesContainer>(
|
||||
STORAGE_KEY_SETTING_VALUES,
|
||||
this.getSettingValuesFromCache(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the order the given categories show up in the settings page
|
||||
*
|
||||
* @param categoryNames the order of the categories
|
||||
*/
|
||||
public setCategoriesOrder(categoryNames: string[]) {
|
||||
@@ -409,11 +449,13 @@ export class SettingsProvider {
|
||||
* @param category Category key name
|
||||
* @param name Setting key name
|
||||
* @param value Value to be set
|
||||
*
|
||||
* @throws Exception if setting is not provided or value not valid to the settings inputType
|
||||
*/
|
||||
public async setSettingValue(category: string, name: string,
|
||||
value: SCSettingValue | SCSettingValues): Promise<void> {
|
||||
public async setSettingValue(
|
||||
category: string,
|
||||
name: string,
|
||||
value: SCSettingValue | SCSettingValues,
|
||||
): Promise<void> {
|
||||
await this.init();
|
||||
if (this.settingExists(category, name)) {
|
||||
const setting: SCSetting = this.settingsCache[category].settings[name];
|
||||
@@ -423,7 +465,10 @@ export class SettingsProvider {
|
||||
this.settingsCache[category].settings[name].value = value;
|
||||
await this.saveSettingValues();
|
||||
// publish setting changes
|
||||
this.settingsActionSource.next({type: 'stapps.settings.changed', payload: {category, name, value}});
|
||||
this.settingsActionSource.next({
|
||||
type: 'stapps.settings.changed',
|
||||
payload: {category, name, value},
|
||||
});
|
||||
} else {
|
||||
throw new Error(`Value "${value}" of type
|
||||
${typeof value} is not valid for ${setting.inputType}`);
|
||||
@@ -435,10 +480,14 @@ export class SettingsProvider {
|
||||
|
||||
/**
|
||||
* Returns true if setting in category exists
|
||||
*
|
||||
* @param category Category key name
|
||||
* @param setting Setting key name
|
||||
*/
|
||||
public settingExists(category: string, setting: string): boolean {
|
||||
return this.categoryExists(category) && this.settingsCache[category].settings[setting] !== undefined;
|
||||
return (
|
||||
this.categoryExists(category) &&
|
||||
this.settingsCache[category].settings[setting] !== undefined
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user