2021-12-12 02:59:24 +01:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
2023-06-01 02:52:21 +02:00
|
|
|
import {Stores, UIEventSource} from "../../Logic/UIEventSource"
|
|
|
|
import {SubtleButton} from "../Base/SubtleButton"
|
2021-12-12 02:59:24 +01:00
|
|
|
import Img from "../Base/Img"
|
2023-06-01 02:52:21 +02:00
|
|
|
import {FixedUiElement} from "../Base/FixedUiElement"
|
2021-12-12 02:59:24 +01:00
|
|
|
import Combine from "../Base/Combine"
|
|
|
|
import Link from "../Base/Link"
|
2023-06-01 02:52:21 +02:00
|
|
|
import {Utils} from "../../Utils"
|
2021-12-12 02:59:24 +01:00
|
|
|
import StaticFeatureSource from "../../Logic/FeatureSource/Sources/StaticFeatureSource"
|
2023-06-01 02:52:21 +02:00
|
|
|
import {VariableUiElement} from "../Base/VariableUIElement"
|
2021-12-12 02:59:24 +01:00
|
|
|
import Loading from "../Base/Loading"
|
2023-06-01 02:52:21 +02:00
|
|
|
import {OsmConnection} from "../../Logic/Osm/OsmConnection"
|
2021-12-12 02:59:24 +01:00
|
|
|
import Translations from "../i18n/Translations"
|
2021-12-13 02:05:34 +01:00
|
|
|
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
|
2023-06-01 02:52:21 +02:00
|
|
|
import {Changes} from "../../Logic/Osm/Changes"
|
|
|
|
import {UIElement} from "../UIElement"
|
2022-02-10 23:16:14 +01:00
|
|
|
import FilteredLayer from "../../Models/FilteredLayer"
|
|
|
|
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig"
|
|
|
|
import Lazy from "../Base/Lazy"
|
2022-06-22 20:30:48 +02:00
|
|
|
import List from "../Base/List"
|
2023-06-01 02:52:21 +02:00
|
|
|
import {SpecialVisualization, SpecialVisualizationState} from "../SpecialVisualization"
|
|
|
|
import {IndexedFeatureSource} from "../../Logic/FeatureSource/FeatureSource"
|
|
|
|
import {MapLibreAdaptor} from "../Map/MapLibreAdaptor"
|
2023-03-28 05:13:48 +02:00
|
|
|
import ShowDataLayer from "../Map/ShowDataLayer"
|
|
|
|
import SvelteUIElement from "../Base/SvelteUIElement"
|
|
|
|
import MaplibreMap from "../Map/MaplibreMap.svelte"
|
2023-04-20 18:58:31 +02:00
|
|
|
import SpecialVisualizations from "../SpecialVisualizations"
|
2023-05-30 02:52:22 +02:00
|
|
|
import {Feature} from "geojson";
|
2021-12-12 02:59:24 +01:00
|
|
|
|
|
|
|
export interface AutoAction extends SpecialVisualization {
|
|
|
|
supportsAutoAction: boolean
|
|
|
|
|
2021-12-13 02:05:34 +01:00
|
|
|
applyActionOn(
|
2023-05-30 02:52:22 +02:00
|
|
|
feature: Feature,
|
2021-12-13 02:05:34 +01:00
|
|
|
state: {
|
2023-03-28 05:13:48 +02:00
|
|
|
layout: LayoutConfig
|
2021-12-13 02:05:34 +01:00
|
|
|
changes: Changes
|
2023-03-28 05:13:48 +02:00
|
|
|
indexedFeatures: IndexedFeatureSource
|
2021-12-13 02:05:34 +01:00
|
|
|
},
|
|
|
|
tagSource: UIEventSource<any>,
|
|
|
|
argument: string[]
|
|
|
|
): Promise<void>
|
2021-12-12 02:59:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:16:14 +01:00
|
|
|
class ApplyButton extends UIElement {
|
|
|
|
private readonly icon: string
|
|
|
|
private readonly text: string
|
|
|
|
private readonly targetTagRendering: string
|
|
|
|
private readonly target_layer_id: string
|
2023-03-28 05:13:48 +02:00
|
|
|
private readonly state: SpecialVisualizationState
|
2022-02-10 23:16:14 +01:00
|
|
|
private readonly target_feature_ids: string[]
|
|
|
|
private readonly buttonState = new UIEventSource<
|
|
|
|
"idle" | "running" | "done" | { error: string }
|
|
|
|
>("idle")
|
|
|
|
private readonly layer: FilteredLayer
|
|
|
|
private readonly tagRenderingConfig: TagRenderingConfig
|
2023-06-01 02:52:21 +02:00
|
|
|
private readonly appliedNumberOfFeatures = new UIEventSource<number>(0)
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-02-10 23:16:14 +01:00
|
|
|
constructor(
|
2023-03-28 05:13:48 +02:00
|
|
|
state: SpecialVisualizationState,
|
2022-02-10 23:16:14 +01:00
|
|
|
target_feature_ids: string[],
|
|
|
|
options: {
|
|
|
|
target_layer_id: string
|
|
|
|
targetTagRendering: string
|
|
|
|
text: string
|
|
|
|
icon: string
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
super()
|
|
|
|
this.state = state
|
|
|
|
this.target_feature_ids = target_feature_ids
|
|
|
|
this.target_layer_id = options.target_layer_id
|
|
|
|
this.targetTagRendering = options.targetTagRendering
|
|
|
|
this.text = options.text
|
|
|
|
this.icon = options.icon
|
2023-03-28 05:13:48 +02:00
|
|
|
this.layer = this.state.layerState.filteredLayers.get(this.target_layer_id)
|
2022-02-11 03:57:39 +01:00
|
|
|
this.tagRenderingConfig = this.layer.layerDef.tagRenderings.find(
|
|
|
|
(tr) => tr.id === this.targetTagRendering
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-02-10 23:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected InnerRender(): string | BaseUIElement {
|
|
|
|
if (this.target_feature_ids.length === 0) {
|
|
|
|
return new FixedUiElement("No elements found to perform action")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.tagRenderingConfig === undefined) {
|
|
|
|
return new FixedUiElement(
|
|
|
|
"Target tagrendering " + this.targetTagRendering + " not found"
|
|
|
|
).SetClass("alert")
|
|
|
|
}
|
|
|
|
const self = this
|
|
|
|
const button = new SubtleButton(new Img(this.icon), this.text).onClick(() => {
|
2022-02-11 03:57:39 +01:00
|
|
|
this.buttonState.setData("running")
|
|
|
|
window.setTimeout(() => {
|
|
|
|
self.Run()
|
|
|
|
}, 50)
|
|
|
|
})
|
2022-02-10 23:16:14 +01:00
|
|
|
|
|
|
|
const explanation = new Combine([
|
|
|
|
"The following objects will be updated: ",
|
|
|
|
...this.target_feature_ids.map(
|
|
|
|
(id) => new Combine([new Link(id, "https:/ /openstreetmap.org/" + id, true), ", "])
|
2022-09-08 21:40:48 +02:00
|
|
|
),
|
2022-02-10 23:16:14 +01:00
|
|
|
]).SetClass("subtle")
|
|
|
|
|
2023-03-28 05:13:48 +02:00
|
|
|
const mlmap = new UIEventSource(undefined)
|
|
|
|
const mla = new MapLibreAdaptor(mlmap, {
|
|
|
|
rasterLayer: this.state.mapProperties.rasterLayer,
|
|
|
|
})
|
|
|
|
mla.allowZooming.setData(false)
|
|
|
|
mla.allowMoving.setData(false)
|
|
|
|
|
2023-06-01 02:52:21 +02:00
|
|
|
const previewMap = new SvelteUIElement(MaplibreMap, {map: mlmap}).SetClass("h-48")
|
2022-02-10 23:16:14 +01:00
|
|
|
|
|
|
|
const features = this.target_feature_ids.map((id) =>
|
2023-03-28 05:13:48 +02:00
|
|
|
this.state.indexedFeatures.featuresById.data.get(id)
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-02-10 23:16:14 +01:00
|
|
|
|
2023-03-28 05:13:48 +02:00
|
|
|
new ShowDataLayer(mlmap, {
|
2022-06-05 02:24:14 +02:00
|
|
|
features: StaticFeatureSource.fromGeojson(features),
|
2023-03-28 05:13:48 +02:00
|
|
|
zoomToFeatures: true,
|
|
|
|
layer: this.layer.layerDef,
|
2022-02-10 23:16:14 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return new VariableUiElement(
|
|
|
|
this.buttonState.map((st) => {
|
|
|
|
if (st === "idle") {
|
|
|
|
return new Combine([button, previewMap, explanation])
|
|
|
|
}
|
|
|
|
if (st === "done") {
|
|
|
|
return new FixedUiElement("All done!").SetClass("thanks")
|
|
|
|
}
|
|
|
|
if (st === "running") {
|
2023-06-01 02:52:21 +02:00
|
|
|
return new Loading(new VariableUiElement(this.appliedNumberOfFeatures.map(appliedTo => {
|
|
|
|
return "Applying changes, currently at " + appliedTo + "/" + this.target_feature_ids.length
|
|
|
|
})))
|
2022-02-10 23:16:14 +01:00
|
|
|
}
|
|
|
|
const error = st.error
|
|
|
|
return new Combine([
|
|
|
|
new FixedUiElement("Something went wrong...").SetClass("alert"),
|
|
|
|
new FixedUiElement(error).SetClass("subtle"),
|
|
|
|
]).SetClass("flex flex-col")
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-01 02:52:21 +02:00
|
|
|
/**
|
|
|
|
* Actually applies all the changes...
|
|
|
|
*/
|
2022-02-11 03:57:39 +01:00
|
|
|
private async Run() {
|
|
|
|
try {
|
|
|
|
console.log("Applying auto-action on " + this.target_feature_ids.length + " features")
|
|
|
|
|
2023-06-01 02:52:21 +02:00
|
|
|
for (let i = 0; i < this.target_feature_ids.length; i++) {
|
|
|
|
const targetFeatureId = this.target_feature_ids[i];
|
|
|
|
const feature = this.state.indexedFeatures.featuresById.data.get(targetFeatureId)
|
2023-03-28 05:13:48 +02:00
|
|
|
const featureTags = this.state.featureProperties.getStore(targetFeatureId)
|
2022-02-11 03:57:39 +01:00
|
|
|
const rendering = this.tagRenderingConfig.GetRenderValue(featureTags.data).txt
|
|
|
|
const specialRenderings = Utils.NoNull(
|
2023-04-20 18:58:31 +02:00
|
|
|
SpecialVisualizations.constructSpecification(rendering)
|
|
|
|
).filter((v) => typeof v !== "string" && v.func["supportsAutoAction"] === true)
|
2022-02-11 03:57:39 +01:00
|
|
|
|
|
|
|
if (specialRenderings.length == 0) {
|
|
|
|
console.warn(
|
|
|
|
"AutoApply: feature " +
|
2023-06-01 02:52:21 +02:00
|
|
|
targetFeatureId +
|
|
|
|
" got a rendering without supported auto actions:",
|
2022-02-11 03:57:39 +01:00
|
|
|
rendering
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const specialRendering of specialRenderings) {
|
2023-04-20 18:58:31 +02:00
|
|
|
if (typeof specialRendering === "string") {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-11 03:57:39 +01:00
|
|
|
const action = <AutoAction>specialRendering.func
|
2023-06-01 02:52:21 +02:00
|
|
|
await action.applyActionOn(feature, this.state, featureTags, specialRendering.args)
|
2022-02-11 03:57:39 +01:00
|
|
|
}
|
2023-06-01 02:52:21 +02:00
|
|
|
if( i % 50 === 0){
|
|
|
|
await this.state.changes.flushChanges("Auto button: intermediate save")
|
|
|
|
}
|
|
|
|
this.appliedNumberOfFeatures.setData(i + 1)
|
2022-02-11 03:57:39 +01:00
|
|
|
}
|
|
|
|
console.log("Flushing changes...")
|
2023-06-01 02:52:21 +02:00
|
|
|
await this.state.changes.flushChanges("Auto button: done")
|
2022-02-11 03:57:39 +01:00
|
|
|
this.buttonState.setData("done")
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error while running autoApply: ", e)
|
2023-06-01 02:52:21 +02:00
|
|
|
this.buttonState.setData({error: e})
|
2022-02-11 03:57:39 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 23:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-12-12 02:59:24 +01:00
|
|
|
export default class AutoApplyButton implements SpecialVisualization {
|
2022-06-22 20:30:48 +02:00
|
|
|
public readonly docs: BaseUIElement
|
2021-12-12 02:59:24 +01:00
|
|
|
public readonly funcName: string = "auto_apply"
|
2022-03-29 00:20:10 +02:00
|
|
|
public readonly args: {
|
|
|
|
name: string
|
|
|
|
defaultValue?: string
|
|
|
|
doc: string
|
|
|
|
required?: boolean
|
|
|
|
}[] = [
|
2021-12-12 02:59:24 +01:00
|
|
|
{
|
|
|
|
name: "target_layer",
|
2022-03-29 00:20:10 +02:00
|
|
|
doc: "The layer that the target features will reside in",
|
|
|
|
required: true,
|
2021-12-12 02:59:24 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "target_feature_ids",
|
2022-03-29 00:20:10 +02:00
|
|
|
doc: "The key, of which the value contains a list of ids",
|
|
|
|
required: true,
|
2021-12-12 02:59:24 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tag_rendering_id",
|
2022-03-29 00:20:10 +02:00
|
|
|
doc: "The ID of the tagRendering containing the autoAction. This tagrendering will be calculated. The embedded actions will be executed",
|
|
|
|
required: true,
|
2021-12-12 02:59:24 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "text",
|
2022-03-29 00:20:10 +02:00
|
|
|
doc: "The text to show on the button",
|
|
|
|
required: true,
|
2021-12-12 02:59:24 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "icon",
|
|
|
|
doc: "The icon to show on the button",
|
|
|
|
defaultValue: "./assets/svg/robot.svg",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
constructor(allSpecialVisualisations: SpecialVisualization[]) {
|
|
|
|
this.docs = AutoApplyButton.generateDocs(
|
|
|
|
allSpecialVisualisations
|
|
|
|
.filter((sv) => sv["supportsAutoAction"] === true)
|
|
|
|
.map((sv) => sv.funcName)
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-12-12 02:59:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 21:40:38 +01:00
|
|
|
private static generateDocs(supportedActions: string[]) {
|
2022-06-22 20:30:48 +02:00
|
|
|
return new Combine([
|
|
|
|
"A button to run many actions for many features at once.",
|
|
|
|
"To effectively use this button, you'll need some ingredients:",
|
|
|
|
new List([
|
|
|
|
"A target layer with features for which an action is defined in a tag rendering. The following special visualisations support an autoAction: " +
|
2023-06-01 02:52:21 +02:00
|
|
|
supportedActions.join(", "),
|
2022-06-22 20:30:48 +02:00
|
|
|
"A host feature to place the auto-action on. This can be a big outline (such as a city). Another good option for this is the layer ",
|
|
|
|
new Link("current_view", "./BuiltinLayers.md#current_view"),
|
|
|
|
"Then, use a calculated tag on the host feature to determine the overlapping object ids",
|
|
|
|
"At last, add this component",
|
|
|
|
]),
|
|
|
|
])
|
2022-01-26 21:40:38 +01:00
|
|
|
}
|
|
|
|
|
2021-12-12 02:59:24 +01:00
|
|
|
constr(
|
2023-03-28 05:13:48 +02:00
|
|
|
state: SpecialVisualizationState,
|
|
|
|
tagSource: UIEventSource<Record<string, string>>,
|
|
|
|
argument: string[]
|
2021-12-12 02:59:24 +01:00
|
|
|
): BaseUIElement {
|
|
|
|
try {
|
2022-02-10 23:16:14 +01:00
|
|
|
if (
|
2023-03-28 05:13:48 +02:00
|
|
|
!state.layout.official &&
|
2022-02-10 23:16:14 +01:00
|
|
|
!(
|
|
|
|
state.featureSwitchIsTesting.data ||
|
|
|
|
state.osmConnection._oauth_config.url ===
|
2023-06-01 02:52:21 +02:00
|
|
|
OsmConnection.oauth_configs["osm-test"].url
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-02-10 23:16:14 +01:00
|
|
|
) {
|
|
|
|
const t = Translations.t.general.add.import
|
|
|
|
return new Combine([
|
|
|
|
new FixedUiElement(
|
|
|
|
"The auto-apply button is only available in official themes (or in testing mode)"
|
|
|
|
).SetClass("alert"),
|
|
|
|
t.howToTest,
|
|
|
|
])
|
2021-12-12 02:59:24 +01:00
|
|
|
}
|
2022-01-26 21:40:38 +01:00
|
|
|
|
2022-02-10 23:16:14 +01:00
|
|
|
const target_layer_id = argument[0]
|
2021-12-12 02:59:24 +01:00
|
|
|
const targetTagRendering = argument[2]
|
|
|
|
const text = argument[3]
|
|
|
|
const icon = argument[4]
|
2022-02-10 23:16:14 +01:00
|
|
|
const options = {
|
|
|
|
target_layer_id,
|
|
|
|
targetTagRendering,
|
|
|
|
text,
|
|
|
|
icon,
|
2021-12-12 02:59:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:16:14 +01:00
|
|
|
return new Lazy(() => {
|
2023-06-01 02:52:21 +02:00
|
|
|
const to_parse = new UIEventSource<string[]>(undefined)
|
2022-02-10 23:16:14 +01:00
|
|
|
// Very ugly hack: read the value every 500ms
|
2022-06-05 02:24:14 +02:00
|
|
|
Stores.Chronic(500, () => to_parse.data === undefined).addCallback(() => {
|
2023-06-01 02:52:21 +02:00
|
|
|
let applicable = <string | string[]> tagSource.data[argument[1]]
|
|
|
|
if(typeof applicable === "string"){
|
|
|
|
applicable = JSON.parse(applicable)
|
|
|
|
}
|
|
|
|
to_parse.setData(<string[]> applicable)
|
2022-02-10 23:16:14 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const loading = new Loading("Gathering which elements support auto-apply... ")
|
|
|
|
return new VariableUiElement(
|
2023-06-01 02:52:21 +02:00
|
|
|
Stores.ListStabilized(to_parse).map((ids) => {
|
2022-02-11 03:57:39 +01:00
|
|
|
if (ids === undefined) {
|
2022-02-10 23:16:14 +01:00
|
|
|
return loading
|
2021-12-12 02:59:24 +01:00
|
|
|
}
|
|
|
|
|
2023-06-01 02:52:21 +02:00
|
|
|
if (typeof ids === "string") {
|
|
|
|
ids = JSON.parse(ids)
|
|
|
|
}
|
|
|
|
return new ApplyButton(state, ids, options)
|
2022-02-10 23:16:14 +01:00
|
|
|
})
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-02-10 23:16:14 +01:00
|
|
|
})
|
2021-12-12 02:59:24 +01:00
|
|
|
} catch (e) {
|
|
|
|
return new FixedUiElement(
|
|
|
|
"Could not generate a auto_apply-button for key " + argument[0] + " due to " + e
|
|
|
|
).SetClass("alert")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLayerDependencies(args: string[]): string[] {
|
|
|
|
return [args[0]]
|
|
|
|
}
|
|
|
|
}
|