add events support

This commit is contained in:
FullGreaM 2026-04-27 03:05:39 +03:00
parent bbbfc0f043
commit 4462d3ba84

View File

@ -2,6 +2,7 @@ import { GefestEngine } from './engine';
import { GefestStyle } from './style'; import { GefestStyle } from './style';
// The general class of Gefest // The general class of Gefest
type gefestElementEvents = "onClick";
export abstract class GefestElement { export abstract class GefestElement {
style: GefestStyle | null = null; style: GefestStyle | null = null;
isHidden: boolean = false; isHidden: boolean = false;
@ -10,9 +11,16 @@ export abstract class GefestElement {
protected content: (GefestElement | string)[] | string; protected content: (GefestElement | string)[] | string;
protected attributes: Record<string, string> = {}; protected attributes: Record<string, string> = {};
protected classList: Set<string> = new Set(); protected classList: Set<string> = new Set();
//protected eventHandlers: Record<string, (eventType: string, eventData: unknown) => void> = {};
protected eventHandlers: ((eventType: string, eventData: unknown) => void)[] = [];
protected clickHandler: (() => void) | null = null; protected clickHandler: (() => void) | null = null;
emit(event: gefestElementEvents, data: unknown = undefined) {
for (let handler of this.eventHandlers)
new Promise((rs) => rs(handler("onClick", data)));
}
set onClick(handler: (() => void) | null) { set onClick(handler: (() => void) | null) {
if (handler) { if (handler) {
this.clickHandler = handler; this.clickHandler = handler;
@ -26,6 +34,8 @@ export abstract class GefestElement {
return null; return null;
return () => { return () => {
this.clickHandler!(); this.clickHandler!();
this.emit("onClick");
GefestEngine.activateOnClick(); GefestEngine.activateOnClick();
}; };
} }