mapcomplete/UI/Input/FixedInputElement.ts

49 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-07-20 15:54:50 +02:00
import {InputElement} from "./InputElement";
import {UIEventSource} from "../../Logic/UIEventSource";
2021-06-14 02:39:23 +02:00
import Translations from "../i18n/Translations";
import BaseUIElement from "../BaseUIElement";
2020-07-20 13:28:45 +02:00
2020-07-20 15:54:50 +02:00
export class FixedInputElement<T> extends InputElement<T> {
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
private readonly value: UIEventSource<T>;
2020-09-09 18:42:13 +02:00
private readonly _comparator: (t0: T, t1: T) => boolean;
2020-07-20 13:28:45 +02:00
private readonly _el: HTMLElement;
constructor(rendering: BaseUIElement | string,
value: T | UIEventSource<T>,
comparator: ((t0: T, t1: T) => boolean) = undefined) {
2021-06-14 02:39:23 +02:00
super();
this._comparator = comparator ?? ((t0, t1) => t0 == t1);
2021-11-07 16:34:51 +01:00
if (value instanceof UIEventSource) {
this.value = value
2021-11-07 16:34:51 +01:00
} else {
this.value = new UIEventSource<T>(value);
}
2021-06-14 02:39:23 +02:00
const selected = this.IsSelected;
this._el = document.createElement("span")
this._el.addEventListener("mouseout", () => selected.setData(false))
const e = Translations.W(rendering)?.ConstructElement()
if (e) {
this._el.appendChild(e)
2021-06-14 02:39:23 +02:00
}
this.onClick(() => {
2021-06-14 02:39:23 +02:00
selected.setData(true)
})
2020-07-20 13:28:45 +02:00
}
GetValue(): UIEventSource<T> {
return this.value;
}
2020-07-20 15:54:50 +02:00
IsValid(t: T): boolean {
2020-09-09 18:42:13 +02:00
return this._comparator(t, this.value.data);
2020-07-20 15:54:50 +02:00
}
protected InnerConstructElement(): HTMLElement {
return this._el;
}
}