2019-05-29 05:48:41 +02:00
import { SettingsCookie } 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-06-03 22:25:08 +02:00
// import EmoteModule from "./emotes";
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-05 06:30:24 +02:00
import EmoteSettings from "../data/emotes/config" ;
import EmoteState from "../data/emotes/state" ;
import TheSettings from "../data/settings/config" ;
import SettingsState from "../data/settings/state" ;
2019-05-31 07:53:11 +02:00
//WebpackModules.getModule(m => m.getSection && m.getProps && !m.getGuildId && !m.getChannel)
//WebpackModules.getByProps("getGuildId", "getSection")
2019-05-29 05:48:41 +02:00
2019-06-04 21:17:23 +02:00
export default new class SettingsManager {
2019-05-29 05:48:41 +02:00
constructor ( ) {
this . renderer = new SettingsRenderer ( { onChange : this . updateSettings . bind ( this ) } ) ;
}
2019-05-31 07:53:11 +02:00
initialize ( ) {
DataStore . initialize ( ) ;
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 . patchSections ( ) ;
2019-06-05 06:30:24 +02:00
this . initializeConfig ( TheSettings , SettingsState ) ;
this . initializeConfig ( EmoteSettings , EmoteState ) ;
}
initializeConfig ( defaultConfig , state ) {
const config = { } ;
for ( let s = 0 ; s < defaultConfig . length ; s ++ ) {
const current = defaultConfig [ s ] ;
if ( current . type != "category" ) { config [ current . id ] = current . value ; }
else {
config [ current . id ] = { } ;
for ( let s = 0 ; s < current . settings . length ; s ++ ) {
const subCurrent = current . settings [ s ] ;
config [ current . id ] [ subCurrent . id ] = subCurrent . value ;
if ( subCurrent . enableWith ) {
Object . defineProperty ( subCurrent , "disabled" , {
get : ( ) => {
return ! state [ current . id ] [ subCurrent . enableWith ] ;
}
} ) ;
}
}
}
}
console . log ( defaultConfig ) ;
console . log ( config ) ;
Object . assign ( state , config ) ;
}
buildSettingsPanel ( config , state , onChange ) {
config . forEach ( section => {
section . settings . forEach ( item => item . value = state [ section . id ] [ item . id ] ) ;
} ) ;
return this . renderer . getSettingsPanel ( config , onChange ) ;
2019-05-31 07:53:11 +02:00
}
async patchSections ( ) {
const UserSettings = await this . getUserSettings ( ) ; // data.returnValue.type;
Utilities . monkeyPatch ( UserSettings . prototype , "generateSections" , { after : ( data ) => {
2019-06-05 06:30:24 +02:00
// console.log(data); /* eslint-disable-line no-console */
2019-05-31 07:53:11 +02:00
data . returnValue . splice ( 23 , 0 , { section : "DIVIDER" } ) ;
data . returnValue . splice ( 24 , 0 , { section : "HEADER" , label : "BandagedBD" } ) ;
2019-06-05 06:30:24 +02:00
// data.returnValue.splice(25, 0, {section: "BBD Settings", label: "Settings", element: () => this.renderer.core2});
data . returnValue . splice ( 25 , 0 , { section : "BBD Settings" , label : "Settings" , element : ( ) => this . buildSettingsPanel ( TheSettings , SettingsState , this . updateSettings ) } ) ;
data . returnValue . splice ( 26 , 0 , { section : "BBD Emotes" , label : "Emotes" , element : ( ) => this . buildSettingsPanel ( EmoteSettings , EmoteState , this . updateSettings ) } ) ;
data . returnValue . splice ( 27 , 0 , { section : "BBD Test" , label : "Test Tab" , onClick : function ( ) { Toasts . success ( "This can just be a click listener!" , { forceShow : true } ) ; } } ) ;
data . returnValue . splice ( 28 , 0 , { section : "CUSTOM" , element : ( ) => this . renderer . attribution } ) ;
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 ( ) {
DataStore . setSettingGroup ( "settings" , SettingsCookie ) ;
}
loadSettings ( ) {
return DataStore . getSettingGroup ( "settings" ) ;
}
2019-05-29 05:48:41 +02:00
updateSettings ( id , enabled ) {
2019-06-05 06:30:24 +02:00
if ( arguments . length == 3 ) {
SettingsState [ arguments [ 0 ] ] [ arguments [ 1 ] ] = arguments [ 2 ] ;
Events . dispatch ( "setting-updated" , arguments [ 0 ] , arguments [ 1 ] , arguments [ 2 ] ) ;
console . log ( SettingsState ) ;
return ;
}
2019-05-30 17:44:05 +02:00
Events . dispatch ( "setting-updated" , "Modules" , id , enabled ) ;
2019-05-29 05:48:41 +02:00
SettingsCookie [ id ] = enabled ;
2019-06-03 22:25:08 +02:00
// if (id == "bda-es-4") {
// if (enabled) EmoteModule.autoCapitalize();
// else EmoteModule.disableAutoCapitalize();
// }
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-05-31 07:53:11 +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
if ( SettingsCookie [ "fork-ps-5" ] ) {
ContentManager . watchContent ( "plugin" ) ;
ContentManager . watchContent ( "theme" ) ;
}
2019-05-31 07:53:11 +02:00
this . saveSettings ( ) ;
2019-05-29 05:48:41 +02:00
}
} ;