mapcomplete/Customizations/AllKnownLayouts.ts

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

63 lines
1.9 KiB
TypeScript
Raw Normal View History

import known_themes from "../assets/generated/known_themes.json"
import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"
2022-01-18 20:18:12 +01:00
import { LayoutConfigJson } from "../Models/ThemeConfig/Json/LayoutConfigJson"
/**
* Somewhat of a dictionary, which lazily parses needed themes
*/
export class AllKnownLayoutsLazy {
private readonly dict: Map<string, { data: LayoutConfig } | { func: () => LayoutConfig }> =
new Map()
constructor() {
for (const layoutConfigJson of known_themes["themes"]) {
this.dict.set(layoutConfigJson.id, {
func: () => {
const layout = new LayoutConfig(<LayoutConfigJson>layoutConfigJson, true)
for (let i = 0; i < layout.layers.length; i++) {
let layer = layout.layers[i]
if (typeof layer === "string") {
throw "Layer " + layer + " was not expanded in " + layout.id
}
2022-05-21 01:02:03 +02:00
}
return layout
},
})
}
}
2021-11-07 16:34:51 +01:00
public get(key: string): LayoutConfig {
const thunk = this.dict.get(key)
if (thunk === undefined) {
return undefined
}
if (thunk["data"]) {
return thunk["data"]
2022-01-26 21:40:38 +01:00
}
const layout = thunk["func"]()
this.dict.set(key, { data: layout })
return layout
2022-01-26 21:40:38 +01:00
}
public keys() {
return this.dict.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()
2023-03-24 19:21:15 +01:00
static AllPublicLayers() {
const layers = [].concat(
...this.allKnownLayouts
.values()
.filter((layout) => !layout.hideFromOverview)
.map((layout) => layout.layers)
)
return layers
}
2020-07-05 18:59:47 +02:00
}