Add settings structs

This commit is contained in:
Samuel Elliott 2018-03-01 18:50:49 +00:00
parent 9a72a7425e
commit 3574d6a5ba
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
18 changed files with 1039 additions and 0 deletions

View File

@ -0,0 +1,4 @@
export { default as SettingsSet } from './settingsset';
export { default as SettingsCategory } from './settingscategory';
export { default as Setting } from './setting';
export { default as SettingsScheme } from './settingsscheme';

View File

@ -0,0 +1,35 @@
/**
* BetterDiscord Multiple Choice Option Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Utils } from 'common';
export default class MultipleChoiceOption {
constructor(args) {
this.args = args.args || args;
}
get id() {
return this.args.id;
}
get text() {
return this.args.text;
}
get value() {
return this.args.value;
}
clone() {
return new MultipleChoiceOption(Utils.deepclone(this.args));
}
}

View File

@ -0,0 +1,42 @@
/**
* BetterDiscord Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Utils } from 'common';
import BoolSetting from './types/bool';
import StringSetting from './types/text';
import NumberSetting from './types/number';
import DropdownSetting from './types/dropdown';
import RadioSetting from './types/radio';
import SliderSetting from './types/slider';
import ColourSetting from './types/colour';
import FileSetting from './types/file';
import ArraySetting from './types/array';
import CustomSetting from './types/custom';
export default class Setting {
constructor(args) {
args = args.args || args;
if (args.type === 'bool') return new BoolSetting(args);
else if (args.type === 'text') return new StringSetting(args);
else if (args.type === 'number') return new NumberSetting(args);
else if (args.type === 'dropdown') return new DropdownSetting(args);
else if (args.type === 'radio') return new RadioSetting(args);
else if (args.type === 'slider') return new SliderSetting(args);
else if (args.type === 'colour') return new ColourSetting(args);
else if (args.type === 'file') return new FileSetting(args);
else if (args.type === 'array') return new ArraySetting(args);
else if (args.type === 'custom') return new CustomSetting(args);
else throw {message: `Setting type ${args.type} unknown`};
}
}

View File

@ -0,0 +1,130 @@
/**
* BetterDiscord Settings Category Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './setting';
import EventEmitter from 'events';
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
export default class SettingsCategory {
constructor(args) {
this.emitter = new EventEmitter();
this.args = args.args || args;
this.args.settings = this.settings.map(setting => new Setting(setting));
for (let setting of this.settings) {
setting.on('setting-updated', ({ value, old_value }) => {
this.emit('setting-updated', new SettingUpdatedEvent({
category: this, category_id: this.id,
setting, setting_id: setting.id,
value, old_value
}));
});
setting.on('settings-updated', ({ updatedSettings }) => this.emit('settings-updated', new SettingsUpdatedEvent({
updatedSettings: updatedSettings.map(updatedSetting => Object.assign({
category: this, category_id: this.id
}, updatedSetting))
})));
}
}
get id() {
return this.args.id || this.args.category;
}
get category() {
return this.id;
}
get name() {
return this.args.category_name;
}
get category_name() {
return this.name;
}
get type() {
return this.args.type;
}
get settings() {
return this.args.settings || [];
}
get changed() {
if (this.settings.find(setting => setting.changed)) return true;
return false;
}
find(f) {
return this.settings.find(f);
}
getSetting(id) {
return this.findSetting(setting => setting.id === id);
}
merge(newCategory, emit_multi = true) {
let updatedSettings = [];
for (let newSetting of newCategory.settings) {
const setting = this.settings.find(setting => setting.id === newSetting.id);
if (!setting) {
Logger.warn('SettingsCategory', `Trying to merge setting ${this.id}/${newSetting.id}, which does not exist.`);
continue;
}
const updatedSetting = setting.merge(newSetting, false);
if (!updatedSetting) continue;
updatedSettings = updatedSettings.concat(updatedSetting.map(({ setting, value, old_value }) => ({
category: this, category_id: this.id,
setting, setting_id: setting.id,
value, old_value
})));
}
if (emit_multi)
this.emit('settings-updated', new SettingsUpdatedEvent({
updatedSettings
}));
return updatedSettings;
}
setSaved() {
for (let setting of this.settings) {
setting.setSaved();
}
}
strip() {
return {
category: this.category,
settings: this.settings.map(setting => setting.strip())
};
}
clone() {
return new SettingsCategory({
id: this.id,
category: this.id,
name: this.name,
category_name: this.category_name,
type: this.type,
settings: this.settings.map(setting => setting.clone())
});
}
on(...args) { return this.emitter.on(...args); }
off(...args) { return this.emitter.removeListener(...args); }
emit(...args) { return this.emitter.emit(...args); }
}

View File

@ -0,0 +1,77 @@
/**
* BetterDiscord Settings Scheme Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Utils, ClientLogger as Logger } from 'common';
export default class SettingsScheme {
constructor(args) {
this.args = args.args || args;
this.args.settings = this.settings.map(({ category, settings }) => ({
category, settings: settings.map(({ id, value }) => ({
id, value
}))
}));
Object.freeze(this);
}
get id() {
return this.args.id;
}
get icon_url() {
return this.args.icon_url;
}
get name() {
return this.args.name;
}
get hint() {
return this.args.hint;
}
get settings() {
return this.args.settings || [];
}
isActive(set) {
for (let schemeCategory of this.settings) {
const category = set.categories.find(c => c.category === schemeCategory.category);
if (!category) {
Logger.warn('SettingsScheme', `Category ${schemeCategory.category} does not exist`);
return false;
}
for (let schemeSetting of schemeCategory.settings) {
const setting = category.settings.find(s => s.id === schemeSetting.id);
if (!setting) {
Logger.warn('SettingsScheme', `Setting ${schemeCategory.category}/${schemeSetting.id} does not exist`);
return false;
}
if (!Utils.compare(setting.value, schemeSetting.value)) return false;
}
}
return true;
}
applyTo(set) {
return set.merge({ settings: this.settings });
}
clone() {
return new SettingsScheme(Utils.deepclone(this.args));
}
}

View File

@ -0,0 +1,163 @@
/**
* BetterDiscord Settings Set Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import SettingsCategory from './settingscategory';
import SettingsScheme from './settingsscheme';
import { AsyncEventEmitter } from 'common';
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
import { Modals } from 'ui';
export default class SettingsSet {
constructor(args) {
this.emitter = new AsyncEventEmitter();
this.args = args.args || args;
this.args.settings = this.settings.map(category => new SettingsCategory(category));
this.args.schemes = this.schemes.map(scheme => new SettingsScheme(scheme));
for (let category of this.settings) {
category.on('setting-updated', ({ setting, value, old_value }) => this.emit('setting-updated', new SettingUpdatedEvent({
set: this, set_id: this.id,
category, category_id: category.id,
setting, setting_id: setting.id,
value, old_value
})));
category.on('settings-updated', ({ updatedSettings }) => this.emit('settings-updated', new SettingsUpdatedEvent({
updatedSettings: updatedSettings.map(updatedSetting => Object.assign({
set: this, set_id: this.id
}, updatedSetting))
})));
}
}
get id() {
return this.args.id;
}
get text() {
return this.args.text;
}
get headertext() {
return this.args.headertext || `${this.text} Settings`;
}
get categories() {
return this.args.settings || [];
}
get settings() {
return this.categories;
}
get schemes() {
return this.args.schemes || [];
}
get changed() {
if (this.categories.find(category => category.changed)) return true;
return false;
}
find(f) {
return this.categories.find(f);
}
getCategory(id) {
return this.find(category => category.id === id);
}
findSetting(f) {
for (let category of this.categories) {
const setting = category.findSetting(f);
if (setting) return setting;
}
}
findSettingInCategory(cf, f) {
for (let category of this.categories.filter(cf)) {
const setting = category.find(f);
if (setting) return setting;
}
}
getSetting(id, sid) {
if (sid) return this.findSettingInCategory(category => category.id === id, setting => setting.id === sid);
return this.findSetting(setting => setting.id === id);
}
get(cid, sid) {
const setting = this.getSetting(cid, sid);
return setting ? setting.value : undefined;
}
showModal(headertext) {
Modals.settings(this, headertext ? headertext : this.headertext);
}
async merge(newSet, emit_multi = true) {
let updatedSettings = [];
const categories = newSet instanceof Array ? newSet : newSet.settings;
if (!categories) return [];
for (let newCategory of categories) {
const category = this.find(category => category.category === newCategory.category);
if (!category) {
Logger.warn('SettingsCategory', `Trying to merge category ${newCategory.id}, which does not exist.`);
continue;
}
const updatedSetting = category.merge(newCategory, false);
if (!updatedSetting) continue;
updatedSettings = updatedSettings.concat(updatedSetting.map(({ category, setting, value, old_value }) => ({
set: this, set_id: this.id,
category, category_id: category.id,
setting, setting_id: setting.id,
value, old_value
})));
}
if (emit_multi)
await this.emit('settings-updated', new SettingsUpdatedEvent({
updatedSettings
}));
return updatedSettings;
}
setSaved() {
for (let category of this.categories) {
category.setSaved();
}
}
strip() {
const stripped = {};
if (this.id) stripped.id = this.id;
stripped.settings = this.categories.map(category => category.strip());
return stripped;
}
clone() {
return new SettingsSet({
id: this.id,
text: this.text,
headertext: this.headertext,
settings: this.categories.map(category => category.clone()),
schemes: this.schemes.map(scheme => scheme.clone())
});
}
on(...args) { return this.emitter.on(...args); }
off(...args) { return this.emitter.removeListener(...args); }
emit(...args) { return this.emitter.emit(...args); }
}

View File

@ -0,0 +1,135 @@
/**
* BetterDiscord Array Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { ThemeManager } from 'modules';
import { Utils } from 'common';
import Setting from './basesetting';
import SettingsSet from '../settingsset';
import SettingsCategory from '../settingscategory';
import SettingsScheme from '../settingsscheme';
export default class ArraySetting extends Setting {
constructor(args) {
super(args);
console.log(this);
this.args.settings = this.settings.map(category => new SettingsCategory(category));
this.args.schemes = this.schemes.map(scheme => new SettingsScheme(scheme));
this.args.items = this.value ? this.value.map(item => this.createItem(item.args || item)) : [];
this.updateValue(false, false);
}
get defaultValue() {
return [];
}
get items() {
return this.args.items || [];
}
set items(items) {
this.args.items = items ? items.map(item => this.createItem(item)) : [];
this.updateValue();
}
get fullwidth() {
return false;
}
get settings() {
return this.args.settings || [];
}
get schemes() {
return this.args.schemes || [];
}
get inline() {
return this.args.inline || false;
}
get allow_external() {
return this.args.allow_external || !this.inline;
}
get min() {
return this.args.min || 0;
}
get max() {
return this.args.max || null;
}
addItem(item) {
const newItem = this.createItem(item);
this.args.items.push(newItem);
// this.items = this.items;
this.updateValue();
return newItem;
}
removeItem(item) {
this.args.items = this.items.filter(i => i !== item);
this.updateValue();
}
createItem(item) {
const set = new SettingsSet({
settings: Utils.deepclone(this.settings),
schemes: this.schemes
});
if (item) set.merge(item.args || item);
set.setSaved();
set.on('settings-updated', () => this.updateValue());
return set;
}
setValue(value, emit_multi = true, emit = true) {
this.items = value;
// this.__proto__.__proto__.apply(this, arguments);
}
updateValue(emit_multi = true, emit = true) {
return this.__proto__.__proto__.setValue.call(this, this.items.map(item => {
console.log('ArraySetting.updateValue:', item);
if (!item) return;
item.setSaved();
return item.strip();
}), emit_multi, emit);
}
setContentPath(contentPath) {
this.args.path = contentPath;
for (let category of this.settings) {
for (let setting of category.settings) {
if (setting.type === 'array' || setting.type === 'custom') setting.setContentPath(contentPath);
}
}
}
async toSCSS() {
const maps = [];
for (let item of this.items)
maps.push(await ThemeManager.getConfigAsSCSSMap(item.settings));
// Final comma ensures the variable is a list
return maps.length ? maps.join(', ') + ',' : '()';
}
clone() {
return new ArraySetting(Utils.deepclone(this.args));
}
}

View File

@ -0,0 +1,130 @@
/**
* BetterDiscord Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { ThemeManager } from 'modules';
import { Utils } from 'common';
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
import EventEmitter from 'events';
export default class Setting {
constructor(args) {
this.args = args.args || args;
if (!this.args.hasOwnProperty('value'))
this.args.value = this.defaultValue;
if (!this.args.hasOwnProperty('saved_value'))
this.args.saved_value = this.args.value;
this.emitter = new EventEmitter();
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
}
get id() {
return this.args.id;
}
get type() {
return this.args.type;
}
get value() {
return this.args.value;
}
set value(value) {
this.setValue(value);
}
get defaultValue() {
return undefined;
}
// get changed() {
// return this.args.changed;
// }
get text() {
return this.args.text;
}
get hint() {
return this.args.hint;
}
get disabled() {
return this.args.disabled || false;
}
get fullwidth() {
return this.args.fullwidth || false;
}
merge(newSetting, emit_multi = true) {
return this.setValue(newSetting.value, emit_multi);
}
setValue(value, emit_multi = true, emit = true) {
const old_value = this.args.value;
if (Utils.compare(value, old_value)) return [];
this.args.value = value;
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
const updatedSetting = {
setting: this, setting_id: this.id,
value, old_value
};
if (emit)
this.emit('setting-updated', new SettingUpdatedEvent(updatedSetting));
if (emit_multi)
this.emit('settings-updated', new SettingsUpdatedEvent({
updatedSettings: [updatedSetting]
}));
return [updatedSetting];
}
setSaved() {
this.args.saved_value = this.args.value;
this.changed = false;
}
setContentPath(contentPath) {
this.args.path = contentPath;
}
strip() {
return {
id: this.id,
value: this.value
};
}
clone() {
return new this.constructor(Utils.deepclone(this.args));
}
toSCSS() {
if (typeof this.value === 'boolean' || typeof this.value === 'number') {
return this.value;
}
if (typeof this.value === 'string') {
return ThemeManager.toSCSSString(this.value);
}
}
on(...args) { return this.emitter.on(...args); }
off(...args) { return this.emitter.removeListener(...args); }
emit(...args) { return this.emitter.emit(...args); }
}

View File

@ -0,0 +1,23 @@
/**
* BetterDiscord Boolean Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
export default class BoolSetting extends Setting {
get defaultValue() {
return false;
}
get fullwidth() {
return false;
}
}

View File

@ -0,0 +1,23 @@
/**
* BetterDiscord Colour Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
export default class ColourSetting extends Setting {
get defaultValue() {
return 'rgba(0, 0, 0, 0)';
}
toSCSS() {
return this.value;
}
}

View File

@ -0,0 +1,49 @@
/**
* BetterDiscord Custom Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
import SettingsCategory from '../settingscategory';
import SettingsScheme from '../settingsscheme';
export default class CustomSetting extends Setting {
constructor(args) {
super(args);
if (this.args.class_file) {
const component = window.require(path.join(this.path, this.args.class_file));
const setting_class = this.args.class ? component[this.args.class](CustomSetting) : component.default ? component.default(CustomSetting) : component(CustomSetting);
const setting = new setting_class(this.args);
if (setting instanceof CustomSetting) return setting;
}
}
get file() {
return this.args.file;
}
get function() {
return this.args.function;
}
get component() {
return this.args.component;
}
get path() {
return this.args.path;
}
get debug() {
return this.args.debug || false;
}
}

View File

@ -0,0 +1,50 @@
/**
* BetterDiscord Dropdown Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
import MultipleChoiceOption from '../multiplechoiceoption';
export default class DropdownSetting extends Setting {
constructor(args) {
super(args);
this.args.options = this.options.map(option => new MultipleChoiceOption(option));
}
get value() {
const selected = this.selected_option;
if (selected) return selected.value;
return this.args.value;
}
set value(value) {
const selected = this.options.find(option => option.id === value);
if (selected) this.setValue(selected.id);
else this.setValue(value);
}
get options() {
return this.args.options || [];
}
get selected_option() {
return this.options.find(option => option.id === this.args.value);
}
set selected_option(selected_option) {
this.args.value = this.options.find(option => option.id === selected_option.id).id;
}
toSCSS() {
return this.value;
}
}

View File

@ -0,0 +1,46 @@
/**
* BetterDiscord File Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { ThemeManager } from 'modules';
import { FileUtils } from 'common';
import Setting from './basesetting';
export default class FileSetting extends Setting {
get defaultValue() {
return [];
}
get dialogOptions() {
return this.args.dialogOptions || {};
}
async openDialog() {
if (this.disabled) return;
const filenames = await ClientIPC.send('bd-native-open', this.dialogOptions);
if (filenames)
this.value = filenames;
}
async toSCSS() {
if (!this.value || !this.value.length) return '()';
const files = [];
for (let filepath of this.value) {
const buffer = await FileUtils.readFileBuffer(filepath);
const type = await FileUtils.getFileType(buffer);
files.push(`(data: ${ThemeManager.toSCSSString(buffer.toString('base64'))}, type: ${ThemeManager.toSCSSString(type.mime)}, url: ${ThemeManager.toSCSSString(await FileUtils.toDataURI(buffer, type.mime))})`);
}
return files.length ? files.join(', ') : '()';
}
}

View File

@ -0,0 +1,15 @@
/**
* BetterDiscord Number Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
export default class NumberSetting extends Setting {
}

View File

@ -0,0 +1,50 @@
/**
* BetterDiscord Radio Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
import MultipleChoiceOption from '../multiplechoiceoption';
export default class RadioSetting extends Setting {
constructor(args) {
super(args);
this.args.options = this.options.map(option => new MultipleChoiceOption(option));
}
get value() {
const selected = this.selected_option;
if (selected) return selected.value;
return this.args.value;
}
set value(value) {
const selected = this.options.find(option => option.id === value);
if (selected) this.setValue(selected.id);
else this.setValue(value);
}
get options() {
return this.args.options || [];
}
get selected_option() {
return this.options.find(option => option.id === this.args.value);
}
set selected_option(selected_option) {
this.value = this.options.find(option => option.id === selected_option.id).id;
}
toSCSS() {
return this.value;
}
}

View File

@ -0,0 +1,39 @@
/**
* BetterDiscord Slider Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
export default class SliderSetting extends Setting {
get defaultValue() {
return this.min;
}
get min() {
return this.args.min || 0;
}
get max() {
return this.args.max || null;
}
get step() {
return this.args.step || 1;
}
get unit() {
return this.args.unit || '';
}
get points() {
return this.args.points;
}
}

View File

@ -0,0 +1,27 @@
/**
* BetterDiscord String Setting Struct
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Setting from './basesetting';
export default class StringSetting extends Setting {
get defaultValue() {
return '';
}
get fullwidth() {
return this.args.fullwidth && !this.multiline;
}
get multiline() {
return this.args.multiline || null;
}
}

View File

@ -1 +1,2 @@
export * from './events/index';
export * from './settings/index';