MapComplete/src/Customizations/AllKnownLayouts.ts

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

70 lines
2.3 KiB
TypeScript
Raw Normal View History

import ThemeConfig from "../Models/ThemeConfig/ThemeConfig"
import favourite from "../assets/generated/layers/favourite.json"
import { ThemeConfigJson } from "../Models/ThemeConfig/Json/ThemeConfigJson"
import { AllSharedLayers } from "./AllSharedLayers"
import Constants from "../Models/Constants"
2025-01-10 22:11:18 +01:00
import ScriptUtils from "../../scripts/ScriptUtils"
import { readFileSync } from "fs"
import { LayerConfigJson } from "../Models/ThemeConfig/Json/LayerConfigJson"
/**
* Somewhat of a dictionary, which lazily parses needed themes
*/
export class AllKnownLayoutsLazy {
private readonly raw: Map<string, ThemeConfigJson> = new Map()
private readonly dict: Map<string, ThemeConfig> = new Map()
constructor(includeFavouriteLayer = true) {
const paths = ScriptUtils.readDirRecSync("./public/assets/generated/themes/",1)
2025-01-10 22:11:18 +01:00
for (const path of paths) {
const themeConfigJson = <ThemeConfigJson> JSON.parse(readFileSync(path, "utf8"))
for (const layerId of Constants.added_by_default) {
2023-12-02 03:19:50 +01:00
if (layerId === "favourite" && favourite.id) {
if (includeFavouriteLayer) {
themeConfigJson.layers.push(<LayerConfigJson> favourite)
2022-05-21 01:02:03 +02:00
}
continue
}
const defaultLayer = AllSharedLayers.getSharedLayersConfigs().get(layerId)
if (defaultLayer === undefined) {
continue
}
2025-01-10 22:11:18 +01:00
themeConfigJson.layers.push(defaultLayer)
}
2025-01-10 22:11:18 +01:00
this.raw.set(themeConfigJson.id, themeConfigJson)
}
}
2021-11-07 16:34:51 +01:00
public getConfig(key: string): ThemeConfigJson {
return this.raw.get(key)
}
2024-08-14 13:53:56 +02:00
public size() {
return this.raw.size
}
public get(key: string): ThemeConfig {
const cached = this.dict.get(key)
if (cached !== undefined) {
return cached
}
const layout = new ThemeConfig(this.getConfig(key))
this.dict.set(key, layout)
return layout
2022-01-26 21:40:38 +01:00
}
public keys() {
return this.raw.keys()
2022-07-11 09:14:26 +02:00
}
public values() {
return Array.from(this.keys()).map((k) => this.get(k))
2021-04-10 03:50:44 +02:00
}
}
2021-04-10 03:50:44 +02:00
export class AllKnownLayouts {
public static allKnownLayouts: AllKnownLayoutsLazy = new AllKnownLayoutsLazy()
2020-07-05 18:59:47 +02:00
}