Compare commits
3 Commits
8877f3e4af
...
6528100e86
| Author | SHA1 | Date | |
|---|---|---|---|
| 6528100e86 | |||
| 4462d3ba84 | |||
| bbbfc0f043 |
@ -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(event, 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();
|
||||
};
|
||||
}
|
||||
@ -62,6 +72,7 @@ export abstract class GefestElement {
|
||||
*/
|
||||
constructor (content : (GefestElement | string)[] | string = []) {
|
||||
this.gefestId = GefestEngine.register(this);
|
||||
this.attributes["data-gefest-id"] = this.gefestId;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@ -93,7 +104,7 @@ export abstract class GefestElement {
|
||||
* @param key The attribute key.
|
||||
*/
|
||||
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 "${
|
||||
key
|
||||
}" is reserved. Use the setPersonalStyle method for styles and setClass method for classes.`);
|
||||
|
||||
@ -7,8 +7,10 @@ export class GefestEngine {
|
||||
static register (element: GefestElement) : string {
|
||||
elements.add(element);
|
||||
// Generate GefestID for the element
|
||||
const gefestId = `gefest-${Math.random().toString(36).substr(2, 9)}`;
|
||||
element.setAttribute('data-gefest-id', gefestId);
|
||||
let gefestId : string;
|
||||
do {
|
||||
gefestId = `gefest-${Math.random().toString(36).substr(2, 9)}`;
|
||||
} while(!!elementsMapping.get(gefestId));
|
||||
elementsMapping.set(gefestId, element);
|
||||
|
||||
return gefestId;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user