mapcomplete/UI/Base/LazyElement.ts

36 lines
972 B
TypeScript
Raw Normal View History

2020-12-05 02:22:17 +00:00
import {UIElement} from "../UIElement";
2021-01-25 02:12:09 +00:00
export default class LazyElement<T extends UIElement> extends UIElement {
2020-12-05 02:22:17 +00:00
2021-01-25 02:12:09 +00:00
public Activate: (onElement?: (element: T) => void) => void;
private _content: T = undefined;
private readonly _loadingContent: string;
2020-12-05 02:22:17 +00:00
2021-01-25 02:12:09 +00:00
constructor(content: (() => T), loadingContent = "Rendering...") {
2020-12-05 02:22:17 +00:00
super();
this._loadingContent = loadingContent;
2020-12-05 02:22:17 +00:00
this.dumbMode = false;
const self = this;
2021-01-25 02:12:09 +00:00
this.Activate = (onElement?: (element: T) => void) => {
console.log("ACTIVATED")
2020-12-05 02:22:17 +00:00
if (this._content === undefined) {
self._content = content();
}
2021-01-25 02:12:09 +00:00
if (onElement) {
onElement(self._content)
}
2020-12-05 02:22:17 +00:00
self.Update();
}
}
InnerRender(): string {
if (this._content === undefined) {
return this._loadingContent;
2020-12-05 02:22:17 +00:00
}
return this._content.InnerRender();
}
2021-01-25 02:12:09 +00:00
2020-12-05 02:22:17 +00:00
}