MapComplete/UI/BigComponents/SimpleAddUI.ts

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

116 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-06-29 03:12:44 +02:00
/**
* Asks to add a feature at the last clicked location, at least if zoom is sufficient
*/
import { UIEventSource } from "../../Logic/UIEventSource"
import Translations from "../i18n/Translations"
2021-06-12 02:58:32 +02:00
import BaseUIElement from "../BaseUIElement"
2021-06-14 02:39:23 +02:00
import { VariableUiElement } from "../Base/VariableUIElement"
import Toggle from "../Input/Toggle"
import CreateNewNodeAction from "../../Logic/Osm/Actions/CreateNewNodeAction"
import { OsmObject, OsmWay } from "../../Logic/Osm/OsmObject"
import PresetConfig from "../../Models/ThemeConfig/PresetConfig"
import FilteredLayer from "../../Models/FilteredLayer"
2022-01-25 21:55:51 +01:00
import Loading from "../Base/Loading"
2022-03-23 12:42:47 +01:00
import Hash from "../../Logic/Web/Hash"
2022-10-27 01:50:41 +02:00
import { WayId } from "../../Models/OsmFeature"
2022-11-08 14:37:48 +01:00
import { Tag } from "../../Logic/Tags/Tag"
import { SpecialVisualizationState } from "../SpecialVisualization"
import { Feature } from "geojson"
import { FixedUiElement } from "../Base/FixedUiElement"
2021-06-14 02:39:23 +02:00
/*
* The SimpleAddUI is a single panel, which can have multiple states:
* - A list of presets which can be added by the user
* - A 'confirm-selection' button (or alternatively: please enable the layer)
* - A 'something is wrong - please soom in further'
* - A 'read your unread messages before adding a point'
*/
export interface PresetInfo extends PresetConfig {
2021-06-14 02:39:23 +02:00
name: string | BaseUIElement
icon: () => BaseUIElement
2022-01-25 21:55:51 +01:00
layerToAddTo: FilteredLayer
boundsFactor?: 0.25 | number
2021-06-14 02:39:23 +02:00
}
2021-01-08 16:49:42 +01:00
export default class SimpleAddUI extends Toggle {
constructor(state: SpecialVisualizationState) {
const takeLocationFrom = state.mapProperties.lastClickLocation
2021-06-14 02:39:23 +02:00
const selectedPreset = new UIEventSource<PresetInfo>(undefined)
takeLocationFrom.addCallback((_) => selectedPreset.setData(undefined))
2022-06-13 01:35:50 +02:00
async function createNewPoint(
tags: Tag[],
2022-06-13 01:35:50 +02:00
location: { lat: number; lon: number },
snapOntoWay?: OsmWay
): Promise<void> {
2023-01-29 13:31:49 +01:00
if (snapOntoWay) {
tags.push(new Tag("_referencing_ways", "way/" + snapOntoWay.id))
}
const newElementAction = new CreateNewNodeAction(tags, location.lat, location.lon, {
theme: state.layout?.id ?? "unkown",
changeType: "create",
snapOnto: snapOntoWay,
})
await state.changes.applyAction(newElementAction)
selectedPreset.setData(undefined)
const selectedFeature: Feature = state.indexedFeatures.featuresById.data.get(
2022-11-08 14:37:48 +01:00
newElementAction.newElementId
)
state.selectedElement.setData(selectedFeature)
2022-03-23 12:42:47 +01:00
Hash.hash.setData(newElementAction.newElementId)
}
2021-06-14 02:39:23 +02:00
const addUi = new VariableUiElement(
selectedPreset.map((preset) => {
function confirm(
tags: any[],
location: { lat: number; lon: number },
2022-09-21 02:21:20 +02:00
snapOntoWayId?: WayId
) {
if (snapOntoWayId === undefined) {
createNewPoint(tags, location, undefined)
} else {
OsmObject.DownloadObject(snapOntoWayId).addCallbackAndRunD((way) => {
2022-09-21 02:21:20 +02:00
createNewPoint(tags, location, way)
return true
2022-09-08 21:40:48 +02:00
})
}
2022-09-08 21:40:48 +02:00
}
function cancel() {
selectedPreset.setData(undefined)
2021-06-14 02:39:23 +02:00
}
2022-04-01 12:51:55 +02:00
const message = Translations.t.general.add.addNew.Subs(
{ category: preset.name },
preset.name["context"]
2022-09-08 21:40:48 +02:00
)
return new FixedUiElement("ConfirmLocationOfPoint...") /*ConfirmLocationOfPoint(
2022-09-08 21:40:48 +02:00
state,
filterViewIsOpened,
2022-09-08 21:40:48 +02:00
preset,
message,
2022-07-18 00:28:26 +02:00
takeLocationFrom.data,
2022-09-08 21:40:48 +02:00
confirm,
cancel,
() => {
selectedPreset.setData(undefined)
},
{
cancelIcon: Svg.back_svg(),
2022-11-14 01:29:22 +01:00
cancelText: Translations.t.general.add.backToSelect,
2022-09-08 21:40:48 +02:00
}
)*/
2022-09-08 21:40:48 +02:00
})
)
2021-06-14 02:39:23 +02:00
super(
new Loading(Translations.t.general.add.stillLoading).SetClass("alert"),
addUi,
state.dataIsLoading
2022-09-08 21:40:48 +02:00
)
2020-06-29 03:12:44 +02:00
}
}