mapcomplete/UI/Input/FixedInputElement.ts

46 lines
1.4 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> {
private readonly value: UIEventSource<T>;
public readonly IsSelected : UIEventSource<boolean> = new UIEventSource<boolean>(false);
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-06-14 02:39:23 +02:00
private readonly _el : HTMLElement;
constructor(rendering: BaseUIElement | string,
2020-09-09 18:42:13 +02:00
value: T,
comparator: ((t0: T, t1: T) => boolean ) = undefined) {
2021-06-14 02:39:23 +02:00
super();
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()
if(e){
this._el.appendChild( e)
}
this.onClick(() => {
2021-06-14 02:39:23 +02:00
selected.setData(true)
})
2020-07-20 13:28:45 +02:00
}
2021-06-14 02:39:23 +02:00
protected InnerConstructElement(): HTMLElement {
2021-06-14 17:28:11 +02:00
return this._el;
2021-06-14 02:39:23 +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-07-20 15:54:50 +02:00
2020-07-20 13:28:45 +02:00
}