| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- export class color_theme {
- #button;
- #themes;
- get themes() {
- return Object.keys(this.#themes);
- }
- theme_name(target) {
- return this.#themes[target];
- }
- constructor(button, themes = null) {
- this.#button = button;
- this.#themes = themes;
- if (this.#themes === null) {
- this.#themes = {
- "dark-theme": "Dark",
- "white-theme": "White"
- };
- }
- this.#load();
- this.#button.addEventListener("click", () => {
- this.change();
- });
- }
- get current() {
- if (localStorage.hasOwnProperty("theme")) {
- return localStorage.getItem("theme");
- }
- return this.themes[this.themes.length - 1];
- }
- #load() {
- this.#show(this.current);
- }
- #save(target) {
- localStorage.setItem("theme", target);
- }
- #show(target) {
- const themes = this.themes;
- const body = document.querySelector("body");
-
- body.classList.forEach(count => {
- if (themes.indexOf(count) !== -1) {
- body.classList.remove(count);
- }
- });
- body.classList.add(target);
- }
- change() {
- const themes = this.themes;
- const current = this.current;
- let position = themes.indexOf(current) + 1;
- if (position === themes.length) {
- position = 0;
- }
- const updated = themes[position];
- this.#save(updated);
- this.#show(updated)
- }
- }
|