add events support
This commit is contained in:
parent
bbbfc0f043
commit
4462d3ba84
@ -2,6 +2,7 @@ import { GefestEngine } from './engine';
|
||||
import { GefestStyle } from './style';
|
||||
|
||||
// The general class of Gefest
|
||||
type gefestElementEvents = "onClick";
|
||||
export abstract class GefestElement {
|
||||
style: GefestStyle | null = null;
|
||||
isHidden: boolean = false;
|
||||
@ -10,9 +11,16 @@ export abstract class GefestElement {
|
||||
protected content: (GefestElement | string)[] | string;
|
||||
protected attributes: Record<string, string> = {};
|
||||
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;
|
||||
|
||||
emit(event: gefestElementEvents, data: unknown = undefined) {
|
||||
for (let handler of this.eventHandlers)
|
||||
new Promise((rs) => rs(handler("onClick", data)));
|
||||
}
|
||||
|
||||
set onClick(handler: (() => void) | null) {
|
||||
if (handler) {
|
||||
this.clickHandler = handler;
|
||||
@ -26,6 +34,8 @@ export abstract class GefestElement {
|
||||
return null;
|
||||
return () => {
|
||||
this.clickHandler!();
|
||||
this.emit("onClick");
|
||||
|
||||
GefestEngine.activateOnClick();
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user