Fix autoapply for GRB theme
This commit is contained in:
parent
db770f2c35
commit
30be86668e
16 changed files with 392 additions and 209 deletions
|
@ -19,6 +19,10 @@ import {OsmConnection} from "../../Logic/Osm/OsmConnection";
|
|||
import Translations from "../i18n/Translations";
|
||||
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig";
|
||||
import {Changes} from "../../Logic/Osm/Changes";
|
||||
import {UIElement} from "../UIElement";
|
||||
import FilteredLayer from "../../Models/FilteredLayer";
|
||||
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig";
|
||||
import Lazy from "../Base/Lazy";
|
||||
|
||||
export interface AutoAction extends SpecialVisualization {
|
||||
supportsAutoAction: boolean
|
||||
|
@ -29,6 +33,120 @@ export interface AutoAction extends SpecialVisualization {
|
|||
}, tagSource: UIEventSource<any>, argument: string[]): Promise<void>
|
||||
}
|
||||
|
||||
class ApplyButton extends UIElement {
|
||||
private readonly icon: string;
|
||||
private readonly text: string;
|
||||
private readonly targetTagRendering: string;
|
||||
private readonly target_layer_id: string;
|
||||
private readonly state: FeaturePipelineState;
|
||||
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;
|
||||
|
||||
constructor(state: FeaturePipelineState, 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
|
||||
this.layer = this.state.filteredLayers.data.find(l => l.layerDef.id === this.target_layer_id)
|
||||
this. tagRenderingConfig = this.layer.layerDef.tagRenderings.find(tr => tr.id === this.targetTagRendering)
|
||||
|
||||
}
|
||||
|
||||
private async Run() {
|
||||
this.buttonState.setData("running")
|
||||
try {
|
||||
console.log("Applying auto-action on " + this.target_feature_ids.length + " features")
|
||||
|
||||
for (const targetFeatureId of this.target_feature_ids) {
|
||||
const featureTags = this.state.allElements.getEventSourceById(targetFeatureId)
|
||||
const rendering = this.tagRenderingConfig.GetRenderValue(featureTags.data).txt
|
||||
const specialRenderings = Utils.NoNull(SubstitutedTranslation.ExtractSpecialComponents(rendering)
|
||||
.map(x => x.special))
|
||||
.filter(v => v.func["supportsAutoAction"] === true)
|
||||
|
||||
if(specialRenderings.length == 0){
|
||||
console.warn("AutoApply: feature "+targetFeatureId+" got a rendering without supported auto actions:", rendering)
|
||||
}
|
||||
|
||||
for (const specialRendering of specialRenderings) {
|
||||
const action = <AutoAction>specialRendering.func
|
||||
await action.applyActionOn(this.state, featureTags, specialRendering.args)
|
||||
}
|
||||
}
|
||||
console.log("Flushing changes...")
|
||||
await this.state.changes.flushChanges("Auto button")
|
||||
this.buttonState.setData("done")
|
||||
} catch (e) {
|
||||
console.error("Error while running autoApply: ", e)
|
||||
this. buttonState.setData({error: e})
|
||||
}
|
||||
}
|
||||
|
||||
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(() => self.Run());
|
||||
|
||||
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), ", "]))]).SetClass("subtle")
|
||||
|
||||
const previewMap = Minimap.createMiniMap({
|
||||
allowMoving: false,
|
||||
background: this.state.backgroundLayer,
|
||||
addLayerControl: true,
|
||||
}).SetClass("h-48")
|
||||
|
||||
const features = this.target_feature_ids.map(id => this.state.allElements.ContainingFeatures.get(id))
|
||||
|
||||
new ShowDataLayer({
|
||||
leafletMap: previewMap.leafletMap,
|
||||
popup: undefined,
|
||||
zoomToFeatures: true,
|
||||
features: new StaticFeatureSource(features, false),
|
||||
state: this.state,
|
||||
layerToShow:this. layer.layerDef,
|
||||
})
|
||||
|
||||
|
||||
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") {
|
||||
return new Loading("Applying changes...")
|
||||
}
|
||||
const error = st.error
|
||||
return new Combine([new FixedUiElement("Something went wrong...").SetClass("alert"), new FixedUiElement(error).SetClass("subtle")]).SetClass("flex flex-col")
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default class AutoApplyButton implements SpecialVisualization {
|
||||
public readonly docs: string;
|
||||
public readonly funcName: string = "auto_apply";
|
||||
|
@ -72,109 +190,44 @@ export default class AutoApplyButton implements SpecialVisualization {
|
|||
}
|
||||
|
||||
constr(state: FeaturePipelineState, tagSource: UIEventSource<any>, argument: string[], guistate: DefaultGuiState): BaseUIElement {
|
||||
|
||||
if (!state.layoutToUse.official && !(state.featureSwitchIsTesting.data || state.osmConnection._oauth_config.url === OsmConnection.oauth_configs["osm-test"].url)) {
|
||||
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])
|
||||
}
|
||||
|
||||
const to_parse = tagSource.data[argument[1]]
|
||||
if (to_parse === undefined) {
|
||||
return new Loading("Gathering which elements support auto-apply... ")
|
||||
}
|
||||
try {
|
||||
|
||||
|
||||
const target_layer_id = argument[0]
|
||||
const target_feature_ids = <string[]>JSON.parse(to_parse)
|
||||
|
||||
if (target_feature_ids.length === 0) {
|
||||
return new FixedUiElement("No elements found to perform action")
|
||||
if (!state.layoutToUse.official && !(state.featureSwitchIsTesting.data || state.osmConnection._oauth_config.url === OsmConnection.oauth_configs["osm-test"].url)) {
|
||||
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])
|
||||
}
|
||||
|
||||
const target_layer_id = argument[0]
|
||||
const targetTagRendering = argument[2]
|
||||
const text = argument[3]
|
||||
const icon = argument[4]
|
||||
|
||||
const layer = state.filteredLayers.data.filter(l => l.layerDef.id === target_layer_id)[0]
|
||||
|
||||
const tagRenderingConfig = layer.layerDef.tagRenderings.filter(tr => tr.id === targetTagRendering)[0]
|
||||
|
||||
if (tagRenderingConfig === undefined) {
|
||||
return new FixedUiElement("Target tagrendering " + targetTagRendering + " not found").SetClass("alert")
|
||||
const options = {
|
||||
target_layer_id, targetTagRendering, text, icon
|
||||
}
|
||||
|
||||
const buttonState = new UIEventSource<"idle" | "running" | "done" | { error: string }>("idle")
|
||||
return new Lazy(() => {
|
||||
const to_parse = new UIEventSource(undefined)
|
||||
// Very ugly hack: read the value every 500ms
|
||||
UIEventSource.Chronic(500, () => to_parse.data === undefined).addCallback(() => {
|
||||
const applicable = tagSource.data[argument[1]]
|
||||
console.log("Current applicable value is: ", applicable)
|
||||
to_parse.setData(applicable)
|
||||
})
|
||||
|
||||
const button = new SubtleButton(
|
||||
new Img(icon),
|
||||
text
|
||||
).onClick(async () => {
|
||||
buttonState.setData("running")
|
||||
try {
|
||||
|
||||
|
||||
for (const targetFeatureId of target_feature_ids) {
|
||||
const featureTags = state.allElements.getEventSourceById(targetFeatureId)
|
||||
const rendering = tagRenderingConfig.GetRenderValue(featureTags.data).txt
|
||||
const specialRenderings = Utils.NoNull(SubstitutedTranslation.ExtractSpecialComponents(rendering)
|
||||
.map(x => x.special))
|
||||
.filter(v => v.func["supportsAutoAction"] === true)
|
||||
|
||||
for (const specialRendering of specialRenderings) {
|
||||
const action = <AutoAction>specialRendering.func
|
||||
await action.applyActionOn(state, featureTags, specialRendering.args)
|
||||
}
|
||||
const loading = new Loading("Gathering which elements support auto-apply... ");
|
||||
return new VariableUiElement(to_parse.map(ids => {
|
||||
if(ids === undefined){
|
||||
return loading
|
||||
}
|
||||
console.log("Flushing changes...")
|
||||
await state.changes.flushChanges("Auto button")
|
||||
buttonState.setData("done")
|
||||
} catch (e) {
|
||||
console.error("Error while running autoApply: ", e)
|
||||
buttonState.setData({error: e})
|
||||
}
|
||||
});
|
||||
|
||||
const explanation = new Combine(["The following objects will be updated: ",
|
||||
...target_feature_ids.map(id => new Combine([new Link(id, "https:/ /openstreetmap.org/" + id, true), ", "]))]).SetClass("subtle")
|
||||
|
||||
const previewMap = Minimap.createMiniMap({
|
||||
allowMoving: false,
|
||||
background: state.backgroundLayer,
|
||||
addLayerControl: true,
|
||||
}).SetClass("h-48")
|
||||
|
||||
const features = target_feature_ids.map(id => state.allElements.ContainingFeatures.get(id))
|
||||
|
||||
new ShowDataLayer({
|
||||
leafletMap: previewMap.leafletMap,
|
||||
popup: undefined,
|
||||
zoomToFeatures: true,
|
||||
features: new StaticFeatureSource(features, false),
|
||||
state,
|
||||
layerToShow: layer.layerDef,
|
||||
return new ApplyButton(state, JSON.parse(ids), options);
|
||||
}))
|
||||
})
|
||||
|
||||
|
||||
|
||||
return new VariableUiElement(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") {
|
||||
return new Loading("Applying changes...")
|
||||
}
|
||||
const error = st.error
|
||||
return new Combine([new FixedUiElement("Something went wrong...").SetClass("alert"), new FixedUiElement(error).SetClass("subtle")]).SetClass("flex flex-col")
|
||||
}
|
||||
))
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.log("To parse is", to_parse)
|
||||
return new FixedUiElement("Could not generate a auto_apply-button for key " + argument[0] + " due to " + e).SetClass("alert")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue