forked from MapComplete/MapComplete
		
	feature(themeconfig): Make filters reusable from builtin layers
This commit is contained in:
		
							parent
							
								
									bdcf8a2601
								
							
						
					
					
						commit
						eb6093dd9f
					
				
					 3 changed files with 34 additions and 20 deletions
				
			
		| 
						 | 
				
			
			@ -23,16 +23,20 @@ import predifined_filters from "../../../assets/layers/filters/filters.json"
 | 
			
		|||
import { TagConfigJson } from "../Json/TagConfigJson"
 | 
			
		||||
import PointRenderingConfigJson from "../Json/PointRenderingConfigJson"
 | 
			
		||||
import LineRenderingConfigJson from "../Json/LineRenderingConfigJson"
 | 
			
		||||
import { type } from "os"
 | 
			
		||||
import exp from "constants"
 | 
			
		||||
 | 
			
		||||
class ExpandFilter extends DesugaringStep<LayerConfigJson> {
 | 
			
		||||
    private static readonly predefinedFilters = ExpandFilter.load_filters()
 | 
			
		||||
    private _state: DesugaringContext
 | 
			
		||||
 | 
			
		||||
    constructor() {
 | 
			
		||||
    constructor(state: DesugaringContext) {
 | 
			
		||||
        super(
 | 
			
		||||
            "Expands filters: replaces a shorthand by the value found in 'filters.json'",
 | 
			
		||||
            "Expands filters: replaces a shorthand by the value found in 'filters.json'. If the string is formatted 'layername.filtername, it will be looked up into that layer instead",
 | 
			
		||||
            ["filter"],
 | 
			
		||||
            "ExpandFilter"
 | 
			
		||||
        )
 | 
			
		||||
        this._state = state
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static load_filters(): Map<string, FilterConfigJson> {
 | 
			
		||||
| 
						 | 
				
			
			@ -62,6 +66,31 @@ class ExpandFilter extends DesugaringStep<LayerConfigJson> {
 | 
			
		|||
                newFilters.push(filter)
 | 
			
		||||
                continue
 | 
			
		||||
            }
 | 
			
		||||
            if (filter.indexOf(".") > 0) {
 | 
			
		||||
                if (this._state.sharedLayers.size > 0) {
 | 
			
		||||
                    const split = filter.split(".")
 | 
			
		||||
                    if (split.length > 2) {
 | 
			
		||||
                        errors.push(
 | 
			
		||||
                            context +
 | 
			
		||||
                                ": invalid filter name: " +
 | 
			
		||||
                                filter +
 | 
			
		||||
                                ", expected `layername.filterid`"
 | 
			
		||||
                        )
 | 
			
		||||
                    }
 | 
			
		||||
                    const layer = this._state.sharedLayers.get(split[0])
 | 
			
		||||
                    if (layer === undefined) {
 | 
			
		||||
                        errors.push(context + ": layer '" + split[0] + "' not found")
 | 
			
		||||
                    }
 | 
			
		||||
                    const expectedId = split[1]
 | 
			
		||||
                    const expandedFilter = (<(FilterConfigJson | string)[]>layer.filter).find(
 | 
			
		||||
                        (f) => typeof f !== "string" && f.id === expectedId
 | 
			
		||||
                    )
 | 
			
		||||
                    newFilters.push(<FilterConfigJson>expandedFilter)
 | 
			
		||||
                } else {
 | 
			
		||||
                    // This is a bootstrapping-run, we can safely ignore this
 | 
			
		||||
                }
 | 
			
		||||
                continue
 | 
			
		||||
            }
 | 
			
		||||
            // Search for the filter:
 | 
			
		||||
            const found = ExpandFilter.predefinedFilters.get(filter)
 | 
			
		||||
            if (found === undefined) {
 | 
			
		||||
| 
						 | 
				
			
			@ -889,7 +918,7 @@ export class PrepareLayer extends Fuse<LayerConfigJson> {
 | 
			
		|||
                (layer) =>
 | 
			
		||||
                    new Concat(new ExpandTagRendering(state, layer, { noHardcodedStrings: true }))
 | 
			
		||||
            ),
 | 
			
		||||
            new ExpandFilter()
 | 
			
		||||
            new ExpandFilter(state)
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -317,7 +317,7 @@ export interface LayerConfigJson {
 | 
			
		|||
 | 
			
		||||
    /**
 | 
			
		||||
     * All the extra questions for filtering.
 | 
			
		||||
     * If a string is given, mapComplete will search in 'filters.json' for the appropriate filter
 | 
			
		||||
     * If a string is given, mapComplete will search in 'filters.json' for the appropriate filter or will try to parse it as `layername.filterid` and us that one
 | 
			
		||||
     */
 | 
			
		||||
    filter?: (FilterConfigJson | string)[] | { sameAs: string }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -299,22 +299,7 @@
 | 
			
		|||
        }
 | 
			
		||||
      ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "id": "changing_table",
 | 
			
		||||
      "options": [
 | 
			
		||||
        {
 | 
			
		||||
          "question": {
 | 
			
		||||
            "en": "Has a changing table",
 | 
			
		||||
            "nl": "Heeft een luiertafel",
 | 
			
		||||
            "de": "Mit Wickeltisch",
 | 
			
		||||
            "es": "Tiene un cambiador",
 | 
			
		||||
            "fr": "A une table à langer",
 | 
			
		||||
            "da": "Har et puslebord"
 | 
			
		||||
          },
 | 
			
		||||
          "osmTags": "changing_table=yes"
 | 
			
		||||
        }
 | 
			
		||||
      ]
 | 
			
		||||
    },
 | 
			
		||||
    "toilet.changing_table",
 | 
			
		||||
    {
 | 
			
		||||
      "id": "free",
 | 
			
		||||
      "options": [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue