2020-07-20 15:54:50 +02:00
|
|
|
import {InputElement} from "./InputElement";
|
2020-08-17 17:23:15 +02:00
|
|
|
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> {
|
2021-09-09 00:05:51 +02:00
|
|
|
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
2020-08-31 02:59:47 +02:00
|
|
|
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
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
private readonly _el: HTMLElement;
|
|
|
|
|
|
|
|
constructor(rendering: BaseUIElement | string,
|
2020-09-09 18:42:13 +02:00
|
|
|
value: T,
|
2021-09-09 00:05:51 +02:00
|
|
|
comparator: ((t0: T, t1: T) => boolean) = undefined) {
|
2021-06-14 02:39:23 +02:00
|
|
|
super();
|
2020-09-30 22:48:58 +02:00
|
|
|
this._comparator = comparator ?? ((t0, t1) => t0 == t1);
|
2020-07-20 13:28:45 +02:00
|
|
|
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()
|
2021-09-09 00:05:51 +02:00
|
|
|
if (e) {
|
|
|
|
this._el.appendChild(e)
|
2021-06-14 02:39:23 +02:00
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2020-09-30 22:48:58 +02:00
|
|
|
this.onClick(() => {
|
2021-06-14 02:39:23 +02:00
|
|
|
selected.setData(true)
|
2020-09-30 22:48:58 +02:00
|
|
|
})
|
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
|
|
|
}
|
2020-08-31 02:59:47 +02:00
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
protected InnerConstructElement(): HTMLElement {
|
|
|
|
return this._el;
|
|
|
|
}
|
2021-09-30 21:41:51 +02:00
|
|
|
}
|