mapcomplete/UI/Studio/EditLayerState.ts

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

58 lines
2 KiB
TypeScript
Raw Normal View History

2023-06-16 02:36:11 +02:00
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
import { ConfigMeta } from "./configMeta"
2023-06-20 01:32:24 +02:00
import { Store, UIEventSource } from "../../Logic/UIEventSource"
import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson"
2023-06-16 02:36:11 +02:00
export default class EditLayerState {
public readonly osmConnection: OsmConnection
public readonly schema: ConfigMeta[]
2023-06-20 01:32:24 +02:00
public readonly featureSwitches: { featureSwitchIsDebugging: UIEventSource<boolean> }
public readonly configuration: UIEventSource<Partial<LayerConfigJson>> = new UIEventSource<
Partial<LayerConfigJson>
>({})
constructor(schema: ConfigMeta[]) {
this.schema = schema
2023-06-16 02:36:11 +02:00
this.osmConnection = new OsmConnection({})
2023-06-20 01:32:24 +02:00
this.featureSwitches = {
featureSwitchIsDebugging: new UIEventSource<boolean>(true),
}
this.configuration.addCallback((config) => console.log("Current config is", config))
}
2023-06-22 15:07:14 +02:00
public register(path: ReadonlyArray<string | number>, value: Store<any>) {
2023-06-20 01:32:24 +02:00
value.addCallbackAndRun((v) => {
let entry = this.configuration.data
for (let i = 0; i < path.length - 1; i++) {
const breadcrumb = path[i]
if (entry[breadcrumb] === undefined) {
entry[breadcrumb] = typeof path[i + 1] === "number" ? [] : {}
}
entry = entry[breadcrumb]
}
if (v) {
entry[path.at(-1)] = v
} else if (entry) {
delete entry[path.at(-1)]
}
this.configuration.ping()
})
2023-06-16 02:36:11 +02:00
}
public getSchemaStartingWith(path: string[]) {
return this.schema.filter(
(sch) =>
!path.some((part, i) => !(sch.path.length > path.length && sch.path[i] === part))
)
}
public getSchema(path: string[]) {
return this.schema.filter(
(sch) =>
!path.some((part, i) => !(sch.path.length == path.length && sch.path[i] === part))
)
}
2023-06-16 02:36:11 +02:00
}