forked from MapComplete/MapComplete
Refactoring: move the units into the layers instead of the themes
This commit is contained in:
parent
3492b5d403
commit
206aff2c9a
16 changed files with 259 additions and 300 deletions
|
@ -2,6 +2,7 @@ import BaseUIElement from "../UI/BaseUIElement";
|
|||
import {FixedUiElement} from "../UI/Base/FixedUiElement";
|
||||
import Combine from "../UI/Base/Combine";
|
||||
import {Denomination} from "./Denomination";
|
||||
import UnitConfigJson from "./ThemeConfig/Json/UnitConfigJson";
|
||||
|
||||
export class Unit {
|
||||
public readonly appliesToKeys: Set<string>;
|
||||
|
@ -52,6 +53,35 @@ export class Unit {
|
|||
this.possiblePostFixes.sort((a, b) => b.length - a.length)
|
||||
}
|
||||
|
||||
|
||||
static fromJson(json: UnitConfigJson, ctx: string){
|
||||
const appliesTo = json.appliesToKey
|
||||
for (let i = 0; i < appliesTo.length; i++) {
|
||||
let key = appliesTo[i];
|
||||
if (key.trim() !== key) {
|
||||
throw `${ctx}.appliesToKey[${i}] is invalid: it starts or ends with whitespace`
|
||||
}
|
||||
}
|
||||
|
||||
if ((json.applicableUnits ?? []).length === 0) {
|
||||
throw `${ctx}: define at least one applicable unit`
|
||||
}
|
||||
// Some keys do have unit handling
|
||||
|
||||
const defaultSet = json.applicableUnits.filter(u => u.default === true)
|
||||
// No default is defined - we pick the first as default
|
||||
if (defaultSet.length === 0) {
|
||||
json.applicableUnits[0].default = true
|
||||
}
|
||||
|
||||
// Check that there are not multiple defaults
|
||||
if (defaultSet.length > 1) {
|
||||
throw `Multiple units are set as default: they have canonical values of ${defaultSet.map(u => u.canonicalDenomination).join(", ")}`
|
||||
}
|
||||
const applicable = json.applicableUnits.map((u, i) => new Denomination(u, `${ctx}.units[${i}]`))
|
||||
return new Unit(appliesTo, applicable, json.eraseInvalidValues ?? false)
|
||||
}
|
||||
|
||||
isApplicableToKey(key: string | undefined): boolean {
|
||||
if (key === undefined) {
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue