mapcomplete/UI/Base/LazyElement.ts

36 lines
966 B
TypeScript
Raw Normal View History

2020-12-05 02:22:17 +00:00
import {UIElement} from "../UIElement";
export default class LazyElement extends UIElement {
2020-12-05 02:22:17 +00:00
public Activate: () => void;
private _content: UIElement = undefined;
2021-01-25 02:12:09 +00:00
private readonly _loadingContent: string;
2020-12-05 02:22:17 +00:00
constructor(content: (() => UIElement), 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;
this.Activate = () => {
2020-12-05 02:22:17 +00:00
if (this._content === undefined) {
self._content = content();
}
self.Update();
// @ts-ignore
2021-03-09 12:10:48 +00:00
if (this._content.Activate) {
// THis is ugly - I know
// @ts-ignore
this._content.Activate();
}
2020-12-05 02:22:17 +00:00
}
}
InnerRender(): string {
if (this._content === undefined) {
return this._loadingContent;
2020-12-05 02:22:17 +00:00
}
2021-01-25 03:21:22 +00:00
return this._content.Render();
2020-12-05 02:22:17 +00:00
}
}