mapcomplete/UI/Input/LevelSelector.ts

70 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-07-21 15:54:24 +02:00
import {InputElement} from "./InputElement";
2022-07-21 19:18:11 +02:00
import {Store, Stores, UIEventSource} from "../../Logic/UIEventSource";
2022-07-21 15:54:24 +02:00
import Combine from "../Base/Combine";
import Slider from "./Slider";
import {ClickableToggle} from "./Toggle";
import {FixedUiElement} from "../Base/FixedUiElement";
2022-07-21 19:18:11 +02:00
import {VariableUiElement} from "../Base/VariableUIElement";
2022-07-21 15:54:24 +02:00
2022-07-21 19:18:11 +02:00
export default class LevelSelector extends VariableUiElement implements InputElement<string> {
private readonly _value: UIEventSource<string>;
constructor(currentLevels: Store<string[]>, options?: {
2022-07-21 15:54:24 +02:00
value?: UIEventSource<string>
}) {
2022-07-21 19:18:11 +02:00
const value = options?.value ?? new UIEventSource<string>(undefined)
super(Stores.ListStabilized(currentLevels).map(levels => {
console.log("CUrrent levels are", levels)
2022-07-21 19:18:11 +02:00
let slider = new Slider(0, levels.length - 1, {vertical: true});
const toggleClass = "flex border-2 border-blue-500 w-10 h-10 place-content-center items-center border-box"
slider.SetClass("flex elevator w-10").SetStyle(`height: ${2.5 * levels.length}rem; background: #00000000`)
2022-07-21 19:18:11 +02:00
const values = levels.map((data, i) => new ClickableToggle(
new FixedUiElement(data).SetClass("font-bold active bg-subtle " + toggleClass),
new FixedUiElement(data).SetClass("normal-background " + toggleClass),
slider.GetValue().sync(
2022-07-21 19:18:11 +02:00
(sliderVal) => {
return sliderVal === i
},
[],
(isSelected) => {
return isSelected ? i : slider.GetValue().data
}
))
.ToggleOnClick()
.SetClass("flex w-10 h-10"))
2022-07-21 19:18:11 +02:00
values.reverse(/* This is a new list, no side-effects */)
const combine = new Combine([new Combine(values), slider])
combine.SetClass("flex flex-row overflow-hidden");
2022-07-21 15:54:24 +02:00
2022-07-21 19:18:11 +02:00
slider.GetValue().addCallbackAndRun(i => {
if (currentLevels?.data === undefined) {
return
2022-07-21 15:54:24 +02:00
}
2022-07-21 19:18:11 +02:00
value.setData(currentLevels?.data[i]);
})
value.addCallback(level => {
const i = currentLevels?.data?.findIndex(l => l === level)
slider.GetValue().setData(i)
})
return combine
}))
this._value = value
2022-07-21 15:54:24 +02:00
}
GetValue(): UIEventSource<string> {
return this._value;
}
IsValid(t: string): boolean {
return false;
}
2022-07-21 19:18:11 +02:00
2022-07-21 15:54:24 +02:00
}