Restructurized the structure

This commit is contained in:
FullGreaM 2026-05-03 02:36:32 +03:00
parent d2d8118ccd
commit c235bf73a8

View File

@ -17,6 +17,44 @@ export abstract class GefestElement {
protected clickHandler: (() => void) | null = null; protected clickHandler: (() => void) | null = null;
/**
* Creates a new instance of the element from an HTML string.
* @param html The HTML string to parse.
* @returns The created GefestElement.
*/
static fromHTML(html: string): GefestElement {
console.warn("Legacy and experiemental function!");
const htmlElement = new DOMParser().parseFromString(html, 'text/html').body.firstChild as HTMLElement;
const element = new (class extends GefestElement {
protected wrapHTML(content: string): string {
return content;
}
})(htmlElement.innerHTML);
for (const attr of htmlElement.attributes) {
try {
element.setAttribute(attr.name, attr.value);
} catch (_) {}
}
for (const className of htmlElement.classList) {
element.addClass(className);
}
return element;
}
/**
* Creates a new instance of the element.
* @param content The content for the element. Can be a string, an array of strings, an array of GefestElements or a combination of both.
*/
constructor (content : (GefestElement | string)[] | string = []) {
this.gefestId = GefestEngine.register(this);
this.attributes["data-gefest-id"] = this.gefestId;
this.content = content;
}
emit(event: GefestElementEvents, data: unknown = undefined) { emit(event: GefestElementEvents, data: unknown = undefined) {
for (let handler of this.eventHandlers) for (let handler of this.eventHandlers)
new Promise((rs) => rs(handler(event, data))); new Promise((rs) => rs(handler(event, data)));
@ -60,44 +98,6 @@ export abstract class GefestElement {
}; };
} }
/**
* Creates a new instance of the element from an HTML string.
* @param html The HTML string to parse.
* @returns The created GefestElement.
*/
static fromHTML(html: string): GefestElement {
console.warn("Legacy and experiemental function!");
const htmlElement = new DOMParser().parseFromString(html, 'text/html').body.firstChild as HTMLElement;
const element = new (class extends GefestElement {
protected wrapHTML(content: string): string {
return content;
}
})(htmlElement.innerHTML);
for (const attr of htmlElement.attributes) {
try {
element.setAttribute(attr.name, attr.value);
} catch (_) {}
}
for (const className of htmlElement.classList) {
element.addClass(className);
}
return element;
}
/**
* Creates a new instance of the element.
* @param content The content for the element. Can be a string, an array of strings, an array of GefestElements or a combination of both.
*/
constructor (content : (GefestElement | string)[] | string = []) {
this.gefestId = GefestEngine.register(this);
this.attributes["data-gefest-id"] = this.gefestId;
this.content = content;
}
/** /**
* Call to GefestEngine for rerender element * Call to GefestEngine for rerender element
*/ */