mapcomplete/UI/Popup/EditableTagRendering.ts

85 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-10-27 01:01:34 +01: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 State from "../../State";
2020-11-06 01:58:26 +01:00
import Svg from "../../Svg";
2021-06-14 02:39:23 +02:00
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";
2020-10-27 01:01:34 +01:00
2021-06-14 02:39:23 +02:00
export default class EditableTagRendering extends Toggle {
2020-10-27 01:01:34 +01:00
constructor(tags: UIEventSource<any>,
2021-06-16 21:23:03 +02:00
configuration: TagRenderingConfig,
units: Unit [],
2021-11-07 16:34:51 +01:00
options: {
editMode?: UIEventSource<boolean>,
innerElementClasses?: string
2021-10-28 00:53:09 +02:00
}
) {
// The tagrendering is hidden if:
// - The answer is unknown. The questionbox will then show the question
// - There is a condition hiding the answer
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)
const rendering = EditableTagRendering.CreateRendering(tags, configuration, units, editMode);
rendering.SetClass(options.innerElementClasses)
return rendering
}),
undefined,
renderingIsShown
)
}
2021-11-07 16:34:51 +01:00
private static CreateRendering(tags: UIEventSource<any>, configuration: TagRenderingConfig, units: Unit[], editMode: UIEventSource<boolean>): BaseUIElement {
2021-12-21 18:35:31 +01:00
const answer: BaseUIElement = new TagRenderingAnswer(tags, configuration, State.state)
2021-06-16 14:23:53 +02:00
answer.SetClass("w-full")
2021-06-14 02:39:23 +02:00
let rendering = answer;
2020-10-27 01:01:34 +01:00
2021-06-14 02:39:23 +02:00
if (configuration.question !== undefined && State.state?.featureSwitchUserbadge?.data) {
// We have a question and editing is enabled
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(() => {
editMode.setData(true);
}),
undefined,
State.state.osmConnection.isLoggedIn)
]).SetClass("flex justify-between w-full")
2021-06-14 02:39:23 +02:00
const question = new Lazy(() =>
new TagRenderingQuestion(tags, configuration,
{
units: units,
cancelButton: Translations.t.general.cancel.Clone()
.SetClass("btn btn-secondary mr-3")
.onClick(() => {
editMode.setData(false)
}),
afterSave: () => {
editMode.setData(false)
}
}))
2021-06-14 02:39:23 +02:00
rendering = new Toggle(
question,
answerWithEditButton,
editMode
)
2020-10-27 01:01:34 +01:00
}
return rendering;
2020-10-27 01:01:34 +01:00
}
}