allow reuse of filters, update to artwork theme

This commit is contained in:
Pieter Vander Vennet 2022-09-14 16:29:41 +02:00
parent a1a12e6f38
commit 585dbb4587
8 changed files with 221 additions and 119 deletions

View file

@ -1,23 +1,65 @@
import {
Concat,
Conversion,
DesugaringContext,
DesugaringStep,
Each,
FirstOf,
Fuse,
On,
SetDefault,
} from "./Conversion"
import { LayerConfigJson } from "../Json/LayerConfigJson"
import { TagRenderingConfigJson } from "../Json/TagRenderingConfigJson"
import { Utils } from "../../../Utils"
import {Concat, Conversion, DesugaringContext, DesugaringStep, Each, FirstOf, Fuse, On, SetDefault,} from "./Conversion"
import {LayerConfigJson} from "../Json/LayerConfigJson"
import {TagRenderingConfigJson} from "../Json/TagRenderingConfigJson"
import {Utils} from "../../../Utils"
import RewritableConfigJson from "../Json/RewritableConfigJson"
import SpecialVisualizations from "../../../UI/SpecialVisualizations"
import Translations from "../../../UI/i18n/Translations"
import { Translation } from "../../../UI/i18n/Translation"
import {Translation} from "../../../UI/i18n/Translation"
import * as tagrenderingconfigmeta from "../../../assets/tagrenderingconfigmeta.json"
import { AddContextToTranslations } from "./AddContextToTranslations"
import {AddContextToTranslations} from "./AddContextToTranslations"
import FilterConfigJson from "../Json/FilterConfigJson";
import * as predifined_filters from "../../../assets/layers/filters/filters.json"
class ExpandFilter extends DesugaringStep<LayerConfigJson>{
private static load_filters(): Map<string, FilterConfigJson>{
let filters = new Map<string, FilterConfigJson>();
for (const filter of (<FilterConfigJson[]>predifined_filters.filter)) {
filters.set(filter.id, filter)
}
return filters;
}
private static readonly predefinedFilters = ExpandFilter.load_filters();
constructor() {
super("Expands filters: replaces a shorthand by the value found in 'filters.json'", ["filter"], "ExpandFilter");
}
convert(json: LayerConfigJson, context: string): { result: LayerConfigJson; errors?: string[]; warnings?: string[]; information?: string[] } {
if(json.filter === undefined || json.filter === null){
return {result: json} // Nothing to change here
}
if( json.filter["sameAs"] !== undefined){
return {result: json} // Nothing to change here
}
const newFilters : FilterConfigJson[] = []
const errors :string[]= []
for (const filter of (<(FilterConfigJson|string)[]> json.filter)) {
if (typeof filter !== "string") {
newFilters.push(filter)
continue
}
// Search for the filter:
const found = ExpandFilter.predefinedFilters.get(filter)
if(found === undefined){
const suggestions = Utils.sortedByLevenshteinDistance(filter, Array.from(ExpandFilter.predefinedFilters.keys()), t => t)
const err = context+".filter: while searching for predifined filter "+filter+": this filter is not found. Perhaps you meant one of: "+suggestions
errors.push(err)
}
newFilters.push(found)
}
return {result: {
...json, filter: newFilters
}, errors};
}
}
class ExpandTagRendering extends Conversion<
string | TagRenderingConfigJson | { builtin: string | string[]; override: any },
@ -178,7 +220,7 @@ class ExpandTagRendering extends Conversion<
if (lookup === undefined) {
let candidates = Array.from(state.tagRenderings.keys())
if (name.indexOf(".") > 0) {
const [layerName, search] = name.split(".")
const [layerName] = name.split(".")
let layer = state.sharedLayers.get(layerName)
if (layerName === this._self.id) {
layer = this._self
@ -699,7 +741,8 @@ export class PrepareLayer extends Fuse<LayerConfigJson> {
)
),
new SetDefault("titleIcons", ["defaults"]),
new On("titleIcons", (layer) => new Concat(new ExpandTagRendering(state, layer)))
new On("titleIcons", (layer) => new Concat(new ExpandTagRendering(state, layer))),
new ExpandFilter()
)
}
}

View file

@ -316,9 +316,10 @@ export interface LayerConfigJson {
)[]
/**
* All the extra questions for filtering
* All the extra questions for filtering.
* If a string is given, mapComplete will search in 'filters.json' for the appropriate filter
*/
filter?: FilterConfigJson[] | { sameAs: string }
filter?: (FilterConfigJson | string)[] | { sameAs: string }
/**
* This block defines under what circumstances the delete dialog is shown for objects of this layer.

View file

@ -267,6 +267,9 @@ export default class TagRenderingConfig {
if (this.freeform.key === "wikidata" && txt.indexOf("{wikipedia()") >= 0) {
continue
}
if (this.freeform.type === "wikidata" && txt.indexOf(`{wikidata_label(${this.freeform.key})`) >= 0) {
continue
}
throw `${context}: The rendering for language ${ln} does not contain the freeform key {${this.freeform.key}}. This is a bug, as this rendering should show exactly this freeform key!\nThe rendering is ${txt} `
}
}