Solved mistake for updating of a style. And.. anything else.. don't remember, and I'm tired, so something like that XD

This commit is contained in:
FullGreaM 2026-04-29 01:58:22 +03:00
parent 56a5639c57
commit b47fe4917e
4 changed files with 92 additions and 57 deletions

View File

@ -4,12 +4,22 @@ import { DarkStyle, LightStyle, SupportedStyles } from "./styles";
import { ControllerAPI } from "./api/RESTful"; import { ControllerAPI } from "./api/RESTful";
import { NewsFeed } from "./pages/feed/news"; import { NewsFeed } from "./pages/feed/news";
import { AuthenticationPage } from "./pages/auth/auth"; import { AuthenticationPage } from "./pages/auth/auth";
import { DefaultElement } from "./pages/default-element";
/*declare global {
interface Window {
selectedStyle: SupportedStyles;
}
}*/
const styleLinker : {
selectedStyle: SupportedStyles
} = {
selectedStyle: new LightStyle()
};
// I'll add a dark mode toggle later, but for now let's just use the light style by default // I'll add a dark mode toggle later, but for now let's just use the light style by default
const defaultStyle = new LightStyle(); //window.selectedStyle = new LightStyle();
const styleMemLink = { const currentStyle: () => SupportedStyles = () => styleLinker.selectedStyle;
currentStyle: () : SupportedStyles => defaultStyle
};
{ // I hate trash in global scope, so I wrap it in a block { // I hate trash in global scope, so I wrap it in a block
const appElement = document.getElementById("app"); const appElement = document.getElementById("app");
@ -22,51 +32,58 @@ const api : ControllerAPI = new ControllerAPI();
async function authInit() { async function authInit() {
console.log("User is authenticated"); console.log("User is authenticated");
const newsFeed = await api.getNews(true) as NewsFeed; return await api.getNews(true) as NewsFeed;
newsFeed.style = styleMemLink.currentStyle;
const appElement = document.getElementById("app");
if (appElement)
appElement.innerHTML = newsFeed.build();
} }
async function logInInit () { async function logInInit () {
console.log("User is not authenticated"); console.log("User isn't authenticated");
const authPage = await api.getAuthRules(true) as AuthenticationPage; return await api.getAuthRules(true) as AuthenticationPage;
authPage.style = styleMemLink.currentStyle;
const appElement = document.getElementById("app");
if (appElement)
appElement.innerHTML = authPage.build();
} }
async function changeStyleLogic () { async function changeStyleLogic (activeElement: DefaultElement, navbar: DefaultElement) {
const themes : SupportedStyles[] = [ const themes : SupportedStyles[] = [
defaultStyle, new LightStyle(),
new DarkStyle() new DarkStyle()
]; ];
let choosed = 0; let choosed = 0;
themeToggle.onClick = () => { themeToggle.onClick = () => {
choosed++; choosed++;
if (choosed >= themes.length) let choosedTheme: SupportedStyles | undefined = themes[choosed];
if (choosedTheme === undefined) {
choosedTheme = themes[0] as SupportedStyles;
choosed = 0; choosed = 0;
}
console.log('choosedTheme', choosedTheme);
styleMemLink.currentStyle = () => themes[choosed] as SupportedStyles; styleLinker.selectedStyle = choosedTheme;
GefestEngine.render(); GefestEngine.render([activeElement.gefestId, navbar.gefestId]);
}; };
} }
async function main() { async function main() {
await changeStyleLogic();
// init navbar // init navbar
const navbar = new Navbar(); const navbar = new Navbar();
navbar.style = styleMemLink.currentStyle; navbar.style = currentStyle;
document.body.innerHTML = "<div id='app'></div>" + navbar.build();
let activeElement: DefaultElement;
if (await api.isAuthenticated()) { if (await api.isAuthenticated()) {
await authInit(); activeElement = await authInit();
} else { } else {
await logInInit(); activeElement = await logInInit();
} }
const appElement : DefaultElement = new (class extends DefaultElement {
constructor() {
super([ activeElement ]);
this.id = "app";
}
});
appElement.style = currentStyle;
//document.body.innerHTML = appElement.build() + navbar.build();
document.body.innerHTML = appElement.build();
await changeStyleLogic(appElement, navbar);
} }
GefestEngine.main(main).catch(error => { GefestEngine.main(main).catch(error => {

View File

@ -151,7 +151,7 @@ export abstract class GefestElement {
/** /**
* Adds a class to the element's class list and updates the "class" attribute accordingly. * Adds a class to the element's class list and updates the "class" attribute accordingly.
* @param className The class name to add. * @param className The class name to add.
*/ */
addClass(className: string): void { addClass(className: string): void {
this.classList.add(className); this.classList.add(className);
this.attributes.class = Array.from(this.classList).join(' '); this.attributes.class = Array.from(this.classList).join(' ');
@ -160,7 +160,7 @@ export abstract class GefestElement {
/** /**
* Removes a class from the element's class list and updates the "class" attribute accordingly. * Removes a class from the element's class list and updates the "class" attribute accordingly.
* @param className The class name to remove. * @param className The class name to remove.
*/ */
removeClass(className: string): void { removeClass(className: string): void {
this.classList.delete(className); this.classList.delete(className);
this.attributes.class = Array.from(this.classList).join(' '); this.attributes.class = Array.from(this.classList).join(' ');
@ -221,9 +221,23 @@ export abstract class GefestElement {
* @returns The attributes string. * @returns The attributes string.
*/ */
protected attributesToString (): string { protected attributesToString (): string {
return Object.entries(this.attributes) let style : GefestStyle | null = null;
.map(([key, value]) => `${key}="${value}"`) if (typeof this.style === 'function') {
.join(' '); style = this.style();
} else {
style = this.style;
}
let attributes = Object.assign({}, this.attributes);
if (style instanceof GefestStyle)
attributes.class = [
attributes.class,
style.getClasses(this).join(" ")
].join(" ");
return Object.entries(
attributes
).map(([key, value]) => `${key}="${value}"`).join(' ');
} }
/** /**

View File

@ -1,7 +1,6 @@
import { GefestElement } from "./element"; import { GefestElement } from "./element";
export abstract class GefestStyle { export abstract class GefestStyle {
applyStyle (html: string, element ?: GefestElement): string { abstract getClasses(element : GefestElement): string[];
throw new Error('You need to implement the applyStyle method in the child class'); abstract applyStyle (html: string, element ?: GefestElement): string;
}
} }

View File

@ -4,63 +4,68 @@ import { Navbar } from "./pages/navbar";
import { isNewsUI, NewsUserInterface } from "./pages/feed/news"; import { isNewsUI, NewsUserInterface } from "./pages/feed/news";
abstract class SkeletonStyle extends GefestStyle { abstract class SkeletonStyle extends GefestStyle {
protected abstract navbar(element: GefestElement): void; protected abstract navbar(styleClasses: Set<string>, element : GefestElement): void;
protected abstract newsui(element: GefestElement): void; protected abstract newsui(styleClasses: Set<string>, element : GefestElement): void;
protected abstract styleHandle(element: GefestElement): void; protected abstract styleHandle(styleClasses: Set<string>, element : GefestElement): void;
applyStyle(html: string, element?: GefestElement): string { applyStyle(html: string, element?: GefestElement): string {
if (!element) if (!element)
element = GefestElement.fromHTML(html); element = GefestElement.fromHTML(html);
//element.addClass("light-mode");
this.styleHandle(element);
this.navbar(element);
this.newsui(element);
return element.build(true); return element.build(true);
} }
getClasses(element: GefestElement): string[] {
const styleClasses : Set<string> = new Set<string>();
this.styleHandle(styleClasses, element);
this.navbar(styleClasses, element);
this.newsui(styleClasses, element);
return Array.from(styleClasses);
}
} }
export class DarkStyle extends SkeletonStyle { export class DarkStyle extends SkeletonStyle {
newsui(element: GefestElement): void { newsui(styleClasses: Set<string>, element : GefestElement): void {
if (isNewsUI(element)) { if (isNewsUI(element)) {
element.removeClass("bg-dark"); styleClasses.delete("bg-dark");
element.addClass("dark-news-ui"); styleClasses.add("dark-news-ui");
} }
if (element instanceof NewsUserInterface) { if (element instanceof NewsUserInterface) {
element.removeClass("bg-dark"); styleClasses.delete("bg-dark");
} }
} }
navbar(element: GefestElement): void { navbar(styleClasses: Set<string>, element : GefestElement): void {
if (element instanceof Navbar) { if (element instanceof Navbar) {
element.addClass("dark-navbar"); styleClasses.add("dark-navbar");
} }
} }
protected styleHandle(element: GefestElement): void { protected styleHandle(styleClasses: Set<string>): void {
element.addClass("bg-dark"); styleClasses.add("bg-dark");
element.addClass("text-light"); styleClasses.add("text-light");
} }
} }
export class LightStyle extends SkeletonStyle { export class LightStyle extends SkeletonStyle {
newsui(element: GefestElement): void { newsui(styleClasses: Set<string>, element : GefestElement): void {
if (isNewsUI(element)) { if (isNewsUI(element)) {
element.removeClass("bg-light"); styleClasses.delete("bg-light");
element.addClass("light-news-ui"); styleClasses.add("light-news-ui");
} }
if (element instanceof NewsUserInterface) { if (element instanceof NewsUserInterface) {
element.removeClass("bg-light"); styleClasses.delete("bg-light");
} }
} }
navbar(element: GefestElement): void { navbar(styleClasses: Set<string>, element : GefestElement): void {
//element.addClass("light-navbar"); //element.addClass("light-navbar");
} }
protected styleHandle(element: GefestElement): void { protected styleHandle(styleClasses: Set<string>, element : GefestElement): void {
element.addClass("bg-light"); styleClasses.add("bg-light");
element.addClass("text-dark"); styleClasses.add("bg-light");
styleClasses.add("text-dark");
} }
} }