forked from MapComplete/MapComplete
First usable sidewalks theme
This commit is contained in:
parent
02a1d9696f
commit
ff0ee35ec1
19 changed files with 537 additions and 174 deletions
|
@ -197,7 +197,12 @@ export interface LayerConfigJson {
|
|||
* 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 | {builtin: string, override: any} | TagRenderingConfigJson) [],
|
||||
tagRenderings?: (string | {builtin: string, override: any} | TagRenderingConfigJson | {
|
||||
leftRightKeys: string[],
|
||||
renderings: (string | {builtin: string, override: any} | TagRenderingConfigJson)[]
|
||||
}) [],
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,7 +15,7 @@ export default interface PointRenderingConfigJson {
|
|||
* All the locations that this point should be rendered at.
|
||||
* Using `location: ["point", "centroid"] will always render centerpoint
|
||||
*/
|
||||
location: ("point" | "centroid")[]
|
||||
location: ("point" | "centroid" | "start" | "end")[]
|
||||
|
||||
/**
|
||||
* The icon for an element.
|
||||
|
|
|
@ -11,6 +11,12 @@ export interface TagRenderingConfigJson {
|
|||
* Used to keep the translations in sync. Only used in the tagRenderings-array of a layerConfig, not requered otherwise
|
||||
*/
|
||||
id?: string,
|
||||
|
||||
/**
|
||||
* Optional: this can group questions together in one question box.
|
||||
* Written by 'left-right'-keys automatically
|
||||
*/
|
||||
group?: string
|
||||
|
||||
/**
|
||||
* Renders this value. Note that "{key}"-parts are substituted by the corresponding values of the element.
|
||||
|
|
|
@ -15,8 +15,9 @@ import WithContextLoader from "./WithContextLoader";
|
|||
import LineRenderingConfig from "./LineRenderingConfig";
|
||||
import PointRenderingConfigJson from "./Json/PointRenderingConfigJson";
|
||||
import LineRenderingConfigJson from "./Json/LineRenderingConfigJson";
|
||||
import {TagRenderingConfigJson} from "./Json/TagRenderingConfigJson";
|
||||
|
||||
export default class LayerConfig extends WithContextLoader{
|
||||
export default class LayerConfig extends WithContextLoader {
|
||||
|
||||
id: string;
|
||||
name: Translation;
|
||||
|
@ -31,7 +32,7 @@ export default class LayerConfig extends WithContextLoader{
|
|||
maxzoom: number;
|
||||
title?: TagRenderingConfig;
|
||||
titleIcons: TagRenderingConfig[];
|
||||
|
||||
|
||||
public readonly mapRendering: PointRenderingConfig[]
|
||||
public readonly lineRendering: LineRenderingConfig[]
|
||||
|
||||
|
@ -82,7 +83,7 @@ export default class LayerConfig extends WithContextLoader{
|
|||
throw context + "Use 'geoJson' instead of 'geojson' (the J is a capital letter)";
|
||||
}
|
||||
|
||||
this. source = new SourceConfig(
|
||||
this.source = new SourceConfig(
|
||||
{
|
||||
osmTags: osmTags,
|
||||
geojsonSource: json.source["geoJson"],
|
||||
|
@ -92,14 +93,15 @@ export default class LayerConfig extends WithContextLoader{
|
|||
},
|
||||
json.id
|
||||
);
|
||||
}else if(legacy === undefined){
|
||||
throw "No valid source defined ("+context+")"
|
||||
} else {
|
||||
this. source = new SourceConfig({
|
||||
this.source = new SourceConfig({
|
||||
osmTags: legacy,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
this.id = json.id;
|
||||
this.allowSplit = json.allowSplit ?? false;
|
||||
this.name = Translations.T(json.name, context + ".name");
|
||||
|
@ -191,22 +193,21 @@ export default class LayerConfig extends WithContextLoader{
|
|||
return config;
|
||||
});
|
||||
|
||||
if(json.mapRendering === undefined){
|
||||
throw "MapRendering is undefined in "+context
|
||||
if (json.mapRendering === undefined) {
|
||||
throw "MapRendering is undefined in " + context
|
||||
}
|
||||
|
||||
this.mapRendering = json.mapRendering
|
||||
.filter(r => r["icon"] !== undefined || r["label"] !== undefined)
|
||||
.map((r, i) => new PointRenderingConfig(<PointRenderingConfigJson>r, context+".mapRendering["+i+"]"))
|
||||
.map((r, i) => new PointRenderingConfig(<PointRenderingConfigJson>r, context + ".mapRendering[" + i + "]"))
|
||||
|
||||
this.lineRendering = json.mapRendering
|
||||
.filter(r => r["icon"] === undefined && r["label"] === undefined)
|
||||
.map((r, i) => new LineRenderingConfig(<LineRenderingConfigJson>r, context+".mapRendering["+i+"]"))
|
||||
.map((r, i) => new LineRenderingConfig(<LineRenderingConfigJson>r, context + ".mapRendering[" + i + "]"))
|
||||
|
||||
|
||||
this.tagRenderings = this.trs(json.tagRenderings, false);
|
||||
|
||||
const missingIds = json.tagRenderings?.filter(tr => typeof tr !== "string" && tr["builtin"] === undefined && tr["id"] === undefined) ?? [];
|
||||
this.tagRenderings = this.ExtractLayerTagRenderings(json)
|
||||
const missingIds = json.tagRenderings?.filter(tr => typeof tr !== "string" && tr["builtin"] === undefined && tr["id"] === undefined && tr["leftRightKeys"] === undefined) ?? [];
|
||||
|
||||
if (missingIds.length > 0 && official) {
|
||||
console.error("Some tagRenderings of", this.id, "are missing an id:", missingIds)
|
||||
|
@ -237,11 +238,11 @@ export default class LayerConfig extends WithContextLoader{
|
|||
}
|
||||
}
|
||||
|
||||
this.titleIcons = this.trs(titleIcons, true);
|
||||
this.titleIcons = this.ParseTagRenderings(titleIcons, true);
|
||||
|
||||
this.title = this.tr("title", undefined);
|
||||
this.isShown = this.tr("isShown", "yes");
|
||||
|
||||
|
||||
this.deletion = null;
|
||||
if (json.deletion === true) {
|
||||
json.deletion = {};
|
||||
|
@ -269,6 +270,98 @@ export default class LayerConfig extends WithContextLoader{
|
|||
}
|
||||
}
|
||||
|
||||
public ExtractLayerTagRenderings(json: LayerConfigJson): TagRenderingConfig[] {
|
||||
|
||||
if (json.tagRenderings === undefined) {
|
||||
return []
|
||||
}
|
||||
|
||||
const normalTagRenderings: (string | { builtin: string, override: any } | TagRenderingConfigJson)[] = []
|
||||
const leftRightRenderings: ({ leftRightKeys: string[], renderings: (string | { builtin: string, override: any } | TagRenderingConfigJson)[] })[] = []
|
||||
for (let i = 0; i < json.tagRenderings.length; i++) {
|
||||
const tr = json.tagRenderings[i];
|
||||
const lrkDefined = tr["leftRightKeys"] !== undefined
|
||||
const renderingsDefined = tr["renderings"]
|
||||
|
||||
if (!lrkDefined && !renderingsDefined) {
|
||||
// @ts-ignore
|
||||
normalTagRenderings.push(tr)
|
||||
continue
|
||||
}
|
||||
if (lrkDefined && renderingsDefined) {
|
||||
// @ts-ignore
|
||||
leftRightRenderings.push(tr)
|
||||
continue
|
||||
}
|
||||
throw `Error in ${this._context}.tagrenderings[${i}]: got a value which defines either \`leftRightKeys\` or \`renderings\`, but not both. Either define both or move the \`renderings\` out of this scope`
|
||||
}
|
||||
|
||||
const allRenderings = this.ParseTagRenderings(normalTagRenderings, false);
|
||||
|
||||
if(leftRightRenderings.length === 0){
|
||||
return allRenderings
|
||||
}
|
||||
|
||||
const leftRenderings : TagRenderingConfig[] = []
|
||||
const rightRenderings : TagRenderingConfig[] = []
|
||||
|
||||
function prepConfig(target:string, tr: TagRenderingConfigJson){
|
||||
|
||||
function replaceRecursive(transl: string | any){
|
||||
if(typeof transl === "string"){
|
||||
return transl.replace("left|right", target)
|
||||
}
|
||||
if(transl.map !== undefined){
|
||||
return transl.map(o => replaceRecursive(o))
|
||||
}
|
||||
transl = {...transl}
|
||||
for (const key in transl) {
|
||||
transl[key] = replaceRecursive(transl[key])
|
||||
}
|
||||
return transl
|
||||
}
|
||||
|
||||
const orig = tr;
|
||||
tr = replaceRecursive(tr)
|
||||
|
||||
tr.id = target+"-"+orig.id
|
||||
tr.group = target
|
||||
return tr
|
||||
}
|
||||
|
||||
|
||||
for (const leftRightRendering of leftRightRenderings) {
|
||||
|
||||
const keysToRewrite = leftRightRendering.leftRightKeys
|
||||
const tagRenderings = leftRightRendering.renderings
|
||||
|
||||
const left = this.ParseTagRenderings(tagRenderings, false, tr => prepConfig("left", tr))
|
||||
const right = this.ParseTagRenderings(tagRenderings, false, tr => prepConfig("right", tr))
|
||||
|
||||
leftRenderings.push(...left)
|
||||
rightRenderings.push(...right)
|
||||
|
||||
}
|
||||
|
||||
leftRenderings.push(new TagRenderingConfig(<TagRenderingConfigJson>{
|
||||
id: "questions",
|
||||
group:"left",
|
||||
}, "layerConfig.ts.leftQuestionBox"))
|
||||
|
||||
rightRenderings.push(new TagRenderingConfig(<TagRenderingConfigJson>{
|
||||
id: "questions",
|
||||
group:"right",
|
||||
}, "layerConfig.ts.rightQuestionBox"))
|
||||
|
||||
allRenderings.push(...leftRenderings)
|
||||
allRenderings.push(...rightRenderings)
|
||||
|
||||
|
||||
return allRenderings;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public CustomCodeSnippets(): string[] {
|
||||
if (this.calculatedTags === undefined) {
|
||||
return [];
|
||||
|
@ -277,8 +370,6 @@ export default class LayerConfig extends WithContextLoader{
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public ExtractImages(): Set<string> {
|
||||
const parts: Set<string>[] = [];
|
||||
parts.push(...this.tagRenderings?.map((tr) => tr.ExtractImages(false)));
|
||||
|
@ -293,12 +384,11 @@ export default class LayerConfig extends WithContextLoader{
|
|||
for (const part of parts) {
|
||||
part?.forEach(allIcons.add, allIcons);
|
||||
}
|
||||
|
||||
|
||||
return allIcons;
|
||||
}
|
||||
|
||||
public isLeftRightSensitive() : boolean{
|
||||
|
||||
public isLeftRightSensitive(): boolean {
|
||||
return this.lineRendering.some(lr => lr.leftRightSensitive)
|
||||
}
|
||||
}
|
|
@ -15,7 +15,7 @@ import {VariableUiElement} from "../../UI/Base/VariableUIElement";
|
|||
|
||||
export default class PointRenderingConfig extends WithContextLoader {
|
||||
|
||||
public readonly location: Set<"point" | "centroid">
|
||||
public readonly location: Set<"point" | "centroid" | "start" | "end">
|
||||
|
||||
public readonly icon: TagRenderingConfig;
|
||||
public readonly iconBadges: { if: TagsFilter; then: TagRenderingConfig }[];
|
||||
|
|
|
@ -14,6 +14,7 @@ import {Utils} from "../../Utils";
|
|||
export default class TagRenderingConfig {
|
||||
|
||||
readonly id: string;
|
||||
readonly group: string;
|
||||
readonly render?: Translation;
|
||||
readonly question?: Translation;
|
||||
readonly condition?: TagsFilter;
|
||||
|
@ -46,7 +47,8 @@ export default class TagRenderingConfig {
|
|||
this.question = null;
|
||||
this.condition = null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(typeof json === "number"){
|
||||
this.render = Translations.WT( ""+json)
|
||||
return;
|
||||
|
@ -64,6 +66,7 @@ export default class TagRenderingConfig {
|
|||
|
||||
|
||||
this.id = json.id ?? "";
|
||||
this.group = json.group ?? "";
|
||||
this.render = Translations.T(json.render, context + ".render");
|
||||
this.question = Translations.T(json.question, context + ".question");
|
||||
this.condition = TagUtils.Tag(json.condition ?? {"and": []}, `${context}.condition`);
|
||||
|
|
|
@ -5,8 +5,8 @@ import {Utils} from "../../Utils";
|
|||
|
||||
export default class WithContextLoader {
|
||||
private readonly _json: any;
|
||||
private readonly _context: string;
|
||||
|
||||
protected readonly _context: string;
|
||||
|
||||
constructor(json: any, context: string) {
|
||||
this._json = json;
|
||||
this._context = context;
|
||||
|
@ -43,20 +43,22 @@ export default class WithContextLoader {
|
|||
* Converts a list of tagRenderingCOnfigJSON in to TagRenderingConfig
|
||||
* A string is interpreted as a name to call
|
||||
*/
|
||||
public trs(
|
||||
public ParseTagRenderings(
|
||||
tagRenderings?: (string | { builtin: string, override: any } | TagRenderingConfigJson)[],
|
||||
readOnly = false
|
||||
readOnly = false,
|
||||
prepConfig: ((config: TagRenderingConfigJson) => TagRenderingConfigJson) = undefined
|
||||
) {
|
||||
if (tagRenderings === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const context = this._context
|
||||
|
||||
const renderings: TagRenderingConfig[] = []
|
||||
|
||||
const context = this._context
|
||||
const renderings: TagRenderingConfig[] = []
|
||||
if (prepConfig === undefined) {
|
||||
prepConfig = c => c
|
||||
}
|
||||
for (let i = 0; i < tagRenderings.length; i++) {
|
||||
let renderingJson= tagRenderings[i]
|
||||
let renderingJson = tagRenderings[i]
|
||||
if (typeof renderingJson === "string") {
|
||||
renderingJson = {builtin: renderingJson, override: undefined}
|
||||
}
|
||||
|
@ -70,41 +72,29 @@ export default class WithContextLoader {
|
|||
)}`;
|
||||
}
|
||||
|
||||
const tr = new TagRenderingConfig("questions", context);
|
||||
renderings.push(tr)
|
||||
const tr = new TagRenderingConfig("questions", context);
|
||||
renderings.push(tr)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (renderingJson["override"] !== undefined) {
|
||||
const sharedJson = SharedTagRenderings.SharedTagRenderingJson.get(renderingId)
|
||||
const tr = new TagRenderingConfig(
|
||||
Utils.Merge(renderingJson["override"], sharedJson),
|
||||
`${context}.tagrendering[${i}]+override`
|
||||
);
|
||||
renderings.push(tr)
|
||||
continue
|
||||
}
|
||||
let sharedJson = SharedTagRenderings.SharedTagRenderingJson.get(renderingId)
|
||||
|
||||
const shared = SharedTagRenderings.SharedTagRendering.get(renderingId);
|
||||
if (sharedJson === undefined) {
|
||||
const keys = Array.from(SharedTagRenderings.SharedTagRenderingJson.keys());
|
||||
throw `Predefined tagRendering ${renderingId} not found in ${context}.\n Try one of ${keys.join(
|
||||
", "
|
||||
)}\n If you intent to output this text literally, use {\"render\": <your text>} instead"}`;
|
||||
}
|
||||
|
||||
if (shared !== undefined) {
|
||||
renderings.push( shared)
|
||||
continue
|
||||
renderingJson = Utils.Merge(renderingJson["override"], sharedJson)
|
||||
}
|
||||
if (Utils.runningFromConsole) {
|
||||
continue
|
||||
}
|
||||
|
||||
const keys = Array.from( SharedTagRenderings.SharedTagRendering.keys() );
|
||||
throw `Predefined tagRendering ${renderingId} not found in ${context}.\n Try one of ${keys.join(
|
||||
", "
|
||||
)}\n If you intent to output this text literally, use {\"render\": <your text>} instead"}`;
|
||||
}
|
||||
|
||||
const tr = new TagRenderingConfig(
|
||||
<TagRenderingConfigJson>renderingJson,
|
||||
`${context}.tagrendering[${i}]`
|
||||
);
|
||||
|
||||
const patchedConfig = prepConfig(<TagRenderingConfigJson>renderingJson)
|
||||
|
||||
const tr = new TagRenderingConfig(patchedConfig, `${context}.tagrendering[${i}]`);
|
||||
renderings.push(tr)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue