2022-11-08 14:37:48 +01:00
|
|
|
import { UIEventSource } from "../../Logic/UIEventSource"
|
|
|
|
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
|
2021-10-31 02:08:39 +01:00
|
|
|
import FeaturePipeline from "../../Logic/FeatureSource/FeaturePipeline"
|
|
|
|
import BaseUIElement from "../BaseUIElement"
|
|
|
|
import LocationInput from "../Input/LocationInput"
|
2022-11-08 14:37:48 +01:00
|
|
|
import { BBox } from "../../Logic/BBox"
|
|
|
|
import { TagUtils } from "../../Logic/Tags/TagUtils"
|
|
|
|
import { SubtleButton } from "../Base/SubtleButton"
|
2021-10-31 02:08:39 +01:00
|
|
|
import Combine from "../Base/Combine"
|
|
|
|
import Translations from "../i18n/Translations"
|
|
|
|
import Svg from "../../Svg"
|
|
|
|
import Toggle from "../Input/Toggle"
|
2022-11-08 14:37:48 +01:00
|
|
|
import SimpleAddUI, { PresetInfo } from "../BigComponents/SimpleAddUI"
|
2022-02-09 03:38:40 +01:00
|
|
|
import Img from "../Base/Img"
|
|
|
|
import Title from "../Base/Title"
|
2022-11-08 14:37:48 +01:00
|
|
|
import { VariableUiElement } from "../Base/VariableUIElement"
|
|
|
|
import { Tag } from "../../Logic/Tags/Tag"
|
|
|
|
import { WayId } from "../../Models/OsmFeature"
|
2022-11-14 01:29:22 +01:00
|
|
|
import { Translation } from "../i18n/Translation"
|
2023-03-24 19:21:15 +01:00
|
|
|
import { Feature } from "geojson"
|
|
|
|
import { AvailableRasterLayers, RasterLayerPolygon } from "../../Models/RasterLayers"
|
|
|
|
import { GlobalFilter } from "../../Logic/State/GlobalFilter"
|
2021-10-31 02:08:39 +01:00
|
|
|
|
|
|
|
export default class ConfirmLocationOfPoint extends Combine {
|
|
|
|
constructor(
|
|
|
|
state: {
|
2022-07-25 16:55:44 +02:00
|
|
|
globalFilters: UIEventSource<GlobalFilter[]>
|
2022-01-21 01:57:16 +01:00
|
|
|
featureSwitchIsTesting: UIEventSource<boolean>
|
2021-10-31 02:08:39 +01:00
|
|
|
osmConnection: OsmConnection
|
2021-11-30 18:10:46 +01:00
|
|
|
featurePipeline: FeaturePipeline
|
2023-03-23 01:42:47 +01:00
|
|
|
backgroundLayer?: UIEventSource<RasterLayerPolygon | undefined>
|
2021-10-31 02:08:39 +01:00
|
|
|
},
|
|
|
|
filterViewIsOpened: UIEventSource<boolean>,
|
|
|
|
preset: PresetInfo,
|
|
|
|
confirmText: BaseUIElement,
|
|
|
|
loc: { lon: number; lat: number },
|
|
|
|
confirm: (
|
|
|
|
tags: any[],
|
|
|
|
location: { lat: number; lon: number },
|
2022-09-21 02:21:20 +02:00
|
|
|
snapOntoWayId: WayId | undefined
|
2021-10-31 02:08:39 +01:00
|
|
|
) => void,
|
|
|
|
cancel: () => void,
|
2022-11-14 01:22:38 +01:00
|
|
|
closePopup: () => void,
|
|
|
|
options?: {
|
2022-11-14 01:29:22 +01:00
|
|
|
cancelIcon: BaseUIElement
|
2022-11-14 01:22:38 +01:00
|
|
|
cancelText?: string | Translation
|
|
|
|
}
|
2021-10-31 02:08:39 +01:00
|
|
|
) {
|
|
|
|
let preciseInput: LocationInput = undefined
|
|
|
|
if (preset.preciseInput !== undefined) {
|
2022-02-09 03:38:40 +01:00
|
|
|
// Create location input
|
2022-07-25 16:55:44 +02:00
|
|
|
|
2021-10-31 02:08:39 +01:00
|
|
|
// We uncouple the event source
|
2022-11-08 14:37:48 +01:00
|
|
|
const zloc = { ...loc, zoom: 19 }
|
2021-10-31 02:08:39 +01:00
|
|
|
const locationSrc = new UIEventSource(zloc)
|
|
|
|
|
2021-11-30 18:10:46 +01:00
|
|
|
let backgroundLayer = new UIEventSource(
|
2023-03-23 01:42:47 +01:00
|
|
|
state?.backgroundLayer?.data ?? AvailableRasterLayers.osmCarto
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-10-31 02:08:39 +01:00
|
|
|
if (preset.preciseInput.preferredBackground) {
|
2023-03-23 01:42:47 +01:00
|
|
|
const defaultBackground = AvailableRasterLayers.SelectBestLayerAccordingTo(
|
2021-11-30 18:10:46 +01:00
|
|
|
locationSrc,
|
|
|
|
new UIEventSource<string | string[]>(preset.preciseInput.preferredBackground)
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-11-30 18:10:46 +01:00
|
|
|
// Note that we _break the link_ here, as the minimap will take care of the switching!
|
|
|
|
backgroundLayer.setData(defaultBackground.data)
|
2021-10-31 02:08:39 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 01:42:47 +01:00
|
|
|
let snapToFeatures: UIEventSource<Feature[]> = undefined
|
2021-10-31 02:08:39 +01:00
|
|
|
let mapBounds: UIEventSource<BBox> = undefined
|
|
|
|
if (preset.preciseInput.snapToLayers && preset.preciseInput.snapToLayers.length > 0) {
|
2023-03-24 19:21:15 +01:00
|
|
|
snapToFeatures = new UIEventSource<Feature[]>([])
|
2021-10-31 02:08:39 +01:00
|
|
|
mapBounds = new UIEventSource<BBox>(undefined)
|
|
|
|
}
|
|
|
|
|
|
|
|
const tags = TagUtils.KVtoProperties(preset.tags ?? [])
|
|
|
|
preciseInput = new LocationInput({
|
|
|
|
mapBackground: backgroundLayer,
|
|
|
|
centerLocation: locationSrc,
|
|
|
|
snapTo: snapToFeatures,
|
2023-01-16 11:58:24 +01:00
|
|
|
renderLayerForSnappedPoint: preset.layerToAddTo.layerDef,
|
2021-10-31 02:08:39 +01:00
|
|
|
snappedPointTags: tags,
|
|
|
|
maxSnapDistance: preset.preciseInput.maxSnapDistance,
|
|
|
|
bounds: mapBounds,
|
2022-10-27 01:50:41 +02:00
|
|
|
state: <any>state,
|
2021-10-31 02:08:39 +01:00
|
|
|
})
|
2022-01-25 21:55:51 +01:00
|
|
|
preciseInput.installBounds(preset.boundsFactor ?? 0.25, true)
|
2022-06-11 14:55:25 +02:00
|
|
|
preciseInput
|
|
|
|
.SetClass("rounded-xl overflow-hidden border border-gray")
|
|
|
|
.SetStyle("height: 18rem; max-height: 50vh")
|
2021-10-31 02:08:39 +01:00
|
|
|
|
|
|
|
if (preset.preciseInput.snapToLayers && preset.preciseInput.snapToLayers.length > 0) {
|
|
|
|
// We have to snap to certain layers.
|
|
|
|
// Lets fetch them
|
|
|
|
|
|
|
|
let loadedBbox: BBox = undefined
|
|
|
|
mapBounds?.addCallbackAndRunD((bbox) => {
|
|
|
|
if (loadedBbox !== undefined && bbox.isContainedIn(loadedBbox)) {
|
|
|
|
// All is already there
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:12:51 +01:00
|
|
|
bbox = bbox.pad(
|
|
|
|
Math.max(preset.boundsFactor ?? 0.25, 2),
|
|
|
|
Math.max(preset.boundsFactor ?? 0.25, 2)
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-10-31 02:08:39 +01:00
|
|
|
loadedBbox = bbox
|
2023-03-23 01:42:47 +01:00
|
|
|
const allFeatures: Feature[] = []
|
2021-10-31 02:08:39 +01:00
|
|
|
preset.preciseInput.snapToLayers.forEach((layerId) => {
|
|
|
|
console.log("Snapping to", layerId)
|
|
|
|
state.featurePipeline
|
|
|
|
.GetFeaturesWithin(layerId, bbox)
|
2023-03-24 19:21:15 +01:00
|
|
|
?.forEach((feats) => allFeatures.push(...(<any[]>feats)))
|
2021-10-31 02:08:39 +01:00
|
|
|
})
|
|
|
|
console.log("Snapping to", allFeatures)
|
|
|
|
snapToFeatures.setData(allFeatures)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let confirmButton: BaseUIElement = new SubtleButton(
|
|
|
|
preset.icon(),
|
2022-11-08 14:37:48 +01:00
|
|
|
new Combine([confirmText]).SetClass("flex flex-col")
|
2021-10-31 02:08:39 +01:00
|
|
|
)
|
|
|
|
.SetClass("font-bold break-words")
|
|
|
|
.onClick(() => {
|
2021-12-05 05:21:03 +01:00
|
|
|
console.log(
|
|
|
|
"The confirmLocationPanel - precise input yielded ",
|
|
|
|
preciseInput?.GetValue()?.data
|
2022-07-25 16:55:44 +02:00
|
|
|
)
|
|
|
|
const globalFilterTagsToAdd: Tag[][] = state.globalFilters.data
|
|
|
|
.filter((gf) => gf.onNewPoint !== undefined)
|
|
|
|
.map((gf) => gf.onNewPoint.tags)
|
|
|
|
const globalTags: Tag[] = [].concat(...globalFilterTagsToAdd)
|
|
|
|
console.log("Global tags to add are: ", globalTags)
|
|
|
|
confirm(
|
|
|
|
[...preset.tags, ...globalTags],
|
|
|
|
preciseInput?.GetValue()?.data ?? loc,
|
|
|
|
preciseInput?.snappedOnto?.data?.properties?.id
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-10-31 02:08:39 +01:00
|
|
|
})
|
|
|
|
|
2022-11-08 14:37:48 +01:00
|
|
|
const warn = Translations.t.general.add.warnVisibleForEveryone
|
|
|
|
.Clone()
|
|
|
|
.SetClass("alert w-full block")
|
2021-10-31 02:08:39 +01:00
|
|
|
if (preciseInput !== undefined) {
|
2022-11-07 23:12:31 +01:00
|
|
|
confirmButton = new Combine([preciseInput, warn, confirmButton])
|
|
|
|
} else {
|
|
|
|
confirmButton = new Combine([warn, confirmButton])
|
2021-10-31 02:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const openLayerControl = new SubtleButton(
|
|
|
|
Svg.layers_ui(),
|
|
|
|
new Combine([
|
|
|
|
Translations.t.general.add.layerNotEnabled
|
2022-11-08 14:37:48 +01:00
|
|
|
.Subs({ layer: preset.layerToAddTo.layerDef.name })
|
2021-10-31 02:08:39 +01:00
|
|
|
.SetClass("alert"),
|
|
|
|
Translations.t.general.add.openLayerControl,
|
|
|
|
])
|
|
|
|
).onClick(() => filterViewIsOpened.setData(true))
|
|
|
|
|
2022-07-25 16:55:44 +02:00
|
|
|
let openLayerOrConfirm = new Toggle(
|
2021-10-31 02:08:39 +01:00
|
|
|
confirmButton,
|
|
|
|
openLayerControl,
|
|
|
|
preset.layerToAddTo.isDisplayed
|
|
|
|
)
|
|
|
|
|
|
|
|
const disableFilter = new SubtleButton(
|
|
|
|
new Combine([
|
|
|
|
Svg.filter_ui().SetClass("absolute w-full"),
|
|
|
|
Svg.cross_bottom_right_svg().SetClass("absolute red-svg"),
|
|
|
|
]).SetClass("relative"),
|
|
|
|
new Combine([
|
|
|
|
Translations.t.general.add.disableFiltersExplanation.Clone(),
|
|
|
|
Translations.t.general.add.disableFilters.Clone().SetClass("text-xl"),
|
|
|
|
]).SetClass("flex flex-col")
|
|
|
|
).onClick(() => {
|
2022-01-08 04:22:50 +01:00
|
|
|
const appliedFilters = preset.layerToAddTo.appliedFilters
|
|
|
|
appliedFilters.data.forEach((_, k) => appliedFilters.data.set(k, undefined))
|
|
|
|
appliedFilters.ping()
|
2021-10-31 02:08:39 +01:00
|
|
|
cancel()
|
2022-01-08 04:22:50 +01:00
|
|
|
closePopup()
|
2021-10-31 02:08:39 +01:00
|
|
|
})
|
|
|
|
|
2022-07-25 16:55:44 +02:00
|
|
|
// We assume the number of global filters won't change during the run of the program
|
|
|
|
for (let i = 0; i < state.globalFilters.data.length; i++) {
|
|
|
|
const hasBeenCheckedOf = new UIEventSource(false)
|
|
|
|
|
|
|
|
const filterConfirmPanel = new VariableUiElement(
|
|
|
|
state.globalFilters.map((gfs) => {
|
|
|
|
const gf = gfs[i]
|
2022-11-08 14:37:48 +01:00
|
|
|
const confirm = gf.onNewPoint?.confirmAddNew?.Subs({ preset: preset.title })
|
2022-07-25 16:55:44 +02:00
|
|
|
return new Combine([
|
|
|
|
gf.onNewPoint?.safetyCheck,
|
|
|
|
new SubtleButton(Svg.confirm_svg(), confirm).onClick(() =>
|
|
|
|
hasBeenCheckedOf.setData(true)
|
2022-09-08 21:40:48 +02:00
|
|
|
),
|
2022-07-25 16:55:44 +02:00
|
|
|
])
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
openLayerOrConfirm = new Toggle(
|
|
|
|
openLayerOrConfirm,
|
|
|
|
filterConfirmPanel,
|
|
|
|
state.globalFilters.map(
|
|
|
|
(f) => hasBeenCheckedOf.data || f[i]?.onNewPoint === undefined,
|
|
|
|
[hasBeenCheckedOf]
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-07-25 16:55:44 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-08 04:22:50 +01:00
|
|
|
const hasActiveFilter = preset.layerToAddTo.appliedFilters.map((appliedFilters) => {
|
|
|
|
const activeFilters = Array.from(appliedFilters.values()).filter(
|
|
|
|
(f) => f?.currentFilter !== undefined
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-01-26 21:40:38 +01:00
|
|
|
return activeFilters.length === 0
|
2022-01-08 04:22:50 +01:00
|
|
|
})
|
2022-01-26 21:40:38 +01:00
|
|
|
|
2022-01-08 04:22:50 +01:00
|
|
|
// If at least one filter is active which _might_ hide a newly added item, this blocks the preset and requests the filter to be disabled
|
2021-10-31 02:08:39 +01:00
|
|
|
const disableFiltersOrConfirm = new Toggle(
|
|
|
|
openLayerOrConfirm,
|
2022-01-26 21:40:38 +01:00
|
|
|
disableFilter,
|
2022-01-08 04:22:50 +01:00
|
|
|
hasActiveFilter
|
|
|
|
)
|
2021-10-31 02:08:39 +01:00
|
|
|
|
|
|
|
const tagInfo = SimpleAddUI.CreateTagInfoFor(preset, state.osmConnection)
|
|
|
|
|
|
|
|
const cancelButton = new SubtleButton(
|
2022-11-14 01:22:38 +01:00
|
|
|
options?.cancelIcon ?? Svg.close_ui(),
|
|
|
|
options?.cancelText ?? Translations.t.general.cancel
|
2021-10-31 02:08:39 +01:00
|
|
|
).onClick(cancel)
|
|
|
|
|
2022-07-25 16:55:44 +02:00
|
|
|
let examples: BaseUIElement = undefined
|
|
|
|
if (preset.exampleImages !== undefined && preset.exampleImages.length > 0) {
|
2022-02-09 03:38:40 +01:00
|
|
|
examples = new Combine([
|
2022-07-25 16:55:44 +02:00
|
|
|
new Title(
|
|
|
|
preset.exampleImages.length == 1
|
|
|
|
? Translations.t.general.example
|
|
|
|
: Translations.t.general.examples
|
|
|
|
),
|
2022-02-09 22:37:21 +01:00
|
|
|
new Combine(
|
|
|
|
preset.exampleImages.map((img) =>
|
|
|
|
new Img(img).SetClass("h-64 m-1 w-auto rounded-lg")
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-02-09 22:37:21 +01:00
|
|
|
).SetClass("flex flex-wrap items-stretch"),
|
2022-02-09 03:38:40 +01:00
|
|
|
])
|
|
|
|
}
|
2022-07-25 16:55:44 +02:00
|
|
|
|
2021-10-31 02:08:39 +01:00
|
|
|
super([
|
2022-01-21 01:57:16 +01:00
|
|
|
new Toggle(
|
|
|
|
Translations.t.general.testing.SetClass("alert"),
|
|
|
|
undefined,
|
|
|
|
state.featureSwitchIsTesting
|
|
|
|
),
|
2021-10-31 02:08:39 +01:00
|
|
|
disableFiltersOrConfirm,
|
|
|
|
cancelButton,
|
|
|
|
preset.description,
|
2022-02-09 03:38:40 +01:00
|
|
|
examples,
|
2021-10-31 02:08:39 +01:00
|
|
|
tagInfo,
|
|
|
|
])
|
|
|
|
|
|
|
|
this.SetClass("flex flex-col")
|
|
|
|
}
|
|
|
|
}
|