2023-06-16 02:36:11 +02:00
|
|
|
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
|
2023-06-18 00:44:57 +02:00
|
|
|
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-23 16:14:43 +02:00
|
|
|
import { DeleteConfigJson } from "../../Models/ThemeConfig/Json/DeleteConfigJson"
|
|
|
|
import { Utils } from "../../Utils"
|
2023-06-16 02:36:11 +02:00
|
|
|
|
|
|
|
export default class EditLayerState {
|
|
|
|
public readonly osmConnection: OsmConnection
|
2023-06-18 00:44:57 +02:00
|
|
|
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>
|
|
|
|
>({})
|
|
|
|
|
2023-06-18 00:44:57 +02:00
|
|
|
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),
|
|
|
|
}
|
2023-06-23 16:14:43 +02:00
|
|
|
this.configuration.addCallback((config) => {
|
|
|
|
if (
|
|
|
|
config?.deletion !== undefined &&
|
|
|
|
(<DeleteConfigJson>config?.deletion)?.neededChangesets === undefined
|
|
|
|
) {
|
|
|
|
console.trace("Needed changesets is undefined")
|
|
|
|
}
|
|
|
|
console.log("Current config is", Utils.Clone(config))
|
|
|
|
})
|
2023-06-20 01:32:24 +02:00
|
|
|
}
|
|
|
|
|
2023-06-23 16:14:43 +02:00
|
|
|
public getCurrentValueFor(path: ReadonlyArray<string | number>): any | undefined {
|
|
|
|
// Walk the path down to see if we find something
|
|
|
|
let entry = this.configuration.data
|
|
|
|
for (let i = 0; i < path.length; i++) {
|
|
|
|
if (entry === undefined) {
|
|
|
|
// We reached a dead end - no old vlaue
|
|
|
|
return undefined
|
2023-06-20 01:32:24 +02:00
|
|
|
}
|
2023-06-23 16:14:43 +02:00
|
|
|
const breadcrumb = path[i]
|
|
|
|
entry = entry[breadcrumb]
|
|
|
|
}
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
|
|
|
public register(
|
|
|
|
path: ReadonlyArray<string | number>,
|
|
|
|
value: Store<any>,
|
|
|
|
noInitialSync: boolean = false
|
|
|
|
): () => void {
|
2023-06-23 17:28:44 +02:00
|
|
|
const unsync = value.addCallback((v) => this.setValueAt(path, v))
|
2023-06-23 16:14:43 +02:00
|
|
|
if (!noInitialSync) {
|
2023-06-23 17:28:44 +02:00
|
|
|
this.setValueAt(path, value.data)
|
2023-06-23 16:14:43 +02:00
|
|
|
}
|
|
|
|
return unsync
|
2023-06-16 02:36:11 +02:00
|
|
|
}
|
2023-06-18 00:44:57 +02:00
|
|
|
|
|
|
|
public getSchemaStartingWith(path: string[]) {
|
|
|
|
return this.schema.filter(
|
|
|
|
(sch) =>
|
|
|
|
!path.some((part, i) => !(sch.path.length > path.length && sch.path[i] === part))
|
|
|
|
)
|
|
|
|
}
|
2023-06-21 17:13:09 +02:00
|
|
|
|
|
|
|
public getSchema(path: string[]) {
|
|
|
|
return this.schema.filter(
|
|
|
|
(sch) =>
|
2023-06-23 16:14:43 +02:00
|
|
|
sch !== undefined &&
|
2023-06-21 17:13:09 +02:00
|
|
|
!path.some((part, i) => !(sch.path.length == path.length && sch.path[i] === part))
|
|
|
|
)
|
|
|
|
}
|
2023-06-23 16:14:43 +02:00
|
|
|
|
2023-06-23 17:28:44 +02:00
|
|
|
public setValueAt(path: ReadonlyArray<string | number>, v: any) {
|
2023-06-23 16:14:43 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|