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-09-15 01:16:33 +02:00
|
|
|
import { QueryParameters } from "../../Logic/Web/QueryParameters"
|
2023-10-12 16:55:26 +02:00
|
|
|
import {
|
|
|
|
ConversionContext,
|
|
|
|
ConversionMessage,
|
|
|
|
DesugaringContext,
|
|
|
|
Pipe,
|
|
|
|
} from "../../Models/ThemeConfig/Conversion/Conversion"
|
|
|
|
import { PrepareLayer } from "../../Models/ThemeConfig/Conversion/PrepareLayer"
|
|
|
|
import { ValidateLayer } from "../../Models/ThemeConfig/Conversion/Validation"
|
|
|
|
import { AllSharedLayers } from "../../Customizations/AllSharedLayers"
|
|
|
|
import { QuestionableTagRenderingConfigJson } from "../../Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
|
2023-10-13 18:46:56 +02:00
|
|
|
import { TagUtils } from "../../Logic/Tags/TagUtils"
|
|
|
|
import StudioServer from "./StudioServer"
|
2023-10-17 00:32:54 +02:00
|
|
|
import { Utils } from "../../Utils"
|
2023-09-15 01:16:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends changes back to the server
|
|
|
|
*/
|
|
|
|
export class LayerStateSender {
|
2023-10-13 18:46:56 +02:00
|
|
|
constructor(layerState: EditLayerState) {
|
2023-09-15 01:16:33 +02:00
|
|
|
layerState.configuration.addCallback(async (config) => {
|
|
|
|
const id = config.id
|
|
|
|
if (id === undefined) {
|
2023-10-13 18:46:56 +02:00
|
|
|
console.warn("No id found in layer, not updating")
|
2023-09-15 01:16:33 +02:00
|
|
|
return
|
|
|
|
}
|
2023-10-13 18:46:56 +02:00
|
|
|
await layerState.server.updateLayer(<LayerConfigJson>config)
|
2023-09-15 01:16:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
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-10-12 16:55:26 +02:00
|
|
|
public readonly messages: Store<ConversionMessage[]>
|
2023-10-13 18:46:56 +02:00
|
|
|
public readonly server: StudioServer
|
2023-06-20 01:32:24 +02:00
|
|
|
|
2023-10-13 18:46:56 +02:00
|
|
|
constructor(schema: ConfigMeta[], server: StudioServer) {
|
2023-06-18 00:44:57 +02:00
|
|
|
this.schema = schema
|
2023-10-13 18:46:56 +02:00
|
|
|
this.server = server
|
2023-09-15 01:16:33 +02:00
|
|
|
this.osmConnection = new OsmConnection({
|
|
|
|
oauth_token: QueryParameters.GetQueryParameter(
|
|
|
|
"oauth_token",
|
|
|
|
undefined,
|
|
|
|
"Used to complete the login"
|
|
|
|
),
|
|
|
|
})
|
2023-06-20 01:32:24 +02:00
|
|
|
this.featureSwitches = {
|
|
|
|
featureSwitchIsDebugging: new UIEventSource<boolean>(true),
|
|
|
|
}
|
2023-10-12 16:55:26 +02:00
|
|
|
let state: DesugaringContext
|
|
|
|
{
|
|
|
|
const layers = AllSharedLayers.getSharedLayersConfigs()
|
|
|
|
const questions = layers.get("questions")
|
|
|
|
const sharedQuestions = new Map<string, QuestionableTagRenderingConfigJson>()
|
|
|
|
for (const question of questions.tagRenderings) {
|
|
|
|
sharedQuestions.set(question["id"], <QuestionableTagRenderingConfigJson>question)
|
|
|
|
}
|
|
|
|
state = {
|
|
|
|
tagRenderings: sharedQuestions,
|
|
|
|
sharedLayers: layers,
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 18:46:56 +02:00
|
|
|
this.messages = this.configuration.mapD((config) => {
|
2023-10-12 16:55:26 +02:00
|
|
|
const context = ConversionContext.construct([], ["prepare"])
|
2023-10-17 00:32:54 +02:00
|
|
|
const trs = Utils.NoNull(config.tagRenderings ?? [])
|
|
|
|
for (let i = 0; i < trs.length; i++) {
|
|
|
|
const tr = trs[i]
|
2023-10-13 18:46:56 +02:00
|
|
|
if (typeof tr === "string") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (!tr["id"] && !tr["override"]) {
|
|
|
|
const qtr = <QuestionableTagRenderingConfigJson>tr
|
|
|
|
let id = "" + i
|
|
|
|
if (qtr?.freeform?.key) {
|
|
|
|
id = qtr?.freeform?.key
|
|
|
|
} else if (qtr.mappings?.[0]?.if) {
|
|
|
|
id =
|
|
|
|
qtr.freeform?.key ??
|
|
|
|
TagUtils.Tag(qtr.mappings[0].if).usedKeys()?.[0] ??
|
|
|
|
"" + i
|
|
|
|
}
|
|
|
|
qtr["id"] = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:55:26 +02:00
|
|
|
const prepare = new Pipe(
|
|
|
|
new PrepareLayer(state),
|
|
|
|
new ValidateLayer("dynamic", false, undefined)
|
|
|
|
)
|
|
|
|
prepare.convert(<LayerConfigJson>config, context)
|
|
|
|
return context.messages
|
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-17 00:32:54 +02:00
|
|
|
private readonly _stores = new Map<string, UIEventSource<any>>()
|
|
|
|
public getStoreFor<T>(path: ReadonlyArray<string | number>): UIEventSource<T | undefined> {
|
|
|
|
const key = path.join(".")
|
|
|
|
|
|
|
|
// TODO check if this gives problems when changing the order of e.g. mappings and questions
|
|
|
|
if (this._stores.has(key)) {
|
|
|
|
return this._stores.get(key)
|
|
|
|
}
|
2023-08-23 11:11:53 +02:00
|
|
|
const store = new UIEventSource<any>(this.getCurrentValueFor(path))
|
|
|
|
store.addCallback((v) => {
|
|
|
|
this.setValueAt(path, v)
|
|
|
|
})
|
2023-10-17 00:32:54 +02:00
|
|
|
this._stores.set(key, store)
|
2023-08-23 11:11:53 +02:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
2023-06-23 16:14:43 +02:00
|
|
|
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
|
|
|
|
2023-06-30 13:36:02 +02:00
|
|
|
public getTranslationAt(path: string[]): ConfigMeta {
|
|
|
|
const origConfig = this.getSchema(path)[0]
|
|
|
|
return {
|
|
|
|
path,
|
|
|
|
type: "translation",
|
|
|
|
hints: {
|
|
|
|
typehint: "translation",
|
|
|
|
},
|
|
|
|
required: origConfig.required ?? false,
|
|
|
|
description: origConfig.description ?? "A translatable object",
|
|
|
|
}
|
|
|
|
}
|
2023-09-15 01:16:33 +02:00
|
|
|
|
2023-06-30 13:36:02 +02:00
|
|
|
public getSchema(path: string[]): ConfigMeta[] {
|
2023-09-15 01:16:33 +02:00
|
|
|
const schemas = this.schema.filter(
|
2023-06-21 17:13:09 +02:00
|
|
|
(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-09-15 01:16:33 +02:00
|
|
|
if (schemas.length == 0) {
|
|
|
|
console.warn("No schemas found for path", path.join("."))
|
|
|
|
}
|
|
|
|
return schemas
|
2023-06-21 17:13:09 +02:00
|
|
|
}
|
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-10-06 23:56:50 +02:00
|
|
|
let entry = this.configuration.data
|
2023-10-17 00:32:54 +02:00
|
|
|
const isUndefined =
|
|
|
|
v !== undefined &&
|
|
|
|
v !== null &&
|
|
|
|
v !== "" &&
|
|
|
|
!(typeof v === "object" && Object.keys({}).length === 0)
|
|
|
|
|
2023-10-06 23:56:50 +02:00
|
|
|
for (let i = 0; i < path.length - 1; i++) {
|
|
|
|
const breadcrumb = path[i]
|
|
|
|
if (entry[breadcrumb] === undefined) {
|
|
|
|
entry[breadcrumb] = typeof path[i + 1] === "number" ? [] : {}
|
2023-06-23 16:14:43 +02:00
|
|
|
}
|
2023-10-06 23:56:50 +02:00
|
|
|
entry = entry[breadcrumb]
|
2023-10-17 00:32:54 +02:00
|
|
|
if (entry === undefined && isUndefined) {
|
|
|
|
// Nothing to do anymore: we cannot traverse the object, but don't have to set something anyway
|
|
|
|
return
|
|
|
|
}
|
2023-10-06 23:56:50 +02:00
|
|
|
}
|
2023-10-17 00:32:54 +02:00
|
|
|
if (isUndefined) {
|
2023-10-06 23:56:50 +02:00
|
|
|
entry[path.at(-1)] = v
|
|
|
|
} else if (entry) {
|
|
|
|
delete entry[path.at(-1)]
|
2023-06-23 16:14:43 +02:00
|
|
|
}
|
2023-10-06 23:56:50 +02:00
|
|
|
this.configuration.ping()
|
2023-06-23 16:14:43 +02:00
|
|
|
}
|
2023-06-16 02:36:11 +02:00
|
|
|
}
|