BetterDiscordApp-v2/client/src/modules/thememanager.js

141 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-01-30 16:59:27 +01:00
/**
* BetterDiscord Theme Manager Module
* 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 ContentManager from './contentmanager';
import Theme from './theme';
2018-01-30 16:59:27 +01:00
export default class ThemeManager extends ContentManager {
2018-01-30 16:59:27 +01:00
static get localThemes() {
return this.localContent;
}
2018-02-13 17:57:05 +01:00
static get contentType() {
return 'theme';
}
static get moduleName() {
return 'Theme Manager';
}
2018-01-30 16:59:27 +01:00
static get pathId() {
return 'themes';
}
static get loadAllThemes() {
return this.loadAllContent;
}
2018-02-05 15:19:24 +01:00
static get loadContent() { return this.loadTheme }
static async loadTheme(paths, configs, info, main) {
try {
const instance = new Theme({
configs, info, main,
paths: {
contentPath: paths.contentPath,
dirName: paths.dirName,
mainPath: paths.mainPath
}
});
if (!instance.css) instance.recompile();
else if (instance.enabled) instance.enable();
2018-02-05 15:19:24 +01:00
return instance;
} catch (err) {
throw err;
}
}
static enableTheme(theme) {
theme.enable();
}
static disableTheme(theme) {
theme.disable();
}
2018-02-11 21:12:15 +01:00
static reloadTheme(theme) {
theme.recompile();
}
static getConfigAsSCSS(config) {
const variables = [];
for (let category of config) {
for (let setting of category.settings) {
const setting_scss = this.parseSetting(setting);
if (setting_scss) variables.push(`$${setting_scss[0]}: ${setting_scss[1]};`);
2018-02-12 01:44:12 +01:00
}
}
2018-02-12 01:44:12 +01:00
return variables.join('\n');
}
static getConfigAsSCSSMap(config) {
const variables = [];
for (let category of config) {
for (let setting of category.settings) {
const setting_scss = this.parseSetting(setting);
if (setting_scss) variables.push(`${setting_scss[0]}: (${setting_scss[1]})`);
}
}
return '(' + variables.join(', ') + ')';
}
2018-02-12 01:44:12 +01:00
static parseSetting(setting) {
const { type, id, value } = setting;
const name = id.replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-');
if (type === 'array') {
const items = JSON.parse(JSON.stringify(value)) || [];
const settings_json = JSON.stringify(setting.settings);
for (let item of items) {
const settings = JSON.parse(settings_json);
for (let category of settings) {
const newCategory = item.settings.find(c => c.category === category.category);
for (let setting of category.settings) {
const newSetting = newCategory.settings.find(s => s.id === setting.id);
setting.value = setting.old_value = newSetting.value;
setting.changed = false;
}
}
item.settings = settings;
}
console.log('items', items);
// Final comma ensures the variable is a list
const maps = items.map(i => this.getConfigAsSCSSMap(i.settings));
return [name, maps.length ? maps.join(', ') + ',' : '()'];
}
2018-02-12 01:44:12 +01:00
if (type === 'slider') {
return [name, value * setting.multi || 1];
2018-02-12 01:44:12 +01:00
}
2018-02-12 01:48:44 +01:00
if (type === 'dropdown' || type === 'radio') {
return [name, setting.options.find(opt => opt.id === value).value];
2018-02-12 01:44:12 +01:00
}
if (typeof value === 'boolean' || typeof value === 'number') {
return [name, value];
2018-02-12 01:44:12 +01:00
}
if (typeof value === 'string') {
return [name, `'${setting.value.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')}'`];
}
}
2018-01-30 16:59:27 +01:00
}