2023-10-09 01:26:12 +02:00
|
|
|
import LayoutConfig from "../../../Models/ThemeConfig/LayoutConfig";
|
|
|
|
import { WritableFeatureSource } from "../FeatureSource";
|
|
|
|
import { ImmutableStore, Store, UIEventSource } from "../../UIEventSource";
|
|
|
|
import { Feature, Point } from "geojson";
|
|
|
|
import { TagUtils } from "../../Tags/TagUtils";
|
|
|
|
import BaseUIElement from "../../../UI/BaseUIElement";
|
|
|
|
import { Utils } from "../../../Utils";
|
|
|
|
import { OsmTags } from "../../../Models/OsmFeature";
|
2023-04-02 02:59:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Highly specialized feature source.
|
|
|
|
* Based on a lon/lat UIEVentSource, will generate the corresponding feature with the correct properties
|
|
|
|
*/
|
2023-04-06 01:33:08 +02:00
|
|
|
export class LastClickFeatureSource implements WritableFeatureSource {
|
|
|
|
public readonly features: UIEventSource<Feature[]> = new UIEventSource<Feature[]>([])
|
2023-10-09 01:26:12 +02:00
|
|
|
private i: number = 0
|
|
|
|
private readonly hasNoteLayer: string
|
|
|
|
private readonly renderings: string[];
|
|
|
|
private readonly hasPresets: string;
|
2023-04-02 02:59:20 +02:00
|
|
|
|
|
|
|
constructor(location: Store<{ lon: number; lat: number }>, layout: LayoutConfig) {
|
2023-10-09 01:26:12 +02:00
|
|
|
this.hasNoteLayer = layout.layers.some((l) => l.id === "note") ? "yes" : "no"
|
|
|
|
this.hasPresets= layout.layers.some((l) => l.presets?.length > 0) ? "yes" : "no"
|
2023-04-02 02:59:20 +02:00
|
|
|
const allPresets: BaseUIElement[] = []
|
|
|
|
for (const layer of layout.layers)
|
|
|
|
for (let i = 0; i < (layer.presets ?? []).length; i++) {
|
|
|
|
const preset = layer.presets[i]
|
|
|
|
const tags = new ImmutableStore(TagUtils.KVtoProperties(preset.tags))
|
|
|
|
const { html } = layer.mapRendering[0].RenderIcon(tags, false, {
|
|
|
|
noSize: true,
|
|
|
|
includeBadges: false,
|
|
|
|
})
|
|
|
|
allPresets.push(html)
|
|
|
|
}
|
|
|
|
|
2023-10-09 01:26:12 +02:00
|
|
|
this.renderings = Utils.Dedup(
|
2023-06-14 20:39:36 +02:00
|
|
|
allPresets.map((uiElem) =>
|
|
|
|
Utils.runningFromConsole ? "" : uiElem.ConstructElement().innerHTML
|
|
|
|
)
|
2023-04-02 02:59:20 +02:00
|
|
|
)
|
|
|
|
|
2023-04-06 01:33:08 +02:00
|
|
|
location.addCallbackAndRunD(({ lon, lat }) => {
|
2023-10-09 01:26:12 +02:00
|
|
|
this.features.setData([this.createFeature(lon, lat)])
|
2023-04-06 01:33:08 +02:00
|
|
|
})
|
2023-04-02 02:59:20 +02:00
|
|
|
}
|
2023-10-09 01:26:12 +02:00
|
|
|
|
|
|
|
public createFeature(lon: number, lat: number): Feature<Point, OsmTags> {
|
|
|
|
const properties: OsmTags = {
|
|
|
|
lastclick: "yes",
|
|
|
|
id: "last_click_" + this.i,
|
|
|
|
has_note_layer: this.hasNoteLayer ,
|
|
|
|
has_presets:this.hasPresets ,
|
|
|
|
renderings: this.renderings.join(""),
|
|
|
|
number_of_presets: "" +this. renderings.length,
|
|
|
|
first_preset: this.renderings[0],
|
|
|
|
}
|
|
|
|
this. i++
|
|
|
|
|
|
|
|
return <Feature<Point, OsmTags>>{
|
|
|
|
type: "Feature",
|
|
|
|
properties,
|
|
|
|
geometry: {
|
|
|
|
type: "Point",
|
|
|
|
coordinates: [lon, lat],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-04-02 02:59:20 +02:00
|
|
|
}
|