Move runningFromConsole to utils

This commit is contained in:
Pieter Vander Vennet 2021-01-06 02:21:50 +01:00
parent c359d43b15
commit 66018cb421
9 changed files with 73 additions and 66 deletions

View file

@ -1,9 +1,12 @@
import Constants from "../../Models/Constants";
import {Utils} from "../../Utils";
export default class Img {
public static runningFromConsole = false;
static AsData(source:string){
if(this.runningFromConsole){
if(Utils.runningFromConsole){
return source;
}
return `data:image/svg+xml;base64,${(btoa(source))}`;

View file

@ -1,27 +1,25 @@
import {UIEventSource} from "../Logic/UIEventSource";
import Constants from "../Models/Constants";
import {Utils} from "../Utils";
export abstract class UIElement extends UIEventSource<string> {
private static nextId: number = 0;
public readonly id: string;
public readonly _source: UIEventSource<any>;
private clss: string[] = []
private style: string;
private _hideIfEmpty = false;
public dumbMode = false;
private lastInnerRender: string;
/**
* 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;
private static nextId: number = 0;
public readonly id: string;
public readonly _source: UIEventSource<any>;
public dumbMode = false;
private clss: string[] = []
private style: string;
private _hideIfEmpty = false;
private lastInnerRender: string;
private _onClick: () => void;
private _onHover: UIEventSource<boolean>;
protected constructor(source: UIEventSource<any> = undefined) {
super("");
@ -32,7 +30,6 @@ export abstract class UIElement extends UIEventSource<string> {
this.ListenTo(source);
}
public ListenTo(source: UIEventSource<any>) {
if (source === undefined) {
return this;
@ -46,8 +43,6 @@ export abstract class UIElement extends UIEventSource<string> {
return this;
}
private _onClick: () => void;
public onClick(f: (() => void)) {
this.dumbMode = false;
this._onClick = f;
@ -56,8 +51,6 @@ export abstract class UIElement extends UIEventSource<string> {
return this;
}
private _onHover: UIEventSource<boolean>;
public IsHovered(): UIEventSource<boolean> {
this.dumbMode = false;
if (this._onHover !== undefined) {
@ -69,10 +62,10 @@ export abstract class UIElement extends UIEventSource<string> {
}
Update(): void {
if (UIElement.runningFromConsole) {
if (Utils.runningFromConsole) {
return;
}
let element = document.getElementById(this.id);
if (element === undefined || element === null) {
// The element is not painted or, in the case of 'dumbmode' this UI-element is not explicitely present
@ -101,7 +94,7 @@ export abstract class UIElement extends UIEventSource<string> {
const self = this;
element.onclick = (e) => {
// @ts-ignore
if(e.consumed){
if (e.consumed) {
return;
}
self._onClick();
@ -123,31 +116,12 @@ export abstract class UIElement extends UIEventSource<string> {
}
private UpdateAllChildren() {
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();
}
}
}
}
}
HideOnEmpty(hide: boolean) {
this._hideIfEmpty = hide;
this.Update();
return this;
}
// Called after the HTML has been replaced. Can be used for css tricks
protected InnerUpdate(htmlElement: HTMLElement) {
}
Render(): string {
this.lastInnerRender = this.InnerRender();
if (this.dumbMode) {
@ -192,6 +166,7 @@ export abstract class UIElement extends UIEventSource<string> {
}
return this;
}
public RemoveClass(clss: string): UIElement {
const i = this.clss.indexOf(clss);
if (i >= 0) {
@ -201,13 +176,31 @@ export abstract class UIElement extends UIEventSource<string> {
return this;
}
public SetStyle(style: string): UIElement {
this.dumbMode = false;
this.style = style;
this.Update();
return this;
}
// Called after the HTML has been replaced. Can be used for css tricks
protected InnerUpdate(htmlElement: HTMLElement) {
}
private UpdateAllChildren() {
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();
}
}
}
}
}
}

View file

@ -1,6 +1,6 @@
import {UIEventSource} from "../../Logic/UIEventSource";
import {UIElement} from "../UIElement";
import {LocalStorageSource} from "../../Logic/Web/LocalStorageSource";
import {Utils} from "../../Utils";
export default class Locale {
@ -9,7 +9,7 @@ export default class Locale {
private static setup() {
const source = LocalStorageSource.Get('language', "en");
if (!UIElement.runningFromConsole) {
if (!Utils.runningFromConsole) {
// @ts-ignore
window.setLanguage = function (language: string) {
source.setData(language)