Simplify PageElement API

This commit is contained in:
Andras Schmelczer 2022-09-23 22:47:33 +02:00
parent f1f9ee539a
commit 4ca000b7a3
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
3 changed files with 13 additions and 20 deletions

View file

@ -60,16 +60,14 @@ export class PageBackground extends PageElement {
requestAnimationFrame(this.drawIfNecessary.bind(this));
}
private parent?: HTMLElement;
protected setParent(parent: PageElement) {
this.parent = parent.htmlRoot;
protected initialize() {
super.initialize();
requestAnimationFrame(this.drawIfNecessary.bind(this));
super.setParent(parent);
}
private getSiblings(): Array<HTMLElement> {
return Array.prototype.slice
.call(this.parent!.childNodes)
.call(this.htmlRoot.parentElement!.childNodes)
.filter((n: HTMLElement) => n !== this.htmlRoot);
}

View file

@ -10,14 +10,14 @@ export class Preview extends PageElement {
this.query('.start-button').addEventListener('click', this.loadContent.bind(this));
}
protected setParent(parent: PageElement) {
protected initialize() {
new IntersectionObserver((e) => {
if (!e[0].isIntersecting) {
this.unloadContent();
}
}).observe(this.htmlRoot.parentElement!);
super.setParent(parent);
super.initialize();
}
public loadContent() {

View file

@ -6,30 +6,25 @@ export class PageElement {
public attachToDOM(target: HTMLElement) {
target.appendChild(this.htmlRoot);
this.setParent(null);
this.initialize();
}
protected setParent(_parent?: PageElement | null): void {
this.children.forEach((c) => c.setParent(this));
protected initialize(): void {
this.children.forEach((c) => c.initialize());
}
protected query(query: string): HTMLElement {
return this.htmlRoot.querySelector(query) as HTMLElement;
}
protected attachElement(element: PageElement) {
this.htmlRoot.appendChild(element.htmlRoot);
this.children.push(element);
}
protected attachElementByReplacing(query: string, element: PageElement) {
const old = this.query(query);
old.parentElement!.replaceChild(element.htmlRoot, old);
this.children.push(element);
}
protected attachElementAsChildOf(query: string, element: PageElement) {
this.query(query).appendChild(element.htmlRoot);
this.children.push(element);
}
protected attachElement(element: PageElement) {
this.htmlRoot.appendChild(element.htmlRoot);
this.children.push(element);
}
}