mapcomplete/UI/Popup/FeatureInfoBox.ts

187 lines
7.4 KiB
TypeScript
Raw Normal View History

2020-10-14 12:15:09 +02:00
import {UIEventSource} from "../../Logic/UIEventSource";
2020-10-27 01:01:34 +01:00
import EditableTagRendering from "./EditableTagRendering";
import QuestionBox from "./QuestionBox";
import Combine from "../Base/Combine";
import TagRenderingAnswer from "./TagRenderingAnswer";
import State from "../../State";
2021-01-08 02:13:44 +01:00
import ScrollableFullScreen from "../Base/ScrollableFullScreen";
import {Tag} from "../../Logic/Tags/Tag";
import Constants from "../../Models/Constants";
import SharedTagRenderings from "../../Customizations/SharedTagRenderings";
2021-06-14 02:39:23 +02:00
import BaseUIElement from "../BaseUIElement";
import {VariableUiElement} from "../Base/VariableUIElement";
import DeleteWizard from "./DeleteWizard";
import SplitRoadWizard from "./SplitRoadWizard";
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig";
import LayerConfig from "../../Models/ThemeConfig/LayerConfig";
import {Translation} from "../i18n/Translation";
import {Utils} from "../../Utils";
import {SubstitutedTranslation} from "../SubstitutedTranslation";
import MoveWizard from "./MoveWizard";
2021-10-22 18:53:07 +02:00
import {FixedUiElement} from "../Base/FixedUiElement";
export default class FeatureInfoBox extends ScrollableFullScreen {
2021-06-14 02:39:23 +02:00
public constructor(
2020-10-27 01:01:34 +01:00
tags: UIEventSource<any>,
layerConfig: LayerConfig,
) {
super(() => FeatureInfoBox.GenerateTitleBar(tags, layerConfig),
() => FeatureInfoBox.GenerateContent(tags, layerConfig),
undefined);
2020-11-17 16:29:51 +01:00
if (layerConfig === undefined) {
throw "Undefined layerconfig";
2020-11-02 18:59:21 +01:00
}
2021-01-25 03:12:09 +01:00
}
2021-01-25 04:21:22 +01:00
private static GenerateTitleBar(tags: UIEventSource<any>,
2021-06-14 02:39:23 +02:00
layerConfig: LayerConfig): BaseUIElement {
const title = new TagRenderingAnswer(tags, layerConfig.title ?? new TagRenderingConfig("POI"))
.SetClass("break-words font-bold sm:p-0.5 md:p-1 sm:p-1.5 md:p-2");
2021-01-08 02:13:44 +01:00
const titleIcons = new Combine(
layerConfig.titleIcons.map(icon => new TagRenderingAnswer(tags, icon,
2021-07-16 02:13:41 +02:00
"block w-8 h-8 align-baseline box-content sm:p-0.5", "width: 2rem;")
))
.SetClass("flex flex-row flex-wrap pt-0.5 sm:pt-1 items-center mr-2")
2021-01-25 03:12:09 +01:00
2021-01-25 04:21:22 +01:00
return new Combine([
new Combine([title, titleIcons]).SetClass("flex flex-col sm:flex-row flex-grow justify-between")
2021-01-25 03:12:09 +01:00
])
}
2021-01-25 04:21:22 +01:00
private static GenerateContent(tags: UIEventSource<any>,
2021-06-14 02:39:23 +02:00
layerConfig: LayerConfig): BaseUIElement {
2021-10-22 18:53:07 +02:00
let questionBoxes: Map<string, BaseUIElement> = new Map<string, BaseUIElement>();
if (State.state.featureSwitchUserbadge.data) {
2021-10-22 18:53:07 +02:00
const allGroupNames = Utils.Dedup(layerConfig.tagRenderings.map(tr => tr.group))
for (const groupName of allGroupNames) {
const questions = layerConfig.tagRenderings.filter(tr => tr.group === groupName)
const questionBox = new QuestionBox(tags, questions, layerConfig.units);
console.log("Groupname:", groupName)
questionBoxes.set(groupName, questionBox)
}
}
const renderings: BaseUIElement[] = layerConfig.tagRenderings.map(tr => {
2021-10-22 18:53:07 +02:00
if (tr.question === null || tr.id === "questions") {
console.log("Rendering is", tr)
// This is a question box!
const questionBox = questionBoxes.get(tr.group)
questionBoxes.delete(tr.group)
2020-12-08 23:44:34 +01:00
return questionBox;
}
return new EditableTagRendering(tags, tr, layerConfig.units);
2020-12-08 23:44:34 +01:00
});
let editElements: BaseUIElement[] = []
2021-10-22 18:53:07 +02:00
questionBoxes.forEach(questionBox => {
2021-07-28 15:14:13 +02:00
editElements.push(questionBox);
2021-10-22 18:53:07 +02:00
})
if(layerConfig.allowMove) {
editElements.push(
new VariableUiElement(tags.map(tags => tags.id).map(id => {
const feature = State.state.allElements.ContainingFeatures.get(id)
return new MoveWizard(
feature,
State.state,
layerConfig.allowMove
);
})
)
);
}
if (layerConfig.deletion) {
2021-07-28 15:14:13 +02:00
editElements.push(
new VariableUiElement(tags.map(tags => tags.id).map(id =>
new DeleteWizard(
id,
layerConfig.deletion
))
))
}
if (layerConfig.allowSplit) {
2021-07-28 15:14:13 +02:00
editElements.push(
new VariableUiElement(tags.map(tags => tags.id).map(id =>
new SplitRoadWizard(id))
))
}
const hasMinimap = layerConfig.tagRenderings.some(tr => FeatureInfoBox.hasMinimap(tr))
if (!hasMinimap) {
renderings.push(new TagRenderingAnswer(tags, SharedTagRenderings.SharedTagRendering.get("minimap")))
}
2021-07-28 15:14:13 +02:00
editElements.push(
new VariableUiElement(
State.state.osmConnection.userDetails
.map(ud => ud.csCount)
.map(csCount => {
if (csCount <= Constants.userJourney.historyLinkVisible
&& State.state.featureSwitchIsDebugging.data == false
&& State.state.featureSwitchIsTesting.data === false) {
return undefined
}
return new TagRenderingAnswer(tags, SharedTagRenderings.SharedTagRendering.get("last_edit"));
}, [State.state.featureSwitchIsDebugging, State.state.featureSwitchIsTesting])
)
)
2021-07-28 15:14:13 +02:00
editElements.push(
new VariableUiElement(
State.state.featureSwitchIsDebugging.map(isDebugging => {
if (isDebugging) {
const config: TagRenderingConfig = new TagRenderingConfig({render: "{all_tags()}"}, "");
return new TagRenderingAnswer(tags, config, "all_tags")
}
})
)
)
2021-07-28 15:14:13 +02:00
const editors = new VariableUiElement(State.state.featureSwitchUserbadge.map(
userbadge => {
if (!userbadge) {
2021-07-28 15:14:13 +02:00
return undefined
}
return new Combine(editElements).SetClass("flex flex-col")
2021-07-28 15:14:13 +02:00
}
))
renderings.push(editors)
2021-01-08 02:13:44 +01:00
return new Combine(renderings).SetClass("block")
}
2020-07-20 15:54:50 +02:00
/**
* Returns true if this tag rendering has a minimap in some language.
* Note: this might be hidden by conditions
*/
private static hasMinimap(renderingConfig: TagRenderingConfig): boolean {
const translations: Translation[] = Utils.NoNull([renderingConfig.render, ...(renderingConfig.mappings ?? []).map(m => m.then)]);
for (const translation of translations) {
for (const key in translation.translations) {
if (!translation.translations.hasOwnProperty(key)) {
continue
}
const template = translation.translations[key]
const parts = SubstitutedTranslation.ExtractSpecialComponents(template)
const hasMiniMap = parts.filter(part => part.special !== undefined).some(special => special.special.func.funcName === "minimap")
if (hasMiniMap) {
return true;
}
}
}
return false;
}
}