Themes(toilets): add an allowed range to some freeform inputs, allow to specify 'units' in the freeform, add the possibility to convert units

This commit is contained in:
Pieter Vander Vennet 2025-05-20 00:34:39 +02:00
parent 32cb8f489f
commit fb8ead2a2c
16 changed files with 270 additions and 103 deletions

View file

@ -5,6 +5,7 @@ import unit from "../../assets/layers/unit/unit.json"
import TagRenderingConfig from "./ThemeConfig/TagRenderingConfig"
import Validators, { ValidatorType } from "../UI/InputElement/Validators"
import { Validator } from "../UI/InputElement/Validator"
import FloatValidator from "../UI/InputElement/Validators/FloatValidator"
export class Unit {
private static allUnits = this.initUnits()
@ -334,10 +335,11 @@ export class Unit {
return [undefined, undefined]
}
asHumanLongValue(value: string, country: () => string): BaseUIElement | string {
asHumanLongValue(value: string | number, country: () => string): BaseUIElement | string {
if (value === undefined) {
return undefined
}
value = "" + value
const [stripped, denom] = this.findDenomination(value, country)
const human = denom?.human
if (this.inverted) {
@ -393,4 +395,19 @@ export class Unit {
}
return this.denominations[0]
}
/**
* Gets the value in the canonical denomination;
* e.g. "1cm -> 0.01" as it is 0.01meter
* @param v
*/
public valueInCanonical(value: string, country: () => string): number {
const denom = this.findDenomination(value, country)
if (!denom) {
return undefined
}
const [v, d] = denom
const vf = new FloatValidator().reformat(v)
return Number(vf) * (d.factorToCanonical ?? 1)
}
}