mapcomplete/UI/Popup/FeatureInfoBox.ts

241 lines
10 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";
2021-01-08 02:13:44 +01:00
import ScrollableFullScreen from "../Base/ScrollableFullScreen";
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 {Utils} from "../../Utils";
import MoveWizard from "./MoveWizard";
import Toggle from "../Input/Toggle";
import Lazy from "../Base/Lazy";
2022-05-01 04:17:40 +02:00
import FeaturePipelineState from "../../Logic/State/FeaturePipelineState";
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,
2022-05-01 04:17:40 +02:00
state: FeaturePipelineState,
2022-07-08 03:14:55 +02:00
options?: {
hashToShow?: string,
isShown?: UIEventSource<boolean>,
setHash?: true | boolean
}
) {
if (state === undefined) {
throw "State is undefined!"
}
super(() => FeatureInfoBox.GenerateTitleBar(tags, layerConfig, state),
() => FeatureInfoBox.GenerateContent(tags, layerConfig, state),
2022-07-08 03:14:55 +02:00
options?.hashToShow ?? tags.data.id ?? "item",
options?.isShown,
options);
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>,
layerConfig: LayerConfig,
state: {}): BaseUIElement {
const title = new TagRenderingAnswer(tags, layerConfig.title ?? new TagRenderingConfig("POI"), state)
.SetClass("break-words font-bold sm:p-0.5 md:p-1 sm:p-1.5 md:p-2 text-2xl");
2021-01-08 02:13:44 +01:00
const titleIcons = new Combine(
2022-04-06 19:16:55 +02:00
layerConfig.titleIcons.map(icon => {
return new TagRenderingAnswer(tags, icon, state,
"block h-8 max-h-8 align-baseline box-content sm:p-0.5").SetClass("flex");
}
))
.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>,
layerConfig: LayerConfig,
2022-05-01 04:17:40 +02:00
state: FeaturePipelineState): BaseUIElement {
let questionBoxes: Map<string, QuestionBox> = new Map<string, QuestionBox>();
const allGroupNames = Utils.Dedup(layerConfig.tagRenderings.map(tr => tr.group))
if (state?.featureSwitchUserbadge?.data ?? true) {
const questionSpecs = layerConfig.tagRenderings.filter(tr => tr.id === "questions")
2021-10-22 18:53:07 +02:00
for (const groupName of allGroupNames) {
const questions = layerConfig.tagRenderings.filter(tr => tr.group === groupName)
const questionSpec = questionSpecs.filter(tr => tr.group === groupName)[0]
const questionBox = new QuestionBox(state, {
tagsSource: tags,
tagRenderings: questions,
units: layerConfig.units,
showAllQuestionsAtOnce: questionSpec?.freeform?.helperArgs["showAllQuestions"] ?? state.featureSwitchShowAllQuestions
});
2021-10-22 18:53:07 +02:00
questionBoxes.set(groupName, questionBox)
}
}
const allRenderings = []
for (let i = 0; i < allGroupNames.length; i++) {
const groupName = allGroupNames[i];
const trs = layerConfig.tagRenderings.filter(tr => tr.group === groupName)
2021-10-28 00:53:09 +02:00
const renderingsForGroup: (EditableTagRendering | BaseUIElement)[] = []
const innerClasses = "block w-full break-word text-default m-1 p-1 border-b border-gray-200 mb-2 pb-2";
for (const tr of trs) {
if (tr.question === null || tr.id === "questions") {
// This is a question box!
const questionBox = questionBoxes.get(tr.group)
questionBoxes.delete(tr.group)
if (tr.render !== undefined) {
questionBox.SetClass("text-sm")
const renderedQuestion = new TagRenderingAnswer(tags, tr, state,
2021-12-21 18:35:31 +01:00
tr.group + " questions", "", {
specialViz: new Map<string, BaseUIElement>([["questions", questionBox]])
})
const possiblyHidden = new Toggle(
renderedQuestion,
undefined,
questionBox.restingQuestions.map(ls => ls?.length > 0)
)
renderingsForGroup.push(possiblyHidden)
} else {
renderingsForGroup.push(questionBox)
}
} else {
2021-10-28 00:53:09 +02:00
let classes = innerClasses
2021-11-07 16:34:51 +01:00
let isHeader = renderingsForGroup.length === 0 && i > 0
if (isHeader) {
2021-10-28 00:53:09 +02:00
// This is the first element of a group!
// It should act as header and be sticky
2021-11-07 16:34:51 +01:00
classes = ""
2021-10-28 00:53:09 +02:00
}
2021-11-07 16:34:51 +01:00
const etr = new EditableTagRendering(tags, tr, layerConfig.units, state, {
2021-10-28 00:53:09 +02:00
innerElementClasses: innerClasses
})
2021-11-07 16:34:51 +01:00
if (isHeader) {
2021-10-28 01:26:35 +02:00
etr.SetClass("sticky top-0")
}
renderingsForGroup.push(etr)
}
}
2021-11-07 16:34:51 +01:00
allRenderings.push(...renderingsForGroup)
}
allRenderings.push(
new Toggle(
new Lazy(() => FeatureInfoBox.createEditElements(questionBoxes, layerConfig, tags, state)),
undefined,
state.featureSwitchUserbadge
))
return new Combine(allRenderings).SetClass("block")
}
/**
* All the edit elements, together (note that the question boxes are passed though)
* @param questionBoxes
* @param layerConfig
* @param tags
* @param state
* @private
*/
private static createEditElements(questionBoxes: Map<string, QuestionBox>,
layerConfig: LayerConfig,
tags: UIEventSource<any>,
2022-05-01 04:17:40 +02:00
state: FeaturePipelineState)
: BaseUIElement {
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.allElements.ContainingFeatures.get(id)
if (feature === undefined) {
return "This feature is not register in the state.allElements and cannot be moved"
}
return new MoveWizard(
feature,
state,
layerConfig.allowMove
);
})
2021-10-26 02:09:04 +02:00
).SetClass("text-base")
);
}
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,
state,
layerConfig.deletion
))
2021-10-26 02:09:04 +02:00
).SetClass("text-base"))
}
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, state))
2021-10-26 02:09:04 +02:00
).SetClass("text-base"))
}
2021-07-28 15:14:13 +02:00
editElements.push(
new VariableUiElement(
state.osmConnection.userDetails
.map(ud => ud.csCount)
.map(csCount => {
if (csCount <= Constants.userJourney.historyLinkVisible
&& state.featureSwitchIsDebugging.data == false
&& state.featureSwitchIsTesting.data === false) {
return undefined
}
return new TagRenderingAnswer(tags, SharedTagRenderings.SharedTagRendering.get("last_edit"), state);
}, [state.featureSwitchIsDebugging, state.featureSwitchIsTesting])
)
)
2021-07-28 15:14:13 +02:00
editElements.push(
new VariableUiElement(
state.featureSwitchIsDebugging.map(isDebugging => {
if (isDebugging) {
const config_all_tags: TagRenderingConfig = new TagRenderingConfig({render: "{all_tags()}"}, "");
const config_download: TagRenderingConfig = new TagRenderingConfig({render: "{export_as_geojson()}"}, "");
const config_id: TagRenderingConfig = new TagRenderingConfig({render: "{open_in_iD()}"}, "");
return new Combine([new TagRenderingAnswer(tags, config_all_tags, state),
new TagRenderingAnswer(tags, config_download, state),
new TagRenderingAnswer(tags, config_id, state),
"This is layer "+layerConfig.id
])
}
})
)
)
return new Combine(editElements).SetClass("flex flex-col")
}
}