mapcomplete/UI/BaseUIElement.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

190 lines
5.3 KiB
TypeScript
Raw Normal View History

2021-06-10 01:36:20 +02:00
/**
* A thin wrapper around a html element, which allows to generate a HTML-element.
2021-06-23 02:14:15 +02:00
*
2021-06-10 01:36:20 +02:00
* Assumes a read-only configuration, so it has no 'ListenTo'
*/
2022-12-16 13:44:25 +01:00
import { Utils } from "../Utils"
2021-06-10 01:36:20 +02:00
export default abstract class BaseUIElement {
2021-06-23 02:14:15 +02:00
protected _constructedHtmlElement: HTMLElement
2022-01-26 21:40:38 +01:00
protected isDestroyed = false
2022-08-22 13:34:47 +02:00
protected readonly clss: Set<string> = new Set<string>()
protected style: string
2022-08-22 19:16:37 +02:00
private _onClick: () => void | Promise<void>
2022-09-08 21:40:48 +02:00
2021-06-10 01:36:20 +02:00
public onClick(f: () => void) {
this._onClick = f
2023-04-21 18:09:14 +02:00
this.SetClass("cursor-pointer")
2021-06-23 02:14:15 +02:00
if (this._constructedHtmlElement !== undefined) {
2021-06-10 01:36:20 +02:00
this._constructedHtmlElement.onclick = f
}
return this
}
2021-06-10 01:36:20 +02:00
AttachTo(divId: string) {
let element = document.getElementById(divId)
if (element === null) {
if (Utils.runningFromConsole) {
this.ConstructElement()
return
}
2021-06-10 01:36:20 +02:00
throw "SEVERE: could not attach UIElement to " + divId
}
let alreadyThere = false
const elementToAdd = this.ConstructElement()
const childs = Array.from(element.childNodes)
for (const child of childs) {
if (child === elementToAdd) {
alreadyThere = true
continue
}
element.removeChild(child)
2021-06-10 01:36:20 +02:00
}
if (elementToAdd !== undefined && !alreadyThere) {
element.appendChild(elementToAdd)
2021-06-10 01:36:20 +02:00
}
2021-06-10 01:36:20 +02:00
return this
}
public ScrollIntoView() {
2022-12-16 13:44:25 +01:00
if (this._constructedHtmlElement === undefined) {
return
}
this._constructedHtmlElement?.scrollIntoView({
behavior: "smooth",
2022-12-16 13:44:25 +01:00
block: "start",
})
}
2021-06-10 01:36:20 +02:00
/**
* Adds all the relevant classes, space separated
2021-06-10 01:36:20 +02:00
*/
public SetClass(clss: string) {
2021-11-07 16:34:51 +01:00
if (clss == undefined) {
2022-04-13 02:44:06 +02:00
return this
2021-11-07 16:34:51 +01:00
}
2021-06-10 01:36:20 +02:00
const all = clss.split(" ").map((clsName) => clsName.trim())
let recordedChange = false
2021-06-12 02:58:32 +02:00
for (let c of all) {
c = c.trim()
2021-06-10 01:36:20 +02:00
if (this.clss.has(clss)) {
continue
}
2021-06-23 02:14:15 +02:00
if (c === undefined || c === "") {
2021-06-12 02:58:32 +02:00
continue
}
2021-06-10 01:36:20 +02:00
this.clss.add(c)
recordedChange = true
}
if (recordedChange) {
this._constructedHtmlElement?.classList.add(...Array.from(this.clss))
}
return this
}
public RemoveClass(classes: string): BaseUIElement {
const all = classes.split(" ").map((clsName) => clsName.trim())
for (let clss of all) {
if (this.clss.has(clss)) {
this.clss.delete(clss)
this._constructedHtmlElement?.classList.remove(clss)
}
2021-06-10 01:36:20 +02:00
}
return this
}
2021-06-23 02:14:15 +02:00
public HasClass(clss: string): boolean {
return this.clss.has(clss)
}
2021-06-10 01:36:20 +02:00
public SetStyle(style: string): BaseUIElement {
this.style = style
2021-06-23 02:14:15 +02:00
if (this._constructedHtmlElement !== undefined) {
2021-06-10 01:36:20 +02:00
this._constructedHtmlElement.style.cssText = style
}
return this
}
2021-06-23 02:14:15 +02:00
2021-06-10 01:36:20 +02:00
/**
* The same as 'Render', but creates a HTML element instead of the HTML representation
*/
public ConstructElement(): HTMLElement {
if (typeof window === undefined) {
2021-06-10 01:36:20 +02:00
return undefined
}
if (this._constructedHtmlElement !== undefined) {
return this._constructedHtmlElement
}
try {
2021-06-23 02:14:15 +02:00
const el = this.InnerConstructElement()
2021-06-10 01:36:20 +02:00
2021-06-23 02:14:15 +02:00
if (el === undefined) {
return undefined
2021-06-10 01:36:20 +02:00
}
2021-06-23 02:14:15 +02:00
this._constructedHtmlElement = el
const style = this.style
if (style !== undefined && style !== "") {
el.style.cssText = style
}
2022-07-20 12:04:14 +02:00
if (this.clss?.size > 0) {
2021-06-23 02:14:15 +02:00
try {
el.classList.add(...Array.from(this.clss))
} catch (e) {
console.error(
"Invalid class name detected in:",
Array.from(this.clss).join(" "),
"\nErr msg is ",
e
)
2021-06-10 01:36:20 +02:00
}
}
2021-06-23 02:14:15 +02:00
if (this._onClick !== undefined) {
const self = this
2022-08-22 19:16:37 +02:00
el.onclick = async (e) => {
2021-06-23 02:14:15 +02:00
// @ts-ignore
if (e.consumed) {
return
}
2022-08-22 19:16:37 +02:00
const v = self._onClick()
if (typeof v === "object") {
await v
}
2021-06-23 02:14:15 +02:00
// @ts-ignore
e.consumed = true
}
2023-04-21 20:50:38 +02:00
el.classList.add("cursor-pointer")
2021-06-23 02:14:15 +02:00
}
2021-06-10 01:36:20 +02:00
2021-06-23 02:14:15 +02:00
return el
} catch (e) {
2021-06-16 14:23:53 +02:00
const domExc = e as DOMException
2021-06-23 02:14:15 +02:00
if (domExc) {
2022-10-27 01:50:01 +02:00
console.error(
"An exception occured",
domExc.code,
domExc.message,
domExc.name,
domExc
)
2021-06-16 14:23:53 +02:00
}
console.error(e)
2021-06-23 02:14:15 +02:00
}
2021-06-10 01:36:20 +02:00
}
2021-06-23 02:14:15 +02:00
public AsMarkdown(): string {
throw "AsMarkdown is not implemented; implement it in the subclass"
}
2022-01-26 21:40:38 +01:00
public Destroy() {
2022-01-06 18:51:52 +01:00
this.isDestroyed = true
}
2021-06-23 02:14:15 +02:00
protected abstract InnerConstructElement(): HTMLElement
}