Refacotring: move themeConfig into models

This commit is contained in:
Pieter Vander Vennet 2021-08-07 23:11:34 +02:00
parent 0a01561d37
commit 647100bee5
79 changed files with 603 additions and 629 deletions

View file

@ -1,6 +1,6 @@
import LayerConfig from "./JSON/LayerConfig";
import * as known_layers from "../assets/generated/known_layers_and_themes.json"
import {Utils} from "../Utils";
import LayerConfig from "../Models/ThemeConfig/LayerConfig";
export default class AllKnownLayers {

View file

@ -1,6 +1,6 @@
import LayoutConfig from "./JSON/LayoutConfig";
import AllKnownLayers from "./AllKnownLayers";
import * as known_themes from "../assets/generated/known_layers_and_themes.json"
import LayoutConfig from "../Models/ThemeConfig/LayoutConfig";
export class AllKnownLayouts {

View file

@ -1,51 +0,0 @@
import {DeleteConfigJson} from "./DeleteConfigJson";
import {Translation} from "../../UI/i18n/Translation";
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
import Translations from "../../UI/i18n/Translations";
import {FromJSON} from "./FromJSON";
export default class DeleteConfig {
public readonly extraDeleteReasons?: {
explanation: Translation,
changesetMessage: string
}[]
public readonly nonDeleteMappings?: { if: TagsFilter, then: Translation }[]
public readonly softDeletionTags?: TagsFilter
public readonly neededChangesets?: number
constructor(json: DeleteConfigJson, context: string) {
this.extraDeleteReasons = json.extraDeleteReasons?.map((reason, i) => {
const ctx = `${context}.extraDeleteReasons[${i}]`
if ((reason.changesetMessage ?? "").length <= 5) {
throw `${ctx}.explanation is too short, needs at least 4 characters`
}
return {
explanation: Translations.T(reason.explanation, ctx + ".explanation"),
changesetMessage: reason.changesetMessage
}
})
this.nonDeleteMappings = json.nonDeleteMappings?.map((nonDelete, i) => {
const ctx = `${context}.extraDeleteReasons[${i}]`
return {
if: FromJSON.Tag(nonDelete.if, ctx + ".if"),
then: Translations.T(nonDelete.then, ctx + ".then")
}
})
this.softDeletionTags = undefined;
if(json.softDeletionTags !== undefined){
this.softDeletionTags = FromJSON.Tag(json.softDeletionTags,`${context}.softDeletionTags`)
}
if(json["hardDeletionTags"] !== undefined){
throw `You probably meant 'softDeletionTags' instead of 'hardDeletionTags' (at ${context})`
}
this.neededChangesets = json.neededChangesets
}
}

View file

@ -1,66 +0,0 @@
import {AndOrTagConfigJson} from "./TagConfigJson";
export interface DeleteConfigJson {
/***
* By default, three reasons to delete a point are shown:
*
* - The point does not exist anymore
* - The point was a testing point
* - THe point could not be found
*
* However, for some layers, there might be different or more specific reasons for deletion which can be user friendly to set, e.g.:
*
* - the shop has closed
* - the climbing route has been closed of for nature conservation reasons
* - ...
*
* These reasons can be stated here and will be shown in the list of options the user can choose from
*/
extraDeleteReasons?: {
/**
* The text that will be shown to the user - translatable
*/
explanation: string | any,
/**
* The text that will be uploaded into the changeset or will be used in the fixme in case of a soft deletion
* Should be a few words, in english
*/
changesetMessage: string
}[]
/**
* In some cases, a (starting) contributor might wish to delete a feature even though deletion is not appropriate.
* (The most relevant case are small paths running over private property. These should be marked as 'private' instead of deleted, as the community might trace the path again from aerial imagery, gettting us back to the original situation).
*
* By adding a 'nonDeleteMapping', an option can be added into the list which will retag the feature.
* It is important that the feature will be retagged in such a way that it won't be picked up by the layer anymore!
*/
nonDeleteMappings?: { if: AndOrTagConfigJson, then: string | any }[],
/**
* In some cases, the contributor is not allowed to delete the current feature (e.g. because it isn't a point, the point is referenced by a relation or the user isn't experienced enough).
* To still offer the user a 'delete'-option, the feature is retagged with these tags. This is a soft deletion, as the point isn't actually removed from OSM but rather marked as 'disused'
* It is important that the feature will be retagged in such a way that it won't be picked up by the layer anymore!
*
* Example (note that "amenity=" erases the 'amenity'-key alltogether):
* ```
* {
* "and": ["disussed:amenity=public_bookcase", "amenity="]
* }
* ```
*
* or (notice the use of the ':='-tag to copy the old value of 'shop=*' into 'disused:shop='):
* ```
* {
* "and": ["disused:shop:={shop}", "shop="]
* }
* ```
*/
softDeletionTags?: AndOrTagConfigJson | string,
/***
* By default, the contributor needs 20 previous changesets to delete points edited by others.
* For some small features (e.g. bicycle racks) this is too much and this requirement can be lowered or dropped, which can be done here.
*/
neededChangesets?: number
}

View file

@ -1,208 +0,0 @@
import {Translation} from "../../UI/i18n/Translation";
import UnitConfigJson from "./UnitConfigJson";
import Translations from "../../UI/i18n/Translations";
import BaseUIElement from "../../UI/BaseUIElement";
import Combine from "../../UI/Base/Combine";
import {FixedUiElement} from "../../UI/Base/FixedUiElement";
export class Unit {
public readonly appliesToKeys: Set<string>;
public readonly denominations: Denomination[];
public readonly denominationsSorted: Denomination[];
public readonly defaultDenom: Denomination;
public readonly eraseInvalid: boolean;
private readonly possiblePostFixes: string[] = []
constructor(appliesToKeys: string[], applicableUnits: Denomination[], eraseInvalid: boolean) {
this.appliesToKeys = new Set(appliesToKeys);
this.denominations = applicableUnits;
this.defaultDenom = applicableUnits.filter(denom => denom.default)[0]
this.eraseInvalid = eraseInvalid
const seenUnitExtensions = new Set<string>();
for (const denomination of this.denominations) {
if(seenUnitExtensions.has(denomination.canonical)){
throw "This canonical unit is already defined in another denomination: "+denomination.canonical
}
const duplicate = denomination.alternativeDenominations.filter(denom => seenUnitExtensions.has(denom))
if(duplicate.length > 0){
throw "A denomination is used multiple times: "+duplicate.join(", ")
}
seenUnitExtensions.add(denomination.canonical)
denomination.alternativeDenominations.forEach(d => seenUnitExtensions.add(d))
}
this.denominationsSorted = [...this.denominations]
this.denominationsSorted.sort((a, b) => b.canonical.length - a.canonical.length)
const possiblePostFixes = new Set<string>()
function addPostfixesOf(str){
str = str.toLowerCase()
for (let i = 0; i < str.length + 1; i++) {
const substr = str.substring(0,i)
possiblePostFixes.add(substr)
}
}
for (const denomination of this.denominations) {
addPostfixesOf(denomination.canonical)
denomination.alternativeDenominations.forEach(addPostfixesOf)
}
this.possiblePostFixes = Array.from(possiblePostFixes)
this.possiblePostFixes.sort((a, b) => b.length - a .length)
}
isApplicableToKey(key: string | undefined): boolean {
if (key === undefined) {
return false;
}
return this.appliesToKeys.has(key);
}
/**
* Finds which denomination is applicable and gives the stripped value back
*/
findDenomination(valueWithDenom: string): [string, Denomination] {
if(valueWithDenom === undefined){
return undefined;
}
for (const denomination of this.denominationsSorted) {
const bare = denomination.StrippedValue(valueWithDenom)
if (bare !== null) {
return [bare, denomination]
}
}
return [undefined, undefined]
}
asHumanLongValue(value: string): BaseUIElement {
if (value === undefined) {
return undefined;
}
const [stripped, denom] = this.findDenomination(value)
const human = denom?.human
if(human === undefined){
return new FixedUiElement(stripped ?? value);
}
const elems = denom.prefix ? [human, stripped] : [stripped, human];
return new Combine(elems)
}
/**
* Returns the value without any (sub)parts of any denomination - usefull as preprocessing step for validating inputs.
* E.g.
* if 'megawatt' is a possible denomination, then '5 Meg' will be rewritten to '5' (which can then be validated as a valid pnat)
*
* Returns the original string if nothign matches
*/
stripUnitParts(str: string) {
if(str === undefined){
return undefined;
}
for (const denominationPart of this.possiblePostFixes) {
if(str.endsWith(denominationPart)){
return str.substring(0, str.length - denominationPart.length).trim()
}
}
return str;
}
}
export class Denomination {
public readonly canonical: string;
readonly default: boolean;
readonly prefix: boolean;
private readonly _human: Translation;
public readonly alternativeDenominations: string [];
constructor(json: UnitConfigJson, context: string) {
context = `${context}.unit(${json.canonicalDenomination})`
this.canonical = json.canonicalDenomination.trim()
if (this.canonical === undefined) {
throw `${context}: this unit has no decent canonical value defined`
}
json.alternativeDenomination.forEach((v, i) => {
if (((v?.trim() ?? "") === "")) {
throw `${context}.alternativeDenomination.${i}: invalid alternative denomination: undefined, null or only whitespace`
}
})
this.alternativeDenominations = json.alternativeDenomination?.map(v => v.trim()) ?? []
this.default = json.default ?? false;
this._human = Translations.T(json.human, context + "human")
this.prefix = json.prefix ?? false;
}
get human(): Translation {
return this._human.Clone()
}
public canonicalValue(value: string, actAsDefault?: boolean) {
if (value === undefined) {
return undefined;
}
const stripped = this.StrippedValue(value, actAsDefault)
if (stripped === null) {
return null;
}
return (stripped + " " + this.canonical.trim()).trim();
}
/**
* Returns the core value (without unit) if:
* - the value ends with the canonical or an alternative value (or begins with if prefix is set)
* - the value is a Number (without unit) and default is set
*
* Returns null if it doesn't match this unit
*/
public StrippedValue(value: string, actAsDefault?: boolean): string {
if (value === undefined) {
return undefined;
}
value = value.toLowerCase()
if (this.prefix) {
if (value.startsWith(this.canonical) && this.canonical !== "") {
return value.substring(this.canonical.length).trim();
}
for (const alternativeValue of this.alternativeDenominations) {
if (value.startsWith(alternativeValue)) {
return value.substring(alternativeValue.length).trim();
}
}
} else {
if (value.endsWith(this.canonical.toLowerCase()) && this.canonical !== "") {
return value.substring(0, value.length - this.canonical.length).trim();
}
for (const alternativeValue of this.alternativeDenominations) {
if (value.endsWith(alternativeValue.toLowerCase())) {
return value.substring(0, value.length - alternativeValue.length).trim();
}
}
}
if (this.default || actAsDefault) {
const parsed = Number(value.trim())
if (!isNaN(parsed)) {
return value.trim();
}
}
return null;
}
}

View file

@ -1,27 +0,0 @@
import { TagsFilter } from "../../Logic/Tags/TagsFilter";
import { Translation } from "../../UI/i18n/Translation";
import Translations from "../../UI/i18n/Translations";
import FilterConfigJson from "./FilterConfigJson";
import { FromJSON } from "./FromJSON";
export default class FilterConfig {
readonly options: {
question: Translation;
osmTags: TagsFilter;
}[];
constructor(json: FilterConfigJson, context: string) {
this.options = json.options.map((option, i) => {
const question = Translations.T(
option.question,
context + ".options-[" + i + "].question"
);
const osmTags = FromJSON.Tag(
option.osmTags ?? {and:[]},
`${context}.options-[${i}].osmTags`
);
return { question: question, osmTags: osmTags };
});
}
}

View file

@ -1,11 +0,0 @@
import { AndOrTagConfigJson } from "./TagConfigJson";
export default interface FilterConfigJson {
/**
* The options for a filter
* If there are multiple options these will be a list of radio buttons
* If there is only one option this will be a checkbox
* Filtering is done based on the given osmTags that are compared to the objects in that layer.
*/
options: { question: string | any; osmTags?: AndOrTagConfigJson | string }[];
}

View file

@ -1,144 +0,0 @@
import {AndOrTagConfigJson} from "./TagConfigJson";
import {Utils} from "../../Utils";
import {RegexTag} from "../../Logic/Tags/RegexTag";
import {Or} from "../../Logic/Tags/Or";
import {And} from "../../Logic/Tags/And";
import {Tag} from "../../Logic/Tags/Tag";
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
import SubstitutingTag from "../../Logic/Tags/SubstitutingTag";
import ComparingTag from "../../Logic/Tags/ComparingTag";
export class FromJSON {
public static SimpleTag(json: string, context?: string): Tag {
const tag = Utils.SplitFirst(json, "=");
if (tag.length !== 2) {
throw `Invalid tag: no (or too much) '=' found (in ${context ?? "unkown context"})`
}
return new Tag(tag[0], tag[1]);
}
public static Tag(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter {
try {
return this.TagUnsafe(json, context);
} catch (e) {
console.error("Could not parse tag", json, "in context", context, "due to ", e)
throw e;
}
}
private static comparators
: [string, (a: number, b: number) => boolean][]
= [
["<=", (a, b) => a <= b],
[">=", (a, b) => a >= b],
["<", (a, b) => a < b],
[">", (a, b) => a > b],
]
private static TagUnsafe(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter {
if (json === undefined) {
throw `Error while parsing a tag: 'json' is undefined in ${context}. Make sure all the tags are defined and at least one tag is present in a complex expression`
}
if (typeof (json) == "string") {
const tag = json as string;
for (const [operator, comparator] of FromJSON.comparators) {
if (tag.indexOf(operator) >= 0) {
const split = Utils.SplitFirst(tag, operator);
const val = Number(split[1].trim())
if (isNaN(val)) {
throw `Error: not a valid value for a comparison: ${split[1]}, make sure it is a number and nothing more (at ${context})`
}
const f = (value: string | undefined) => {
const b = Number(value?.replace(/[^\d.]/g,''))
if (isNaN(b)) {
return false;
}
return comparator(b, val)
}
return new ComparingTag(split[0], f, operator + val)
}
}
if (tag.indexOf("!~") >= 0) {
const split = Utils.SplitFirst(tag, "!~");
if (split[1] === "*") {
throw `Don't use 'key!~*' - use 'key=' instead (empty string as value (in the tag ${tag} while parsing ${context})`
}
return new RegexTag(
split[0],
new RegExp("^" + split[1] + "$"),
true
);
}
if (tag.indexOf("~~") >= 0) {
const split = Utils.SplitFirst(tag, "~~");
if (split[1] === "*") {
split[1] = "..*"
}
return new RegexTag(
new RegExp("^" + split[0] + "$"),
new RegExp("^" + split[1] + "$")
);
}
if (tag.indexOf(":=") >= 0) {
const split = Utils.SplitFirst(tag, ":=");
return new SubstitutingTag(split[0], split[1]);
}
if (tag.indexOf("!=") >= 0) {
const split = Utils.SplitFirst(tag, "!=");
if (split[1] === "*") {
split[1] = "..*"
}
return new RegexTag(
split[0],
new RegExp("^" + split[1] + "$"),
true
);
}
if (tag.indexOf("!~") >= 0) {
const split = Utils.SplitFirst(tag, "!~");
if (split[1] === "*") {
split[1] = "..*"
}
return new RegexTag(
split[0],
new RegExp("^" + split[1] + "$"),
true
);
}
if (tag.indexOf("~") >= 0) {
const split = Utils.SplitFirst(tag, "~");
if (split[1] === "*") {
split[1] = "..*"
}
return new RegexTag(
split[0],
new RegExp("^" + split[1] + "$")
);
}
if (tag.indexOf("=") >= 0) {
const split = Utils.SplitFirst(tag, "=");
if (split[1] == "*") {
throw `Error while parsing tag '${tag}' in ${context}: detected a wildcard on a normal value. Use a regex pattern instead`
}
return new Tag(split[0], split[1])
}
throw `Error while parsing tag '${tag}' in ${context}: no key part and value part were found`
}
if (json.and !== undefined) {
return new And(json.and.map(t => FromJSON.Tag(t, context)));
}
if (json.or !== undefined) {
return new Or(json.or.map(t => FromJSON.Tag(t, context)));
}
}
}

View file

@ -1,594 +0,0 @@
import Translations from "../../UI/i18n/Translations";
import TagRenderingConfig from "./TagRenderingConfig";
import {LayerConfigJson} from "./LayerConfigJson";
import {FromJSON} from "./FromJSON";
import SharedTagRenderings from "../SharedTagRenderings";
import {TagRenderingConfigJson} from "./TagRenderingConfigJson";
import {Translation} from "../../UI/i18n/Translation";
import Svg from "../../Svg";
import {Utils} from "../../Utils";
import Combine from "../../UI/Base/Combine";
import {VariableUiElement} from "../../UI/Base/VariableUIElement";
import {UIEventSource} from "../../Logic/UIEventSource";
import {FixedUiElement} from "../../UI/Base/FixedUiElement";
import SourceConfig from "./SourceConfig";
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
import BaseUIElement from "../../UI/BaseUIElement";
import {Unit} from "./Denomination";
import DeleteConfig from "./DeleteConfig";
import FilterConfig from "./FilterConfig";
import PresetConfig from "./PresetConfig";
export default class LayerConfig {
static WAYHANDLING_DEFAULT = 0;
static WAYHANDLING_CENTER_ONLY = 1;
static WAYHANDLING_CENTER_AND_WAY = 2;
id: string;
name: Translation;
description: Translation;
source: SourceConfig;
calculatedTags: [string, string][];
doNotDownload: boolean;
passAllFeatures: boolean;
isShown: TagRenderingConfig;
minzoom: number;
minzoomVisible: number;
maxzoom: number;
title?: TagRenderingConfig;
titleIcons: TagRenderingConfig[];
icon: TagRenderingConfig;
iconOverlays: { if: TagsFilter; then: TagRenderingConfig; badge: boolean }[];
iconSize: TagRenderingConfig;
label: TagRenderingConfig;
rotation: TagRenderingConfig;
color: TagRenderingConfig;
width: TagRenderingConfig;
dashArray: TagRenderingConfig;
wayHandling: number;
public readonly units: Unit[];
public readonly deletion: DeleteConfig | null;
public readonly allowSplit: boolean
presets: PresetConfig[];
tagRenderings: TagRenderingConfig[];
filters: FilterConfig[];
constructor(
json: LayerConfigJson,
units?: Unit[],
context?: string,
official: boolean = true
) {
this.units = units ?? [];
context = context + "." + json.id;
const self = this;
this.id = json.id;
this.allowSplit = json.allowSplit ?? false;
this.name = Translations.T(json.name, context + ".name");
if (json.description !== undefined) {
if (Object.keys(json.description).length === 0) {
json.description = undefined;
}
}
this.description = Translations.T(
json.description,
context + ".description"
);
let legacy = undefined;
if (json["overpassTags"] !== undefined) {
// @ts-ignore
legacy = FromJSON.Tag(json["overpassTags"], context + ".overpasstags");
}
if (json.source !== undefined) {
if (legacy !== undefined) {
throw (
context +
"Both the legacy 'layer.overpasstags' and the new 'layer.source'-field are defined"
);
}
let osmTags: TagsFilter = legacy;
if (json.source["osmTags"]) {
osmTags = FromJSON.Tag(
json.source["osmTags"],
context + "source.osmTags"
);
}
if (json.source["geoJsonSource"] !== undefined) {
throw context + "Use 'geoJson' instead of 'geoJsonSource'";
}
this.source = new SourceConfig(
{
osmTags: osmTags,
geojsonSource: json.source["geoJson"],
geojsonSourceLevel: json.source["geoJsonZoomLevel"],
overpassScript: json.source["overpassScript"],
isOsmCache: json.source["isOsmCache"],
},
this.id
);
} else {
this.source = new SourceConfig({
osmTags: legacy,
});
}
this.calculatedTags = undefined;
if (json.calculatedTags !== undefined) {
if (!official) {
console.warn(
`Unofficial theme ${this.id} with custom javascript! This is a security risk`
);
}
this.calculatedTags = [];
for (const kv of json.calculatedTags) {
const index = kv.indexOf("=");
const key = kv.substring(0, index);
const code = kv.substring(index + 1);
this.calculatedTags.push([key, code]);
}
}
this.doNotDownload = json.doNotDownload ?? false;
this.passAllFeatures = json.passAllFeatures ?? false;
this.minzoom = json.minzoom ?? 0;
this.minzoomVisible = json.minzoomVisible ?? this.minzoom;
this.wayHandling = json.wayHandling ?? 0;
this.presets = (json.presets ?? []).map((pr, i) => {
let preciseInput = undefined;
if(pr.preciseInput !== undefined){
if (pr.preciseInput === true) {
pr.preciseInput = {
preferredBackground: undefined
}
}
let snapToLayers: string[];
if (typeof pr.preciseInput.snapToLayer === "string") {
snapToLayers = [pr.preciseInput.snapToLayer]
} else {
snapToLayers = pr.preciseInput.snapToLayer
}
let preferredBackground : string[]
if (typeof pr.preciseInput.preferredBackground === "string") {
preferredBackground = [pr.preciseInput.preferredBackground]
} else {
preferredBackground = pr.preciseInput.preferredBackground
}
preciseInput = {
preferredBackground: preferredBackground,
snapToLayers: snapToLayers,
maxSnapDistance: pr.preciseInput.maxSnapDistance ?? 10
}
}
const config : PresetConfig= {
title: Translations.T(pr.title, `${context}.presets[${i}].title`),
tags: pr.tags.map((t) => FromJSON.SimpleTag(t)),
description: Translations.T(pr.description, `${context}.presets[${i}].description`),
preciseInput: preciseInput,
}
return config;
});
/** Given a key, gets the corresponding property from the json (or the default if not found
*
* The found value is interpreted as a tagrendering and fetched/parsed
* */
function tr(key: string, deflt) {
const v = json[key];
if (v === undefined || v === null) {
if (deflt === undefined) {
return undefined;
}
return new TagRenderingConfig(
deflt,
self.source.osmTags,
`${context}.${key}.default value`
);
}
if (typeof v === "string") {
const shared = SharedTagRenderings.SharedTagRendering.get(v);
if (shared) {
return shared;
}
}
return new TagRenderingConfig(
v,
self.source.osmTags,
`${context}.${key}`
);
}
/**
* Converts a list of tagRenderingCOnfigJSON in to TagRenderingConfig
* A string is interpreted as a name to call
*/
function trs(
tagRenderings?: (string | TagRenderingConfigJson)[],
readOnly = false
) {
if (tagRenderings === undefined) {
return [];
}
return Utils.NoNull(
tagRenderings.map((renderingJson, i) => {
if (typeof renderingJson === "string") {
if (renderingJson === "questions") {
if (readOnly) {
throw `A tagrendering has a question, but asking a question does not make sense here: is it a title icon or a geojson-layer? ${context}. The offending tagrendering is ${JSON.stringify(
renderingJson
)}`;
}
return new TagRenderingConfig("questions", undefined);
}
const shared =
SharedTagRenderings.SharedTagRendering.get(renderingJson);
if (shared !== undefined) {
return shared;
}
const keys = Array.from(
SharedTagRenderings.SharedTagRendering.keys()
);
if (Utils.runningFromConsole) {
return undefined;
}
throw `Predefined tagRendering ${renderingJson} not found in ${context}.\n Try one of ${keys.join(
", "
)}\n If you intent to output this text literally, use {\"render\": <your text>} instead"}`;
}
return new TagRenderingConfig(
renderingJson,
self.source.osmTags,
`${context}.tagrendering[${i}]`
);
})
);
}
this.tagRenderings = trs(json.tagRenderings, false);
this.filters = (json.filter ?? []).map((option, i) => {
return new FilterConfig(option, `${context}.filter-[${i}]`)
});
const titleIcons = [];
const defaultIcons = [
"phonelink",
"emaillink",
"wikipedialink",
"osmlink",
"sharelink",
];
for (const icon of json.titleIcons ?? defaultIcons) {
if (icon === "defaults") {
titleIcons.push(...defaultIcons);
} else {
titleIcons.push(icon);
}
}
this.titleIcons = trs(titleIcons, true);
this.title = tr("title", undefined);
this.icon = tr("icon", "");
this.iconOverlays = (json.iconOverlays ?? []).map((overlay, i) => {
let tr = new TagRenderingConfig(
overlay.then,
self.source.osmTags,
`iconoverlays.${i}`
);
if (
typeof overlay.then === "string" &&
SharedTagRenderings.SharedIcons.get(overlay.then) !== undefined
) {
tr = SharedTagRenderings.SharedIcons.get(overlay.then);
}
return {
if: FromJSON.Tag(overlay.if),
then: tr,
badge: overlay.badge ?? false,
};
});
const iconPath = this.icon.GetRenderValue({id: "node/-1"}).txt;
if (iconPath.startsWith(Utils.assets_path)) {
const iconKey = iconPath.substr(Utils.assets_path.length);
if (Svg.All[iconKey] === undefined) {
throw "Builtin SVG asset not found: " + iconPath;
}
}
this.isShown = tr("isShown", "yes");
this.iconSize = tr("iconSize", "40,40,center");
this.label = tr("label", "");
this.color = tr("color", "#0000ff");
this.width = tr("width", "7");
this.rotation = tr("rotation", "0");
this.dashArray = tr("dashArray", "");
this.deletion = null;
if (json.deletion === true) {
json.deletion = {};
}
if (json.deletion !== undefined && json.deletion !== false) {
this.deletion = new DeleteConfig(json.deletion, `${context}.deletion`);
}
if (json["showIf"] !== undefined) {
throw (
"Invalid key on layerconfig " +
this.id +
": showIf. Did you mean 'isShown' instead?"
);
}
}
public CustomCodeSnippets(): string[] {
if (this.calculatedTags === undefined) {
return [];
}
return this.calculatedTags.map((code) => code[1]);
}
public AddRoamingRenderings(addAll: {
tagRenderings: TagRenderingConfig[];
titleIcons: TagRenderingConfig[];
iconOverlays: {
if: TagsFilter;
then: TagRenderingConfig;
badge: boolean;
}[];
}): LayerConfig {
let insertionPoint = this.tagRenderings
.map((tr) => tr.IsQuestionBoxElement())
.indexOf(true);
if (insertionPoint < 0) {
// No 'questions' defined - we just add them all to the end
insertionPoint = this.tagRenderings.length;
}
this.tagRenderings.splice(insertionPoint, 0, ...addAll.tagRenderings);
this.iconOverlays.push(...addAll.iconOverlays);
for (const icon of addAll.titleIcons) {
this.titleIcons.splice(0, 0, icon);
}
return this;
}
public GetRoamingRenderings(): {
tagRenderings: TagRenderingConfig[];
titleIcons: TagRenderingConfig[];
iconOverlays: {
if: TagsFilter;
then: TagRenderingConfig;
badge: boolean;
}[];
} {
const tagRenderings = this.tagRenderings.filter((tr) => tr.roaming);
const titleIcons = this.titleIcons.filter((tr) => tr.roaming);
const iconOverlays = this.iconOverlays.filter((io) => io.then.roaming);
return {
tagRenderings: tagRenderings,
titleIcons: titleIcons,
iconOverlays: iconOverlays,
};
}
public GenerateLeafletStyle(
tags: UIEventSource<any>,
clickable: boolean
): {
icon: {
html: BaseUIElement;
iconSize: [number, number];
iconAnchor: [number, number];
popupAnchor: [number, number];
iconUrl: string;
className: string;
};
color: string;
weight: number;
dashArray: number[];
} {
function num(str, deflt = 40) {
const n = Number(str);
if (isNaN(n)) {
return deflt;
}
return n;
}
function rendernum(tr: TagRenderingConfig, deflt: number) {
const str = Number(render(tr, "" + deflt));
const n = Number(str);
if (isNaN(n)) {
return deflt;
}
return n;
}
function render(tr: TagRenderingConfig, deflt?: string) {
if(tags === undefined){
return deflt
}
const str = tr?.GetRenderValue(tags.data)?.txt ?? deflt;
return Utils.SubstituteKeys(str, tags.data).replace(/{.*}/g, "");
}
const iconSize = render(this.iconSize, "40,40,center").split(",");
const dashArray = render(this.dashArray)?.split(" ")?.map(Number);
let color = render(this.color, "#00f");
if (color.startsWith("--")) {
color = getComputedStyle(document.body).getPropertyValue(
"--catch-detail-color"
);
}
const weight = rendernum(this.width, 5);
const iconW = num(iconSize[0]);
let iconH = num(iconSize[1]);
const mode = iconSize[2]?.trim()?.toLowerCase() ?? "center";
let anchorW = iconW / 2;
let anchorH = iconH / 2;
if (mode === "left") {
anchorW = 0;
}
if (mode === "right") {
anchorW = iconW;
}
if (mode === "top") {
anchorH = 0;
}
if (mode === "bottom") {
anchorH = iconH;
}
const iconUrlStatic = render(this.icon);
const self = this;
function genHtmlFromString(sourcePart: string, rotation: string): BaseUIElement {
const style = `width:100%;height:100%;transform: rotate( ${rotation} );display:block;position: absolute; top: 0; left: 0`;
let html: BaseUIElement = new FixedUiElement(
`<img src="${sourcePart}" style="${style}" />`
);
const match = sourcePart.match(/([a-zA-Z0-9_]*):([^;]*)/);
if (match !== null && Svg.All[match[1] + ".svg"] !== undefined) {
html = new Combine([
(Svg.All[match[1] + ".svg"] as string).replace(
/#000000/g,
match[2]
),
]).SetStyle(style);
}
return html;
}
const mappedHtml = tags?.map((tgs) => {
// What do you mean, 'tgs' is never read?
// It is read implicitly in the 'render' method
const iconUrl = render(self.icon);
const rotation = render(self.rotation, "0deg");
let htmlParts: BaseUIElement[] = [];
let sourceParts = Utils.NoNull(
iconUrl.split(";").filter((prt) => prt != "")
);
for (const sourcePart of sourceParts) {
htmlParts.push(genHtmlFromString(sourcePart, rotation));
}
let badges = [];
for (const iconOverlay of self.iconOverlays) {
if (!iconOverlay.if.matchesProperties(tgs)) {
continue;
}
if (iconOverlay.badge) {
const badgeParts: BaseUIElement[] = [];
const partDefs = iconOverlay.then
.GetRenderValue(tgs)
.txt.split(";")
.filter((prt) => prt != "");
for (const badgePartStr of partDefs) {
badgeParts.push(genHtmlFromString(badgePartStr, "0"));
}
const badgeCompound = new Combine(badgeParts).SetStyle(
"display:flex;position:relative;width:100%;height:100%;"
);
badges.push(badgeCompound);
} else {
htmlParts.push(
genHtmlFromString(iconOverlay.then.GetRenderValue(tgs).txt, "0")
);
}
}
if (badges.length > 0) {
const badgesComponent = new Combine(badges).SetStyle(
"display:flex;height:50%;width:100%;position:absolute;top:50%;left:50%;"
);
htmlParts.push(badgesComponent);
}
if (sourceParts.length == 0) {
iconH = 0;
}
try {
const label = self.label
?.GetRenderValue(tgs)
?.Subs(tgs)
?.SetClass("block text-center")
?.SetStyle("margin-top: " + (iconH + 2) + "px");
if (label !== undefined) {
htmlParts.push(
new Combine([label]).SetClass("flex flex-col items-center")
);
}
} catch (e) {
console.error(e, tgs);
}
return new Combine(htmlParts);
});
return {
icon: {
html: mappedHtml === undefined ? new FixedUiElement(self.icon.render.txt) : new VariableUiElement(mappedHtml),
iconSize: [iconW, iconH],
iconAnchor: [anchorW, anchorH],
popupAnchor: [0, 3 - anchorH],
iconUrl: iconUrlStatic,
className: clickable
? "leaflet-div-icon"
: "leaflet-div-icon unclickable",
},
color: color,
weight: weight,
dashArray: dashArray,
};
}
public ExtractImages(): Set<string> {
const parts: Set<string>[] = [];
parts.push(...this.tagRenderings?.map((tr) => tr.ExtractImages(false)));
parts.push(...this.titleIcons?.map((tr) => tr.ExtractImages(true)));
parts.push(this.icon?.ExtractImages(true));
parts.push(
...this.iconOverlays?.map((overlay) => overlay.then.ExtractImages(true))
);
for (const preset of this.presets) {
parts.push(new Set<string>(preset.description?.ExtractImages(false)));
}
const allIcons = new Set<string>();
for (const part of parts) {
part?.forEach(allIcons.add, allIcons);
}
return allIcons;
}
}

View file

@ -1,320 +0,0 @@
import {TagRenderingConfigJson} from "./TagRenderingConfigJson";
import {AndOrTagConfigJson} from "./TagConfigJson";
import {DeleteConfigJson} from "./DeleteConfigJson";
import FilterConfigJson from "./FilterConfigJson";
/**
* Configuration for a single layer
*/
export interface LayerConfigJson {
/**
* The id of this layer.
* This should be a simple, lowercase, human readable string that is used to identify the layer.
*/
id: string;
/**
* The name of this layer
* Used in the layer control panel and the 'Personal theme'.
*
* If not given, will be hidden (and thus not toggable) in the layer control
*/
name?: string | any
/**
* A description for this layer.
* Shown in the layer selections and in the personel theme
*/
description?: string | any;
/**
* This determines where the data for the layer is fetched.
* There are some options:
*
* # Query OSM directly
* source: {osmTags: "key=value"}
* will fetch all objects with given tags from OSM.
* Currently, this will create a query to overpass and fetch the data - in the future this might fetch from the OSM API
*
* # Query OSM Via the overpass API with a custom script
* source: {overpassScript: "<custom overpass tags>"} when you want to do special things. _This should be really rare_.
* This means that the data will be pulled from overpass with this script, and will ignore the osmTags for the query
* However, for the rest of the pipeline, the OsmTags will _still_ be used. This is important to enable layers etc...
*
*
* # A single geojson-file
* source: {geoJson: "https://my.source.net/some-geo-data.geojson"}
* fetches a geojson from a third party source
*
* # A tiled geojson source
* source: {geoJson: "https://my.source.net/some-tile-geojson-{layer}-{z}-{x}-{y}.geojson", geoJsonZoomLevel: 14}
* to use a tiled geojson source. The web server must offer multiple geojsons. {z}, {x} and {y} are substituted by the location; {layer} is substituted with the id of the loaded layer
*
*
* Note that both geojson-options might set a flag 'isOsmCache' indicating that the data originally comes from OSM too
*
*
* NOTE: the previous format was 'overpassTags: AndOrTagConfigJson | string', which is interpreted as a shorthand for source: {osmTags: "key=value"}
* While still supported, this is considered deprecated
*/
source: { osmTags: AndOrTagConfigJson | string } |
{ osmTags: AndOrTagConfigJson | string, geoJson: string, geoJsonZoomLevel?: number, isOsmCache?: boolean } |
{ osmTags: AndOrTagConfigJson | string, overpassScript: string }
/**
*
* A list of extra tags to calculate, specified as "keyToAssignTo=javascript-expression".
* There are a few extra functions available. Refer to <a>Docs/CalculatedTags.md</a> for more information
* The functions will be run in order, e.g.
* [
* "_max_overlap_m2=Math.max(...feat.overlapsWith("someOtherLayer").map(o => o.overlap))
* "_max_overlap_ratio=Number(feat._max_overlap_m2)/feat.area
* ]
*
*/
calculatedTags?: string[];
/**
* If set, this layer will not query overpass; but it'll still match the tags above which are by chance returned by other layers.
* Works well together with 'passAllFeatures', to add decoration
*/
doNotDownload?: boolean;
/**
* This tag rendering should either be 'yes' or 'no'. If 'no' is returned, then the feature will be hidden from view.
* This is useful to hide certain features from view. Important: hiding features does not work dynamically, but is only calculated when the data is first renders.
* This implies that it is not possible to hide a feature after a tagging change
*
* The default value is 'yes'
*/
isShown?: TagRenderingConfigJson;
/**
* The minimum needed zoomlevel required before loading of the data start
* Default: 0
*/
minzoom?: number;
/**
* The zoom level at which point the data is hidden again
* Default: 100 (thus: always visible
*/
minzoomVisible?: number;
/**
* The title shown in a popup for elements of this layer.
*/
title?: string | TagRenderingConfigJson;
/**
* Small icons shown next to the title.
* If not specified, the OsmLink and wikipedia links will be used by default.
* Use an empty array to hide them.
* Note that "defaults" will insert all the default titleIcons
*/
titleIcons?: (string | TagRenderingConfigJson)[];
/**
* The icon for an element.
* Note that this also doubles as the icon for this layer (rendered with the overpass-tags) ánd the icon in the presets.
*
* The result of the icon is rendered as follows:
* the resulting string is interpreted as a _list_ of items, separated by ";". The bottommost layer is the first layer.
* As a result, on could use a generic pin, then overlay it with a specific icon.
* To make things even more practical, one can use all SVG's from the folder "assets/svg" and _substitute the color_ in it.
* E.g. to draw a red pin, use "pin:#f00", to have a green circle with your icon on top, use `circle:#0f0;<path to my icon.svg>`
*
*/
icon?: string | TagRenderingConfigJson;
/**
* IconsOverlays are a list of extra icons/badges to overlay over the icon.
* The 'badge'-toggle changes their behaviour.
* If badge is set, it will be added as a 25% height icon at the bottom right of the icon, with all the badges in a flex layout.
* If badges is false, it'll be a simple overlay
*
* Note: strings are interpreted as icons, so layering and substituting is supported
*/
iconOverlays?: { if: string | AndOrTagConfigJson, then: string | TagRenderingConfigJson, badge?: boolean }[]
/**
* A string containing "width,height" or "width,height,anchorpoint" where anchorpoint is any of 'center', 'top', 'bottom', 'left', 'right', 'bottomleft','topright', ...
* Default is '40,40,center'
*/
iconSize?: string | TagRenderingConfigJson;
/**
* The rotation of an icon, useful for e.g. directions.
* Usage: as if it were a css property for 'rotate', thus has to end with 'deg', e.g. `90deg`, `{direction}deg`, `calc(90deg - {camera:direction}deg)``
*/
rotation?: string | TagRenderingConfigJson;
/**
* A HTML-fragment that is shown below the icon, for example:
* <div style="background: white; display: block">{name}</div>
*
* If the icon is undefined, then the label is shown in the center of the feature.
* Note that, if the wayhandling hides the icon then no label is shown as well.
*/
label?: string | TagRenderingConfigJson;
/**
* The color for way-elements and SVG-elements.
* If the value starts with "--", the style of the body element will be queried for the corresponding variable instead
*/
color?: string | TagRenderingConfigJson;
/**
* The stroke-width for way-elements
*/
width?: string | TagRenderingConfigJson;
/**
* A dasharray, e.g. "5 6"
* The dasharray defines 'pixels of line, pixels of gap, pixels of line, pixels of gap',
* Default value: "" (empty string == full line)
*/
dashArray?: string | TagRenderingConfigJson
/**
* Wayhandling: should a way/area be displayed as:
* 0) The way itself
* 1) Only the centerpoint
* 2) The centerpoint and the way
*/
wayHandling?: number;
/**
* If set, this layer will pass all the features it receives onto the next layer.
* This is ideal for decoration, e.g. directionss on cameras
*/
passAllFeatures?: boolean
/**
* Presets for this layer.
* A preset shows up when clicking the map on a without data (or when right-clicking/long-pressing);
* it will prompt the user to add a new point.
*
* The most important aspect are the tags, which define which tags the new point will have;
* The title is shown in the dialog, along with the first sentence of the description.
*
* Upon confirmation, the full description is shown beneath the buttons - perfect to add pictures and examples.
*
* Note: the icon of the preset is determined automatically based on the tags and the icon above. Don't worry about that!
* NB: if no presets are defined, the popup to add new points doesn't show up at all
*/
presets?: {
/**
* The title - shown on the 'add-new'-button.
*/
title: string | any,
/**
* The tags to add. It determines the icon too
*/
tags: string[],
/**
* The _first sentence_ of the description is shown on the button of the `add` menu.
* The full description is shown in the confirmation dialog.
*
* (The first sentence is until the first '.'-character in the description)
*/
description?: string | any,
/**
* If set, the user will prompted to confirm the location before actually adding the data.
* This will be with a 'drag crosshair'-method.
*
* If 'preferredBackgroundCategory' is set, the element will attempt to pick a background layer of that category.
*/
preciseInput?: true | {
/**
* The type of background picture
*/
preferredBackground: "osmbasedmap" | "photo" | "historicphoto" | "map" | string | string [],
/**
* If specified, these layers will be shown to and the new point will be snapped towards it
*/
snapToLayer?: string | string[],
/**
* If specified, a new point will only be snapped if it is within this range.
* Distance in meter
*
* Default: 10
*/
maxSnapDistance?: number
}
}[],
/**
* All the tag renderings.
* A tag rendering is a block that either shows the known value or asks a question.
*
* Refer to the class `TagRenderingConfigJson` to see the possibilities.
*
* Note that we can also use a string here - where the string refers to a tag rendering defined in `assets/questions/questions.json`,
* where a few very general questions are defined e.g. website, phone number, ...
*
* A special value is 'questions', which indicates the location of the questions box. If not specified, it'll be appended to the bottom of the featureInfobox.
*
*/
tagRenderings?: (string | TagRenderingConfigJson) [],
/**
* All the extra questions for filtering
*/
filter?: (FilterConfigJson) [],
/**
* This block defines under what circumstances the delete dialog is shown for objects of this layer.
* If set, a dialog is shown to the user to (soft) delete the point.
* The dialog is built to be user friendly and to prevent mistakes.
* If deletion is not possible, the dialog will hide itself and show the reason of non-deletability instead.
*
* To configure, the following values are possible:
*
* - false: never ever show the delete button
* - true: show the default delete button
* - undefined: use the mapcomplete default to show deletion or not. Currently, this is the same as 'false' but this will change in the future
* - or: a hash with options (see below)
*
* The delete dialog
* =================
*
*
*
#### Hard deletion if enough experience
A feature can only be deleted from OpenStreetMap by mapcomplete if:
- It is a node
- No ways or relations use the node
- The logged-in user has enough experience OR the user is the only one to have edited the point previously
- The logged-in user has no unread messages (or has a ton of experience)
- The user did not select one of the 'non-delete-options' (see below)
In all other cases, a 'soft deletion' is used.
#### Soft deletion
A 'soft deletion' is when the point isn't deleted from OSM but retagged so that it'll won't how up in the mapcomplete theme anymore.
This makes it look like it was deleted, without doing damage. A fixme will be added to the point.
Note that a soft deletion is _only_ possible if these tags are provided by the theme creator, as they'll be different for every theme
#### No-delete options
In some cases, the contributor might want to delete something for the wrong reason (e.g. someone who wants to have a path removed "because the path is on their private property").
However, the path exists in reality and should thus be on OSM - otherwise the next contributor will pass by and notice "hey, there is a path missing here! Let me redraw it in OSM!)
The correct approach is to retag the feature in such a way that it is semantically correct *and* that it doesn't show up on the theme anymore.
A no-delete option is offered as 'reason to delete it', but secretly retags.
*/
deletion?: boolean | DeleteConfigJson
/**
* IF set, a 'split this road' button is shown
*/
allowSplit?: boolean
}

View file

@ -1,352 +0,0 @@
import {Translation} from "../../UI/i18n/Translation";
import TagRenderingConfig from "./TagRenderingConfig";
import LayerConfig from "./LayerConfig";
import {LayoutConfigJson} from "./LayoutConfigJson";
import AllKnownLayers from "../AllKnownLayers";
import SharedTagRenderings from "../SharedTagRenderings";
import {Utils} from "../../Utils";
import {Denomination, Unit} from "./Denomination";
export default class LayoutConfig {
public readonly id: string;
public readonly maintainer: string;
public readonly credits?: string;
public readonly changesetmessage?: string;
public readonly version: string;
public readonly language: string[];
public readonly title: Translation;
public readonly shortDescription?: Translation;
public readonly description: Translation;
public readonly descriptionTail?: Translation;
public readonly icon: string;
public readonly socialImage?: string;
public readonly startZoom: number;
public readonly startLat: number;
public readonly startLon: number;
public readonly widenFactor: number;
public readonly roamingRenderings: TagRenderingConfig[];
public readonly defaultBackgroundId?: string;
public layers: LayerConfig[];
public readonly clustering?: {
maxZoom: number,
minNeededElements: number
};
public readonly hideFromOverview: boolean;
public lockLocation: boolean | [[number, number], [number, number]];
public readonly enableUserBadge: boolean;
public readonly enableShareScreen: boolean;
public readonly enableMoreQuests: boolean;
public readonly enableAddNewPoints: boolean;
public readonly enableLayers: boolean;
public readonly enableSearch: boolean;
public readonly enableGeolocation: boolean;
public readonly enableBackgroundLayerSelection: boolean;
public readonly enableShowAllQuestions: boolean;
public readonly enableExportButton: boolean;
public readonly enablePdfDownload: boolean;
public readonly customCss?: string;
/*
How long is the cache valid, in seconds?
*/
public readonly cacheTimeout?: number;
public readonly units: Unit[] = []
private readonly _official: boolean;
constructor(json: LayoutConfigJson, official = true, context?: string) {
this._official = official;
this.id = json.id;
context = (context ?? "") + "." + this.id;
this.maintainer = json.maintainer;
this.credits = json.credits;
this.changesetmessage = json.changesetmessage;
this.version = json.version;
this.language = [];
if (typeof json.language === "string") {
this.language = [json.language];
} else {
this.language = json.language;
}
if (this.language.length == 0) {
throw `No languages defined. Define at least one language. (${context}.languages)`
}
if (json.title === undefined) {
throw "Title not defined in " + this.id;
}
if (json.description === undefined) {
throw "Description not defined in " + this.id;
}
this.units = LayoutConfig.ExtractUnits(json, context) ?? [];
this.title = new Translation(json.title, context + ".title");
this.description = new Translation(json.description, context + ".description");
this.shortDescription = json.shortDescription === undefined ? this.description.FirstSentence() : new Translation(json.shortDescription, context + ".shortdescription");
this.descriptionTail = json.descriptionTail === undefined ? new Translation({"*": ""}, context + ".descriptionTail") : new Translation(json.descriptionTail, context + ".descriptionTail");
this.icon = json.icon;
this.socialImage = json.socialImage;
this.startZoom = json.startZoom;
this.startLat = json.startLat;
this.startLon = json.startLon;
this.widenFactor = json.widenFactor ?? 0.05;
this.roamingRenderings = (json.roamingRenderings ?? []).map((tr, i) => {
if (typeof tr === "string") {
if (SharedTagRenderings.SharedTagRendering.get(tr) !== undefined) {
return SharedTagRenderings.SharedTagRendering.get(tr);
}
}
return new TagRenderingConfig(tr, undefined, `${this.id}.roaming_renderings[${i}]`);
}
);
this.defaultBackgroundId = json.defaultBackgroundId;
this.layers = LayoutConfig.ExtractLayers(json, this.units, official, context);
// ALl the layers are constructed, let them share tagRenderings now!
const roaming: { r, source: LayerConfig }[] = []
for (const layer of this.layers) {
roaming.push({r: layer.GetRoamingRenderings(), source: layer});
}
for (const layer of this.layers) {
for (const r of roaming) {
if (r.source == layer) {
continue;
}
layer.AddRoamingRenderings(r.r);
}
}
for (const layer of this.layers) {
layer.AddRoamingRenderings(
{
titleIcons: [],
iconOverlays: [],
tagRenderings: this.roamingRenderings
}
);
}
this.clustering = {
maxZoom: 16,
minNeededElements: 500
};
if (json.clustering) {
this.clustering = {
maxZoom: json.clustering.maxZoom ?? 18,
minNeededElements: json.clustering.minNeededElements ?? 1
}
for (const layer of this.layers) {
if (layer.wayHandling !== LayerConfig.WAYHANDLING_CENTER_ONLY) {
console.debug("WARNING: In order to allow clustering, every layer must be set to CENTER_ONLY. Layer", layer.id, "does not respect this for layout", this.id);
}
}
}
this.hideFromOverview = json.hideFromOverview ?? false;
// @ts-ignore
if (json.hideInOverview) {
throw "The json for " + this.id + " contains a 'hideInOverview'. Did you mean hideFromOverview instead?"
}
this.lockLocation = json.lockLocation ?? undefined;
this.enableUserBadge = json.enableUserBadge ?? true;
this.enableShareScreen = json.enableShareScreen ?? true;
this.enableMoreQuests = json.enableMoreQuests ?? true;
this.enableLayers = json.enableLayers ?? true;
this.enableSearch = json.enableSearch ?? true;
this.enableGeolocation = json.enableGeolocation ?? true;
this.enableAddNewPoints = json.enableAddNewPoints ?? true;
this.enableBackgroundLayerSelection = json.enableBackgroundLayerSelection ?? true;
this.enableShowAllQuestions = json.enableShowAllQuestions ?? false;
this.enableExportButton = json.enableDownload ?? false;
this.enablePdfDownload = json.enablePdfDownload ?? false;
this.customCss = json.customCss;
this.cacheTimeout = json.cacheTimout ?? (60 * 24 * 60 * 60)
}
private static ExtractLayers(json: LayoutConfigJson, units: Unit[], official: boolean, context: string): LayerConfig[] {
const result: LayerConfig[] = []
json.layers.forEach((layer, i) => {
if (typeof layer === "string") {
if (AllKnownLayers.sharedLayersJson.get(layer) !== undefined) {
if (json.overrideAll !== undefined) {
let lyr = JSON.parse(JSON.stringify(AllKnownLayers.sharedLayersJson[layer]));
const newLayer = new LayerConfig(Utils.Merge(json.overrideAll, lyr), units, `${json.id}+overrideAll.layers[${i}]`, official)
result.push(newLayer)
return
} else {
result.push(AllKnownLayers.sharedLayers[layer])
return
}
} else {
console.log("Layer ", layer, " not kown, try one of", Array.from(AllKnownLayers.sharedLayers.keys()).join(", "))
throw `Unknown builtin layer ${layer} at ${context}.layers[${i}]`;
}
}
if (layer["builtin"] === undefined) {
if (json.overrideAll !== undefined) {
layer = Utils.Merge(json.overrideAll, layer);
}
// @ts-ignore
const newLayer = new LayerConfig(layer, units, `${json.id}.layers[${i}]`, official)
result.push(newLayer)
return
}
// @ts-ignore
let names = layer.builtin;
if (typeof names === "string") {
names = [names]
}
names.forEach(name => {
const shared = AllKnownLayers.sharedLayersJson.get(name);
if (shared === undefined) {
throw `Unknown shared/builtin layer ${name} at ${context}.layers[${i}]. Available layers are ${Array.from(AllKnownLayers.sharedLayersJson.keys()).join(", ")}`;
}
// @ts-ignore
let newLayer: LayerConfigJson = Utils.Merge(layer.override, JSON.parse(JSON.stringify(shared))); // We make a deep copy of the shared layer, in order to protect it from changes
if (json.overrideAll !== undefined) {
newLayer = Utils.Merge(json.overrideAll, newLayer);
}
// @ts-ignore
const layerConfig = new LayerConfig(newLayer, units, `${json.id}.layers[${i}]`, official)
result.push(layerConfig)
return
})
});
return result
}
private static ExtractUnits(json: LayoutConfigJson, context: string): Unit[] {
const result: Unit[] = []
if ((json.units ?? []).length !== 0) {
for (let i1 = 0; i1 < json.units.length; i1++) {
let unit = json.units[i1];
const appliesTo = unit.appliesToKey
for (let i = 0; i < appliesTo.length; i++) {
let key = appliesTo[i];
if (key.trim() !== key) {
throw `${context}.unit[${i1}].appliesToKey[${i}] is invalid: it starts or ends with whitespace`
}
}
if ((unit.applicableUnits ?? []).length === 0) {
throw `${context}: define at least one applicable unit`
}
// Some keys do have unit handling
const defaultSet = unit.applicableUnits.filter(u => u.default === true)
// No default is defined - we pick the first as default
if (defaultSet.length === 0) {
unit.applicableUnits[0].default = true
}
// Check that there are not multiple defaults
if (defaultSet.length > 1) {
throw `Multiple units are set as default: they have canonical values of ${defaultSet.map(u => u.canonicalDenomination).join(", ")}`
}
const applicable = unit.applicableUnits.map((u, i) => new Denomination(u, `${context}.units[${i}]`))
result.push(new Unit(appliesTo, applicable, unit.eraseInvalidValues ?? false));
}
const seenKeys = new Set<string>()
for (const unit of result) {
const alreadySeen = Array.from(unit.appliesToKeys).filter((key: string) => seenKeys.has(key));
if (alreadySeen.length > 0) {
throw `${context}.units: multiple units define the same keys. The key(s) ${alreadySeen.join(",")} occur multiple times`
}
unit.appliesToKeys.forEach(key => seenKeys.add(key))
}
return result;
}
}
public CustomCodeSnippets(): string[] {
if (this._official) {
return [];
}
const msg = "<br/><b>This layout uses <span class='alert'>custom javascript</span>, loaded for the wide internet. The code is printed below, please report suspicious code on the issue tracker of MapComplete:</b><br/>"
const custom = [];
for (const layer of this.layers) {
custom.push(...layer.CustomCodeSnippets().map(code => code + "<br />"))
}
if (custom.length === 0) {
return custom;
}
custom.splice(0, 0, msg);
return custom;
}
public ExtractImages(): Set<string> {
const icons = new Set<string>()
for (const layer of this.layers) {
layer.ExtractImages().forEach(icons.add, icons)
}
icons.add(this.icon)
icons.add(this.socialImage)
return icons
}
public LayerIndex(): Map<string, LayerConfig> {
const index = new Map<string, LayerConfig>();
for (const layer of this.layers) {
index.set(layer.id, layer)
}
return index;
}
/**
* Replaces all the relative image-urls with a fixed image url
* This is to fix loading from external sources
*
* It should be passed the location where the original theme file is hosted.
*
* If no images are rewritten, the same object is returned, otherwise a new copy is returned
*/
public patchImages(originalURL: string, originalJson: string): LayoutConfig {
const allImages = Array.from(this.ExtractImages())
const rewriting = new Map<string, string>()
// Needed for absolute urls: note that it doesn't contain a trailing slash
const origin = new URL(originalURL).origin
let path = new URL(originalURL).href
path = path.substring(0, path.lastIndexOf("/"))
for (const image of allImages) {
if (image == "" || image == undefined) {
continue
}
if (image.startsWith("http://") || image.startsWith("https://")) {
continue
}
if (image.startsWith("/")) {
// This is an absolute path
rewriting.set(image, origin + image)
} else if (image.startsWith("./assets/themes")) {
// Legacy workaround
rewriting.set(image, path + image.substring(image.lastIndexOf("/")))
} else if (image.startsWith("./")) {
// This is a relative url
rewriting.set(image, path + image.substring(1))
} else {
// This is a relative URL with only the path
rewriting.set(image, path + image)
}
}
if (rewriting.size == 0) {
return this;
}
rewriting.forEach((value, key) => {
console.log("Rewriting", key, "==>", value)
originalJson = originalJson.replace(new RegExp(key, "g"), value)
})
return new LayoutConfig(JSON.parse(originalJson), false, "Layout rewriting")
}
}

View file

@ -1,342 +0,0 @@
import {LayerConfigJson} from "./LayerConfigJson";
import {TagRenderingConfigJson} from "./TagRenderingConfigJson";
import UnitConfigJson from "./UnitConfigJson";
/**
* Defines the entire theme.
*
* A theme is the collection of the layers that are shown; the intro text, the icon, ...
* It more or less defines the entire experience.
*
* Most of the fields defined here are metadata about the theme, such as its name, description, supported languages, default starting location, ...
*
* The main chunk of the json will however be the 'layers'-array, where the details of your layers are.
*
* General remark: a type (string | any) indicates either a fixed or a translatable string.
*/
export interface LayoutConfigJson {
/**
* The id of this layout.
*
* This is used as hashtag in the changeset message, which will read something like "Adding data with #mapcomplete for theme #<the theme id>"
* Make sure it is something decent and descriptive, it should be a simple, lowercase string.
*
* On official themes, it'll become the name of the page, e.g.
* 'cyclestreets' which become 'cyclestreets.html'
*/
id: string;
/**
* Who helped to create this theme and should be attributed?
*/
credits?: string;
/**
* Who does maintian this preset?
*/
maintainer: string;
/**
* Extra piece of text that can be added to the changeset
*/
changesetmessage?: string;
/**
* A version number, either semantically or by date.
* Should be sortable, where the higher value is the later version
*/
version: string;
/**
* The supported language(s).
* This should be a two-letter, lowercase code which identifies the language, e.g. "en", "nl", ...
* If the theme supports multiple languages, use a list: `["en","nl","fr"]` to allow the user to pick any of them
*/
language: string | string[];
/**
* The title, as shown in the welcome message and the more-screen
*/
title: string | any;
/**
* A short description, showed as social description and in the 'more theme'-buttons.
* Note that if this one is not defined, the first sentence of 'description' is used
*/
shortDescription?: string | any;
/**
* The description, as shown in the welcome message and the more-screen
*/
description: string | any;
/**
* A part of the description, shown under the login-button.
*/
descriptionTail?: string | any;
/**
* The icon representing this theme.
* Used as logo in the more-screen and (for official themes) as favicon, webmanifest logo, ...
* Either a URL or a base64 encoded value (which should include 'data:image/svg+xml;base64)
*/
icon: string;
/**
* Link to a 'social image' which is included as og:image-tag on official themes.
* Useful to share the theme on social media.
* See https://www.h3xed.com/web-and-internet/how-to-use-og-image-meta-tag-facebook-reddit for more information
*/
socialImage?: string;
/**
* Default location and zoom to start.
* Note that this is barely used. Once the user has visited mapcomplete at least once, the previous location of the user will be used
*/
startZoom: number;
startLat: number;
startLon: number;
/**
* When a query is run, the data within bounds of the visible map is loaded.
* However, users tend to pan and zoom a lot. It is pretty annoying if every single pan means a reloading of the data.
* For this, the bounds are widened in order to make a small pan still within bounds of the loaded data.
*
* IF widenfactor is 0, this feature is disabled. A recommended value is between 0.5 and 0.01 (the latter for very dense queries)
*/
widenFactor?: number;
/**
* A tagrendering depicts how to show some tags or how to show a question for it.
*
* These tagrenderings are applied to _all_ the loaded layers and are a way to reuse tagrenderings.
* Note that if multiple themes are loaded (e.g. via the personal theme)
* that these roamingRenderings are applied to the layers of the OTHER themes too!
*
* In order to prevent them to do too much damage, all the overpass-tags of the layers are taken and combined as OR.
* These tag renderings will only show up if the object matches this filter.
*/
roamingRenderings?: (TagRenderingConfigJson | string)[],
/**
* An override applied on all layers of the theme.
*
* E.g.: if there are two layers defined:
* ```
* "layers":[
* {"title": ..., "tagRenderings": [...], "osmSource":{"tags": ...}},
* {"title", ..., "tagRenderings", [...], "osmSource":{"tags" ...}}
* ]
* ```
*
* and overrideAll is specified:
* ```
* "overrideAll": {
* "osmSource":{"geoJsonSource":"xyz"}
* }
* then the result will be that all the layers will have these properties applied and result in:
* "layers":[
* {"title": ..., "tagRenderings": [...], "osmSource":{"tags": ..., "geoJsonSource":"xyz"}},
* {"title", ..., "tagRenderings", [...], "osmSource":{"tags" ..., "geoJsonSource":"xyz"}}
* ]
* ```
*
* If the overrideAll contains a list where the keys starts with a plus, the values will be appended (instead of discarding the old list), for example
*
* "overrideAll": {
* "+tagRenderings": [ { ... some tagrendering ... }]
* }
*
* In the above scenario, `sometagrendering` will be added at the beginning of the tagrenderings of every layer
*/
overrideAll?: any;
/**
* The id of the default background. BY default: vanilla OSM
*/
defaultBackgroundId?: string;
/**
* The number of seconds that a feature is allowed to stay in the cache.
* The caching flow is as following:
*
* 1. The application is opened the first time
* 2. An overpass query is run
* 3. The result is saved to local storage
*
* On the next opening:
*
* 1. The application is opened
* 2. Data is loaded from cache and displayed
* 3. An overpass query is run
* 4. All data (both from overpass ánd local storage) are saved again to local storage (except when to old)
*
* Default value: 60 days
*/
cacheTimout?: number;
/**
* The layers to display.
*
* Every layer contains a description of which feature to display - the overpassTags which are queried.
* Instead of running one query for every layer, the query is fused.
*
* Afterwards, every layer is given the list of features.
* Every layer takes away the features that match with them*, and give the leftovers to the next layers.
*
* This implies that the _order_ of the layers is important in the case of features with the same tags;
* as the later layers might never receive their feature.
*
* *layers can also remove 'leftover'-features if the leftovers overlap with a feature in the layer itself
*
* Note that builtin layers can be reused. Either put in the name of the layer to reuse, or use {builtin: "layername", override: ...}
*
* The 'override'-object will be copied over the original values of the layer, which allows to change certain aspects of the layer
*
* For example: If you would like to use layer nature reserves, but only from a specific operator (eg. Natuurpunt) you would use the following in your theme:
*
* ```
* "layer": {
* "builtin": "nature_reserve",
* "override": {"source":
* {"osmTags": {
* "+and":["operator=Natuurpunt"]
* }
* }
* }
* }
* ```
*
* It's also possible to load multiple layers at once, for example, if you would like for both drinking water and benches to start at the zoomlevel at 12, you would use the following:
*
* ```
* "layer": {
* "builtin": ["benches", "drinking_water"],
* "override": {"minzoom": 12}
* }
*```
*/
layers: (LayerConfigJson | string | { builtin: string | string[], override: any })[],
/**
* In some cases, a value is represented in a certain unit (such as meters for heigt/distance/..., km/h for speed, ...)
*
* Sometimes, multiple denominations are possible (e.g. km/h vs mile/h; megawatt vs kilowatt vs gigawatt for power generators, ...)
*
* This brings in some troubles, as there are multiple ways to write it (no denomitation, 'm' vs 'meter' 'metre', ...)
*
* Not only do we want to write consistent data to OSM, we also want to present this consistently to the user.
* This is handled by defining units.
*
* # Rendering
*
* To render a value with long (human) denomination, use {canonical(key)}
*
* # Usage
*
* First of all, you define which keys have units applied, for example:
*
* ```
* units: [
* appliesTo: ["maxspeed", "maxspeed:hgv", "maxspeed:bus"]
* applicableUnits: [
* ...
* ]
* ]
* ```
*
* ApplicableUnits defines which is the canonical extension, how it is presented to the user, ...:
*
* ```
* applicableUnits: [
* {
* canonicalDenomination: "km/h",
* alternativeDenomination: ["km/u", "kmh", "kph"]
* default: true,
* human: {
* en: "kilometer/hour",
* nl: "kilometer/uur"
* },
* humanShort: {
* en: "km/h",
* nl: "km/u"
* }
* },
* {
* canoncialDenomination: "mph",
* ... similar for miles an hour ...
* }
* ]
* ```
*
*
* If this is defined, then every key which the denominations apply to (`maxspeed`, `maxspeed:hgv` and `maxspeed:bus`) will be rewritten at the metatagging stage:
* every value will be parsed and the canonical extension will be added add presented to the other parts of the code.
*
* Also, if a freeform text field is used, an extra dropdown with applicable denominations will be given
*
*/
units?: {
/**
* Every key from this list will be normalized
*/
appliesToKey: string[],
/**
* The possible denominations
*/
applicableUnits: UnitConfigJson[]
/**
* If set, invalid values will be erased in the MC application (but not in OSM of course!)
* Be careful with setting this
*/
eraseInvalidValues?: boolean;
}[]
/**
* If defined, data will be clustered.
* Defaults to {maxZoom: 16, minNeeded: 500}
*/
clustering?: {
/**
* All zoom levels above 'maxzoom' are not clustered anymore.
* Defaults to 18
*/
maxZoom?: number,
/**
* The number of elements that should be showed (in total) before clustering starts to happen.
* If clustering is defined, defaults to 0
*/
minNeededElements?: number
},
/**
* The URL of a custom CSS stylesheet to modify the layout
*/
customCss?: string;
/**
* If set to true, this layout will not be shown in the overview with more themes
*/
hideFromOverview?: boolean;
/**
* If set to true, the basemap will not scroll outside of the area visible on initial zoom.
* If set to [[lat0, lon0], [lat1, lon1]], the map will not scroll outside of those bounds.
* Off by default, which will enable panning to the entire world
*/
lockLocation?: boolean | [[number, number], [number, number]];
enableUserBadge?: boolean;
enableShareScreen?: boolean;
enableMoreQuests?: boolean;
enableLayers?: boolean;
enableSearch?: boolean;
enableAddNewPoints?: boolean;
enableGeolocation?: boolean;
enableBackgroundLayerSelection?: boolean;
enableShowAllQuestions?: boolean;
enableDownload?: boolean;
enablePdfDownload?: boolean;
}

View file

@ -1,16 +0,0 @@
import {Translation} from "../../UI/i18n/Translation";
import {Tag} from "../../Logic/Tags/Tag";
export default interface PresetConfig {
title: Translation,
tags: Tag[],
description?: Translation,
/**
* If precise input is set, then an extra map is shown in which the user can drag the map to the precise location
*/
preciseInput?: {
preferredBackground?: string[],
snapToLayers?: string[],
maxSnapDistance?: number
}
}

View file

@ -1,42 +0,0 @@
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
export default class SourceConfig {
osmTags?: TagsFilter;
overpassScript?: string;
geojsonSource?: string;
geojsonZoomLevel?: number;
isOsmCacheLayer: boolean;
constructor(params: {
osmTags?: TagsFilter,
overpassScript?: string,
geojsonSource?: string,
isOsmCache?: boolean,
geojsonSourceLevel?: number
}, context?: string) {
let defined = 0;
if (params.osmTags) {
defined++;
}
if (params.overpassScript) {
defined++;
}
if (params.geojsonSource) {
defined++;
}
if (defined == 0) {
throw `Source: nothing correct defined in the source (in ${context}) (the params are ${JSON.stringify(params)})`
}
if(params.isOsmCache && params.geojsonSource == undefined){
console.error(params)
throw `Source said it is a OSM-cached layer, but didn't define the actual source of the cache (in context ${context})`
}
this.osmTags = params.osmTags;
this.overpassScript = params.overpassScript;
this.geojsonSource = params.geojsonSource;
this.geojsonZoomLevel = params.geojsonSourceLevel;
this.isOsmCacheLayer = params.isOsmCache ?? false;
}
}

View file

@ -1,5 +0,0 @@
export interface AndOrTagConfigJson {
and?: (string | AndOrTagConfigJson)[]
or?: (string | AndOrTagConfigJson)[]
}

View file

@ -1,360 +0,0 @@
import {TagRenderingConfigJson} from "./TagRenderingConfigJson";
import Translations from "../../UI/i18n/Translations";
import {FromJSON} from "./FromJSON";
import ValidatedTextField from "../../UI/Input/ValidatedTextField";
import {Translation} from "../../UI/i18n/Translation";
import {Utils} from "../../Utils";
import {TagUtils} from "../../Logic/Tags/TagUtils";
import {And} from "../../Logic/Tags/And";
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
import {SubstitutedTranslation} from "../../UI/SubstitutedTranslation";
/***
* The parsed version of TagRenderingConfigJSON
* Identical data, but with some methods and validation
*/
export default class TagRenderingConfig {
readonly render?: Translation;
readonly question?: Translation;
readonly condition?: TagsFilter;
readonly configuration_warnings: string[] = []
readonly freeform?: {
readonly key: string,
readonly type: string,
readonly addExtraTags: TagsFilter[];
readonly inline: boolean,
readonly default?: string,
readonly helperArgs?: (string | number | boolean)[]
};
readonly multiAnswer: boolean;
readonly mappings?: {
readonly if: TagsFilter,
readonly ifnot?: TagsFilter,
readonly then: Translation
readonly hideInAnswer: boolean | TagsFilter
}[]
readonly roaming: boolean;
constructor(json: string | TagRenderingConfigJson, conditionIfRoaming: TagsFilter, context?: string) {
if (json === "questions") {
// Very special value
this.render = null;
this.question = null;
this.condition = null;
}
if (json === undefined) {
throw "Initing a TagRenderingConfig with undefined in " + context;
}
if (typeof json === "string") {
this.render = Translations.T(json, context + ".render");
this.multiAnswer = false;
return;
}
this.render = Translations.T(json.render, context + ".render");
this.question = Translations.T(json.question, context + ".question");
this.roaming = json.roaming ?? false;
const condition = FromJSON.Tag(json.condition ?? {"and": []}, `${context}.condition`);
if (this.roaming && conditionIfRoaming !== undefined) {
this.condition = new And([condition, conditionIfRoaming]);
} else {
this.condition = condition;
}
if (json.freeform) {
this.freeform = {
key: json.freeform.key,
type: json.freeform.type ?? "string",
addExtraTags: json.freeform.addExtraTags?.map((tg, i) =>
FromJSON.Tag(tg, `${context}.extratag[${i}]`)) ?? [],
inline: json.freeform.inline ?? false,
default: json.freeform.default,
helperArgs: json.freeform.helperArgs
}
if (json.freeform["extraTags"] !== undefined) {
throw `Freeform.extraTags is defined. This should probably be 'freeform.addExtraTag' (at ${context})`
}
if (this.freeform.key === undefined || this.freeform.key === "") {
throw `Freeform.key is undefined or the empty string - this is not allowed; either fill out something or remove the freeform block alltogether. Error in ${context}`
}
if(json.freeform["args"] !== undefined){
throw `Freeform.args is defined. This should probably be 'freeform.helperArgs' (at ${context})`
}
if (ValidatedTextField.AllTypes[this.freeform.type] === undefined) {
const knownKeys = ValidatedTextField.tpList.map(tp => tp.name).join(", ");
throw `Freeform.key ${this.freeform.key} is an invalid type. Known keys are ${knownKeys}`
}
if (this.freeform.addExtraTags) {
const usedKeys = new And(this.freeform.addExtraTags).usedKeys();
if (usedKeys.indexOf(this.freeform.key) >= 0) {
throw `The freeform key ${this.freeform.key} will be overwritten by one of the extra tags, as they use the same key too. This is in ${context}`;
}
}
}
this.multiAnswer = json.multiAnswer ?? false
if (json.mappings) {
if (!Array.isArray(json.mappings)) {
throw "Tagrendering has a 'mappings'-object, but expected a list (" + context + ")"
}
this.mappings = json.mappings.map((mapping, i) => {
if (mapping.then === undefined) {
throw `${context}.mapping[${i}]: Invalid mapping: if without body`
}
if (mapping.ifnot !== undefined && !this.multiAnswer) {
throw `${context}.mapping[${i}]: Invalid mapping: ifnot defined, but the tagrendering is not a multianswer`
}
if (mapping.if === undefined) {
throw `${context}.mapping[${i}]: Invalid mapping: "if" is not defined, but the tagrendering is not a multianswer`
}
if (typeof mapping.if !== "string" && mapping.if["length"] !== undefined) {
throw `${context}.mapping[${i}]: Invalid mapping: "if" is defined as an array. Use {"and": <your conditions>} or {"or": <your conditions>} instead`
}
let hideInAnswer: boolean | TagsFilter = false;
if (typeof mapping.hideInAnswer === "boolean") {
hideInAnswer = mapping.hideInAnswer;
} else if (mapping.hideInAnswer !== undefined) {
hideInAnswer = FromJSON.Tag(mapping.hideInAnswer, `${context}.mapping[${i}].hideInAnswer`);
}
const mappingContext = `${context}.mapping[${i}]`
const mp = {
if: FromJSON.Tag(mapping.if, `${mappingContext}.if`),
ifnot: (mapping.ifnot !== undefined ? FromJSON.Tag(mapping.ifnot, `${mappingContext}.ifnot`) : undefined),
then: Translations.T(mapping.then, `{mappingContext}.then`),
hideInAnswer: hideInAnswer
};
if (this.question) {
if (hideInAnswer !== true && mp.if !== undefined && !mp.if.isUsableAsAnswer()) {
throw `${context}.mapping[${i}].if: This value cannot be used to answer a question, probably because it contains a regex or an OR. Either change it or set 'hideInAnswer'`
}
if (hideInAnswer !== true && !(mp.ifnot?.isUsableAsAnswer() ?? true)) {
throw `${context}.mapping[${i}].ifnot: This value cannot be used to answer a question, probably because it contains a regex or an OR. Either change it or set 'hideInAnswer'`
}
}
return mp;
});
}
if (this.question && this.freeform?.key === undefined && this.mappings === undefined) {
throw `${context}: A question is defined, but no mappings nor freeform (key) are. The question is ${this.question.txt} at ${context}`
}
if (this.freeform && this.render === undefined) {
throw `${context}: Detected a freeform key without rendering... Key: ${this.freeform.key} in ${context}`
}
if (this.render && this.question && this.freeform === undefined) {
throw `${context}: Detected a tagrendering which takes input without freeform key in ${context}; the question is ${this.question.txt}`
}
if (!json.multiAnswer && this.mappings !== undefined && this.question !== undefined) {
let keys = []
for (let i = 0; i < this.mappings.length; i++) {
const mapping = this.mappings[i];
if (mapping.if === undefined) {
throw `${context}.mappings[${i}].if is undefined`
}
keys.push(...mapping.if.usedKeys())
}
keys = Utils.Dedup(keys)
for (let i = 0; i < this.mappings.length; i++) {
const mapping = this.mappings[i];
if (mapping.hideInAnswer) {
continue
}
const usedKeys = mapping.if.usedKeys();
for (const expectedKey of keys) {
if (usedKeys.indexOf(expectedKey) < 0) {
const msg = `${context}.mappings[${i}]: This mapping only defines values for ${usedKeys.join(', ')}, but it should also give a value for ${expectedKey}`
this.configuration_warnings.push(msg)
}
}
}
}
if (this.question !== undefined && json.multiAnswer) {
if ((this.mappings?.length ?? 0) === 0) {
throw `${context} MultiAnswer is set, but no mappings are defined`
}
let allKeys = [];
let allHaveIfNot = true;
for (const mapping of this.mappings) {
if (mapping.hideInAnswer) {
continue;
}
if (mapping.ifnot === undefined) {
allHaveIfNot = false;
}
allKeys = allKeys.concat(mapping.if.usedKeys());
}
allKeys = Utils.Dedup(allKeys);
if (allKeys.length > 1 && !allHaveIfNot) {
throw `${context}: A multi-answer is defined, which generates values over multiple keys. Please define ifnot-tags too on every mapping`
}
}
}
/**
* Returns true if it is known or not shown, false if the question should be asked
* @constructor
*/
public IsKnown(tags: any): boolean {
if (this.condition &&
!this.condition.matchesProperties(tags)) {
// Filtered away by the condition
return true;
}
if (this.multiAnswer) {
for (const m of this.mappings ?? []) {
if (TagUtils.MatchesMultiAnswer(m.if, tags)) {
return true;
}
}
const free = this.freeform?.key
if (free !== undefined) {
return tags[free] !== undefined
}
return false
}
if (this.GetRenderValue(tags) !== undefined) {
// This value is known and can be rendered
return true;
}
return false;
}
public IsQuestionBoxElement(): boolean {
return this.question === null && this.condition === null;
}
/**
* Gets all the render values. Will return multiple render values if 'multianswer' is enabled.
* The result will equal [GetRenderValue] if not 'multiAnswer'
* @param tags
* @constructor
*/
public GetRenderValues(tags: any): Translation[] {
if (!this.multiAnswer) {
return [this.GetRenderValue(tags)]
}
// A flag to check that the freeform key isn't matched multiple times
// If it is undefined, it is "used" already, or at least we don't have to check for it anymore
let freeformKeyUsed = this.freeform?.key === undefined;
// We run over all the mappings first, to check if the mapping matches
const applicableMappings: Translation[] = Utils.NoNull((this.mappings ?? [])?.map(mapping => {
if (mapping.if === undefined) {
return mapping.then;
}
if (TagUtils.MatchesMultiAnswer(mapping.if, tags)) {
if (!freeformKeyUsed) {
if (mapping.if.usedKeys().indexOf(this.freeform.key) >= 0) {
// This mapping matches the freeform key - we mark the freeform key to be ignored!
freeformKeyUsed = true;
}
}
return mapping.then;
}
return undefined;
}))
if (!freeformKeyUsed
&& tags[this.freeform.key] !== undefined) {
applicableMappings.push(this.render)
}
return applicableMappings
}
/**
* Gets the correct rendering value (or undefined if not known)
* Not compatible with multiAnswer - use GetRenderValueS instead in that case
* @constructor
*/
public GetRenderValue(tags: any): Translation {
if (this.mappings !== undefined && !this.multiAnswer) {
for (const mapping of this.mappings) {
if (mapping.if === undefined) {
return mapping.then;
}
if (mapping.if.matchesProperties(tags)) {
return mapping.then;
}
}
}
if (this.freeform?.key === undefined) {
return this.render;
}
if (tags[this.freeform.key] !== undefined) {
return this.render;
}
return undefined;
}
public ExtractImages(isIcon: boolean): Set<string> {
const usedIcons = new Set<string>()
this.render?.ExtractImages(isIcon)?.forEach(usedIcons.add, usedIcons)
for (const mapping of this.mappings ?? []) {
mapping.then.ExtractImages(isIcon).forEach(usedIcons.add, usedIcons)
}
return usedIcons;
}
/**
* Returns true if this tag rendering has a minimap in some language.
* Note: this might be hidden by conditions
*/
public hasMinimap(): boolean {
const translations: Translation[] = Utils.NoNull([this.render, ...(this.mappings ?? []).map(m => m.then)]);
for (const translation of translations) {
for (const key in translation.translations) {
if (!translation.translations.hasOwnProperty(key)) {
continue
}
const template = translation.translations[key]
const parts = SubstitutedTranslation.ExtractSpecialComponents(template)
const hasMiniMap = parts.filter(part => part.special !== undefined).some(special => special.special.func.funcName === "minimap")
if (hasMiniMap) {
return true;
}
}
}
return false;
}
}

View file

@ -1,167 +0,0 @@
import {AndOrTagConfigJson} from "./TagConfigJson";
/**
* A TagRenderingConfigJson is a single piece of code which converts one ore more tags into a HTML-snippet.
* If the desired tags are missing and a question is defined, a question will be shown instead.
*/
export interface TagRenderingConfigJson {
/**
* Renders this value. Note that "{key}"-parts are substituted by the corresponding values of the element.
* If neither 'textFieldQuestion' nor 'mappings' are defined, this text is simply shown as default value.
*
* Note that this is a HTML-interpreted value, so you can add links as e.g. '<a href='{website}'>{website}</a>' or include images such as `This is of type A <br><img src='typeA-icon.svg' />`
*/
render?: string | any,
/**
* If it turns out that this tagRendering doesn't match _any_ value, then we show this question.
* If undefined, the question is never asked and this tagrendering is read-only
*/
question?: string | any,
/**
* Only show this question if the object also matches the following tags.
*
* This is useful to ask a follow-up question. E.g. if there is a diaper table, then ask a follow-up question on diaper tables...
* */
condition?: AndOrTagConfigJson | string;
/**
* Allow freeform text input from the user
*/
freeform?: {
/**
* If this key is present, then 'render' is used to display the value.
* If this is undefined, the rendering is _always_ shown
*/
key: string,
/**
* The type of the text-field, e.g. 'string', 'nat', 'float', 'date',...
* See Docs/SpecialInputElements.md and UI/Input/ValidatedTextField.ts for supported values
*/
type?: string,
/**
* Extra parameters to initialize the input helper arguments.
* For semantics, see the 'SpecialInputElements.md'
*/
helperArgs?: (string | number | boolean)[];
/**
* If a value is added with the textfield, these extra tag is addded.
* Useful to add a 'fixme=freeform textfield used - to be checked'
**/
addExtraTags?: string[];
/**
* When set, influences the way a question is asked.
* Instead of showing a full-widht text field, the text field will be shown within the rendering of the question.
*
* This combines badly with special input elements, as it'll distort the layout.
*/
inline?: boolean
/**
* default value to enter if no previous tagging is present.
* Normally undefined (aka do not enter anything)
*/
default?: string
},
/**
* If true, use checkboxes instead of radio buttons when asking the question
*/
multiAnswer?: boolean,
/**
* Allows fixed-tag inputs, shown either as radiobuttons or as checkboxes
*/
mappings?: {
/**
* If this condition is met, then the text under `then` will be shown.
* If no value matches, and the user selects this mapping as an option, then these tags will be uploaded to OSM.
*/
if: AndOrTagConfigJson | string,
/**
* If the condition `if` is met, the text `then` will be rendered.
* If not known yet, the user will be presented with `then` as an option
*/
then: string | any,
/**
* In some cases, multiple taggings exist (e.g. a default assumption, or a commonly mapped abbreviation and a fully written variation).
*
* In the latter case, a correct text should be shown, but only a single, canonical tagging should be selectable by the user.
* In this case, one of the mappings can be hiden by setting this flag.
*
* To demonstrate an example making a default assumption:
*
* mappings: [
* {
* if: "access=", -- no access tag present, we assume accessible
* then: "Accessible to the general public",
* hideInAnswer: true
* },
* {
* if: "access=yes",
* then: "Accessible to the general public", -- the user selected this, we add that to OSM
* },
* {
* if: "access=no",
* then: "Not accessible to the public"
* }
* ]
*
*
* For example, for an operator, we have `operator=Agentschap Natuur en Bos`, which is often abbreviated to `operator=ANB`.
* Then, we would add two mappings:
* {
* if: "operator=Agentschap Natuur en Bos" -- the non-abbreviated version which should be uploaded
* then: "Maintained by Agentschap Natuur en Bos"
* },
* {
* if: "operator=ANB", -- we don't want to upload abbreviations
* then: "Maintained by Agentschap Natuur en Bos"
* hideInAnswer: true
* }
*
* Hide in answer can also be a tagsfilter, e.g. to make sure an option is only shown when appropriate.
* Keep in mind that this is reverse logic: it will be hidden in the answer if the condition is true, it will thus only show in the case of a mismatch
*
* e.g., for toilets: if "wheelchair=no", we know there is no wheelchair dedicated room.
* For the location of the changing table, the option "in the wheelchair accessible toilet is weird", so we write:
*
* {
* "question": "Where is the changing table located?"
* "mappings": [
* {"if":"changing_table:location=female","then":"In the female restroom"},
* {"if":"changing_table:location=male","then":"In the male restroom"},
* {"if":"changing_table:location=wheelchair","then":"In the wheelchair accessible restroom", "hideInAnswer": "wheelchair=no"},
*
* ]
* }
*
* Also have a look for the meta-tags
* {
* if: "operator=Agentschap Natuur en Bos",
* then: "Maintained by Agentschap Natuur en Bos",
* hideInAnswer: "_country!=be"
* }
*/
hideInAnswer?: boolean | string | AndOrTagConfigJson,
/**
* Only applicable if 'multiAnswer' is set.
* This is for situations such as:
* `accepts:coins=no` where one can select all the possible payment methods. However, we want to make explicit that some options _were not_ selected.
* This can be done with `ifnot`
* Note that we can not explicitly render this negative case to the user, we cannot show `does _not_ accept coins`.
* If this is important to your usecase, consider using multiple radiobutton-fields without `multiAnswer`
*/
ifnot?: AndOrTagConfigJson | string
}[]
/**
* If set to true, this tagRendering will escape the current layer and attach itself to all the other layers too.
* However, it will _only_ be shown if it matches the overpass-tags of the layer it was originally defined in.
*/
roaming?: boolean
}

View file

@ -1,36 +0,0 @@
export default interface UnitConfigJson{
/**
* The canonical value which will be added to the text.
* e.g. "m" for meters
* If the user inputs '42', the canonical value will be added and it'll become '42m'
*/
canonicalDenomination: string,
/**
* A list of alternative values which can occur in the OSM database - used for parsing.
*/
alternativeDenomination?: string[],
/**
* The value for humans in the dropdown. This should not use abbreviations and should be translated, e.g.
* {
* "en": "meter",
* "fr": "metre"
* }
*/
human?:string | any
/**
* If set, then the canonical value will be prefixed instead, e.g. for '€'
* Note that if all values use 'prefix', the dropdown might move to before the text field
*/
prefix?: boolean
/**
* The default interpretation - only one can be set.
* If none is set, the first unit will be considered the default interpretation of a value without a unit
*/
default?: boolean
}

View file

@ -1,7 +1,7 @@
import TagRenderingConfig from "./JSON/TagRenderingConfig";
import * as questions from "../assets/tagRenderings/questions.json";
import * as icons from "../assets/tagRenderings/icons.json";
import {Utils} from "../Utils";
import TagRenderingConfig from "../Models/ThemeConfig/TagRenderingConfig";
export default class SharedTagRenderings {