Compare commits

..

3 Commits

Author SHA1 Message Date
6528100e86 fix stupid bug 2026-04-27 03:08:36 +03:00
4462d3ba84 add events support 2026-04-27 03:05:39 +03:00
bbbfc0f043 Fix working of Gefes IDs 2026-04-27 02:57:40 +03:00
2 changed files with 16 additions and 3 deletions

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(event, 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();
}; };
} }
@ -62,6 +72,7 @@ export abstract class GefestElement {
*/ */
constructor (content : (GefestElement | string)[] | string = []) { constructor (content : (GefestElement | string)[] | string = []) {
this.gefestId = GefestEngine.register(this); this.gefestId = GefestEngine.register(this);
this.attributes["data-gefest-id"] = this.gefestId;
this.content = content; this.content = content;
} }
@ -93,7 +104,7 @@ export abstract class GefestElement {
* @param key The attribute key. * @param key The attribute key.
*/ */
checkReservedAttribute(key: string): void { checkReservedAttribute(key: string): void {
if (({ "style": true, "class": true })[key]) if (({ "style": true, "class": true, "data-gefest-id": true })[key])
throw new Error(`The attribute "${ throw new Error(`The attribute "${
key key
}" is reserved. Use the setPersonalStyle method for styles and setClass method for classes.`); }" is reserved. Use the setPersonalStyle method for styles and setClass method for classes.`);

View File

@ -7,8 +7,10 @@ export class GefestEngine {
static register (element: GefestElement) : string { static register (element: GefestElement) : string {
elements.add(element); elements.add(element);
// Generate GefestID for the element // Generate GefestID for the element
const gefestId = `gefest-${Math.random().toString(36).substr(2, 9)}`; let gefestId : string;
element.setAttribute('data-gefest-id', gefestId); do {
gefestId = `gefest-${Math.random().toString(36).substr(2, 9)}`;
} while(!!elementsMapping.get(gefestId));
elementsMapping.set(gefestId, element); elementsMapping.set(gefestId, element);
return gefestId; return gefestId;