2019-05-29 05:48:41 +02:00
|
|
|
import {SettingsCookie, ThemeCookie, Themes} from "data";
|
|
|
|
import ContentManager from "./contentmanager";
|
|
|
|
import Utilities from "./utilities";
|
|
|
|
import Emitter from "./emitter";
|
|
|
|
import DataStore from "./datastore";
|
2019-05-31 07:53:11 +02:00
|
|
|
import {Toasts, Modals} from "ui";
|
2019-05-28 20:19:48 +02:00
|
|
|
|
|
|
|
function ThemeModule() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ThemeModule.prototype.loadThemes = function () {
|
|
|
|
this.loadThemeData();
|
2019-05-28 23:27:25 +02:00
|
|
|
const errors = ContentManager.loadThemes();
|
2019-05-29 06:29:15 +02:00
|
|
|
const themes = Object.keys(Themes);
|
2019-05-28 20:19:48 +02:00
|
|
|
|
2019-05-29 06:29:15 +02:00
|
|
|
for (let i = 0; i < themes.length; i++) {
|
|
|
|
const name = Themes[themes[i]].name;
|
2019-05-29 05:48:41 +02:00
|
|
|
if (!ThemeCookie[name]) ThemeCookie[name] = false;
|
|
|
|
if (ThemeCookie[name]) $("head").append($("<style>", {id: Utilities.escapeID(name), text: unescape(Themes[name].css)}));
|
2019-05-28 20:19:48 +02:00
|
|
|
}
|
2019-05-29 06:29:15 +02:00
|
|
|
for (const theme in ThemeCookie) {
|
2019-05-29 05:48:41 +02:00
|
|
|
if (!Themes[theme]) delete ThemeCookie[theme];
|
2019-05-28 20:19:48 +02:00
|
|
|
}
|
|
|
|
this.saveThemeData();
|
2019-05-28 23:27:25 +02:00
|
|
|
return errors;
|
2019-05-29 05:48:41 +02:00
|
|
|
// if (SettingsCookie["fork-ps-5"]) ContentManager.watchContent("theme");
|
2019-05-28 20:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.enableTheme = function(theme, reload = false) {
|
2019-05-29 05:48:41 +02:00
|
|
|
ThemeCookie[theme] = true;
|
2019-05-28 20:19:48 +02:00
|
|
|
this.saveThemeData();
|
2019-05-29 05:48:41 +02:00
|
|
|
$("head").append($("<style>", {id: Utilities.escapeID(theme), text: unescape(Themes[theme].css)}));
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-2"] && !reload) Toasts.show(`${Themes[theme].name} v${Themes[theme].version} has been applied.`);
|
2019-05-28 20:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.disableTheme = function(theme, reload = false) {
|
2019-05-29 05:48:41 +02:00
|
|
|
ThemeCookie[theme] = false;
|
2019-05-28 20:19:48 +02:00
|
|
|
this.saveThemeData();
|
2019-05-29 05:48:41 +02:00
|
|
|
$(`#${Utilities.escapeID(Themes[theme].name)}`).remove();
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-2"] && !reload) Toasts.show(`${Themes[theme].name} v${Themes[theme].version} has been disabled.`);
|
2019-05-28 20:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.toggleTheme = function(theme) {
|
2019-05-29 05:48:41 +02:00
|
|
|
if (ThemeCookie[theme]) this.disableTheme(theme);
|
2019-05-28 20:19:48 +02:00
|
|
|
else this.enableTheme(theme);
|
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.loadTheme = function(filename) {
|
|
|
|
const error = ContentManager.loadContent(filename, "theme");
|
|
|
|
if (error) {
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-1"]) Modals.showContentErrors({themes: [error]});
|
|
|
|
if (SettingsCookie["fork-ps-2"]) Toasts.show(`${filename} could not be loaded. It may not have been loaded.`, {type: "error"});
|
2019-05-29 05:48:41 +02:00
|
|
|
return Utilities.err("ContentManager", `${filename} could not be loaded.`, error);
|
2019-05-28 20:19:48 +02:00
|
|
|
}
|
2019-05-29 05:48:41 +02:00
|
|
|
const theme = Object.values(Themes).find(p => p.filename == filename);
|
|
|
|
Utilities.log("ContentManager", `${theme.name} v${theme.version} was loaded.`);
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-2"]) Toasts.show(`${theme.name} v${theme.version} was loaded.`, {type: "success"});
|
2019-05-29 05:48:41 +02:00
|
|
|
Emitter.dispatch("theme-loaded", theme.name);
|
2019-05-28 20:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.unloadTheme = function(filenameOrName) {
|
2019-05-29 05:48:41 +02:00
|
|
|
const bdtheme = Object.values(Themes).find(p => p.filename == filenameOrName) || Themes[filenameOrName];
|
2019-05-28 20:19:48 +02:00
|
|
|
if (!bdtheme) return;
|
|
|
|
const theme = bdtheme.name;
|
2019-05-29 05:48:41 +02:00
|
|
|
if (ThemeCookie[theme]) this.disableTheme(theme, true);
|
|
|
|
const error = ContentManager.unloadContent(Themes[theme].filename, "theme");
|
|
|
|
delete Themes[theme];
|
2019-05-28 20:19:48 +02:00
|
|
|
if (error) {
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-1"]) Modals.showContentErrors({themes: [error]});
|
|
|
|
if (SettingsCookie["fork-ps-2"]) Toasts.show(`${theme} could not be unloaded. It may have not been loaded yet.`, {type: "error"});
|
2019-05-29 05:48:41 +02:00
|
|
|
return Utilities.err("ContentManager", `${theme} could not be unloaded. It may have not been loaded yet.`, error);
|
2019-05-28 20:19:48 +02:00
|
|
|
}
|
2019-05-29 05:48:41 +02:00
|
|
|
Utilities.log("ContentManager", `${theme} was unloaded.`);
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-2"]) Toasts.show(`${theme} was unloaded.`, {type: "success"});
|
2019-05-29 05:48:41 +02:00
|
|
|
Emitter.dispatch("theme-unloaded", theme);
|
2019-05-28 20:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.reloadTheme = function(filenameOrName) {
|
2019-05-29 05:48:41 +02:00
|
|
|
const bdtheme = Object.values(Themes).find(p => p.filename == filenameOrName) || Themes[filenameOrName];
|
2019-05-28 20:19:48 +02:00
|
|
|
if (!bdtheme) return this.loadTheme(filenameOrName);
|
|
|
|
const theme = bdtheme.name;
|
2019-05-29 05:48:41 +02:00
|
|
|
const error = ContentManager.reloadContent(Themes[theme].filename, "theme");
|
|
|
|
if (ThemeCookie[theme]) this.disableTheme(theme, true), this.enableTheme(theme, true);
|
2019-05-28 20:19:48 +02:00
|
|
|
if (error) {
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-1"]) Modals.showContentErrors({themes: [error]});
|
|
|
|
if (SettingsCookie["fork-ps-2"]) Toasts.show(`${theme} could not be reloaded.`, {type: "error"});
|
2019-05-29 05:48:41 +02:00
|
|
|
return Utilities.err("ContentManager", `${theme} could not be reloaded.`, error);
|
2019-05-28 20:19:48 +02:00
|
|
|
}
|
2019-05-29 05:48:41 +02:00
|
|
|
Utilities.log("ContentManager", `${theme} v${Themes[theme].version} was reloaded.`);
|
2019-05-31 07:53:11 +02:00
|
|
|
if (SettingsCookie["fork-ps-2"]) Toasts.show(`${theme} v${Themes[theme].version} was reloaded.`, {type: "success"});
|
2019-05-29 05:48:41 +02:00
|
|
|
Emitter.dispatch("theme-reloaded", theme);
|
2019-05-28 20:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.updateThemeList = function() {
|
|
|
|
const results = ContentManager.loadNewContent("theme");
|
|
|
|
for (const filename of results.added) this.loadTheme(filename);
|
|
|
|
for (const name of results.removed) this.unloadTheme(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.loadThemeData = function() {
|
2019-05-29 05:48:41 +02:00
|
|
|
const saved = DataStore.getSettingGroup("themes");
|
|
|
|
if (!saved) return;
|
|
|
|
Object.assign(ThemeCookie, saved);
|
2019-05-28 20:19:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ThemeModule.prototype.saveThemeData = function () {
|
2019-05-29 05:48:41 +02:00
|
|
|
DataStore.setSettingGroup("themes", ThemeCookie);
|
2019-05-28 23:27:25 +02:00
|
|
|
};
|
|
|
|
|
2019-05-29 05:48:41 +02:00
|
|
|
export default new ThemeModule();
|