mapcomplete/UI/Popup/EditableTagRendering.ts

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

107 lines
4 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { UIEventSource } from "../../Logic/UIEventSource"
import TagRenderingQuestion from "./TagRenderingQuestion"
import Translations from "../i18n/Translations"
import Combine from "../Base/Combine"
import TagRenderingAnswer from "./TagRenderingAnswer"
import Svg from "../../Svg"
import Toggle from "../Input/Toggle"
import BaseUIElement from "../BaseUIElement"
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig"
import { Unit } from "../../Models/Unit"
import Lazy from "../Base/Lazy"
import { FixedUiElement } from "../Base/FixedUiElement"
import FeaturePipelineState from "../../Logic/State/FeaturePipelineState"
2020-10-27 01:01:34 +01:00
2021-06-14 02:39:23 +02:00
export default class EditableTagRendering extends Toggle {
2022-09-08 21:40:48 +02:00
constructor(
tags: UIEventSource<any>,
configuration: TagRenderingConfig,
units: Unit[],
state,
options: {
editMode?: UIEventSource<boolean>
innerElementClasses?: string
}
) {
// The tagrendering is hidden if:
// - The answer is unknown. The questionbox will then show the question
// - There is a condition hiding the answer
2022-09-08 21:40:48 +02:00
const renderingIsShown = tags.map(
(tags) =>
configuration.IsKnown(tags) &&
(configuration?.condition?.matchesProperties(tags) ?? true)
)
2021-11-07 16:34:51 +01:00
super(
2021-10-28 00:53:09 +02:00
new Lazy(() => {
const editMode = options.editMode ?? new UIEventSource<boolean>(false)
2022-09-08 21:40:48 +02:00
let rendering = EditableTagRendering.CreateRendering(
state,
tags,
configuration,
units,
editMode
)
2021-10-28 00:53:09 +02:00
rendering.SetClass(options.innerElementClasses)
2022-09-08 21:40:48 +02:00
if (state.featureSwitchIsDebugging.data || state.featureSwitchIsTesting.data) {
2022-01-30 18:29:00 +01:00
rendering = new Combine([
new FixedUiElement(configuration.id).SetClass("self-end subtle"),
2022-09-08 21:40:48 +02:00
rendering,
2022-01-30 18:29:00 +01:00
]).SetClass("flex flex-col")
}
2021-10-28 00:53:09 +02:00
return rendering
}),
undefined,
renderingIsShown
)
}
2021-11-07 16:34:51 +01:00
2022-09-08 21:40:48 +02:00
private static CreateRendering(
state: FeaturePipelineState,
tags: UIEventSource<any>,
configuration: TagRenderingConfig,
units: Unit[],
editMode: UIEventSource<boolean>
): BaseUIElement {
const answer: BaseUIElement = new TagRenderingAnswer(tags, configuration, state)
2021-06-16 14:23:53 +02:00
answer.SetClass("w-full")
2022-09-08 21:40:48 +02:00
let rendering = answer
2020-10-27 01:01:34 +01:00
if (configuration.question !== undefined && state?.featureSwitchUserbadge?.data) {
2021-06-14 02:39:23 +02:00
// We have a question and editing is enabled
2022-09-08 21:40:48 +02:00
const answerWithEditButton = new Combine([
answer,
new Toggle(
new Combine([Svg.pencil_ui()])
.SetClass("block relative h-10 w-10 p-2 float-right")
.SetStyle("border: 1px solid black; border-radius: 0.7em")
.onClick(() => {
2022-09-08 21:40:48 +02:00
editMode.setData(true)
}),
undefined,
2022-09-08 21:40:48 +02:00
state.osmConnection.isLoggedIn
),
]).SetClass("flex justify-between w-full")
2021-06-14 02:39:23 +02:00
2022-09-08 21:40:48 +02:00
const question = new Lazy(
() =>
new TagRenderingQuestion(tags, configuration, state, {
units: units,
2022-09-08 21:40:48 +02:00
cancelButton: Translations.t.general.cancel
.Clone()
2022-02-04 00:45:22 +01:00
.SetClass("btn btn-secondary")
.onClick(() => {
editMode.setData(false)
}),
afterSave: () => {
editMode.setData(false)
2022-09-08 21:40:48 +02:00
},
})
2021-06-14 02:39:23 +02:00
)
2022-09-08 21:40:48 +02:00
rendering = new Toggle(question, answerWithEditButton, editMode)
2020-10-27 01:01:34 +01:00
}
2022-09-08 21:40:48 +02:00
return rendering
2020-10-27 01:01:34 +01:00
}
2022-09-08 21:40:48 +02:00
}