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';
|
2018-02-14 00:23:52 +01:00
|
|
|
import Theme from './theme';
|
2018-01-30 16:59:27 +01:00
|
|
|
|
2018-02-11 20:31:24 +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 {
|
2018-02-11 20:31:24 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:48:03 +01:00
|
|
|
static enableTheme(theme) {
|
|
|
|
theme.enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static disableTheme(theme) {
|
|
|
|
theme.disable();
|
|
|
|
}
|
|
|
|
|
2018-02-11 21:12:15 +01:00
|
|
|
static reloadTheme(theme) {
|
|
|
|
theme.recompile();
|
|
|
|
}
|
|
|
|
|
2018-02-11 20:31:24 +01:00
|
|
|
static getConfigAsSCSS(config) {
|
|
|
|
const variables = [];
|
|
|
|
|
|
|
|
for (let category of config) {
|
|
|
|
for (let setting of category.settings) {
|
2018-02-20 01:01:23 +01:00
|
|
|
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-20 01:01:23 +01:00
|
|
|
|
2018-02-12 01:44:12 +01:00
|
|
|
return variables.join('\n');
|
|
|
|
}
|
2018-02-11 20:31:24 +01:00
|
|
|
|
2018-02-20 01:01:23 +01:00
|
|
|
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, '-');
|
2018-02-11 20:31:24 +01:00
|
|
|
|
2018-02-20 01:01:23 +01:00
|
|
|
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') {
|
2018-02-20 01:01:23 +01:00
|
|
|
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') {
|
2018-02-20 01:01:23 +01:00
|
|
|
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') {
|
2018-02-20 01:01:23 +01:00
|
|
|
return [name, value];
|
2018-02-12 01:44:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
2018-02-20 01:01:23 +01:00
|
|
|
return [name, `'${setting.value.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')}'`];
|
2018-02-11 20:31:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:59:27 +01:00
|
|
|
}
|