MapComplete/UI/Input/LevelSelector.ts

93 lines
3.4 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";
import {Utils} from "../../Utils";
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 => {
let slider = new Slider(0, levels.length - 1, {vertical: true});
slider.SetClass("flex m-1 elevatorslider mb-0 mt-8").SetStyle("height: " + 2.5 * levels.length + "rem ")
const toggleClass = "flex border-2 border-blue-500 w-10 h-10 place-content-center items-center"
const values = levels.map((data, i) => new ClickableToggle(
new FixedUiElement(data).SetClass("active bg-subtle " + toggleClass), new FixedUiElement(data).SetClass(toggleClass), slider.GetValue().sync(
(sliderVal) => {
return sliderVal === i
},
[],
(isSelected) => {
return isSelected ? i : slider.GetValue().data
}
))
.ToggleOnClick()
.SetClass("flex flex-column ml-5 bg-slate-200 w-10 h-10 valuesContainer"))
const combine = new Combine([new Combine(values).SetClass("mt-8"), slider])
combine.SetClass("flex flex-row h-14");
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
}))
2022-07-21 15:54:24 +02:00
}
GetValue(): UIEventSource<string> {
return this._value;
}
IsValid(t: string): boolean {
return false;
}
/**
* Parses a level specifier to the various available levels
2022-07-21 19:18:11 +02:00
*
2022-07-21 15:54:24 +02:00
* LevelSelector.LevelsParser("0") // => ["0"]
* LevelSelector.LevelsParser("1") // => ["1"]
* LevelSelector.LevelsParser("0;2") // => ["0","2"]
* LevelSelector.LevelsParser("0-5") // => ["0","1","2","3","4","5"]
* LevelSelector.LevelsParser("0") // => ["0"]
*/
public static LevelsParser(level: string): string[] {
let spec = [level]
spec = [].concat(...spec.map(s => s.split(";")))
spec = [].concat(...spec.map(s => {
s = s.trim()
2022-07-21 19:18:11 +02:00
if (s.indexOf("-") < 0) {
2022-07-21 15:54:24 +02:00
return s
}
const [start, end] = s.split("-").map(s => Number(s.trim()))
2022-07-21 19:18:11 +02:00
if (isNaN(start) || isNaN(end)) {
2022-07-21 15:54:24 +02:00
return undefined
}
const values = []
for (let i = start; i <= end; i++) {
2022-07-21 19:18:11 +02:00
values.push(i + "")
2022-07-21 15:54:24 +02:00
}
return values
}))
return Utils.NoNull(spec);
}
2022-07-21 19:18:11 +02:00
2022-07-21 15:54:24 +02:00
}