BetterDiscordApp-rauenzi/src/modules/settingsmanager.js

212 lines
8.7 KiB
JavaScript
Raw Normal View History

2019-06-06 21:57:25 +02:00
import {SettingsConfig, SettingsState} from "data";
2019-05-31 07:53:11 +02:00
import DataStore from "./datastore";
2019-05-29 05:48:41 +02:00
import ContentManager from "./contentmanager";
import BdApi from "./pluginapi";
2019-05-30 17:44:05 +02:00
import Events from "./emitter";
2019-05-31 07:53:11 +02:00
import WebpackModules from "./webpackmodules";
2019-05-29 05:48:41 +02:00
import {SettingsPanel as SettingsRenderer} from "ui";
2019-05-31 07:53:11 +02:00
import Utilities from "./utilities";
2019-06-04 06:18:15 +02:00
import {Toasts} from "ui";
2019-05-31 07:53:11 +02:00
2019-06-04 21:17:23 +02:00
export default new class SettingsManager {
2019-05-29 05:48:41 +02:00
constructor() {
2019-06-06 21:57:25 +02:00
this.renderer = new SettingsRenderer();
this.config = SettingsConfig;
this.state = SettingsState;
this.setup(SettingsConfig, SettingsState);
2019-05-29 05:48:41 +02:00
}
2019-05-31 07:53:11 +02:00
initialize() {
DataStore.initialize();
2019-06-06 21:57:25 +02:00
// if (!DataStore.getSettingGroup("settings")) return this.saveSettings();
// const savedSettings = this.loadSettings();
// $("<style id=\"customcss\">").text(atob(DataStore.getBDData("bdcustomcss"))).appendTo(document.head);
// for (const setting in savedSettings) {
// if (savedSettings[setting] !== undefined) SettingsCookie[setting] = savedSettings[setting];
// }
// this.saveSettings();
this.loadSettings();
2019-05-31 07:53:11 +02:00
this.patchSections();
2019-06-05 06:30:24 +02:00
2019-06-06 21:57:25 +02:00
// Object.assign(this.state, this.defaultState);
// this.initializeConfig(EmoteSettings, EmoteState);
2019-06-05 06:30:24 +02:00
}
2019-06-06 21:57:25 +02:00
getPath(path, collectionId = "", categoryId = "") {
const collection = path.length == 3 ? path[0] : collectionId;
const category = path.length == 3 ? path[1] : path.length == 2 ? path[0] : categoryId;
const setting = path[path.length - 1];
return {collection, category, setting};
}
setup(collections, state) {
2019-06-05 06:30:24 +02:00
const config = {};
2019-06-06 21:57:25 +02:00
for (let c = 0; c < collections.length; c++) {
const collection = collections[c];
const categories = collections[c].settings;
config[collection.id] = {};
for (let s = 0; s < categories.length; s++) {
const category = categories[s];
if (category.type != "category") {config[collection.id][category.id] = category.value;}
else {
config[collection.id][category.id] = {};
for (let s = 0; s < category.settings.length; s++) {
const setting = category.settings[s];
config[collection.id][category.id][setting.id] = setting.value;
if (setting.enableWith) {
const path = this.getPath(setting.enableWith.split("."), collection.id, category.id);
Object.defineProperty(setting, "disabled", {
get: () => {
return !state[path.collection][path.category][path.setting];
}
});
}
2019-06-05 06:30:24 +02:00
}
}
}
2019-06-06 21:57:25 +02:00
if (collection.enableWith) {
const path = this.getPath(collection.enableWith.split("."));
Object.defineProperty(collection, "disabled", {
get: () => {
return !state[path.collection][path.category][path.setting];
}
});
}
2019-06-05 06:30:24 +02:00
}
2019-06-06 21:57:25 +02:00
this.defaultState = config;
Object.assign(this.state, this.defaultState);
2019-06-05 06:30:24 +02:00
}
2019-06-06 06:28:43 +02:00
buildSettingsPanel(title, config, state, onChange) {
2019-06-05 06:30:24 +02:00
config.forEach(section => {
section.settings.forEach(item => item.value = state[section.id][item.id]);
});
2019-06-06 06:28:43 +02:00
return this.renderer.getSettingsPanel(title, config, onChange);
2019-05-31 07:53:11 +02:00
}
async patchSections() {
2019-06-06 21:57:25 +02:00
const UserSettings = await this.getUserSettings();
2019-05-31 07:53:11 +02:00
Utilities.monkeyPatch(UserSettings.prototype, "generateSections", {after: (data) => {
2019-06-06 06:28:43 +02:00
let location = data.returnValue.findIndex(s => s.section.toLowerCase() == "linux") + 1;
const insert = (section) => {
data.returnValue.splice(location, 0, section);
location++;
};
console.log(data); /* eslint-disable-line no-console */
insert({section: "DIVIDER"});
insert({section: "HEADER", label: "BandagedBD"});
2019-06-06 21:57:25 +02:00
for (const collection of this.config) {
if (collection.disabled) continue;
insert({
section: collection.name,
label: collection.name,
element: () => this.buildSettingsPanel(collection.name, collection.settings, SettingsState[collection.id], this.onSettingChange.bind(this, collection.id))
});
}
2019-06-06 06:28:43 +02:00
insert({section: "BBD Test", label: "Test Tab", onClick: function() {Toasts.success("This can just be a click listener!", {forceShow: true});}});
insert({section: "CUSTOM", element: () => this.renderer.attribution});
2019-05-31 07:53:11 +02:00
}});
2019-06-06 06:28:43 +02:00
this.forceUpdate();
}
forceUpdate() {
2019-05-31 07:53:11 +02:00
const viewClass = WebpackModules.getByProps("standardSidebarView").standardSidebarView.split(" ")[0];
const node = document.querySelector(`.${viewClass}`);
Utilities.getInternalInstance(node).return.return.return.return.return.return.stateNode.forceUpdate();
}
getUserSettings() {
return new Promise(resolve => {
const cancel = Utilities.monkeyPatch(WebpackModules.getByProps("getUserSettingsSections").default.prototype, "render", {after: (data) => {
resolve(data.returnValue.type);
data.thisObject.forceUpdate();
cancel();
}});
});
}
saveSettings() {
2019-06-06 21:57:25 +02:00
DataStore.setData("settings", this.state);
2019-05-31 07:53:11 +02:00
}
loadSettings() {
2019-06-06 21:57:25 +02:00
const previousState = DataStore.getData("settings");
if (!previousState) return this.saveSettings();
for (const collection in this.defaultState) {
if (!previousState[collection]) Object.assign(previousState, {[collection]: this.defaultState[collection]});
for (const category in this.defaultState[collection]) {
if (!previousState[collection][category]) Object.assign(previousState[collection][category], {[category]: this.defaultState[collection][category]});
for (const setting in this.defaultState[collection][category]) {
if (previousState[collection][category][setting] == undefined) continue;
this.state[collection][category][setting] = previousState[collection][category][setting];
}
}
}
this.saveSettings(); // in case new things were added
2019-05-31 07:53:11 +02:00
}
2019-06-06 21:57:25 +02:00
onSettingChange(collection, category, id, value) {
const before = this.config.filter(c => c.disabled).length;
this.state[collection][category][id] = value;
Events.dispatch("setting-updated", collection, category, id, value);
const after = this.config.filter(c => c.disabled).length;
this.saveSettings();
if (before != after) this.forceUpdate();
2019-06-06 06:28:43 +02:00
}
2019-06-06 21:57:25 +02:00
getSetting(collection, category, id) {
if (arguments.length == 2) return this.config[0].find(c => c.id == arguments[0]).settings.find(s => s.id == arguments[1]);
return this.config.find(c => c.id == collection).find(c => c.id == category).settings.find(s => s.id == id);
2019-06-06 06:28:43 +02:00
}
2019-06-06 21:57:25 +02:00
get(collection, category, id) {
if (arguments.length == 2) return this.state[this.config[0].id][arguments[0]][arguments[1]];
return this.state[collection][category][id];
}
on(collection, category, identifier, callback) {
const handler = (col, cat, id, value) => {
if (col !== collection || cat !== category || id !== identifier) return;
callback(value);
};
Events.on("setting-updated", handler);
return () => {Events.off("setting-updated", handler);};
2019-06-06 06:28:43 +02:00
}
updateSettings(collection, category, id, enabled) {
2019-05-29 05:48:41 +02:00
if (id == "fork-ps-5") {
if (enabled) {
ContentManager.watchContent("plugin");
ContentManager.watchContent("theme");
}
else {
ContentManager.unwatchContent("plugin");
ContentManager.unwatchContent("theme");
}
}
if (id == "fork-wp-1") {
BdApi.setWindowPreference("transparent", enabled);
if (enabled) BdApi.setWindowPreference("backgroundColor", null);
else BdApi.setWindowPreference("backgroundColor", "#2f3136");
}
2019-06-06 06:28:43 +02:00
// this.saveSettings();
2019-05-29 05:48:41 +02:00
}
initializeSettings() {
2019-06-03 22:25:08 +02:00
// if (SettingsCookie["bda-es-4"]) EmoteModule.autoCapitalize();
2019-05-29 05:48:41 +02:00
2019-06-06 21:57:25 +02:00
// if (SettingsCookie["fork-ps-5"]) {
// ContentManager.watchContent("plugin");
// ContentManager.watchContent("theme");
// }
2019-05-29 05:48:41 +02:00
2019-05-31 07:53:11 +02:00
this.saveSettings();
2019-05-29 05:48:41 +02:00
}
};