From 4ca000b7a3b16b9da5a9a60e534850c3e894837d Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Fri, 23 Sep 2022 22:47:33 +0200 Subject: [PATCH] Simplify PageElement API --- src/page/background/background.ts | 8 +++----- src/page/basics/preview/preview.ts | 4 ++-- src/page/page-element.ts | 21 ++++++++------------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/page/background/background.ts b/src/page/background/background.ts index 53b1b5a..85ec8cc 100644 --- a/src/page/background/background.ts +++ b/src/page/background/background.ts @@ -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 { return Array.prototype.slice - .call(this.parent!.childNodes) + .call(this.htmlRoot.parentElement!.childNodes) .filter((n: HTMLElement) => n !== this.htmlRoot); } diff --git a/src/page/basics/preview/preview.ts b/src/page/basics/preview/preview.ts index 3eebc63..f292a6e 100644 --- a/src/page/basics/preview/preview.ts +++ b/src/page/basics/preview/preview.ts @@ -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() { diff --git a/src/page/page-element.ts b/src/page/page-element.ts index 0db1568..bae0ea1 100644 --- a/src/page/page-element.ts +++ b/src/page/page-element.ts @@ -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); - } }