canopy/www/js/themeManager.js

90 lines
No EOL
2.6 KiB
JavaScript

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2026 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
/**
* Class to manage
*/
class themeManager{
/**
* Instantiates new Theme Manager
*/
constructor(){
/**
* Map of all available themes
*/
this.themes = new Map([
["Movie Night", "/css/theme/movie-night.css"],
["The Network", "/css/theme/the-network.css"]
]);
/**
* Sets default theme for new users
*/
this.defaultTheme = "The Network";
/**
* Define property to hold theme link DOM node
*/
this.themeLink = null;
//Set theme from preference
this.setTheme(localStorage.getItem("theme"));
}
/**
* Sets theme by name
* @param {String} theme - name of theme to set, keep blank for default theme
*/
setTheme(theme = localStorage.getItem("theme")){
//If we didnt get a theme to set
if(theme == null){
//Use the first theme in the list
theme = this.defaultTheme;
}
//If we have a link already setup
if(this.themeLink != null){
//Get Ridda it!
this.themeLink.remove();
}
//Create new link element and use it to set themeLink
this.themeLink = this.genThemeLink(theme);
//Append themeLink to header
document.querySelector('head').appendChild(this.themeLink);
//Save theme choice to local storage
localStorage.setItem("theme", theme);
}
genThemeLink(theme = localStorage.getItem("theme")){
//Create new link element and use it to set themeLink
const themeLink = document.createElement('link');
//Set themeLink attributes
themeLink.type = "text/css";
themeLink.rel = "stylesheet";
//Set theme href to theme location
themeLink.href = this.themes.get(theme);
return themeLink;
}
}
ThemeManager = new themeManager();