mapcomplete/UI/Input/InputElementMap.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-09-10 19:33:06 +02:00
import { InputElement } from "./InputElement"
2022-07-10 03:58:07 +02:00
import { Store, UIEventSource } from "../../Logic/UIEventSource"
2020-09-10 19:33:06 +02:00
/**
* @deprecated
*/
2020-09-10 19:33:06 +02:00
export default class InputElementMap<T, X> extends InputElement<X> {
private readonly _inputElement: InputElement<T>
private isSame: (x0: X, x1: X) => boolean
private readonly fromX: (x: X) => T
private readonly toX: (t: T) => X
private readonly _value: UIEventSource<X>
constructor(
inputElement: InputElement<T>,
isSame: (x0: X, x1: X) => boolean,
toX: (t: T) => X,
2020-09-11 00:23:19 +02:00
fromX: (x: X) => T,
2022-07-10 03:58:07 +02:00
extraSources: Store<any>[] = []
2020-09-10 19:33:06 +02:00
) {
super()
this.isSame = isSame
this.fromX = fromX
this.toX = toX
this._inputElement = inputElement
const self = this
this._value = inputElement.GetValue().sync(
2020-09-10 19:33:06 +02:00
(t) => {
const newX = toX(t)
const currentX = self.GetValue()?.data
2020-09-10 19:33:06 +02:00
if (isSame(currentX, newX)) {
return currentX
}
return newX
2020-09-11 00:23:19 +02:00
},
extraSources,
(x) => {
2020-09-25 12:44:04 +02:00
return fromX(x)
2020-09-10 19:33:06 +02:00
}
2022-09-08 21:40:48 +02:00
)
}
2020-09-10 19:33:06 +02:00
GetValue(): UIEventSource<X> {
return this._value
}
IsValid(x: X): boolean {
2021-06-10 01:36:20 +02:00
if (x === undefined) {
2020-09-25 12:44:04 +02:00
return false
}
const t = this.fromX(x)
2021-06-10 01:36:20 +02:00
if (t === undefined) {
2020-09-25 12:44:04 +02:00
return false
}
return this._inputElement.IsValid(t)
2020-09-10 19:33:06 +02:00
}
2021-06-10 01:36:20 +02:00
protected InnerConstructElement(): HTMLElement {
return this._inputElement.ConstructElement()
}
2020-09-10 19:33:06 +02:00
}