mapcomplete/UI/UIElement.ts

217 lines
5.9 KiB
TypeScript
Raw Normal View History

import {UIEventSource} from "../Logic/UIEventSource";
2020-06-23 22:35:19 +00:00
2020-09-02 09:37:34 +00:00
export abstract class UIElement extends UIEventSource<string> {
2020-06-23 22:35:19 +00:00
private static nextId: number = 0;
public readonly id: string;
public readonly _source: UIEventSource<any>;
private clss: string[] = []
2020-09-02 09:37:34 +00:00
private style: string;
private _hideIfEmpty = false;
2020-09-02 09:37:34 +00:00
public dumbMode = false;
/**
* In the 'deploy'-step, some code needs to be run by ts-node.
* However, ts-node crashes when it sees 'document'. When running from console, we flag this and disable all code where document is needed.
* This is a workaround and yet another hack
*/
public static runningFromConsole = false;
2020-06-23 22:35:19 +00:00
protected constructor(source: UIEventSource<any> = undefined) {
2020-07-26 23:19:38 +00:00
super("");
2020-06-23 22:35:19 +00:00
this.id = "ui-element-" + UIElement.nextId;
this._source = source;
UIElement.nextId++;
this.ListenTo(source);
}
2020-07-08 11:12:23 +00:00
public ListenTo(source: UIEventSource<any>) {
2020-07-01 00:12:33 +00:00
if (source === undefined) {
2020-07-20 23:37:48 +00:00
return this;
2020-06-23 22:35:19 +00:00
}
2020-09-02 09:37:34 +00:00
this.dumbMode = false;
2020-06-23 22:35:19 +00:00
const self = this;
source.addCallback(() => {
self.Update();
})
2020-07-20 23:37:48 +00:00
return this;
2020-06-23 22:35:19 +00:00
}
2020-07-01 00:12:33 +00:00
private _onClick: () => void;
public onClick(f: (() => void)) {
2020-09-02 09:37:34 +00:00
this.dumbMode = false;
2020-07-01 00:12:33 +00:00
this._onClick = f;
this.SetClass("clickable")
2020-07-01 00:12:33 +00:00
this.Update();
return this;
2020-07-01 00:12:33 +00:00
}
2020-09-02 09:37:34 +00:00
private _onHover: UIEventSource<boolean>;
public IsHovered(): UIEventSource<boolean> {
this.dumbMode = false;
if (this._onHover !== undefined) {
return this._onHover;
}
// Note: we just save it. 'Update' will register that an eventsource exist and install the necessary hooks
this._onHover = new UIEventSource<boolean>(false);
return this._onHover;
}
2020-06-23 22:35:19 +00:00
Update(): void {
2020-09-02 09:37:34 +00:00
if (UIElement.runningFromConsole) {
return;
2020-07-25 16:00:08 +00:00
}
2020-07-01 00:12:33 +00:00
let element = document.getElementById(this.id);
2020-07-20 22:07:04 +00:00
if (element === undefined || element === null) {
2020-06-23 22:35:19 +00:00
// The element is not painted
2020-09-02 09:37:34 +00:00
if (this.dumbMode) {
// We update all the children anyway
for (const i in this) {
const child = this[i];
if (child instanceof UIElement) {
child.Update();
} else if (child instanceof Array) {
for (const ch of child) {
if (ch instanceof UIElement) {
ch.Update();
}
}
}
}
}
2020-06-23 22:35:19 +00:00
return;
}
2020-07-26 23:19:38 +00:00
this.setData(this.InnerRender());
element.innerHTML = this.data;
2020-09-02 09:37:34 +00:00
2020-07-01 00:12:33 +00:00
if (this._hideIfEmpty) {
if (element.innerHTML === "") {
element.parentElement.style.display = "none";
2020-07-01 00:12:33 +00:00
} else {
element.parentElement.style.display = "block";
}
}
2020-07-01 00:12:33 +00:00
if (this._onClick !== undefined) {
const self = this;
2020-07-22 10:17:06 +00:00
element.onclick = (e) => {
2020-07-24 13:22:28 +00:00
// @ts-ignore
2020-09-02 09:37:34 +00:00
if (e.consumed) {
2020-07-22 10:17:06 +00:00
return;
}
2020-07-01 00:12:33 +00:00
self._onClick();
2020-07-24 13:22:28 +00:00
// @ts-ignore
2020-07-22 10:17:06 +00:00
e.consumed = true;
2020-07-01 00:12:33 +00:00
}
2020-07-01 19:21:29 +00:00
element.style.pointerEvents = "all";
2020-07-01 00:12:33 +00:00
element.style.cursor = "pointer";
}
2020-09-02 09:37:34 +00:00
if (this._onHover !== undefined) {
const self = this;
element.addEventListener('mouseover', () => self._onHover.setData(true));
element.addEventListener('mouseout', () => self._onHover.setData(false));
}
2020-06-23 22:35:19 +00:00
this.InnerUpdate(element);
2020-07-20 07:57:19 +00:00
for (const i in this) {
const child = this[i];
if (child instanceof UIElement) {
child.Update();
} else if (child instanceof Array) {
for (const ch of child) {
if (ch instanceof UIElement) {
ch.Update();
}
}
}
}
2020-06-23 22:35:19 +00:00
}
HideOnEmpty(hide : boolean){
this._hideIfEmpty = hide;
this.Update();
return this;
}
2020-06-23 22:35:19 +00:00
// Called after the HTML has been replaced. Can be used for css tricks
2020-07-20 19:39:07 +00:00
protected InnerUpdate(htmlElement: HTMLElement) {
}
2020-06-23 22:35:19 +00:00
Render(): string {
2020-09-02 09:37:34 +00:00
if (this.dumbMode) {
return this.InnerRender();
}
let style = "";
if (this.style !== undefined && this.style !== "") {
style = `style="${this.style}"`;
}
return `<span class='uielement ${this.clss.join(" ")}' ${style} id='${this.id}'>${this.InnerRender()}</span>`
2020-06-23 22:35:19 +00:00
}
AttachTo(divId: string) {
2020-09-02 09:37:34 +00:00
this.dumbMode = false;
2020-06-23 22:35:19 +00:00
let element = document.getElementById(divId);
2020-07-20 07:57:19 +00:00
if (element === null) {
throw "SEVERE: could not attach UIElement to " + divId;
}
2020-06-23 22:35:19 +00:00
element.innerHTML = this.Render();
this.Update();
return this;
2020-06-23 22:35:19 +00:00
}
2020-07-20 16:24:00 +00:00
public abstract InnerRender(): string;
2020-07-20 07:57:19 +00:00
public Activate(): UIElement {
2020-07-20 07:57:19 +00:00
for (const i in this) {
const child = this[i];
if (child instanceof UIElement) {
child.Activate();
} else if (child instanceof Array) {
for (const ch of child) {
if (ch instanceof UIElement) {
ch.Activate();
}
}
}
}
return this;
2020-07-20 07:57:19 +00:00
};
2020-06-23 22:35:19 +00:00
public IsEmpty(): boolean {
return this.InnerRender() === "";
}
public SetClass(clss: string): UIElement {
2020-09-02 09:37:34 +00:00
this.dumbMode = false;
if (this.clss.indexOf(clss) < 0) {
this.clss.push(clss);
}
this.Update();
return this;
}
2020-09-02 09:37:34 +00:00
public SetStyle(style: string): UIElement {
this.dumbMode = false;
this.style = style;
this.Update();
return this;
}
2020-07-20 13:54:50 +00:00
}
2020-06-23 22:35:19 +00:00
2020-07-29 13:05:19 +00:00